如何將移動停利2%,改成自動抓當日最高點

  •   448 
  • 最後發表   hello10282000  2024 十二月 26
hello10282000 發文於   2024/12/23

//各位大大您好
//想請教一下,當程式交易自動買入後,目前作法是停利2%,當回檔0.5%時出場
//因為常常會發生已經進黨,但後續沒有漲到2%所以移動停利功能沒有啟動,
//導致原本鑽錢變成虧錢
//現在想說是否可以改成在買入後自行追中最高點,當作停利的啟動。


//設定變數
value1 = getField("Close","D")[1];
value2 = value1*1.1;
value4 = GetField("Open", "D");
//
input: profit_percent(2, "停利(%)");
input: profit_drawback_percent(0.5, "停利回跌(%)");
input: loss_percent(1.5, "停損(%)");

{ 
    以成交價為基礎, 設定固定停損以及移動停利
}
if profit_percent = 0 then raiseruntimeerror("請設定停利(點)");
if profit_drawback_percent = 0 then raiseruntimeerror("請設定停利回跌(點)");
if profit_drawback_percent > profit_percent then raiseruntimeerror("停利(點)需大於停利回跌(點)");

if Position <> 0 and Filled <> 0 then begin
    { 依照成本價格設定停損/停利 }

        var: intrabarpersist max_profit_percent(0); { 啟動停利後最大獲利百分比 }

    if loss_percent > 0 and Close <= FilledAvgPrice*(1-0.01*loss_percent) then begin
        { 停損 }
        SetPosition(0, MARKET);
        max_profit_percent = 0;
    end else begin

        { 判斷是否要啟動停利 }
        if max_profit_percent = 0 and Close >= FilledAvgPrice*(1+0.01*profit_percent) then begin
            max_profit_percent = Close;
        end;    
    if max_profit_percent <> 0 then begin     
            if Close <= max_profit_percent - profit_drawback_percent then begin
                { 停利 }
                SetPosition(0, MARKET);
                max_profit_percent = 0;
            end else if Close > max_profit_percent then begin
                { 移動最大獲利點 }
                max_profit_percent = Close;

                                   end else if close < value4 then begin
                    SetPosition(0, MARKET);     { 分K收盤小於開盤價就全部市價賣出 } 

                                   end else if close = value2 then begin
                    SetPosition(0, MARKET);     { 分K收盤等於漲停價以市價全部賣出 } 
                                                           end else if CurrentTime = 132300 then begin
                    SetPosition(0, MARKET);     { 分K收盤等於漲停價以市價全部賣出 }
            end;    
        end;        
    end;

end;

排序方式: 標準 | 最新
虎科大許教授 發文於   2024/12/23

從最高價掉0.5%:

input: drawback_percent(0.5, "最高點回跌(%)");

var: intrabarpersist myMax(0);

//進場時設定myMax=h;

if h>myMax then myMax=h;

if Close <= myMax * (1 - drawback_percent/100) then setposition(0,market);

hello10282000 發文於   2024/12/23

謝謝許教授,晚點來試試看,感謝感謝。

XS小編 發文於   2024/12/25

Hello hello10282000,

 

小編補充,上面的範例是用進場後的最高點來計算停損點。

若要使用當日最高點的話只要改成 GetField("High", "D") 即可。

hello10282000 發文於   2024/12/26

感謝小編

發表回覆
Close