Last active
December 7, 2018 10:38
-
-
Save yunfzhai/644632e95760cb3362fe7b2c3d8a80f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 环境变量的使用,及命令加载的技巧 | |
libdir: hsym `${x,"/qtips"}getenv `HOME ; | |
system "l ",1_string ` sv libdir,`stat.q ; | |
/ unstack a table | |
exec indexcode!brate by year:year from table | |
//加快查询速度 | |
update `g#secucode,`s#date from `date`secucode xasc `quota; | |
// 从保存的context恢复到目前的context,其实是完全覆盖当前空间的 | |
`. set get `:inting; | |
crossup:{[x;y](y>x)&(prev y)<=prev x}; /y上穿x | |
// 定义空表 | |
flip `date`rate`dzrate`ndzrate!"****"$\:() ; | |
// 解决csv文件读取股票代码 不足6位的问题 | |
update secucode:{`${reverse x til 6} reverse "000000", string x } | |
each secucode from | |
// 统计回归 | |
/ 单因子回归,没有截距项 | |
residual:{ coef: first (enlist y) lsq x xexp/: til 2;y- coef[0] +coef[1]*x} | |
/ 多因子回归 | |
\l qml.q | |
.qml.mlsq[x;y] | |
a:(1 1 4f;1 2 6f;1 3 2f;1 4 1f); b:11 2 -3 -4 | |
.qml.mlsq[a;b] // 得到回归系数 b作为被解释变量,a为解释变量 | |
//20.19048 -6.238095 -0.952381 | |
//残差呢 | |
{y-x mmu .qml.mlsq[x;y]}[a;b] | |
// 链接数据库 | |
eppice: .odbc.eval h: .odbc.open (`eppic32;`sa;`$"Pass@word"); | |
// 四舍五入函数 rnd[0.25864;1] | |
rnd:{x*"j"$y%x} | |
// 移动平均算法,与matlab定义相同 [Short, Long] = movavg(1:10, 3, 3, 'e') | |
myewma:{x:2%1+x;first[y](1f-x)\x*y} | |
// 确定属于哪个季度 | |
quarter:{year:`year$x; Q:{?[x<=3;`Q1;?[x<=6;`Q2;?[x<=9;`Q3;`Q4]]]}`mm$x; `$ (string year),' string Q } | |
// 一个有趣的问题,quota中是行情数据,要删除每个股票上市以来的前20个交易数据,以避开新股涨停的情况 | |
// 两种做法:1、给每个股票编号,取大于20的行 | |
// 2、使用fby函数 | |
// 第二种方法,fby方法比编号方法效率高一倍左右!! 0.5秒左右即可完成 | |
load `:quotadata; | |
update `g#secucode,`s#date from `date`secucode xasc `quotadata; | |
update index: til count i by secucode from `quotadata | |
cz : select from quotadata where index>=20 | |
cz2: select from quotadata where i >= (first 20_;i) fby secucode | |
// scan函数详解!! 不是函数,是个副词,修饰动词(函数)的,该副词长这样 \ | |
二元函数 f(x,y) 作用在list L上,得到结果记为T | |
T0 = L0 | |
T1 = f(T0,L1) | |
T2 = f(T1,L2) | |
example,f(x,y) = 10*x+y Q_Form is: ({y+10*x}\) | |
L:0 1 2 3 4f | |
T0 = 0; | |
T1 = 10*0+1 =1 | |
T2 = 10*1+2 =12 | |
T3 = 10*12+3 = 123 | |
三元函数 f(x,y,z) 作用在三个list L M N上(其中L应为单变量),得到结果记为T | |
T0 = f(L,M0,N0) | |
T1 = f(T0,M1,N1) | |
T2 = f(T1,M2,N2) | |
T3 = f(T2,M3,N3) | |
example,f(x,y) = x*y+z Q_Form is: ({z+x*y}\) | |
L=1;M=1 2 3 4;N=5 6 7 8; | |
T0 = 5+1*1 = 6 | |
T1 = 6+2*6 = 18 | |
T2 = 7+3*18 = 61 | |
({z+x*y}\)[1f;1 2 3 4f; 5 6 7 8f] | |
/ 策略 | |
TARULE:()!(); /rule | |
TAPARA:()!(); /parameter | |
APPLYTA:{TARULE[x][TAPARA[x];]}; /qpply the rule and parameter | |
/ 锤头策略,寻找买入时点,20个交易日后卖出,看有效性 | |
/ 1、下影线达到股价2%以上:low%close&open<0.98 | |
/ 2、上影线小于股价的1%:high%close&open<1.01 | |
/ 3. 收盘价低于30日内最高价的70%:close<0.7* 30 mmax close | |
TARULE[`101]:{ | |
o:y`o; | |
h:y`h; | |
l:y`l; | |
c:y`c; | |
((x 0)>l%c&o)&((x 1)>h%c&o)&(c<(x 2)*(x 3) mmax c) | |
}; | |
TAPARA[`101]:(0.98;1.01;0.7;12); /1.锤头 | |
one:update rate:-1+future % close from | |
{select secucode,date,close,future from x where sig} | |
ungroup select date,sig:APPLYTA[`101] `o`h`l`c!(open;high;low;close),close,future:-12 xprev close by secucode from quotaM | |
/ 秋影金波:低位连续小阳,表明下档买气旺盛,犹如金色阳光下的波影 | |
/ 1、阳线,阳线实体小于3%,距今超过或等于5天 | |
/ 2、10日内最低价就是百日内最低价 | |
/ 3、3日均价比5日均价高出1%以上 | |
TARULE[`102]:{ | |
o:y`o;l:y`l;c:y`c; | |
((x 0)=(x 0) msum (c>=o)&(x 1)>=c%o)& | |
(((x 2) mmin l)=(x 3) mmin l)& | |
(((x 4) mavg c)>(x 5)*(x 6) mavg c) | |
}; | |
TAPARA[`102]:(5;1.03;10;100;3;1.01;5); | |
/3.好友反攻:下跌市势,见底转向形态,一般要求一支长阴烛后,次日跳空下跌, | |
// 收市时回到前收盘价位 | |
// 1、昨日阴线实体大于3%; prev close % open < 0.97 | |
// 2、当日收阳线; open<close | |
// 3、当日收盘价大于等于昨收盘 close>= prev close | |
// 4、当日收盘价小于30日最高价的70% 30 mmax close> close | |
TARULE[`103]:{ | |
o:y`o;c:y`c; | |
((x 0)>prev c%o)& | |
(o<prev c)&(c>prev c)& | |
(c<(x 1)*(x 2) mmax c) | |
}; | |
TAPARA[`103]:(0.97;0.7;30); | |
/4.卷土重来:一种双底形态 | |
// 1、当日收阳线 | |
// 2、当日跳空低开 | |
// 3、收盘价位于昨日K线实体中部以上 | |
// 4、昨日阴线实体在3%以上 | |
// 5、上述条件在10日内发生两次以上 | |
// 6、10日内最低价就是百日内最低价 | |
// 7、解决信号连发问题,10日内不允许重复 | |
TARULE[`104]:{ | |
o:y`o;l:y`l;c:y`c; | |
1=(x 0) msum ((x 1)<=(x 0) msum (c>o)&(o<prev c)&(c>0.5*prev o+c)&((x 2)>prev c%o))& | |
(((x 3) mmin l)=(x 4) mmin l) | |
}; | |
TAPARA[`104]:(10;2;0.97;10;100); | |
/5.早晨之星: | |
// 1、前天阴线实体大于3% 0.97< 2 xprev close%open | |
// 2、昨天跳空低开 (2 xprev close) > prev open | |
// 3、今天阳线实体大于3% 1.03<close%open | |
// 4、今日收盘价高于前天收盘价 2 xprev close<close | |
// 5、昨日K线实体小于2% 0.02>(abs prev close-open)%prev close | |
// 6、确认走势出于低位,要求数值由-5 以下上行至-5以上(45日最低价与收盘价的差值比上45日最高价与45日最低价的差值,然后求5日平均) | |
// 7、以规则1-5为买入条件1,规则6为买入条件2,其中一条发出信号后,要求另一条在前后10日内同时出现,则确认 | |
TARULE[`105]:{ | |
o:y`o;h:y`h;l:y`l;c:y`c; | |
m:x 7; | |
{(x&1<=z msum y)|y&1<=z msum x} | |
[;;x 0] [((x 1)>(x 2) xprev c%o)& //con 1 | |
((prev o)<(x 2) xprev c)& //condition 2 | |
((x 3)<c%o)& //condition 3 | |
(c>(x 2) xprev c)& //condition 4 | |
((x 4)>abs prev -1+o%c); //condition 5 | |
crossup[x 5;] (x 6) mavg 100*((m mmin l)-c)%((m mmax h)-m mmin l)] //condition 6 | |
}; | |
TAPARA[`105]:(10;0.97;2;1.03;0.02;-5;5); | |
// 6.炉架底:多头不死,跌势不止:行情在绝望中产生,并凝聚出向上的爆发力,有点倾向于报复性回升 | |
// 1、确认中期低点,10日内最低价即30日最低价 (10 xprev close) = 30 xprev close | |
// 2、10日最高收盘价与10日最低收盘价的比值,低于5日前该比值 ((10 mmax close) % 10 mmin) <5 xprev (10 mmax close) % 10 mmin close | |
// 3、当日阳线实体大于5% 1.05<close%open | |
// 4、5日内出现过实体大于5%的阴线 1&5 msum 0.95<close%open | |
TARULE[`106]:{ | |
o:y`o;l:y`l;c:y`c; | |
(((x 0) mmin l)=(x 1) mmin l)& //con 1 | |
({x<y xprev x}[;x 2] ((x 3) mmax c)% (x 3) mmin c)& //con 2 | |
((x 4)<c%o)& //con 3 | |
(1<=(x 5) msum (x 6)>c%o) //con 4 | |
}; | |
TAPARA[`106]:(10;100;5;10;1.05;5;0.95); / | |
/ 7.大阳烛:强者恒强,大道至简 | |
//1 昨日收盘价低于10日内最高价的80% (prev close)< 0.8* 10 mmax high | |
//2 开盘价等于最低价 open=low | |
//3 阳线实体大于4% 1.04<close%open | |
TARULE[`107]:{ | |
o:y`o;l:y`l;c:y`c; | |
((prev c)<(x 0)*(x 1) mmax c)& //cond 1 | |
(l=o)& //cond 2 | |
((x 2)<c%o) //cond 3 | |
}; | |
TAPARA[`107]:(0.8;10;1.04); | |
///一浪零点过滤: | |
TARULE[`ZERO]:{ | |
l:y`l;c:y`c; | |
m:x 0; n:x 1; | |
(l=m mmin l)& | |
(0<m msum c=m min c)& | |
(c<prev c) | |
}; | |
TAPARA[`ZERO]:(100;3); | |
TARULE[`WAVE2]:{ | |
h:y`h;l:y`l;c:y`c; | |
m:x 0; a:x 1; b:x 2; | |
mh:m mmax h; | |
ml:m mmin l; | |
(l>mh-a*mh-ml)& | |
(l<mh-b*mh-ml)& | |
((irlast c=m mmax c)<irlast c=m mmin c) | |
}; | |
TAPARA[`WAVE2]:(100;0.64;0.60); /二浪底过滤 | |
TARULE[`WAVE3]:{ | |
o:y`o;h:y`h;l:y`l; | |
(o&l)>prev h | |
}; | |
TAPARA[`WAVE3]:(); /三浪启动过滤 | |
TARULE[`WAVE4]:{ | |
h:y`h;l:y`l;c:y`c; | |
m:x 0; a:x 1; b:x 2; | |
mh:m mmax h; | |
ml:m mmin l; | |
(l>mh-a*mh-ml)& | |
(l<mh-b*mh-ml)& | |
((irlast c=m mmax c)<irlast c=m mmin c) | |
}; | |
TAPARA[`WAVE4]:(200;0.40;0.36); /四浪底过滤 | |
TARULE[`WAVEB]:{ | |
h:y`h;l:y`l;c:y`c; | |
m:x 0; n x 1; r:x 2; a:x 3; b:x 4; | |
mh:m mmax h; | |
nl:m xprev n mmin l; | |
(mh=n mmax h)& | |
(l=m mmin l)& | |
(mh>r*nl)& | |
(l<mh-a*(mh-nl))& | |
(l>mh-b*(mh-nl)) | |
}; | |
TAPARA[`WAVEB]:(30;100;1.3;0.618;1.236); /B浪底过滤 | |
//Chap 4 | |
// AO指标 5日中价与34日中价的差值,中价是最高价与最低价的平均 | |
TARULE[`AO]:{ | |
h:y`h;l:y`l; | |
{(y mavg x)-z mavg x}[;x 0;x 1] 0.5*h+l | |
}; | |
TAPARA[`AO]:(5 34); /AO动量 | |
TARULE[`AC]:{ | |
ao:TARULE[`AO][2#x;y]; | |
(x 3) mavg (ao- (x 2) mavg ao) | |
}; | |
TAPARA[`AC]:(5 34 5 5); /AC动量加速 | |
//chap 5 | |
TARULE[`Oh]:{ | |
o:y`o;c:y`c; | |
(c<(x 0)*(x 1) mavg c)& | |
(o<(x 2)*prev c)& | |
c>prev c | |
}; | |
TAPARA[`Oh]:(0.96;5;0.99); /5.2 哎呀跳空 | |
TARULE[`123]:{ | |
m:x 0; n:x 1; a:x 2; c:y`c; | |
c0:m mmin c; | |
c1:n xprev c0; | |
(c>c1)&(c0<c1)&c0>a*c1 | |
}; | |
TAPARA[`123]:(100;10;0.95); /5.3 123法则 | |
TARULE[`TD]:{ | |
m:x 0; n:x 1; u:x 2; | |
h:y`h;l:y`l;c:y`c; | |
fz:(0|h-prev c)+c-l; | |
fm:(0|(prev c)-l)+h-l; | |
td:100*(8 msum fz)%(8 msum fm+fz); | |
u=u msum td<n | |
}; | |
TAPARA[`TD]:(8;40;7); /5.4 TD价格区间 | |
TARULE[`TR]:{ | |
m:x 5; | |
h:y`h;l:y`l;c:y`c; | |
ao:{(5 mavg x)-34 mavg x} 0.5*(h+l); | |
ac: {x - 5 mavg x} ao; | |
s0:(x 0) mdev c; | |
s1:(x 1) mdev c; | |
s2:(x 2) mdev c; | |
s3:(x 3) mdev c; | |
s4:(x 4) mdev c; | |
(s0<s1)&(s1<s2)&(s2<s3)&(s3<s4)& | |
(m=m msum ac<prev ac)& | |
(m=m msum ao<prev ao) | |
}; | |
TAPARA[`TR]:(5 10 20 30 100 5); /5.5 波动性交易 | |
TARULE[`BD]:{ | |
m:x 0; r:x 1; c:y`c; | |
r>100*-1+c%28 mavg c | |
}; | |
TAPARA[`REV]:(28;-10) /5.6 摆荡交易系统 | |
TARULE[`YSZD]:{ | |
c:y`c; | |
{[x]y:prev x;(x>y)&y=100 mmin y} | |
{x-y mavg x}[;x 3] | |
(x 2) mavg (x 1) mavg rsi[x 0;c] | |
}; | |
TAPARA[`YSZD]:(14;5;3;9); /5.7 衍生振荡指标 | |
//Chap 9.5 | |
TARULE[`902]:{ | |
h:y`h;l:y`l;c:y`c; | |
((x 0)<((x 1) mmax c)%c)& | |
((x 2)<h%l)& | |
(crossup[x 3;] 100*-1+c%(x 4) mavg c) | |
}; | |
TAPARA[`902]:(1.15;20;1.05;-28;42); /公式2(90%,12%) | |
TARULE[`908]:{ | |
h:y`h;l:y`l;c:y`c; | |
({x&1<y msum x}[;x 0] (x 1)<h%l)& | |
((x 2)>100*c%(x 3) mavg c)| | |
((x 4)>100*c%(x 5) mavg c)| | |
((x 6)>100*c%(x 7) mavg c) | |
}; | |
TAPARA[`908]:(5;1.051;72;60;78;40;80;20); /公式8(82%,13%) | |
TARULE[`913]:{ | |
h:y`h;l:y`l;c:y`c; | |
(1<=(x 0) msum (x 1)>c %(x 2) mavg c)& | |
((x 3)<h%l)& | |
((x 4)>c%(x 5) mavg c) | |
}; | |
TAPARA[`913]:(3;0.67;48;1.06;0.8;8); /公式13(97%,85%) | |
//Chap 10 | |
/10.1 对称重演判强弱 | |
TARULE[`28]:{ | |
c:y`c; | |
s0:(x 0)mdev c; | |
s1:(x 1)mdev c; | |
s2:(x 2)mdev c; | |
s3:(x 3)mdev c; | |
crossup[s0;s1]&(s1>s2)&(s2>s3) | |
}; | |
TAPARA[`28]:(5 10 20 30); /10.2 二八法则选择异动股票 | |
/10.3 魔鬼定律 | |
/10.4 墨菲法则 | |
TARULE[`REVTHINK]:{ | |
m:x 0; | |
c:y`c; | |
m=m msum c<prev c | |
}; | |
TAPARA[`REVTHINK]:(enlist 6); /10.5 逆向思考选择六连阴 | |
TARULE[`MATAI]:{ | |
c:y`c; | |
(x 0)<100*c%(x 1) xprev c | |
}; | |
TAPARA[`MATAI]:(25;11); /10.6 马太效应11日上涨25% | |
TARULE[`OCARM]:{ | |
h:y`h;c:y`c; | |
c>prev (x 0) mmax h | |
}; | |
TAPARA[`OCARM]:(enlist 90); /10.7 奥卡姆剃刀交易系统 | |
/10.8 彼得原理:上/下分形判超买/卖 | |
TARULE[`AOMARK]:{ | |
h:y`h;l:y`l; | |
{(y mavg x)-z mavg x}[;x 0;x 1] 0.5*h+l}; | |
TAPARA[`AOMARK]:(5 34 0); /10.9 大盘监测:AO动量 | |
//好牛逼,看不懂 | |
q)D1Bar:2013.07.26 2013.07.29 2013.07.30 2013.07.31 2013.08.01 2013.08.02 2013.08.04 2013.08.05 2013.08.06 2013.08.07 2013.08.08 2013.08.09 2013.08.11 2013.08.12 2013.08.13 | |
q)OpenAsk:1680.4 1686.5 1690.5 1693.9 1703.8 1703.7 1706.1 1707.1 1696.2 1690.2 1698.9 1692.9 1687.6 1684.3 1691.3 | |
q)t:([]D1Bar;OpenAsk) | |
q)f:{l@'(first z@) @/:y l:neg[x]#/:,\[til count y]} | |
q)update max5Date:D1Bar f[5;OpenAsk;idesc],Min5Date:D1Bar f[5;OpenAsk;iasc] from t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment