自動交易腳本停損後再進場問題

  •   46 
  • 最後發表   蘆葦  2025 七月 28
蘆葦 發文於   2025/07/28

各位大神您們好,我想要寫停損5%後出場,但我遇到問題是出場後馬上就進場了,我想要當天出場後要隔一天才重新計算,我設定進場條件為昨日外資買超>300張,請問我程式碼要怎麼改呢,以下是我的程式碼

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

value1=getField("外資買賣超","D")[1];
condition1=value1>300;
if condition1 and position=0 then begin
setposition(1);
print("進場價格",close,"進場日期與時間",date,currentTime);
end;

if position <> 0 then begin

    if loss_percent > 0 and //停損
        Close <= FilledAvgPrice*(1-0.01*loss_percent) then begin
        print("出場價格:",close,"出場時間:",date,currentTime,"停損出場","。停損區間",FilledAvgPrice*(1-0.01*loss_percent));
        SetPosition(0, label:="停損出場");
    end;
end;

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

使用一個變數,每天一開盤歸零,然後在出場時設它為1,進場條件加上必須它為0的時候,才可進場。

蘆葦 發文於   2025/07/28

我使用value2變數了,我設定他出場為1,進場條件也設定必須為0的時候進場,但我print出來發現重新進場的時候value2就會自動更新為0,而不是我最後設定的出場條件1,雖然我沒有還沒開盤前歸零,但現在這個問題要如何解決?

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

vars:Exit(0);

value1=getField("外資買賣超","D")[1];

condition1=value1>300;


if condition1 and position=0 and Exit=0 then begin
setposition(1);
print("進場價格",close,"進場日期與時間",date,currentTime,Exit);
end;

if position <> 0 then begin

    if profit_percent > 0 and //停利
        Close >= FilledAvgPrice*(1+0.01*profit_percent) then begin
        Exit = 1;
        SetPosition(0, label:="停利出場");
        print("出場價格:",close,"出場時間:",date,currentTime,Exit,"停利出場","。停利區間",FilledAvgPrice*(1+0.01*profit_percent));
    end;
end;

謝謝許教授回答

虎科大許教授 發文於   2025/07/28

不要用免宣告的變數。例如用hasIn變數,宣告方式如下:

var: intrabarpersist hasIn(0);

發表回覆
Close