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將整個代碼優化一下。
?
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將整個代碼優化一下。
?
?
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的時刻是空頭進場之后;同理對于,空頭止損,邏輯也是一樣的}
?
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的時刻是空頭進場之后;同理對于,空頭止損,邏輯也是一樣的}