交易模型源码合集


程序化交易模型源码:
{K:10,M:5}
A:=SUM( IF(CLOSE>REF(CLOSE,1), VOL/CLOSE,0), K); 当收大于昨收时成交量 / 收盘价的K周期的的总和
B:=SUM( IF(CLOSE<REF(CLOSE,1), VOL/CLOSE,0), K); 当收小于昨收时成交量 / 收盘价的K周期的的总和
D:=(A - B); {两者之差值}
G:=MA( (A-B), M); { 的均线 }
CROSS(D,G),BK; {差值上穿其均线, 买开 }
CROSS(G,D),SP; { 下穿 卖开}
CROSS(G,D),SK; { 下穿 卖平 }
CROSS(D,G),BP; { 上穿 买平 }

=========================================================================


PEAKBARS(MA(CLOSE,20),10,1,0)=1,SPK;
TROUGHBARS(MA(CLOSE,20),10,1,0)=1,BPK;

H:=HIGH;
L:=LOW;
A:=IF(HIGH>=REF(HIGH,1)&&HIGH>=REF(HIGH,2)&&HIGH>=REF(HIGH,3),LOW,0);
B:=IF(LOW<=REF(LOW,1)&&LOW<=REF(LOW,2)&&LOW<=REF(LOW,3),HIGH,888888);
P1:=IF(A>REF(L,1),REF(L,1),0);
P2:=IF(A>REF(L,2)&&A<=REF(L,1),REF(L,2),0);
P3:=IF(A>REF(L,3)&&A<=REF(L,1)&&A<=REF(L,2),REF(L,3),0);
P4:=IF(A>REF(L,4)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3),REF(L,4),0);
P5:=IF(A>REF(L,5)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4),REF(L,5),0);
P6:=IF(A>REF(L,6)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4)&&A<=REF(L,5),REF(L,6),0);
P7:=IF(A>REF(L,7)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4)&&A<=REF(L,5)&&A<=REF(L,6),REF(L,7),0);
P8:=IF(A>REF(L,8)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4)&&A<=REF(L,5)&&A<=REF(L,6)&&A<=REF(L,7),REF(L,8),0);
P9:=IF(A>REF(L,9)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4)&&A<=REF(L,5)&&A<=REF(L,6)&&A<=REF(L,7)&&A<=REF(L,8),REF(L,9),0);
P10:=IF(A>REF(L,10)&&A<=REF(L,1)&&A<=REF(L,2)&&A<=REF(L,3)&&A<=REF(L,4)&&A<=REF(L,5)&&A<=REF(L,6)&&A<=REF(L,7)&&A<=REF(L,8)&&A<=REF(L,9),REF (L,10),0);
AA:=P1+P2+P3+P4+P5+P6+P7+P8+P9+P10;
PP1:=IF(AA>REF(L,1),REF(L,1),0);
PP2:=IF(AA>REF(L,2)&&AA<=REF(L,1),REF(L,2),0);
PP3:=IF(AA>REF(L,3)&&AA<=REF(L,1)&&AA<=REF(L,2),REF(L,3),0);
PP4:=IF(AA>REF(L,4)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3),REF(L,4),0);
PP5:=IF(AA>REF(L,5)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4),REF(L,5),0);
PP6:=IF(AA>REF(L,6)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4)&&AA<=REF(L,5),REF(L,6),0);
PP7:=IF(AA>REF(L,7)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4)&&AA<=REF(L,5)&&AA<=REF(L,6),REF(L,7),0);
PP8:=IF(AA>REF(L,8)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4)&&AA<=REF(L,5)&&AA<=REF(L,6)&&AA<=REF(L,7),REF(L,8),0);
PP9:=IF(AA>REF(L,9)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4)&&AA<=REF(L,5)&&AA<=REF(L,6)&&AA<=REF(L,7)&&AA<=REF(L,8),REF (L,9),0);
PP10:=IF(AA>REF(L,10)&&AA<=REF(L,1)&&AA<=REF(L,2)&&AA<=REF(L,3)&&AA<=REF(L,4)&&AA<=REF(L,5)&&AA<=REF(L,6)&&AA<=REF(L,7)&&AA<=REF(L,8)&&AA<=REF (L,9),REF(L,10),0);
AAA:=PP1+PP2+PP3+PP4+PP5+PP6+PP7+PP8+PP9+PP10;
C:=VALUEWHEN(AAA>0,AAA);
T1:=IF(B<REF(H,1),REF(H,1),0);
T2:=IF(B<REF(H,2)&&B>=REF(H,1),REF(H,2),0);
T3:=IF(B<REF(H,3)&&B>=REF(H,1)&&B>=REF(H,2),REF(H,3),0);
T4:=IF(B<REF(H,4)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3),REF(H,4),0);
T5:=IF(B<REF(H,5)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4),REF(H,5),0);
T6:=IF(B<REF(H,6)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4)&&B>=REF(H,5),REF(H,6),0);
T7:=IF(B<REF(H,7)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4)&&B>=REF(H,5)&&B>=REF(H,6),REF(H,7),0);
T8:=IF(B<REF(H,8)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4)&&B>=REF(H,5)&&B>=REF(H,6)&&B>=REF(H,7),REF(H,8),0);
T9:=IF(B<REF(H,9)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4)&&B>=REF(H,5)&&B>=REF(H,6)&&B>=REF(H,7)&&B>=REF(H,8),REF(H,9),0);
T10:=IF(B<REF(H,10)&&B>=REF(H,1)&&B>=REF(H,2)&&B>=REF(H,3)&&B>=REF(H,4)&&B>=REF(H,5)&&B>=REF(H,6)&&B>=REF(H,7)&&B>=REF(H,8)&&B>=REF(H,9),REF (H,10),0);
BB:=IF((T1+T2+T3+T4+T5+T6+T7+T8+T9+T10)=0,888888,T1+T2+T3+T4+T5+T6+T7+T8+T9+T10);
TT1:=IF(BB<REF(H,1),REF(H,1),0);
TT2:=IF(BB<REF(H,2)&&BB>=REF(H,1),REF(H,2),0);
TT3:=IF(BB<REF(H,3)&&BB>=REF(H,1)&&BB>=REF(H,2),REF(H,3),0);
TT4:=IF(BB<REF(H,4)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3),REF(H,4),0);
TT5:=IF(BB<REF(H,5)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4),REF(H,5),0);
TT6:=IF(BB<REF(H,6)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4)&&BB>=REF(H,5),REF(H,6),0);
TT7:=IF(BB<REF(H,7)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4)&&BB>=REF(H,5)&&BB>=REF(H,6),REF(H,7),0);
TT8:=IF(BB<REF(H,8)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4)&&BB>=REF(H,5)&&BB>=REF(H,6)&&BB>=REF(H,7),REF(H,8),0);
TT9:=IF(BB<REF(H,9)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4)&&BB>=REF(H,5)&&BB>=REF(H,6)&&BB>=REF(H,7)&&BB>=REF(H,8),REF (H,9),0);
TT10:=IF(BB<REF(H,10)&&BB>=REF(H,1)&&BB>=REF(H,2)&&BB>=REF(H,3)&&BB>=REF(H,4)&&BB>=REF(H,5)&&BB>=REF(H,6)&&BB>=REF(H,7)&&BB>=REF(H,8)&&BB>=REF (H,9),REF(H,10),0);
BBB:=TT1+TT2+TT3+TT4+TT5+TT6+TT7+TT8+TT9+TT10;
D:=VALUEWHEN(BBB>0,BBB);
UD:=IF(CLOSE>D&&REF(CLOSE,1)<=D,1,IF(CLOSE<C&&REF(CLOSE,1)>=C,-3,0));
K:=VALUEWHEN(UD<>0,UD);
G:=IF(K=1,C,D);
CROSS(CLOSE,REF(G,1)),BPK;
CROSS(REF(G,1),CLOSE),SPK;


而效果测试中可选择的测试价位都是当前周期的各个价位,二者的成交价差差距会比较大。(解决方法:将交易模型中的买卖条件判断数值均替换为上一周期的数值REF(A,1))
胶5分钟期货交易模型{有文华未知函数}

JJ:=DMA(((((((((HIGH + LOW) + REF(HIGH,1)) + REF(LOW,1)) + REF(HIGH,2)) +
REF(LOW,2)) + REF(CLOSE,1)) + CLOSE) / 8),((VOL * 240) / OPENINT));
JJ1:=EMA(JJ,7);
MJ:=(((2 * (((((((HIGH + LOW) + REF(HIGH,1)) + REF(LOW,1)) + REF(HIGH,2)) +
REF(LOW,2)) + REF(CLOSE,1)) + CLOSE)) / 8) - JJ1);
MJ2:=FORCAST(((2 * EMA(MJ,45)) - EMA(EMA(EMA(MJ,45),45),45)),43);
MJ3:=FORCAST(MJ2,145);
MJ31:=EMA(EMA(EMA(MJ3,105),105),105);
EXITLONG:CROSS(MJ31,JJ1);
ENTERLONG:CROSS(JJ1,MJ31);

{MJ未知}
pp:=ma(close,26);
E:=BETWEEN(ABS(CLOSE-MJ),0,10.5)&&TIME=1458;
B2:=EVERY(CLOSE>PP,10)&&CLOSE-OPEN>3.5;
S2:=(EVERY(CLOSE<PP,10)&&OPEN-CLOSE>3.5)||(REF(OPEN,1)-CLOSE>8.8&&CLOSE<PP&&REF(OPEN,1)>PP);
B3:=CROSS(CLOSE,PP)&&CLOSE-OPEN>3.5&&TIME>0906&&TIME<1450;
S3:=CROSS(PP,CLOSE)&&OPEN-CLOSE>3.5&&TIME>0906&&TIME<1450;
F:=TIME<1458&&ABS(CLOSE-OPEN)>1.2;
(B2||B3)&&F,BK;
(S2||S3)&&F,SK;
((B2||B3)&&F)||E,BP;
((S2||S3)&&F)||E,SP;


当diff下穿dea,并且价格向下突破ema15日线,就卖平同时等价等量卖开指令,
当diff上穿dea,并且价格向上突破ema15日线,就买平同时等价等量买开指令:
DIFF := EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA:= EMA(DIFF,M);
EMA15:=EMA(CLOSE,15);
DIFF>DEA&&CLOSE>EMA15,BPK;
DEA>DIFF&&EMA15>CLOSE,SPK;

这样才比较靠谱:
DIFF := EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA:= EMA(DIFF,M);
EMA15:=EMA(CLOSE,15);
CROSS(DIFF,DEA)&&CLOSE>EMA15,BPK;
CROSS(DEA,DIFF)&&EMA15>CLOSE,SPK;


HH:=HHV(HIGH,N);
LL:=LLV(LOW,N);
HH1:=BARSLAST((HH > REF(HH,1)));
LL1:=BARSLAST((LL < REF(LL,1)));
CROSS(HH1,LL1),SPK;
CROSS(LL1,HH1),BPK;

VAR1:=(HHV(HIGH,8)-LLV(LOW,8));
VAR2:=(HHV(HIGH,8)-CLOSE);
VAR3:=(CLOSE-LLV(LOW,8));
VAR4:=VAR2/VAR1*100-70 ;
VAR5:=(CLOSE-LLV(LOW,60))/(HHV(HIGH,60)-LLV(LOW,60))*100;
VAR6:=(2*CLOSE+HIGH+LOW)/4;
VAR7:=SMA((VAR3/VAR1*100),3,1);
VAR8:=LLV(LOW,30);
VAR9:=SMA(VAR7,3,1)-SMA(VAR4,9,1);
VAR10:= IF(VAR9>100,VAR9-100,0);
VARA:=HHV(HIGH,30);
B:=EMA((VAR6-VAR8)/(VARA-VAR8)*100,8),COLORCYAN;
B1:=EMA(B,5);
B-B1>0,BPK;
B-B1<0,SPK;

SARLINE:=ABS(SAR(3,0.01,0.1));
CLOSE>SARLINE,BPK;
CLOSE<SARLINE,SPK;


LC:=REF(CLOSE,1);
WRSI:=SUM(MAX(CLOSE-LC,0),N)/SUM(ABS(CLOSE-LC),N)*100;
(CROSS(WRSI,LL)),BK;
(CROSS(LH,WRSI)),SP;


VAR1:=LLV(LOW,34);
VAR2:=HHV(HIGH,30);
VAR3:=REF(CLOSE,1);
VAR4:=EMA((CLOSE-VAR1)/(VAR2-VAR1),3)*100;
ZL:=SMA(MAX(CLOSE-VAR3,0),7,1)/SMA(ABS(CLOSE-VAR3),7,1)*100;
CC:=ZL;
SH:=VAR4;
VAR5:=(VAR4-LLV(VAR4,21))/(HHV(VAR4,21)-LLV(VAR4,21))*100;
Q1:=IF(CROSS(VAR5,90) && SH<40,18,10);
Q2:=IF(CROSS(ZL,12) && REF(SH,5)<20 && REF(SH,3)<20,18,10);
P1:=IF(CROSS(84,ZL),82,90);
P2:=IF(CROSS(84,SH),82,90);
VY:=SMA((CLOSE-LLV(LOW,18))/(HHV(HIGH,18)-LLV(LOW,18))*100,5,1);
L2:=SMA(0.618*REF(VY,1)+0.382*VY,3,1);
L3:=MA(L2,5);
L2>L3,BPK;
L2<=L3,SPK;

MA60:=MA(CLOSE,60);
MA20:=MA(CLOSE,20);
MA5:=MA(CLOSE,5);
CLOSE>MA60&&CROSS(MA5,MA20)||CLOSE<MA60&&CROSS(MA5,MA20)&&TIME<1455,BPK;
CLOSE>MA60&&CROSS(MA20,MA5)&&TIME<1455||CLOSE<MA60&&CROSS(MA20,MA5),SPK;
CLOSE>MA60&&CROSS(MA20,MA5)&&TIME=1455,BP;
CLOSE<MA60&&CROSS(MA5,MA20)&&TIME=1455,SP;
修改后再看看
MA60:=MA(CLOSE,60);
MA20:=MA(CLOSE,20);
MA5:=MA(CLOSE,5);
CLOSE>MA60&&CROSS(MA5,MA20)||CLOSE<MA60&&CROSS(MA5,MA20)&&TIME<1455,BPK;
CLOSE>MA60&&CROSS(MA20,MA5)&&TIME<1455||CLOSE<MA60&&CROSS(MA20,MA5),SPK;
CLOSE>MA60&&TIME=1455,BP;
CLOSE<MA60&&TIME=1455,SP;


H:=VALUEWHEN(DATE<>REF(DATE,1),HIGH);
CLOSE>H&&TIME>=0930,BK;
CLOSE>H||TIME>=1430,BP;
CLOSE<H&&TIME>=0930,SK;
CLOSE<H||TIME>=1430,SP;


{漂渺峰沪胶15分钟模型}
[N,1.000000,5.000000,1.000000]
[M,1.000000,20.000000,14.000000]
[B,1.000000,5.000000,2.000000]
A:=EMA(CLOSE,N);
AA:=A-REF(A,M);
BB:=SMA(AA,B,2);
BB>0,BPK;
BB<0,SPK;


WROC:=(CLOSE-REF(CLOSE,N))/REF(CLOSE,N)*100;
(CROSS(WROC,0)),BK;
(CROSS(0,WROC)),SP;


自适应均线的交易模型:
DI:=CLOSE-REF(CLOSE,N);
VO:=SUM(ABS((CLOSE-REF(CLOSE,1))),N);
ER:=ABS(DI/VO);
FASTSC:=2/(2 + 1);
SLOWSC:=2/(30 + 1);
SSC:=ER*(FASTSC-SLOWSC)+SLOWSC;
CONSTANT:=SSC*SSC;
AMAHIGH:REF(EMA(HIGH,N),1)+CONSTANT*(HIGH- REF(EMA(HIGH,N),1));
AMALOW:REF(EMA(LOW,N),1)+CONSTANT*(LOW- REF(EMA(LOW,N),1));
AMACLOSE:=REF(EMA(CLOSE,N),1)+CONSTANT*(CLOSE- REF(EMA(CLOSE,N),1));
LOW>AMAHIGH,BK;
CLOSE<AMACLOSE,SP;
HIGH<AMALOW,SK;
CLOSE>AMACLOSE,BP;