环境介绍:
//运行周期:1分钟
//运行模式:固定时间间隔,1秒轮询
//运行品种:IF1303
会多个策略对改品种下单,希望各策略的开平仓独立,每个策略管好自己的开平仓
系统很简单,就是最高价突破均线平空开多,最低价突破平多开空,每根K线都只开平一次,开仓后当根K线后面的信号可以忽略
利用火哥的模板http://www.weistock.com/bbs/dispbbs.asp?boardid=10&id=9112
蓝色为测试的图表交易系统:
globalvariable:hold=drawnull;
//以下为系统内容
ss:=1; //手数
//variable:myenterbars=0;
maa:ema(c,10);
tt1:=h>maa;
tt2:=l<maa;
//当开始没有持仓
if holding=0 then begin
if tt1 then begin
buy(1,ss,market)
goto continueline;
end
if tt2 then begin
buyshort(1,ss,market);
goto continueline;
end
end
if tt1 and holding<0 and enterbars>=0 then begin
sellshort(1,ss,market);
buy(1,ss,market);
goto continueline;
end
if tt2 and holding>0 and enterbars>=0 then begin
sell(1,ss,market);
buyshort(1,ss,market);
goto continueline;
end
continueline@ 资产:tasset,linethick0;
aa-tt:if(tt1,1,if(tt2,-1,0)),nodraw;
bb-holding:holding,nodraw;
cc-enterbars:enterbars,nodraw;
//以上为模型内容
cc803555:=holding;//这句放在信号稳定的地方
drawtextex(1,1,800,0,'虚拟持仓为:'+numtostr(cc803555,0));//在图表上输入虚拟持仓以便监控
if not(islastbar) or workmode<>1 then exit;
xiadan803555:=cc803555-hold;
if xiadan803555>0.5 then begin
cang:=min(xiadan803555,abs(hold));
if hold<0 then begin
tsellshort(1,cang,mkt,0,0,'803555'),allowrepeat;
debugfile('d:\803555.txt',numtostr(hold,0)+' '+numtostr(cc803555,0)+' 平空 %.0f',cang);
end
cang:=xiadan803555+min(hold,0);
if cang>0 then begin
tbuy(1,cang,mkt,0,0,'803555'),allowrepeat;
debugfile('d:\803555.txt',numtostr(hold,0)+' '+numtostr(cc803555,0)+' 开多 %.0f',cang);
end
end
if xiadan803555<-0.5 then begin
cang:=min(abs(xiadan803555),abs(hold));
if hold>0 then begin
tsell(1,cang,mkt,0,0,'803555'),allowrepeat;
debugfile('d:\803555.txt',numtostr(hold,0)+' '+numtostr(cc803555,0)+' 平多 %.0f',cang);
end
cang:=abs(xiadan803555)-max(hold,0);
if cang>0 then begin
tbuyshort(1,cang,mkt,0,0,'803555'),allowrepeat;
debugfile('d:\803555.txt',numtostr(hold,0)+' '+numtostr(cc803555,0)+' 开空 %.0f',cang);
end
end
hold:=cc803555;
运行环境:(1手单)1分钟 IF1303 逐K模式 账号为虚拟账号
图表系统单独运行,测试正常
套入后台模板后,有几个问题:
1)单账号没有持仓的时候,交易正常
2)当我手动将账号调成2手多头2手空头同时持有,再加载这个后台系统,系统开始只会把其中的一手空仓或者多仓做反手(就是系统一直都是持有4手单),总单量没变
3)之前在图表加载的时候,一根K线只会执行一次开平仓,但套入后套模板后,却会在同一K线出现多次开平仓(不知道是不是因为模板里面的allowrepeat函数问题),
请问是否将其去除就不会出现多次开平仓的问题?
2.今天1:30左右模拟交易服务器出了问题
3.是的,allowrepeat允许指令在同一个周期内反复发出信号.
同样的交易系统,我也写了一个后台程序:
ss:=1; //手数
extgbdataset('t1_position',0);
//0表示没有仓位,1表示持有多头, -1表示持有空头
extgbdataset('t1_holding',0);
//0表示没有仓位,>0表示持有多头, <0表示持有空头
extgbdataset('t1_enterbarpos',0);//记录其开仓的K线
maa:ema(c,10);
bpk:=h>maa;
spk:=l<maa;
//非最后一根K线退出
if not(islastbar) or workmode<>1 then exit;
//如果当是最后一根k线,执行
IF islastbar and time<151000 then begin
// 如果最后一根k线发生过开仓信号,则那一根k线不再交易
if extgbdata('t1_enterbarpos') = barpos then begin
goto continueline ;
end
//没有持仓状态
if extgbdata('t1_position')=0 and extgbdata('t1_holding')=0 then begin
if bpk then begin
tbuy(1,ss,mkt);
extgbdataset('t1_position',1);
extgbdataset('t1_holding',ss);
extgbdataset('t1_enterbarpos',barpos);
goto continueline ;
end
if spk then begin
tbuyshort(1,ss,mkt);
extgbdataset('t1_position',-1);
extgbdataset('t1_holding',-ss);
extgbdataset('t1_enterbarpos',barpos);
goto continueline ;
end
end//没有持仓状态
//持有仓位状态
//持有空头
if bpk and extgbdata('t1_position')=-1 and extgbdata('t1_holding')<0 then begin
tsellshort(1,ss,mkt);
tbuy(1,ss,mkt);
extgbdataset('t1_position',1);
extgbdataset('t1_holding',ss);
extgbdataset('t1_enterbarpos',barpos);
goto continueline ;
end
//持有多头
if spk and extgbdata('t1_position')=1 and extgbdata('t1_holding')>0 then begin
tsell(1,ss,mkt);
tbuyshort(1,ss,mkt);
extgbdataset('t1_position',-1);
extgbdataset('t1_holding',-ss);
extgbdataset('t1_enterbarpos',barpos);
goto continueline ;
end
END//if ISLASTBAR
if time>=151300 then begin
tsell(extgbdata('t1_holding')>0,ss,mkt);
tsellshort(extgbdata('t1_holding')<0,ss,mkt);
extgbdataset('t1_position',0);
extgbdataset('t1_holding',0);
end
continueline@ 资产:tasset,linethick0;
position:=extgbdata('t1_position');
t1holding:=extgbdata('t1_holding');
debugfile('d:\debug\803555.txt','position=%.0f' ,position) ;
debugfile('d:\debug\803555.txt','t1holding=%.0f' ,t1holding) ;
如果上模板不能很好执行,不知道我这个能否可行,谢谢
蓝色部分少做修改,供您参考
楼主的公式,固定时间间隔,照1楼所写,会造成信号闪烁,推荐将条件适当修改,以使信号稳定,才能使用阿火的策略
修改部分红色显示
ss:=1; //手数
//variable:myenterbars=0;
maa:ref(ema(c,10),1);
buycond:=h>maa;
sellcond:=l<maa;
if holding>0 and sellcond then sell(1,ss,market);
if holding<0 and buycond then sellshort(1,ss,market);
if holding=0 and buycond then buy(1,ss,market);
if holding=0 and sellcond then buyshort(1,ss,market);
固定时间间隔,有信号就下单,信号闪烁情况下造成HOLDING值不稳定,故不能调用图表的HOLDING来控制仓位
必须使用后台程序化交易,
对3楼的少做修改,供您参考
全局变量使用注意事项:
策略运行过程中,手动平仓进行干预,请到"工具--数据--全局变量"里,将对应的全局变量清0,否则会引起开平仓混乱
//序列模式运行
//t1_flag 0表示没有仓位,1表示持有多头,-1表示持有空头
ss:=1; //手数
maa:ema(c,5);
buycond:=h>maa;
sellcond:=l<maa;
//平多
if extgbdata('t1_flag')>0 and sellcond then
begin
tsell(1,ss,mkt);
extgbdataset('t1_flag',0);
end
//平空
if extgbdata('t1_flag')<0 and buycond then
begin
tsellshort(1,ss,mkt);
extgbdataset('t1_flag',0);
end
//开多
if extgbdata('t1_flag')=0 and buycond then
begin
tbuy(1,ss,mkt);
extgbdataset('t1_flag',1);
end
//开空
if extgbdata('t1_flag')=0 and sellcond then
begin
tbuyshort(1,ss,mkt);
extgbdataset('t1_flag',-1);
end
平仓反手下,一根K线只能有一次开平仓
//序列模式运行
//t1_flagd 0表示没有仓位,1表示持有多头,-1表示持有空头
//bar控制一根K线只能有一次开平仓
ss:=1; //手数
maa:ema(c,5);
buycond:=h>maa;
sellcond:=l<maa;
//平多
if extgbdata('t1_flag')>0 and sellcond and barpos>extgbdata('bar') then
begin
tsell(1,ss,mkt);
extgbdataset('t1_flag',0);
end
//平空
if extgbdata('t1_flag')<0 and buycond and barpos>extgbdata('bar') then
begin
tsellshort(1,ss,mkt);
extgbdataset('t1_flag',0);
end
//开多
if extgbdata('t1_flag')=0 and buycond then
begin
tbuy(1,ss,mkt);
extgbdataset('t1_flag',1);
extgbdataset('bar',barpos);
end
//开空
if extgbdata('t1_flag')=0 and sellcond then
begin
tbuyshort(1,ss,mkt);
extgbdataset('t1_flag',-1);
extgbdataset('bar',barpos);
end
老师,还有个小问题想请教
你的模型在没有持仓的环境下已经测试过,可以正常运行
然后在换一下环境,在账号里面先各开一张多单和空单,然后把全局变量清零
之后开始运行,发现监控其启动操作,第一个操作时平仓
这样如果账号有持仓就会少了1手单,请问这样应该怎么加条件限制,第一次只开仓而不平仓呢?
麻烦老师,谢谢!!!
你同样的策略在图表上加载了,
或者是全局变量清0的步骤没处理好