自動交易_當天一直重複進出場

  •   591 
  • 最後發表   Nestor  2024 五月 06
Nestor 發文於   2024/04/22

小幫手您好,我在設定自動交易時腳本如下,指定選股是選股法,策略部位依庫存,開啟庫存異動時自動加入執行,在安控上也有勾選每日最多進場一次,但在啟動自動交易後,他還是會一直重複進出場,再請協助解惑,謝謝。

input:

    maxprice(300,"股價不可超過"),

    lostex(10,"虧損多少出場%"),

    dayexit(3, "多少天出場");

var:

    intraBarPersist dayentry(0); // 控制當天出場後,不會當天又再進場

if date <> date[1] and currenttime = 090000 then

    dayentry = 0;

if date <> date[1] and position = 0 and dayentry = 0 and close <= maxprice then

    setposition(1);

if position > 0 and filled > 0 and (

    currenttime >= 090200 and date[dayexit - 3] > FilledRecordDate(FilledRecordCount) or

    close <= filledAvgPrice * (1 - lostex * 0.01)

) then

    setposition(0);

排序方式: 標準 | 最新
XS小編 發文於   2024/05/06

Hello Nestor,

 

策略部位計算功能會模擬從設定日期開始的策略運行狀況。

除了 虎科大許教授 所說的 getinfo("IsRealTime") 以外,可以直接使用交易函數來取得模擬的交易資訊。

所以並不需要另外使用FD來記錄日期,可以直接使用 FilledRecordDate 搭配 FilledRecordCount 來取得最後一筆交易的日期。

 

感謝 虎科大許教授 的熱心回覆。

虎科大許教授 發文於   2024/05/03

兩個問題:

(1)若需要用歷史K棒做運算,則不能使用if getinfo("IsRealTime")=0 then return;

(2)記錄FD的指令不能放在If Position=0裡面,否則永遠無法被運算。這裡要特別注意,使用的排律必須是日頻率,否則會有邏輯錯誤的問題。

 

input:lostex(10,"虧損多少出場%"), addate(3,"幾天後到期");

var: intraBarPersist hasOrdered(false), intraBarPersist FD(0); // 控制當天出場後,不會當天再進場

 

//進場

if position = 0 and hasOrdered = false then

begin

setposition(1);

hasOrdered = true; // 進場後設置 hasOrdered為True,防止再次進場

end;

 if Filled[1]=0 and Filled>0 then FD=Date;

//出場

if getfield("Date", "D")[addate] = FD or close <= filledAvgPrice * (1 - lostex * 0.01) and position = 1 then SetPosition(0);

 

print(Date, FD, addate);

Nestor 發文於   2024/05/03

想請問一下小幫手

若是透過策略部位計算功能來模擬計算,也是用我上述的策略就可以執行了嗎?

還是有甚麼特定策略才能使用??

策略如下:

input:lostex(10,"虧損多少出場%"), addate(3,"幾天後到期");

var: intraBarPersist hasOrdered(false), intraBarPersist FD(0); // 控制當天出場後,不會當天再進場

if getinfo("IsRealTime")=0 then return;

//進場

if position = 0 and hasOrdered = false then

begin

setposition(1);

if Filled[1]=0 and Filled>0 then FD=Date;

hasOrdered = true; // 進場後設置 hasOrdered為True,防止再次進場

end;

 

//出場

if getfield("Date", "D")[addate] = FD or close <= filledAvgPrice * (1 - lostex * 0.01) and position = 1 then SetPosition(0);

 

print(Date, FD, addate);

XS小編 發文於   2024/05/03

Hello Nestor,

 

除非您的策略持續啟動著,這樣變數才會維持在該數值,否則再次啟動時變數會重新計算。

目前只能夠透過 策略部位計算功能 來達到模擬計算從設定的起點開始運作的策略進場,並以此來判斷N日後出場的條件。

相關人員有在規劃讓策略可以取得前次啟動時的部位庫存以及交易的日期時間,待該功能上線後應該能夠解決您的問題。

Nestor 發文於   2024/04/30

許教授您好:

了解,那看來只能把進出場策略寫在一起,在進場時抓成交日期

這樣不知是否可行。

input:lostex(10,"虧損多少出場%"), addate(3,"幾天後到期");

var: intraBarPersist hasOrdered(false), intraBarPersist FD(0); // 控制當天出場後,不會當天再進場

if getinfo("IsRealTime")=0 then return;

//進場

if position = 0 and hasOrdered = false then

begin

setposition(1);

if Filled[1]=0 and Filled>0 then FD=Date;

hasOrdered = true; // 進場後設置 hasOrdered為True,防止再次進場

end;

 

//出場

if getfield("Date", "D")[addate] = FD or close <= filledAvgPrice * (1 - lostex * 0.01) and position = 1 then SetPosition(0);

print(Date, FD, addate);

虎科大許教授 發文於   2024/04/30

只能根據你的策略進場規則,在跑歷史K棒時計算進場日期,像往上二樓我提示的做法。

Nestor 發文於   2024/04/30

許教授您好,爬了一些文之後,還是不知道有什麼好方法,

若用FilledRecordCount好像只要重新執行策略,日期就會跑掉。

虎科大許教授 發文於   2024/04/30

若4/1進場,Filled>0,沒平倉。4/2的Filled也是大於0,FD會更新為4/2。

改為if Filled[1]=0 and Filled>0 then FD=Date;

Nestor 發文於   2024/04/29

許教授您好,那如果用Bar數量來計算可行嗎??

編碼如下:

vars: intraBarPersist FD(0);

input: addate(3, "幾天後到期");

// 如果已持倉且有成交,則更新 FD 變數

if filled > 0 then begin

    FD = date;

end;

// 出場策略

if getfield("Date", "D")[addate] = FD and position > 0 then

    SetPosition(0, market);

再請教授指正

虎科大許教授 發文於   2024/04/29

FilledRecordCount只會在策略執行期間,傳回執行期間的成交筆數,並沒有辦法查詢歷史的成交記錄。重新啟動策略之後,FilledRecordDate(FilledRecordCount)應該會出現「取成交資料記錄時,索引值超出範圍」的錯誤,因為FilledRecordCount為0。

顯示更多回應 發表回覆
Close