如何實現由最近的連續虧損次數決定下一次開倉手數? [MC]
-
MC用戶求助:
inputs: Price( close ), FastLength( 9 ), SlowLength( 18 ), MAX_shares(5);
variables: var0( 0 ), var1( 0 ), var2(" "), lots(1);
{max_shares作為初始的輸入參數,表示最大手數;lots是變量,表示下單的手數}
var0 = AverageFC( Price, FastLength ) ;??
var1 = AverageFC( Price, SlowLength ) ;
{var0和var1分別表示快速均線和慢速均線,針對您的想法,我通過在最簡單的均線策略中具體化您的想法}
condition1 = CurrentBar > 1 and var0 crosses over var1 ;
if condition1 then begin
? ? ? ? if marketposition=0 or (marketposition=-1 and openpositionprofit>0) then begin
{conditio1成立表示金叉出現;lots為1的情況只有兩種情況,一種是第一次下單時(通過marketposition=0來表示;第二種是未平倉收益是正的時候,由于不允許加倉,所以使用了marketposition=-1的情況,同時使用openpositionprofit來判斷是否盈利}?
? ? ? ? ? ? ? ? lots=1;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?
? ? ? ? ? ? ? ? Buy ( "MA2CrossLE" ) lots shares next bar at market ;
? ? ? ? end
? ? ? ? else if marketposition=-1 and openpositionprofit<=0 then begin
{若即不是第一次下單,又不是盈利狀態時,此時通過marketposition=-1 and openpositionprofit<=0來判斷,表示虧損,此時會執行下面的語句,也就是取 lots+1和max_shares的最小值賦值給lots,然后下單lots手}
? ? ? ? ? ? ? ? lots=minlist(lots+1,max_shares);
? ? ? ? ? ? ? ? Buy ( "MA2CrossLE1" ) lots shares next bar at market ;
? ? ? ? end;
end;
{以上部位是金叉出現的情況,下面的部位是死叉出現的情況,邏輯類似}
condition1 = CurrentBar > 1 and var0 crosses under var1 ;
if condition1 then begin
? ? ? ? if marketposition=0 or (marketposition=1 and openpositionprofit>0) then begin
? ? ? ? ? ? ? ???lots=1;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
? ? ? ? ? ? ? ? Sell Short ( "MA2CrossSE" ) lots shares next bar at market;
? ? ? ? end
? ? ? ? else if marketposition=1 and openpositionprofit<0 then begin
? ? ? ? ? ? ? ? lots=minlist(lots+1,max_shares);
? ? ? ? ? ? ? ? Sell Short ( "MA2CrossSE1" ) lots shares next bar at market;
? ? ? ? end;
end;
注意事項:
一、整個策略都是使用平倉反向語句,所以下單的手數在下單之前就需要確定,如果先平倉再開倉,這樣會有一定的延遲。
二、使用的是市價單來下單,會立即以對手價成交,不過這樣會和下單判斷語句中openpositionprofit的值有一定的誤差,因為一個是市價之間的盈虧值,另一個是市價成交之后的盈虧值;如果需要更準確的是,那么需要將平倉和開倉分開來寫。
三、這里使用的是市價單委托,如果需要使用條件單,因為條件單不一定會成交,而且成交的時間也不確定,所以條件單和盈虧判斷一起使用的話,需要將平倉和開倉分開來執行,而且也會稍微復雜一些。?
-
MC回復討論一:
inputs: Price( close ), FastLength( 9 ), SlowLength( 18 ), MAX_shares(5);
variables: var0( 0 ), var1( 0 ), var2(" "), lots(1);
{max_shares作為初始的輸入參數,表示最大手數;lots是變量,表示下單的手數}
var0 = AverageFC( Price, FastLength ) ;??
var1 = AverageFC( Price, SlowLength ) ;
{var0和var1分別表示快速均線和慢速均線,針對您的想法,我通過在最簡單的均線策略中具體化您的想法}
condition1 = CurrentBar > 1 and var0 crosses over var1 ;
if condition1 then begin
? ? ? ? if marketposition=0 or (marketposition=-1 and openpositionprofit>0) then begin
{conditio1成立表示金叉出現;lots為1的情況只有兩種情況,一種是第一次下單時(通過marketposition=0來表示;第二種是未平倉收益是正的時候,由于不允許加倉,所以使用了marketposition=-1的情況,同時使用openpositionprofit來判斷是否盈利}?
? ? ? ? ? ? ? ? lots=1;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?
? ? ? ? ? ? ? ? Buy ( "MA2CrossLE" ) lots shares next bar at market ;
? ? ? ? end
? ? ? ? else if marketposition=-1 and openpositionprofit<=0 then begin
{若即不是第一次下單,又不是盈利狀態時,此時通過marketposition=-1 and openpositionprofit<=0來判斷,表示虧損,此時會執行下面的語句,也就是取 lots+1和max_shares的最小值賦值給lots,然后下單lots手}
? ? ? ? ? ? ? ? lots=minlist(lots+1,max_shares);
? ? ? ? ? ? ? ? Buy ( "MA2CrossLE1" ) lots shares next bar at market ;
? ? ? ? end;
end;
{以上部位是金叉出現的情況,下面的部位是死叉出現的情況,邏輯類似}
condition1 = CurrentBar > 1 and var0 crosses under var1 ;
if condition1 then begin
? ? ? ? if marketposition=0 or (marketposition=1 and openpositionprofit>0) then begin
? ? ? ? ? ? ? ???lots=1;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
? ? ? ? ? ? ? ? Sell Short ( "MA2CrossSE" ) lots shares next bar at market;
? ? ? ? end
? ? ? ? else if marketposition=1 and openpositionprofit<0 then begin
? ? ? ? ? ? ? ? lots=minlist(lots+1,max_shares);
? ? ? ? ? ? ? ? Sell Short ( "MA2CrossSE1" ) lots shares next bar at market;
? ? ? ? end;
end;
注意事項:
一、整個策略都是使用平倉反向語句,所以下單的手數在下單之前就需要確定,如果先平倉再開倉,這樣會有一定的延遲。
二、使用的是市價單來下單,會立即以對手價成交,不過這樣會和下單判斷語句中openpositionprofit的值有一定的誤差,因為一個是市價之間的盈虧值,另一個是市價成交之后的盈虧值;如果需要更準確的是,那么需要將平倉和開倉分開來寫。
三、這里使用的是市價單委托,如果需要使用條件單,因為條件單不一定會成交,而且成交的時間也不確定,所以條件單和盈虧判斷一起使用的話,需要將平倉和開倉分開來執行,而且也會稍微復雜一些。
有思路,想編寫各種指標公式,程序化交易模型,選股公式,預警公式的朋友
可聯系技術人員 QQ: 511411198 進行 有償 編寫!(不貴!點擊查看價格!)
相關文章
-
沒有相關內容