請問在交易語法中
有沒有辦法寫出指定天數出場
例如依照進場條件買進台積電之後
無論如何20個交易日之後就是強制出場呢
持有天數出場
- 763
- 最後發表 Golly 2 週前
Hello Golly,
如果是回測的話,您可以在進場的時候將變數歸0,並在每過一個交易日時讓其增加1。
舉例來說,在非逐筆洗價的狀況下:
if date <> date[1] then value1 += 1;
condition1 = 進場條件;
if condition1 and position = 0 and filled = 0 then begin
value1 = 0;
setposition(1, market);
end;
if position = 1 and filled = 1 and value1 >= 20 then setposition(0, market);
這樣 value1 就會記錄經過幾日,並在持有部位且滿20日的狀況下出場。
即時的話因為每次關閉策略時相關計算就會重置,所以除非您維持20個工作日不關XQ,或是將 策略部位 設定為依腳本計算並將 策略部位計算起點 設為當初進場的日期,這樣才有辦法模擬這段區間的變數計算。
請教如果是日K回測,逐筆洗價,上方程式碼要怎麼修改?感謝
Hello 石頭,
您可以改使用 IntrabarPersist 所宣告的變數來紀錄進場日期並比較。
舉例來說:
var: intrabarpersist _rec(0);
condition1 = 進場條件;
if position = 0 and filled = 0 and condition1 then begin
setposition(1, market);
_rec = date;
end;
if position = 1 and filled = 1 and date[20] >= _rec then setposition(0, market);
這樣 _rec 就會是進場的日期,date[20] >= _rec 則會比較20根Bar前的日期是否大於等於進場日期 (相當於過了20日)。
感謝小編。我再試試
請教小編,這樣的寫法在庫存那邊是可以選依庫存變化,還是一樣要選用依腳本策略呢?
感謝教授回覆,
請教是指在position >= 1的時候使用filledEntryDate抓庫存現有的股票進場日期嗎? 還是可以照小編那種寫法去計算持股天數呢?
我照小編給的建議練習寫一個進出場的交易腳本, 配合策略部位選用"延續前次執行", 這樣是可以的嗎?
input: profit_percent(20, "停利(%)");
input: loss_percent(15, "停損(%)");
var: intrabarpersist _rec(0);
if position <= 0 and filled <= 0 then begin
SetPosition(1, market);
_rec = date;
end;
if Position >= 1 and Filled >= 1 then begin
condition1 = profit_percent > 0 and Close >= FilledAvgPrice*(1+0.01*profit_percent);
condition2 = loss_percent > 0 and Close <= FilledAvgPrice*(1-0.01*loss_percent);
if condition1 or condition2 then setposition(0, market);
if date[30] >= _rec then setposition(0, market, label:="持股天數滿工作天30天");
end;
這樣不行!你這樣寫,只能用在回測,無法直接用來交易。
我這邊舉一個類似的例子給你參考。不過,這個例子使用的DataDateDiff函數是一個需要綁定我優惠碼才可使用的函數。
//策略部位:延續前次執行
//資料頻率:分鐘或日或日還原(注意準備資料要足夠)
//洗價模式:逐筆洗價
//假設頻率為日頻率,則setTotalBar(HoldDays+2); //涵蓋兩個日期的筆數
//假設頻率為5分鐘,則
//個股,setTotalBar((HoldDays+2)*54);
//期貨日盤,setTotalBar((HoldDays+2)*60);
//期貨夜盤,setTotalBar((HoldDays+2)*228);
if barFreq<>"D" then raiseRunTimeError("限用日資料");
input: HoldDays(3,"最多持有N天。分鐘頻率時,N<=9");
input: LossRatio(5,"虧損趴數");
var: intrabarpersist _rec(0);
setTotalBar(HoldDays+2);
if isfirstCall("RealTime") then
_rec=IFF(position>0,DataDateDiff(currentDate,filledEntryDate),0);
value2=IFF(position>0,100*(c/filledAvgPrice-1),0);
if position>0 then
if _rec>HoldDays or value2<-LossRatio then setPosition(0,market);
謝謝教授,請問您的優惠碼是多少?怎麼跟您取得這參數呢?
@8596。你可以追蹤我的粉專:
10 評論