請問該如何修改成多空都做?

  •   141 
  • 最後發表   swer  2022 六月 27
swer 發文於   2022/06/22

input: profit_point(10, "停利(點)");

 

input: profit_drawback_point(10, "停利回跌(點)");

 

input: loss_point(20, "停損(點)");

 

 

 

var: long_condition(false); { 進場買進作多 }

 

var: intrabarpersist _currentbar(0);    //紀錄當下的K棒編號

 

 

 

if profit_point = 0 then raiseruntimeerror("請設定停利(點)");

 

if profit_drawback_point = 0 then raiseruntimeerror("請設定停利回跌(點)");

 

if profit_drawback_point > profit_point then raiseruntimeerror("停利(點)需大於停利回跌(點)");

 

 

 

long_condition = Average(Close, 5) cross over Average(Close, 10);

 

condition1 = currentbar <> _currentbar;    //紀錄的K棒編號不等於當下的K棒編號

 

 

 

if Position = 0 and long_condition and condition1 then begin

 

    SetPosition(1);{ 買進1張 }

 

    end;

 

 

 

if Position > 0 and Filled > 0 then begin

 

var: intrabarpersist max_profit_point(0);{ 啟動停利後最大獲利點 }

 

if loss_point > 0 and Close <= FilledAvgPrice - loss_point then begin

 

{ 停損 }

 

SetPosition(0);

 

_currentbar = currentbar;    //更新紀錄的K棒編號

 

max_profit_point = 0;

 

end else begin

 

{ 判斷是否要啟動停利 }

 

if max_profit_point = 0 and Close >= FilledAvgPrice + profit_point then begin

 

max_profit_point = Close;

 

end;

 

 

 

if max_profit_point <> 0 then begin

 

if Close <= max_profit_point - profit_drawback_point then begin

 

{ 停利 }

 

SetPosition(0);

 

_currentbar = currentbar;    //更新紀錄的K棒編號

 

max_profit_point = 0;

 

end else if Close > max_profit_point then begin

 

{ 移動最大獲利點 }

 

max_profit_point = Close;

 

end;

 

end;

 

end;

 

end;

XQ小幫手 發文於   2022/06/27

Hello swer,

 

您的腳本中只有多方進場條件 (long_condition) 跟停利停損出場。

如果您想修改成多空都作的話,需要在腳本下面補上對應的空方出場條件,以及空方的停利停損出場。

空方的停利停損您可以參考內建的交易腳本。

另外需注意的若同時觸發多個 SetPosition ,只會執行第一個觸發的。

發表回覆
Close