使用計數器的語法

  •   376 
  • 最後發表   大A  2025 五月 29
大A 發文於   2025/05/26

請問一下,以下語法在交易腳本回測中,都可以符合condition2 or _count>=10 的條件出場,但在實際進場後,condition2仍是正常執行會被觸發而出場,但_count>=10符合時,就不會被觸發執行,會是因為我的電腦每天會關機的關係嗎?如果是的話可以怎麼修改語法讓_count>=10能正常被觸發

<交易腳本,日頻率,逐筆洗價,指定選股>

<策略部位為延續前次執行,自動執行有部位商品啟用>

var:_count(0);

condition1=進場條件

condition2=出場條件

if position=0 and filled=0 and condition1 then begin

_count=0;

setposition(1,mraket);

end;

if filled>0 then _count +=1

if  position>0 and filled>0 and condition2  then setposition(0,market);

end else if  _count>=10 then setposition(0,market);

排序方式: 標準 | 最新
虎科大許教授 發文於   2025/05/26

重啟策略之後,所有變數都恢復預設值。雖然你使用延續前次執行的策略部位,但啟動策略時,filled若大於0,count只會變成1,更早累加的次數不會被保留。解決的方法:你只能用變數來模擬部位,在跑歷史K棒時,讓count累加。

大A 發文於   2025/05/27

請問一下,我把語法改成這樣,目前以交易腳本回測是會正常觸發_count>=10,這個寫法邏輯上是正確的嗎?

<交易腳本,日頻率,逐筆洗價,指定選股>

<策略部位為延續前次執行,自動執行有部位商品啟用>

var:_count(0);

condition1=進場條件

condition2=出場條件

value1=barslast(condition1); //增設這個條件讓系統跑歷史K棒時,從這邊讓_count累加

if position=0 and filled=0 and condition1 then begin

_count=0;

setposition(1,mraket);

end;

if value1>=0 and filled>0 then _count +=1;

if  position>0 and filled>0 and condition2  then setposition(0,market);

end else if  _count>=10 then begin

setposition(0,market);

_count=-1;

end;

虎科大許教授 發文於   2025/05/28

這樣寫是可以,但是沒有效率。你判斷過去持有部位幾天,只要判斷一次就好,不需要盤中每個Tick都判斷。建議用isFirstCall函數協助處理。

大A 發文於   2025/05/28

請問一下,把barslast換成isfirstcall(""),目前以交易腳本回測是會正常觸發_count>=10,這個寫法邏輯上是正確的嗎?

<交易腳本,日頻率,逐筆洗價,指定選股>

<策略部位為延續前次執行,自動執行有部位商品啟用>

var:_count(0);

condition1=進場條件

condition2=出場條件

if position=0 and filled=0 and condition1 then begin

if isfirstcall("") then _count=0;  //只有是第一次觸發時才開啟計數器

setposition(1,mraket);

end;

if DS>=0 and filled>0 then _count +=1;

if  position>0 and filled>0 and condition2  then setposition(0,market);

end else if  _count>=10 then begin

setposition(0,market);

_count=-1;

虎科大許教授 發文於   2025/05/29

 

//策略部位:延續前次執行
if barfreq<>"D" then raiseRunTimeError("限用日資料");
var: intraBarPersist count(0);
if isFirstCall("RealTime") then count=getBarOffset(filledEntryDate);
condition1=進場條件;
condition2=出場條件;
if position=0 and filled=0 and condition1 then setposition(1,market);
if position>0 and filled>0 then
    if condition2 or count>=10 then setposition(0,market);
print(date,position,count);

發表回覆
Close