相同手数平仓反向几次后比如三次,就平仓出场该怎么写 [MC]
-
MC用户求助:
input: lot(5), num(3); //lot是手数,num是平仓反向的次数
var: mp(0), flag(num);
mp=marketposition;
if mp[1]*mp=-1 then
flag=flag-1
else if mp[1]=0 and mp<>0 then
flag=num;
{这段是平仓反向的次数统计;平仓反向一次,flag就减少一次;从持仓为0到开仓之后,flag的值重新赋值为平仓反向的次数num}
if marketposition=0 and condition1 then
buy lot shares next bar at market;
if marketposition=0 and condition2 then
sellshort lot shares next bar at market;
if marketposition=-1 and condition1 and flag>0 then
buy lot shares next bar at market;
if marketposition=1 and condition2 and flag>0 then
sellshort lot shares next bar at market;
if marketposition=1 and condition3 and flag=0 then
sell next bar at market;
if marketposition=-1 and condition4 and flag=0 then
buytocover next bar at market;
{condition1是买入进场的条件;condition2是卖出进场的条件;condition3和condition4都是出场条件}
注意:从代码的效率来说,这样并不高;您可以使用if else将整个代码优化一下。 -
MC回复讨论一:
input: lot(5), num(3); //lot是手数,num是平仓反向的次数
var: mp(0), flag(num);
mp=marketposition;
if mp[1]*mp=-1 then
flag=flag-1
else if mp[1]=0 and mp<>0 then
flag=num;
{这段是平仓反向的次数统计;平仓反向一次,flag就减少一次;从持仓为0到开仓之后,flag的值重新赋值为平仓反向的次数num}
if marketposition=0 and condition1 then
buy lot shares next bar at market;
if marketposition=0 and condition2 then
sellshort lot shares next bar at market;
if marketposition=-1 and condition1 and flag>0 then
buy lot shares next bar at market;
if marketposition=1 and condition2 and flag>0 then
sellshort lot shares next bar at market;
if marketposition=1 and condition3 and flag=0 then
sell next bar at market;
if marketposition=-1 and condition4 and flag=0 then
buytocover next bar at market;
{condition1是买入进场的条件;condition2是卖出进场的条件;condition3和condition4都是出场条件}
注意:从代码的效率来说,这样并不高;您可以使用if else将整个代码优化一下。 -
MC回复讨论二:
好的,非常感谢,我想再问个问题,止损之后不再开同向单,比如空单止损了,即使再有开空的条件也不进场,要等到开多的条件才会进场,该怎么表示呢,之前你帮我写了一个新开单和上一单方向不一样的条件来限制,但是我止盈的也可能是空单,如果是止盈的空单,那是可以接着开空的。我是用setstoploss止损的
-
MC回复讨论三:
input: sf(5);
var: mp(0), flag_b(1), flag_s(1);
{原理还是一样的,都是通过变量来控制条件和相关的逻辑;这里flag_b为1时,可以买入进场,0时不允许;flag_s为1时,可以卖出进场,0时不允许}
mp=marketposition;
if flag_b=1 and condition1 then begin
buy next bar at market;
flag_s=1;
end;
{当买入进场之后,将flag_s赋值为1,也就是多头进场之后,空头可以允许进场}
if flag_s=1 and condition2 then begin
sellshort next bar at market;
flag_b=1;
end;
{空头进场之后,允许多头进场}
setstoploss(bigpointvalue*minmove*sf point);
setprofittarget(bigpointvalue*minmove*sf point);
{使用set系列关键字进行实时止损止盈}
if mp[1]=1 and mp=0 and positionprofit(1)<0 then
flag_b=0
else if mp[1]=-1 and mp=0 and positionprofit(1)<0 then
flag_s=0;
{当多头止损时,将flag_b赋值为0,表示不允许多头进场,唯一将flag_b赋值为1的时刻是空头进场之后;同理对于,空头止损,逻辑也是一样的} -
MC回复讨论四:
input: sf(5);
var: mp(0), flag_b(1), flag_s(1);
{原理还是一样的,都是通过变量来控制条件和相关的逻辑;这里flag_b为1时,可以买入进场,0时不允许;flag_s为1时,可以卖出进场,0时不允许}
mp=marketposition;
if flag_b=1 and condition1 then begin
buy next bar at market;
flag_s=1;
end;
{当买入进场之后,将flag_s赋值为1,也就是多头进场之后,空头可以允许进场}
if flag_s=1 and condition2 then begin
sellshort next bar at market;
flag_b=1;
end;
{空头进场之后,允许多头进场}
setstoploss(bigpointvalue*minmove*sf point);
setprofittarget(bigpointvalue*minmove*sf point);
{使用set系列关键字进行实时止损止盈}
if mp[1]=1 and mp=0 and positionprofit(1)<0 then
flag_b=0
else if mp[1]=-1 and mp=0 and positionprofit(1)<0 then
flag_s=0;
{当多头止损时,将flag_b赋值为0,表示不允许多头进场,唯一将flag_b赋值为1的时刻是空头进场之后;同理对于,空头止损,逻辑也是一样的}
有思路,想编写各种指标公式,程序化交易模型,选股公式,预警公式的朋友
可联系技术人员 QQ: 511411198 进行 有偿 编写!(不贵!点击查看价格!)
相关文章
-
没有相关内容