自動交易回測 加碼不正常出場

  •   300 
  • 最後發表   蓬蘇王杜  2025 五月 18
蓬蘇王杜 發文於   2025/05/18

以下程式碼再回測時 會大量的不正常出場 , 我只是希望他加碼但是他會變成出場指令 , 我附上 加碼這段程式碼

max_pos = IntPortion((max_order_price * 10000) / (Close * 1000));


//加碼條件
if position > 0 and filled > 0  and close*filled*1000 >= (filledavgPrice*filled*1000)+add_profit then begin
    SetPosition(max_pos, label:="獲利加碼");   
end;

  目前爬文我了解到 回測跟實單應該是有不同的 , 平時用在實單的程式碼要做回測,是有部分需要修改的 
  請問哪裡可以爬文到這相關的內容呢, 想盡可能的模擬真實情況 , 以下我附上全部程式碼  謝謝各位的幫忙!!

 

 

//參數設定
input: order_price(4, "下單金額(萬)"),
       stop_loss(1500, "停損金額"),
       add_profit(1500, "獲利金額加碼條件"),
       max_order_price(10, "加碼後總金額(萬)");

//變數宣告
vars: order_size(0),
      add_order_size(0),
      max_pos(0),
      highest_20(0),
      lowest_10(0),
      stop_price(0),
      profit(0);


//計算指標
highest_20 = Highest(High, 20)[1];
lowest_10 = Lowest(Low, 10)[1];

//計算部位大小
order_size = IntPortion((order_price * 10000) / (Close * 1000));
max_pos = IntPortion((max_order_price * 10000) / (Close * 1000));

//進場邏輯
//這裡加上position = 0 and FIlled= 0 的條件更警慎,並漲幅超過3%才算有效突破
if position = 0 and FIlled= 0 and Close Cross Over highest_20 and close>=close[1]*1.03 then begin  
    SetPosition(order_size, label:="突破20日高進場");                                              
end;


//停損條件
if position > 0 and filled > 0 then begin
   stop_price = ((filledavgPrice*1000*filled*1.003)-stop_loss) / (filled*1000);  //這裡用filledavgPrice抓庫存均價會更準確,這裡的公式為計算要停損的股價
   if close < stop_price then SetPosition(0, label:="停損出場");
end;

//加碼條件
if position > 0 and filled > 0  and close*filled*1000 >= (filledavgPrice*filled*1000)+add_profit then begin
    SetPosition(max_pos, label:="獲利加碼");   
end;

//停利條件
if position > 0 and filled> 0 and Close < lowest_10 then begin
    SetPosition(0, label:="跌破10日低出場");
end;

//顯示資訊
print("停損價", stop_price, "20日高", highest_20, "10日低", lowest_10, "滿倉", max_pos);

 

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

你需要提供不正常出場的範例。你的程式,由於滿倉的數量是由單價決定,在價格上漲後,滿倉的數值變小,加碼時有可能變成出場。例如滿倉本來是10張,已持有9張,後來價格上漲,滿倉的數量變成8張,加碼時setposition(8),會將庫存的9張賣掉1張。

蓬蘇王杜 發文於   2025/05/18

謝謝教授 , 在我回測中每隻股票都有這問題  , 但我聽了教授的指點後 稍微改動了程式碼 , 這樣或許沒問題了 

//進場邏輯
//這裡加上position = 0 and FIlled= 0 的條件更警慎,並漲幅超過3.5%才算有效突破
if position = 0 and FIlled= 0 and Close Cross Over highest_20 and close>=close[1]*1.035 then begin  
    order_size = IntPortion((order_price * 10000) / (Close * 1000));
    if order_size > 0 then  SetPosition(order_size, label:="突破20日高進場");
    hasAdded =  false ;
end;


//停損條件
if position > 0 and filled > 0 then begin
   stop_price = ((filledavgPrice*1000*filled*1.003)-stop_loss) / (filled*1000);  //這裡用filledavgPrice抓庫存均價會更準確,這裡的公式為計算要停損的股價
   if close < stop_price then SetPosition(0, label:="停損出場");
end;

//加碼條件
if position > 0 and filled > 0 and close*filled*1000 >= (filledavgPrice*filled*1000)+add_profit then begin
    if hasAdded = false then begin
        max_pos = IntPortion((max_order_price * 10000) / (Close * 1000));
        if max_pos > 0 then SetPosition(max_pos, label:="獲利加碼"); 
        hasAdded = true; // 僅在條件滿足且尚未加碼時才更新
    end;
end;


//停利條件
if position > 0 and filled> 0 and Close < lowest_10 then begin
    SetPosition(0, label:="跌破10日低出場");
end;

虎科大許教授 發文於   2025/05/18

我會用比較有效率的方式來寫。

//有部位時
if position > 0 and filled > 0 then 
    begin
        //停損條件
        stop_price = ((filledavgPrice*1000*filled*1.003)-stop_loss) / (filled*1000);  //這裡用filledavgPrice抓庫存均價會更準確,這裡的公式為計算要停損的股價
        max_pos = IntPortion((max_order_price * 10000) / (Close * 1000));
        if close < stop_price then SetPosition(0, label:="停損出場");
        //加碼條件
        if close*filled*1000 >= (filledavgPrice*filled*1000)+add_profit then 
            if filled < max_pos then
                begin
                    if hasAdded = false then 
                        begin
                            SetPosition(max_pos, label:="獲利加碼"); 
                            hasAdded = true; // 僅在條件滿足且尚未加碼時才更新
                        end;
                end;
        //停利條件
        if Close < lowest_10 then SetPosition(0, label:="跌破10日低出場");
    end;

蓬蘇王杜 發文於   2025/05/18

謝謝教授

發表回覆
Close