等级: 免费版
- 注册:
- 2023-10-12
- 曾用名:
|

楼主 |
发表于 2023-10-27 15:09
|
显示全部楼层
老师您好,针对这个策略,我参照多头调整后的代码写了段空头方向的,交易条件略做了一些调整,经回测结果好像还是有点问题,还想请老师帮看下(应该是出在空头交易代码中)哪里有问题;另外,各个交易指令都写成了模块化的,如果做调整的话也可以写成模块化的方式嘛(比如按多头入场-多头止损-止损后加仓-多头出场-空头入场-空头止损-止损后加仓-空头出场这样的),主要是刚开始这样写好理解一点
//声明变量
input:s(12,5,40,4),p(26,20,100,8),m(9,1,100,10);
input:n(2,1,10,1),m1(20,1,1000,1);
input:m2(14,1,50,1);
input:amounts(1);
input:s_out(1.05,0.9,3,0.1),s_in(0.97,0.7,1,0.01),s_atr(2,0.5,3,0.1);
variable:mark:=0; //多头止损标记
variable:mark1:=0; //空头止损标记
variable:one_enterprice:=0; //标记多头开仓(非止损后买入)入场价
variable:one_enterprice1:=0; //标记空头开仓(非止损后买入)入场价
//中间变量
diff:=ema(close,s) - ema(close,p);
dea:=ema(diff,m);
macd1:=2*(diff-dea), colorstick;
bbi:(ma(close,3)+ma(close,6)+ma(close,12)+ma(close,24))/4;
upr:bbi+n*std(bbi,m1);
dwn:bbi-n*std(bbi,m1);
tr1:=max(max((high-low),abs(ref(close,1)-high)),abs(ref(close,1)-low));
atr:=ma(tr1,m2);
//买入开仓
cond_buy1:=cross(diff,dea); //macd金叉
cond_buy3:=close/dwn<=s_in; //最新价突破boll下轨超过3%
if cond_buy1 or cond_buy3 then begin
sellshort(holding<0,0,market); //先平再开
buy(holding=0,amounts,market); //开仓
one_enterprice:=enterprice;
end
//买入止损
cond_stop_buy:=one_enterprice-close>=s_atr*atr; //两倍atr止损
if holding>0 and cond_stop_buy and enterbars>0 then begin
止损:sell(1,0,market); //当买入后触发2倍atr止损
mark:=1; //标记下 当前触发了止损了
end
//止损后开仓
cond_buy4:=close>=one_enterprice-s_atr*atr; //止损后反弹至止损位时重新开入订单
if mark=1 and cond_buy4 then begin
反弹开仓:buy(holding=0,amounts,market); //止损后反弹至2倍atr后重新买入
mark:=0;
end
//卖出平仓
cond_sell1:=cross(dea,diff); //macd死叉
cond_sell2:=close/upr>=s_out; // 最新价穿过boll上轨超过5%
if holding>0 and enterbars>0 and (cond_sell1 or cond_sell2) then begin
sell(1,0,market); //平多仓
end
//卖出开仓
cond_buyshort1:=cross(dea,diff); //死叉
cond_buyshort2:=upr/close<=s_in; //最新价突破boll上轨超过3%
if cond_buyshort1 or cond_buyshort2 then begin
sell(holding>0,0,market); //先平再开
buyshort(holding=0,amounts,market); //开空仓
one_enterprice1:=enterprice;
end
//卖出止损
cond_stop_buyshort:=close-one_enterprice1>=s_atr*atr; //两倍atr止损
if holding<0 and cond_stop_buyshort and enterbars>0 then begin
止损1:sellshort(1,0,market); //止损
mark1:=1; //标记止损
end
//止损后开仓
cond_buyshort3:=close<=one_enterprice1+s_atr*atr; //止损后反弹至2倍atr后重新开空仓
if mark1=1 and cond_buyshort3 then begin
反弹开仓1:buyshort(holding=0,amounts,market); //反弹开仓
mark1:=0;
end
//买入平仓
cond_sellshort1:=cross(diff,dea); //金叉
cond_sellshort2:=dwn/close>=s_out; //最新价突破boll下轨超过5%
if holding<0 and enterbars>0 and (cond_sellshort1 or cond_sellshort2) then begin
sellshort(1,0,market); //平空仓
end
持仓:holding,linethick0;
//资产:asset,noaxis;
//可用现金:cash(0),linethick2,coloryellow,noaxis;
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?
x
|