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