多空單程式碼.問題

  •   329 
  • 最後發表   賢仔  2025 一月 02
賢仔 發文於   2025/01/02

小編.教授 您好

可以麻煩幫我看看那邊出錯嗎?目前編輯是沒問題.可是試跑回測.只會跑多單謝謝

 

{

多空單停損(%)

}

 

input: profit_percent(1, "停利(%)");

input: loss_percent(1, "停損(%)");

 

 

 

var: long_condition(false); { 進場買進作多 }

var: long_condition1(false);         {進場買進做空 }

均線穿越時以市價買進1張

以成交價為基礎, 設定固定的停損/停利價格, 觸及時出場

}

 

long_condition = eMA(Close, 13) cross over eMA(Close, 34);

 

long_condition1 = eMA(close, 13)cross under eMA(close,34); 

 

if Position = 0 and long_condition then begin

SetPosition(1,market ); { 以市價買進 }

 

if Position = 0 and long_condition1 then begin

SetPosition(-1, MARKET); { 以市價賣進 }

end;

end;

 

if Position = 1 and Filled = 1 then begin

{ 依照成本價格設定停損/停利 }

 

if profit_percent > 0 and Close >= FilledAvgPrice*(1+0.01*profit_percent) then begin

{ 停利 }

SetPosition(0);

end else if loss_percent > 0 and Close <= FilledAvgPrice*(1-0.01*loss_percent) then begin

{ 停損 }

SetPosition(0);

 

if Position = -1 and Filled = -1 then begin

{ 依照成本價格設定停損/停利: 請注意當作空時, 判斷是否獲利的方向要改變 }

 

if profit_percent > 0 and Close <= FilledAvgPrice*(1-0.01*profit_percent) then begin

{ 停利 }

SetPosition(0);

end else if loss_percent > 0 and Close >= FilledAvgPrice*(1+0.01*loss_percent) then begin

{ 停損 }

SetPosition(0);

end;

end;

end;

end;

 

排序方式: 標準 | 最新
虎科大許教授 發文於   2025/01/02

if Position = 0 and long_condition then SetPosition(1,market ); { 以市價買進 }

if Position = 0 and long_condition1 then SetPosition(-1, MARKET); { 以市價賣進 }

 

賢仔 發文於   2025/01/02

感謝。教授

目前測試都正常了!

賢仔 發文於   2025/01/02

教授 您好

剛剛測試可以多空跑單,只是遇到一個問題就是多單他會出清,空單停利/停損點到了沒出清,可否幫我看看那邊出錯,謝謝

虎科大許教授 發文於   2025/01/02

與進場的問題一樣,你不應該將有空頭部位的程式碼放在有多頭部位的程式碼裡面。

if Position = 1 and Filled = 1 then 
    { 依照成本價格設定停損/停利 } 
    if profit_percent > 0 and Close >= FilledAvgPrice*(1+0.01*profit_percent) then 
        begin
            { 停利 }
            SetPosition(0);
        end 
    else 
        if loss_percent > 0 and Close <= FilledAvgPrice*(1-0.01*loss_percent) then 
            { 停損 }
            SetPosition(0);
if Position = -1 and Filled = -1 then
    { 依照成本價格設定停損/停利: 請注意當作空時, 判斷是否獲利的方向要改變 }
    if profit_percent > 0 and Close <= FilledAvgPrice*(1-0.01*profit_percent) then 
        begin
            { 停利 }
            SetPosition(0);
        end 
    else 
        if loss_percent > 0 and Close >= FilledAvgPrice*(1+0.01*loss_percent) then
            { 停損 }
            SetPosition(0);

 

賢仔 發文於   2025/01/02

教授 早安

我有頭緒了,謝謝您

發表回覆
Close