微台交易腳本顯示資料準備不足

  •   294 
  • 最後發表   蓬蘇王杜  2025 二月 07
蓬蘇王杜 發文於   2025/02/06

寫了一個簡單的微台策略 突破或跌破最近30根30分k的最大量k收盤就做空或做多
用虛擬帳號跑卻出現異常 顯示資料不足,爬了文也沒找到相關資料,自己檢查程式碼也沒檢查出異常

準備資料我只設定50筆,實在是沒想出哪裡會資料準不足 ,這個腳本沒有使用到很長的資料

以下附上程式碼 以及交易腳本設定畫面和異常畫面  謝謝各位!!

 

 

 

 

// 設定參數
Input: StopLossPoints1(100, "1 口停損點數"),
       StopLossPoints2(50, "2 口停損點數"),
       ProfitAddPoints(50, "加碼獲利點數"),
       MaxPosition(2, "最大部位數"),
       VolumeLookback(30, "最大量 K 棒回溯期數"),
       MAExitLookback(20, "均線停利回溯期數");

Var: maxVolClose(0), maExit(0);

// 計算最近 30 根最大量 K 棒的收盤價
maxVolClose = Close[Highest(Volume, VolumeLookback)];

// 計算 20 均線
maExit = Average(Close, MAExitLookback);

// **進場邏輯**
If Position = 0 Then Begin
    If Close Cross Above maxVolClose Then Begin
        SetPosition(1);  // **做多 1 口**
        Print("【進場】 做多 1 口 | 參考K棒收盤價:", maxVolClose, " | 均線價格:", maExit);
    End
    Else If Close Cross Below maxVolClose Then Begin
        SetPosition(-1); // **做空 1 口**
        Print("【進場】 做空 1 口 | 參考K棒收盤價:", maxVolClose, " | 均線價格:", maExit);
    End;
End;

// **停損邏輯**
If Position = 1 Then Begin
    If Close <= FilledAvgPrice - StopLossPoints1 Then Begin
        Print("【停損】 多單出場 | 停損價:", FilledAvgPrice - StopLossPoints1, " | 均線價格:", maExit);
        SetPosition(0); // **1 口多單,跌 100 點停損**
    End;
End
Else If Position = -1 Then Begin
    If Close >= FilledAvgPrice + StopLossPoints1 Then Begin
        Print("【停損】 空單出場 | 停損價:", FilledAvgPrice + StopLossPoints1, " | 均線價格:", maExit);
        SetPosition(0); // **1 口空單,漲 100 點停損**
    End;
End;

// **加碼機制**
If AbsValue(Filled) < MaxPosition Then Begin
    If Position = 1 And Close >= FilledAvgPrice + ProfitAddPoints Then Begin
        SetPosition(Position + 1); // **加碼多單**
        Print("【加碼】 多單加碼至", Position, "口 | 參考K棒收盤價:", maxVolClose, " | 均線價格:", maExit);
    End
    Else If Position = -1 And Close <= FilledAvgPrice - ProfitAddPoints Then Begin
        SetPosition(Position - 1); // **加碼空單**
        Print("【加碼】 空單加碼至", Position, "口 | 參考K棒收盤價:", maxVolClose, " | 均線價格:", maExit);
    End;
End;

// **2 口部位的停損邏輯**
If Position = 2 Then Begin
    If Close <= FilledAvgPrice - StopLossPoints2 Then Begin
        Print("【停損】 多單 2 口出場 | 停損價:", FilledAvgPrice - StopLossPoints2, " | 均線價格:", maExit);
        SetPosition(0); // **2 口多單,跌 50 點停損**
    End;
End
Else If Position = -2 Then Begin
    If Close >= FilledAvgPrice + StopLossPoints2 Then Begin
        Print("【停損】 空單 2 口出場 | 停損價:", FilledAvgPrice + StopLossPoints2, " | 均線價格:", maExit);
        SetPosition(0); // **2 口空單,漲 50 點停損**
    End;
End;

// **2 口部位的停利邏輯**
If Position = 2 And Close Cross Below maExit Then Begin
    Print("【停利】 多單 2 口出場 | 停利條件: 跌破均線 | 均線價格:", maExit);
    SetPosition(0); // **2 口多單,跌破 20 均線停利**
End
Else If Position = -2 And Close Cross Above maExit Then Begin
    Print("【停利】 空單 2 口出場 | 停利條件: 漲破均線 | 均線價格:", maExit);
    SetPosition(0); // **2 口空單,漲破 20 均線停利**
End;

附加文件

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

maxVolClose = Close[HighestBar(Volume, VolumeLookback)];

蓬蘇王杜 發文於   2025/02/07

原來是要用bar 謝謝教授

發表回覆
Close