如圖
進場條件只有一行,time=090500 and close[1]>open[1] then setposition(-1*pstn,market);
出場只有設尾盤出場和系統預設的%停損
但是我發現很多筆交易的持有區間都是1,進場以後,下一分鐘又出場(價位也沒觸發停損,更不可能是尾盤),請問為什麼會有這種情形,謝謝

如圖
進場條件只有一行,time=090500 and close[1]>open[1] then setposition(-1*pstn,market);
出場只有設尾盤出場和系統預設的%停損
但是我發現很多筆交易的持有區間都是1,進場以後,下一分鐘又出場(價位也沒觸發停損,更不可能是尾盤),請問為什麼會有這種情形,謝謝

需提供腳本內容才能檢查問題
您好
如下
input: money(150),profit_percent(10, "停利(%)"),loss_percent(3, "停損(%)"),starttime(090500),endtime(132000);
variables:pstn(0);
pstn=floor(money/close);
if time=starttime and close[1]>open[1] then begin
setposition( -1*pstn ,market);
end;
if time>=endtime and position<0 then setposition(0,market);
if Position <0 and Filled <0 then begin
{ 依照成本價格設定停損/停利: 請注意當作空時, 判斷是否獲利的方向要改變 }
if profit_percent > 0 and Close <= FilledAvgPrice*(1-0.01*profit_percent) then begin
{ 停利 }
SetPosition(0);
end else if loss_percent > 0 and Close >= FilledAvgPrice*(1+0.01*loss_percent) then begin
{ 停損 }
SetPosition(0);
end;
end;
停損停利計算相反了(1-0.01*profit_percent)=0.9,(1+0.01*profit_percent)=1.1,程式會先乘除後加減,但最好習慣自己用()決定計算順序。
不懂您的意思耶,進場是空單,停利停損是用系統預設的程式碼,完全沒改,而且有()沒錯呀
如果100做空 close<=100*(1-0.01*10)=90 所以close<=90停利____沒錯啊
close>=100*(1+0.01* 3)=103 所以close>=103停損___也沒錯啊
系統原始空單固定停利停損%如以下
{
空單停損(%)
}
input: profit_percent(2, "停利(%)");
input: loss_percent(2, "停損(%)");
var: short_condition(false); { 進場做空 }
{
範例:
均線跌破時以市價賣出1張做空
以成交價為基礎, 設定固定的停損/停利價格, 觸及時出場
}
short_condition = Average(Close, 5) cross under Average(Close, 20);
if Position = 0 and short_condition then begin
SetPosition(-1, MARKET);{ 以市價賣出 }
end;
if Position = -1 and Filled = -1 then begin
{ 依照成本價格設定停損/停利: 請注意當作空時, 判斷是否獲利的方向要改變 }
if profit_percent > 0 and Close <= FilledAvgPrice*(1-0.01*profit_percent) then begin
{ 停利 }
SetPosition(0);
end else if loss_percent > 0 and Close >= FilledAvgPrice*(1+0.01*loss_percent) then begin
{ 停損 }
SetPosition(0);
end;
end;
抱歉,沒看到進場放空,你的出場不是因為停利停損,剛剛print出來的結果如下
第一次洗價: SetPosition = -5、Position= 0、Filled = 0、tick成交量=1
第二次洗價: SetPosition = -4、Position= -5、Filled = -5、tick成交量=3
第三次洗價: SetPosition = -5、Position= -5、Filled = -5、tick成交量=2
因為第一次洗價只有一張,所以在第二次洗價 SetPosition = -4,但是此時卻判斷成Position和Filled 都等於 -5了,於是又買進一張,之所以會這樣應該是SetPosition進場條件還要加入Position= 0、Filled =0的時候才避免重複送出進場訊號。
Hello LIJU,
您可以參考 musashi 的回覆。
另外,如果要限定一根Bar只能進場一次的話,可以用變數紀錄當下Bar的序號來當作條件。
舉例來說:
var: intrabarpersist count(0),
condition1 = 進場條件;
if condition1 and count <> currentbar then begin
setposition(1, market);
count = currentbar;
end;
這樣1根Bar做多就只能觸發1次。
感謝 musashi 的熱心回覆。
7 評論