相同手?jǐn)?shù)平倉反向幾次后比如三次,就平倉出場該怎么寫 [MC]
-
MC用戶求助:
input: lot(5), num(3);??//lot是手?jǐn)?shù),num是平倉反向的次數(shù)
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;
{這段是平倉反向的次數(shù)統(tǒng)計(jì);平倉反向一次,flag就減少一次;從持倉為0到開倉之后,flag的值重新賦值為平倉反向的次數(shù)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是買入進(jìn)場的條件;condition2是賣出進(jìn)場的條件;condition3和condition4都是出場條件}
注意:從代碼的效率來說,這樣并不高;您可以使用if else將整個(gè)代碼優(yōu)化一下。?
-
MC回復(fù)討論一:
input: lot(5), num(3);??//lot是手?jǐn)?shù),num是平倉反向的次數(shù)
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;
{這段是平倉反向的次數(shù)統(tǒng)計(jì);平倉反向一次,flag就減少一次;從持倉為0到開倉之后,flag的值重新賦值為平倉反向的次數(shù)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是買入進(jìn)場的條件;condition2是賣出進(jìn)場的條件;condition3和condition4都是出場條件}
注意:從代碼的效率來說,這樣并不高;您可以使用if else將整個(gè)代碼優(yōu)化一下。?
-
MC回復(fù)討論二:
好的,非常感謝,我想再問個(gè)問題,止損之后不再開同向單,比如空單止損了,即使再有開空的條件也不進(jìn)場,要等到開多的條件才會進(jìn)場,該怎么表示呢,之前你幫我寫了一個(gè)新開單和上一單方向不一樣的條件來限制,但是我止盈的也可能是空單,如果是止盈的空單,那是可以接著開空的。我是用setstoploss止損的
?
-
MC回復(fù)討論三:
input: sf(5);
var: mp(0), flag_b(1), flag_s(1);
{原理還是一樣的,都是通過變量來控制條件和相關(guān)的邏輯;這里flag_b為1時(shí),可以買入進(jìn)場,0時(shí)不允許;flag_s為1時(shí),可以賣出進(jìn)場,0時(shí)不允許}
mp=marketposition;
if flag_b=1 and condition1 then begin
? ? ? ? buy next bar at market;
? ? ? ? flag_s=1;
end;
{當(dāng)買入進(jìn)場之后,將flag_s賦值為1,也就是多頭進(jìn)場之后,空頭可以允許進(jìn)場}
if flag_s=1 and condition2 then begin
? ? ? ? sellshort next bar at market;
? ? ? ? flag_b=1;
end;
{空頭進(jìn)場之后,允許多頭進(jìn)場}
setstoploss(bigpointvalue*minmove*sf point);
setprofittarget(bigpointvalue*minmove*sf point);
{使用set系列關(guān)鍵字進(jìn)行實(shí)時(shí)止損止盈}
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;
{當(dāng)多頭止損時(shí),將flag_b賦值為0,表示不允許多頭進(jìn)場,唯一將flag_b賦值為1的時(shí)刻是空頭進(jìn)場之后;同理對于,空頭止損,邏輯也是一樣的}?
-
MC回復(fù)討論四:
input: sf(5);
var: mp(0), flag_b(1), flag_s(1);
{原理還是一樣的,都是通過變量來控制條件和相關(guān)的邏輯;這里flag_b為1時(shí),可以買入進(jìn)場,0時(shí)不允許;flag_s為1時(shí),可以賣出進(jìn)場,0時(shí)不允許}
mp=marketposition;
if flag_b=1 and condition1 then begin
? ? ? ? buy next bar at market;
? ? ? ? flag_s=1;
end;
{當(dāng)買入進(jìn)場之后,將flag_s賦值為1,也就是多頭進(jìn)場之后,空頭可以允許進(jìn)場}
if flag_s=1 and condition2 then begin
? ? ? ? sellshort next bar at market;
? ? ? ? flag_b=1;
end;
{空頭進(jìn)場之后,允許多頭進(jìn)場}
setstoploss(bigpointvalue*minmove*sf point);
setprofittarget(bigpointvalue*minmove*sf point);
{使用set系列關(guān)鍵字進(jìn)行實(shí)時(shí)止損止盈}
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;
{當(dāng)多頭止損時(shí),將flag_b賦值為0,表示不允許多頭進(jìn)場,唯一將flag_b賦值為1的時(shí)刻是空頭進(jìn)場之后;同理對于,空頭止損,邏輯也是一樣的}
有思路,想編寫各種指標(biāo)公式,程序化交易模型,選股公式,預(yù)警公式的朋友
可聯(lián)系技術(shù)人員 QQ: 511411198 進(jìn)行 有償 編寫!(不貴!點(diǎn)擊查看價(jià)格!)
相關(guān)文章
-
沒有相關(guān)內(nèi)容