我想实现这样的思路
空头 开仓的话要距离 上一次空头 平仓 N 根K线之后, 也就是说 平空 了, 隔开3 根 K 线 再做下一次开仓, 但是 上一笔如果是 平 多仓, 则 不做 限制, 可以下一根就可以马上开仓
只是 要求 新开一笔空单, 要距离上一笔 平空仓 3根K 以上,, 如果上一笔 是平多仓,则不 需要限制!
麻烦帮忙老师给个范例,谢谢了
//上次平仓前一个K的持仓小于0
//EXITBARS是平仓历时,但是平仓当根K返回值是-1,所以判断EXITBARS+1情况
cond: ((ref(holding,EXITBARS+2)<0 and EXITBARS+1>=3 ) or ref(holding,EXITBARS+2)>0)and holding=0;
看下这个是否满足。
老师,这个不行,,还是有 上一笔 平多之后,, 下一根K 不开空的情况,
我只是需要 上一笔 平空的话,如果 下一次开空,就需要隔开3个K线
上一笔平多仓, 开空 就不受影响
我试过老师给的代码,测试加载上去还是不能满足
zs:(TYPE(1)=3 or (TYPE(1)=4 and TYPEBAR(1,4)>=3)) and holding=0;
这样呢?
换个思维去写,也就是说 在N 个周期内,只开一次空仓, 不能开超过2次仓,,
那你需要统计完整的开空条件,那个条件触发的时候 一定是能触发开空下单就行了。
另外4楼的代码其实是可以的,至多应用到你具体代码上需要微调下。你可以单独输出下TYPE, TYPEBAR的返回值进行一些调试。我这里 距离3个K的统计是不包括平空K本身的。
diff := ema( close,12 ) - ema( close,26 ); dea := ema( diff,9 ) ;
macd := 2 * ( diff - dea ) ;
zs:=(TYPE(1)=3 or (TYPE(1)=4 and TYPEBAR(1,4)>=3)) and holding=0;
Buycondition := ref( cross( diff,dea ),1 ) ;
Sellcondition := ZS and ref( cross( dea,diff ),1 ) ;
//--------------------- 先 平 后 开 ----------------------------------------------------------
if ref( cross( dea,diff ),1 ) then sell( holding > 0, holding , limitr, open - 1 * mindiff ), ignorecheckprice ;
//------------
if ref( cross( diff,dea ),1 ) then sellshort( holding < 0, holding , limitr, open + 1 * mindiff ), ignorecheckprice ;
//--------------------- 多 空 进 场 -------------------------------------------------------------------------
if holding = 0 and Buycondition then buy( 1, 1, limitr, open + 1 * mindiff ), ignorecheckprice ;
if holding = 0 and Sellcondition then buyshort( 1, 1, limitr, open - 1 * mindiff ), ignorecheckprice ;
老师试一下这个代码,看哪里出问题了,我按照老师的代码,加载进去几次,测试出来都是空头没交易的,帮忙看看哪里出问题了
diff := ema( close,12 ) - ema( close,26 );
dea := ema( diff,9 ) ;
macd := 2 * ( diff - dea ) ;
Buycondition:ref( cross( diff,dea ),1 ),NODRAW ;
Sellcondition:ref( cross( dea,diff ),1 ),NODRAW;
if ref( cross( dea,diff ),1 ) then sell( holding > 0, holding , limitr, open - 1 * mindiff ), ignorecheckprice ;
if ref( cross( diff,dea ),1 ) then sellshort( holding < 0, holding , limitr, open + 1 * mindiff ), ignorecheckprice ;
if holding = 0 and Buycondition then buy( 1, 1, limitr, open + 1 * mindiff ),ignorecheckprice ;
zs:(TYPE(1)=2 or (TYPE(1)=4 and TYPEBAR(1,4)>=3)),NODRAW; //调整下位置,因为你这个有很多是同个K平多开空的,因此这行代码需要在这里,才能正确判断
if holding = 0 and Sellcondition and zs then buyshort( 1, 1, limitr, open - 1 * mindiff ), ignorecheckprice ;
此外你这种上下穿作为开平条件的 平多开空或者平空开多很多都是一个K上触发的,你希望的至少三个K之后开空,好像是无法触发的。因为开空触发的那个必然也是平多触发的,按照你之前给的思路,那也就直接开空了。
看下上面的,之前是有个地方写错了,函数参数写错了。另外你原先把zs直接放到Sellcondition里面是不行的,这个也要注意下。
[此贴子已经被作者于2018/8/3 10:48:49编辑过]