相同手數平倉反向幾次后比如三次,就平倉出場該怎么寫 [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 進行 有償 編寫!(不貴!點擊查看價格!)
相關文章
-
沒有相關內容