1.下面為程式中移動停利的範本,如果我已經買了一口期貨,然後才啟動下列的交易腳本,因為手中已有一口,所以long_condition應該是不會運作的吧?
2.因為手上已有多單一口,應該只會執行「if Position = 1 and Filled = 1 then begin」的部分,對嗎?
以上兩個疑問,麻煩小幫手回復,謝謝!
------------------------------------------------------------------------------------------------------------------------------------------------------------
{ 多單移動停利(點)
設定停損點(如果不設定的話, 請把loss_point設定成0), 以及停利點, 跟回跌點數
價格下跌到停損時出場
價格上漲到停利點後啟動移動停利, 如果價格繼續上漲, 則繼續持有, 如果價格回檔超過回跌點數時, 則停利出場
}
input: profit_point(10, "停利(點)");
input: profit_drawback_point(5, "停利回跌(點)");
input: loss_point(10, "停損(點)");
var: long_condition(false); { 進場買進作多 }
{
範例:
均線穿越時買進1張
以成交價為基礎, 設定固定停損以及移動停利
}
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, 20);
if Position = 0 and long_condition then begin
SetPosition(1);{ 買進1張 }
end;
if Position = 1 and Filled = 1 then begin
var: intrabarpersist max_profit_point(0);{ 啟動停利後最大獲利點 }
if loss_point > 0 and Close <= FilledAvgPrice - loss_point then begin
{ 停損 }
SetPosition(0);
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);
max_profit_point = 0;
end else if Close > max_profit_point then begin
{ 移動最大獲利點 }
max_profit_point = Close;
end;
end;
end;
end;
7 評論