各位前輩好:
目前希望在交易策略中,加入定時的策略,例如"平倉後,半小時內禁止買進"之類的來控制進場次數。
參考了CurrentTime、TimeDiff、TimeAdd 之類的語法,但功力太淺,試著寫完跑回測後反而一筆交易都沒有...
希望有相關經驗的前輩可以分享下語法~~感謝!
各位前輩好:
目前希望在交易策略中,加入定時的策略,例如"平倉後,半小時內禁止買進"之類的來控制進場次數。
參考了CurrentTime、TimeDiff、TimeAdd 之類的語法,但功力太淺,試著寫完跑回測後反而一筆交易都沒有...
希望有相關經驗的前輩可以分享下語法~~感謝!
Hello XUKG,
您可以參考 Noodle 的回覆。
使用變數紀錄平倉的時間點,然後在與 currenttime 相減,超過半小時以上才可以在進場。
另外一種則是用變數紀錄出場後過了幾根 Bar, 然後以 Bar 數做限制。
簡單的範例,假設以10根 Bar做限制:
var: count(10), controller(0);
condition1 = 進場條件...;
condition2 = count >= 10; //至少要出場10根Bar
if controller = 1 then count += 1;
if condition1 and condition2 then begin
setposition(1, market);
controller = 0;
count = 0;
end;
condition3 = 出場條件...;
if condition3 then begin
setposition(0, market);
controller = 1;
end;
感謝 Noodle 的熱心回覆。
感謝Noodle特地撥冗回復!! 不勝感激!!
真的,想了好幾天了,再次感謝!
感謝小幫手的回覆!!
請問小幫手,這裡的controller=1是代表什麼意思呢?
另外--->if controller = 1 then count += 1; 後面這個+=,也不太明白~
有請小幫手解惑!!~感謝
Hello XUKG,
count是用來計算 出場後過了幾根,而controller是用來控制count。
當controller = 1 的時候為出場狀態,而controller = 0 時為進場狀態。
count += 1 就相當於 count = count + 1。
每經過一根 bar,count就會增加1。
小幫手後來想了一下後可以將腳本更簡化些,不用 controller 改用 filled控制。
var: count(10);
condition1 = 進場條件...;
condition2 = count >= 10; //至少要出場10根Bar
if filled = 0 then count += 1; // 空手的狀態下 count 每經過1跟bar 就會增加 1
if condition1 and condition2 and filled = 0 and position = 0 then begin
setposition(1, market);
end;
condition3 = 出場條件...;
if condition3 and position <> 0 then begin
setposition(0, market);
count = 0; //出場時將 count 歸 0;
end;
另外,想再請教小幫手
關於Noodle提到的"使用變數紀錄平倉的時間點,然後在與 currenttime 相減,超過半小時以上才可以在進場。"
能否示範下該怎麼撰寫呢? >"<菜鳥試不出來,感謝!!
Hello XUKG,
小幫手需要您提供您的 自動交易中心匯出檔勾選包含腳本 才能檢查問題。
至於用時間控制的話,您可以使用 FilledRecordCount, FilledRecordDate, FilledRecordTime, DateAdd, TimeAdd 這幾個函數。
舉例來說,假設每次出場後要隔一天以上才能進場。
if FilledRecordCount <> 0 and filled = 0 then condition1 = dateadd(FilledRecordDate(FilledRecordCount), "D", 1) <= date
else condition1 = True;
condition2 = 進場條件...;
if position = 0 and filled = 0 and condition1 and condition2 then setposition(1, market);
condition3 = 出場條件...;
if position = 1 and filled = 1 and condition3 then setposition(0, market);
這樣的話距離最後一筆出場交易至少要過一天才能再交易。
附上小幫手測試的 "至少間隔10分鐘以上" 交易中心匯出檔供您參考。
謝謝小幫手~~
請問附加的檔案我該用什麼開啟呢?(我有試過用XQ的XScript匯入開啟,但失敗..)
-------------------------------------------
小幫手早安! 我已經成功打開了~~感謝您~~
9 評論