后台程序化函数例如THOLDING返回的是当前我们实际的持仓,故多策略同品种会出现因为持仓和资金相互干扰的现象。解决方案是使用图表的HOLING的虚拟持仓和资金与后台的TBUY,TSELL等混用的方案,每个策略里的持仓和资金都是自己独立的,这样就完全可以避免这种共振现象,但是推荐高级用户使用。
INPUT:P(50,2,200,1){建仓量},P1(1,0,50,1){初始止损幅度},P2(5,2,100,1){止盈幅度},P3(30,5,60,5){回撤止盈};
VARIABLE:MAXPROFIT=0,{有仓位时最大获利幅度}VMIN = 090000;{用于隔夜高开或低开时间差}
WIN1:=0;
WIN2:=0;//止盈、止损、回撤控制
交易时间:=TIME>VMIN AND TIME<151430;
a:=open+10;
b:=open-10;
HNL:=IF(HIGH>REF(HHV(HIGH,3),1),LOW,0);
L1:=IF(HNL>REF(L,1),REF(L,1),IF(HNL>REF(L,2),REF(L,2),IF(HNL>REF(L,3),REF(L,3),IF(HNL>REF(L,4),REF(L,4),0))));
L2:=IF(L1>REF(L,1),REF(L,1),IF(L1>REF(L,2),REF(L,2),IF(L1>REF(L,3),REF(L,3),IF(L1>REF(L,4),REF(L,4),0))));
L3:=VALUEWHEN(L2>0,L2);
LNH:=IF(LOW<REF(LLV(LOW,3),1),HIGH,99999);
H1:=IF(LNH<REF(H,1),REF(H,1),IF(LNH<REF(H,2),REF(H,2),IF(LNH<REF(H,3),REF(H,3),IF(LNH<REF(H,4),REF(H,4),99999))));
H2:=IF(H1<REF(H,1),REF(H,1),IF(H1<REF(H,2),REF(H,2),IF(H1<REF(H,3),REF(H,3),IF(H1<REF(H,4),REF(H,4),0))));
H3:=VALUEWHEN(H2>0,H2);
SEL:=VALUEWHEN((CLOSE>H3 and REF(CLOSE,1)<=H3)or(CLOSE<L3 and REF(CLOSE,1)>=L3),IF(CLOSE>H3 and REF(CLOSE,1)<=H3,1,0));
LINE:IF(SEL=1,L3,H3),COLORblue,LINETHICK2;
DRAWNUMBER(ISLASTBAR,LINE,LINE,0,COLORblue);
//建立多头的进场条件
long:=H>a AND C>LINE ;
if long then
begin
sellshort(holding<0,0,MARKET);
buy(holding=0,p,limitr,a);
end
//建立空头的进场条件
short:=L<b AND C<LINE ;
if short then
begin
sell(holding>0,0,MARKET);
buyshort(holding=0,p,limitr,b);
end
//盈亏计算
IF ENTERBARS>0 THEN BEGIN
WIN1:=(C-ENTERPRICE)/ENTERPRICE*100;
IF WIN1>MAXPROFIT THEN
MAXPROFIT:=WIN1;
WIN2:=(MAXPROFIT-WIN1)/MAXPROFIT*100;
END
//多头初始浮亏 P1% 止损
IF WIN1<-P1 THEN
SELL(1,HOLDING,LIMITR,CLOSE);
//多头利润大于 P2% 止盈
IF WIN1>P2 THEN
SELL(1,HOLDING,LIMITR,CLOSE);
//多头获利后回撤 P3%止盈
IF WIN2>P3 AND OPENPROFIT>0 THEN
SELL(1,HOLDING,LIMITR,CLOSE);
//END
// IF HOLDING<0 THEN BEGIN
//空头平仓
// IF 平空 THEN
// SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//空头收盘平仓
// IF NOT(交易时间) THEN
// SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//盈亏计算
IF ENTERBARS>0 THEN BEGIN
WIN1:=(ENTERPRICE-C)/ENTERPRICE*100;
IF WIN1>MAXPROFIT THEN
MAXPROFIT:=WIN1;
WIN2:=(MAXPROFIT-WIN1)/MAXPROFIT*100;
END
//空头初始浮亏超过 P1% 止损
IF WIN1<-P1 THEN
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//空头利润大于 P2%止盈
IF WIN1>P2 THEN
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//空头回撤 P3% 止盈
IF WIN2>P3 AND OPENPROFIT>0 THEN
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
// END
后台策略如下:
因为楼主没有搞清楚后台自动交易的原理,所以我们采用了依旧取前台信号,后台下单的方法来解决问题。
希望楼主仔细研究下面的代码,至于后台的工作机理部分,希望楼主日后熟练使用金字塔的交易策略后再来自己尝试编写更复杂的后台策略。
下面代码红色部分就是我们给你添加的代码。
注:为用了虚拟持仓,后台就只能逐周期模式。
INPUT:P(50,2,200,1){建仓量},P1(1,0,50,1){初始止损幅度},P2(5,2,100,1){止盈幅度},P3(30,5,60,5){回撤止盈};
VARIABLE:MAXPROFIT=0,{有仓位时最大获利幅度}VMIN = 090000;{用于隔夜高开或低开时间差}
WIN1:=0;
WIN2:=0;//止盈、止损、回撤控制
交易时间:=TIME>VMIN AND TIME<151430;
a:=open+10;
b:=open-10;
HNL:=IF(HIGH>REF(HHV(HIGH,3),1),LOW,0);
L1:=IF(HNL>REF(L,1),REF(L,1),IF(HNL>REF(L,2),REF(L,2),IF(HNL>REF(L,3),REF(L,3),IF(HNL>REF(L,4),REF(L,4),0))));
L2:=IF(L1>REF(L,1),REF(L,1),IF(L1>REF(L,2),REF(L,2),IF(L1>REF(L,3),REF(L,3),IF(L1>REF(L,4),REF(L,4),0))));
L3:=VALUEWHEN(L2>0,L2);
LNH:=IF(LOW<REF(LLV(LOW,3),1),HIGH,99999);
H1:=IF(LNH<REF(H,1),REF(H,1),IF(LNH<REF(H,2),REF(H,2),IF(LNH<REF(H,3),REF(H,3),IF(LNH<REF(H,4),REF(H,4),99999))));
H2:=IF(H1<REF(H,1),REF(H,1),IF(H1<REF(H,2),REF(H,2),IF(H1<REF(H,3),REF(H,3),IF(H1<REF(H,4),REF(H,4),0))));
H3:=VALUEWHEN(H2>0,H2);
SEL:=VALUEWHEN((CLOSE>H3 and REF(CLOSE,1)<=H3)or(CLOSE<L3 and REF(CLOSE,1)>=L3),IF(CLOSE>H3 and REF(CLOSE,1)<=H3,1,0));
LINE:IF(SEL=1,L3,H3),COLORblue,LINETHICK2;
DRAWNUMBER(ISLASTBAR,LINE,LINE,0,COLORblue);
//建立多头的进场条件
long:=c>a AND C>LINE ;
if long then
begin
tsellshort(holding<0,0,mkt); //注意:将后台交易代码放在虚拟交易代码前面,不然sellshort后holding就变0了
sellshort(holding<0,0,MARKET);
tbuy(holding=0,lmt,a);
buy(holding=0,p,limitr,a);
end
//建立空头的进场条件
short:=c<b AND C<LINE ;
if short then
begin
tsell(holding>0,0,mkt);
sell(holding>0,0,MARKET);
tbuyshort(holding=0,p,lmt,b);
buyshort(holding=0,p,limitr,b);
end
//盈亏计算
IF ENTERBARS>0 THEN BEGIN
WIN1:=(C-ENTERPRICE)/ENTERPRICE*100;
IF WIN1>MAXPROFIT THEN
MAXPROFIT:=WIN1;
WIN2:=(MAXPROFIT-WIN1)/MAXPROFIT*100;
END
//多头初始浮亏 P1% 止损
IF WIN1<-P1 THEN
begin
TSELL(1,HOLDING,MKT);
SELL(1,HOLDING,LIMITR,CLOSE);
end
//多头利润大于 P2% 止盈
IF WIN1>P2 THEN
begin
TSELL(1,HOLDING,MKT);
SELL(1,HOLDING,LIMITR,CLOSE);
end
//多头获利后回撤 P3%止盈
IF WIN2>P3 AND OPENPROFIT>0 THEN
begin
TSELL(1,HOLDING,MKT);
SELL(1,HOLDING,LIMITR,CLOSE);
end
//END
// IF HOLDING<0 THEN BEGIN
//空头平仓
// IF 平空 THEN
// SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//空头收盘平仓
// IF NOT(交易时间) THEN
// SELLSHORT(1,HOLDING,LIMITR,CLOSE);
//盈亏计算
IF ENTERBARS>0 THEN BEGIN
WIN1:=(ENTERPRICE-C)/ENTERPRICE*100;
IF WIN1>MAXPROFIT THEN
MAXPROFIT:=WIN1;
WIN2:=(MAXPROFIT-WIN1)/MAXPROFIT*100;
END
//空头初始浮亏超过 P1% 止损
IF WIN1<-P1 THEN
BEGIN
TSELLSHORT(1,HOLDING,MKT);
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
END
//空头利润大于 P2%止盈
IF WIN1>P2 THEN
BEGIN
TSELLSHORT(1,HOLDING,MKT);
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
END
//空头回撤 P3% 止盈
IF WIN2>P3 AND OPENPROFIT>0 THEN
BEGIN
TSELLSHORT(1,HOLDING,MKT);
SELLSHORT(1,HOLDING,LIMITR,CLOSE);
END
// END