當沖腳本回測無法停損, 單次洗價只能觸發第一次SetPosition,已避免這這種狀況但回測還是無法停損

  •   351 
  • 最後發表   蓬蘇王杜  2025 一月 14
蓬蘇王杜 發文於   2025/01/13

這回測不能停損的問題,我爬文猜測應該是因為條件觸發 SetPosition  的話只能執行第一次SetPosition

但我的腳本應該沒有這個問題, 思考了許久一直想不出來問題在哪......缺高人指點...只好上來當伸手牌...我先道歉
在先謝謝各位


一個單純的當沖腳本

跌破月線放空  漲2.5%停損
時間13.20回補

 




// 設定參數

input: capital(10, "下單金額(萬)"),  // 開倉金額

       stop_loss_percentage(2.5, "停損百分比 (%)"),

       ma_length(20, "月均線天數");

 

// 宣告變數

var: ma_value(0),                  // 月均線值(使用日級別)

     position_size(0),             // 開倉股數

     stop_loss_price(0),           // 停損價格

     current_time(0),              // 當前時間

     day_close(0),                 // 日級別收盤價

     day_high(0);                  // 日級別最高價

 

// 取得日級別數據

day_close = GetField("收盤價", "D");

day_high = GetField("最高價", "D");

 

// 計算日級別的 20 日均線

ma_value = Average(GetField("收盤價", "D"), ma_length);

 

// 取得當前時間

current_time = GetField("時間");

 

// 開倉邏輯:日級別股價剛從月線上方跌破月線,且在交易時間內

if Position = 0 and day_close < ma_value and day_high >= ma_value and current_time < 132000 then begin

    // 計算開倉股數(取整數)

    position_size = IntPortion((capital * 10000) / (Close * 1000));  // 使用 5 分鐘收盤價作為進場價格

    if position_size > 0 then begin

        SetPosition(-position_size);  // 掛出空單委託

    end;

end;

 

// 初始化停損價格

if Position < 0 and Filled < 0 and stop_loss_price = 0 then begin

    stop_loss_price = FilledAvgPrice * (1 + stop_loss_percentage * 0.01);  // 設定初始停損價格

end;

 

// 停損邏輯

if Position < 0 and Filled < 0 and Close > stop_loss_price then begin

    SetPosition(0);  // 停損出場

end;

 

// 強制平倉邏輯:在 13:20 時間平倉

if Position < 0 and Filled < 0 and current_time >= 132000 then begin

    SetPosition(0);  // 強制平倉

end;

 

// 輸出測試資訊

print("當前價格:", Close, " | 月均線:", ma_value, 

      " | 庫存成本:", FilledAvgPrice, " | 停損價格:", stop_loss_price, 

      " | 當前時間:", current_time, " | 日級別收盤價:", day_close, 

      " | 日級別最高價:", day_high);

 

 

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

使用什麼頻率?

蓬蘇王杜 發文於   2025/01/14

五分鐘

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

用5分鐘頻率測試2105正新,是有進出場,但由於停損價在部位結清時並沒有歸零,而是一直延用,所以有些交易一進場就出場。提供你測試的商品及出現問題的交易,這樣好幫你分析問題出在哪裡。

蓬蘇王杜 發文於   2025/01/14

以下提供我回測的選股腳本

// 設定參數

input: ma_length(20, "均線天數"),

       percentage_threshold(2, "均線偏差範圍 (%)"),

       volatility_days(5, "波動率計算天數"),

       volatility_threshold(3, "波動率門檻 (%)"),

       volume_days(10, "成交量計算天數"),

       volume_threshold(2000, "成交量門檻 (張)");

 

// 宣告變數

var: ma_value(0),                // 20日均線值

     xvolatility(0),              // 最近五天的平均波動率

     avg_volume(0),              // 最近十天的平均成交量

     i(0),                       // 迴圈計數器

     total_volatility(0),        // 波動率總和

     total_volume(0);            // 成交量總和

 

// 計算20日均線

ma_value = Average(Close, ma_length);

 

// 檢查今天和昨天的收盤價是否在20日均線的±2%範圍內

if Close >= ma_value * (1 - percentage_threshold / 100) and

   Close <= ma_value * (1 + percentage_threshold / 100) and

   Close[1] >= ma_value * (1 - percentage_threshold / 100) and

   Close[1] <= ma_value * (1 + percentage_threshold / 100) then begin

 

    // 計算最近五天的波動率

    total_volatility = 0;

    for i = 0 to volatility_days - 1 begin

        total_volatility += ((High[i] - Low[i]) / High[i]) * 100;

    end;

    xvolatility = total_volatility / volatility_days;

 

    // 計算最近十天的平均成交量

    total_volume = 0;

    for i = 0 to volume_days - 1 begin

        total_volume += GetField("成交量", "D")[i];

    end;

    avg_volume = total_volume / volume_days;

 

    // 檢查波動率和成交量條件

    if xvolatility >= volatility_threshold and avg_volume >= volume_threshold then begin

        ret = 1;  // 符合條件

    end;

end;

 





我都是使用選股腳本找出快跌破月線的標的來進行回測,現在回測起來有兩個主要問題
1.沒有執行漲2.5的停損
2.有些標的進場就出場




2024/08/01 的國建2501 就有沒停損的問題
2023/08/25 的星通3025  也是在尾盤1305買進又賣出 

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

// 設定參數
input: capital(10, "下單金額(萬)"),  // 開倉金額
       stop_loss_percentage(2.5, "停損百分比 (%)"),
       ma_length(20, "月均線天數");
// 宣告變數
var: ma_value(0),                  // 月均線值(使用日級別)
     position_size(0),             // 開倉股數
     intrabarpersist stop_loss_price(0),           // 停損價格
     current_time(0),              // 當前時間
     day_close(0),                 // 日級別收盤價
     day_high(0);                  // 日級別最高價
// 取得日級別數據
day_close = GetField("收盤價", "D");
day_high = GetField("最高價", "D");
// 計算日級別的 20 日均線
ma_value = Average(GetField("收盤價", "D"), ma_length);
// 取得當前時間
current_time = GetField("時間");
// 開倉邏輯:日級別股價剛從月線上方跌破月線,且在交易時間內
if Position = 0 and day_close < ma_value and day_high >= ma_value and current_time < 132000 then 
    begin
        // 計算開倉股數(取整數)
        position_size = IntPortion((capital * 10000) / (Close * 1000));  // 使用 5 分鐘收盤價作為進場價格
        if position_size > 0 then 
            SetPosition(-position_size);  // 掛出空單委託
    end;
// 平倉
if Position < 0 and Filled < 0 then
    begin
        stop_loss_price = FilledAvgPrice * (1 + stop_loss_percentage * 0.01);
        if Close > stop_loss_price or current_time >= 132000 then 
            begin
                SetPosition(0);  // 停損出場
                stop_loss_price=0;
            end;
    end;

蓬蘇王杜 發文於   2025/01/14

原來僅是縮排的問題嗎!!

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

進場之後馬上出場是邏輯錯誤。stop_loss_price必須在平倉之後歸零。

蓬蘇王杜 發文於   2025/01/14

謝謝教授的回答!! 目前測試起來停損價還是會超標  但狀況已經有好一點,不會有誇張的-10%停損
停損失真或許是模擬逐筆搓的一分k開高低收 本身還是離停損價位很遠? 我的猜測是這樣

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

停損超標是因為回測時用1分鐘洗價造成。

  • 按讚來自於
  • YEAH301000
發表回覆
Close