XS環境是否有bug?

  •   373 
  • 最後發表   小富  2024 八月 31
小富 發文於   2024/08/28

親愛的小編,

     想了解XS的環境是否有bug。因為如昨天所反應的~~做多腳本沒有任何問題。執行回測,結果也都如預期。但同樣的腳本直接copy,轉成做空腳本,卻發現在進場策略中的 if then begin當中的第三式 lowestprice = FilledAvgPrice 就沒有正常執行。所以後續的stopprice = lowestprice +riskpoint.....答案只有riskpoint。所以都會立即出場。(所以print也無法出現結果? 因為根本沒執行該式)。

 

//期指MACD做空當沖交易



// 宣告參數

input: FastLength(12,"DIF短天數"), SlowLength(26,"DIF長天數"), MACDLength(9,"MACD天數");

input: riskpoint(35,"停利停損點數");

var: intrabarpersist lowestprice(0);

var: intrabarpersist limit_control(0);



// if date<>date[1] then lowestprice =0; //自訂義變數歸零

if barfreq<>"Min"or barinterval<>5  then return;

 var: price(0),dif_s(0),macd_s(0),osc_s(0);

settotalbar((maxlist(FastLength,SlowLength,MACDLength)+1)*3.5);

 price = WeightedClose();



//macd25計算式

dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);

macd_s = XAverage(dif_s, MACDLength) ;

osc_s = dif_s - macd_s ;

value1 = ema(close,13); // ma13以下才能空



condition1 = close< value1 and osc_s cross below 0;

condition2 = limit_control <> currentbar;



// 空方進場策略:MACD由正轉負。

if position = 0 and condition1 and condition2 then begin

   setposition(-1);

   limit_control = currentbar;

   lowestprice = FilledAvgPrice;

end;



if close < lowestprice then lowestprice = close; //採最低價位做為停利(損)起算點

stopprice = lowestprice + riskpoint;



//監控

print("lowestprice=",numtostr(lowestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0));



//出場策略:同一K線時,以點數控制。不同K線時,三條件擇一出場

condition3 = osc_s cross above 0 or close > stopprice; 

 if limit_control<>currentbar then if

  condition3 then setposition(0)    //不同根bar時的停損模式

else if

  close>stopprice then setposition(0) ; //同一根bar的停損模式

排序方式: 標準 | 最新
虎科大許教授 發文於   2024/08/28

(1)有現成的MACD函數可以,為何自己計算?

(2)一進場,就用變數接收filledAvgPrice,會得到零,且因為Position已經不是零,因此無法讓變數抓到庫存成本。庫存成本一直為零。

(3)庫存成本為零,則stopprice=35,價格只要高於35,就會停損,這也是為什麼你一進場就馬上出場的原因。

小富 發文於   2024/08/29

謝謝許教授,

    因為事實上我用了兩組的MACD,所以自己編寫這部分的腳本。另外了解您的意思,所以應該要加上" and Filled=-1"? 我試試看。感謝!!!

 

小陳

小富 發文於   2024/08/29

許教授,

        我在加入了and filled =-1後,回測"似乎"是沒問題。但結果在今早實際執行時,仍然出現了立即停損的狀況。(請看附圖,同一秒鐘交易五次),顯示連limit_control = currentbar都沒執行。是否有那裡還是沒考慮到? [ps.我用同樣的邏輯寫了做多程式~~結果卻是符合個人想法預期的],還請許教授指教,謝謝!

附加文件

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

//期指MACD做空當沖交易
// 宣告參數
input: FastLength(12,"DIF短天數"), SlowLength(26,"DIF長天數"), MACDLength(9,"MACD天數");
input: riskpoint(35,"停利停損點數");
var: intrabarpersist lowestprice(0);
var: intrabarpersist limit_control(0);
var: intrabarpersist stopprice(0);
if barfreq<>"Min"or barinterval<>5  then return;
var: price(0),dif_s(0),macd_s(0),osc_s(0);
settotalbar((maxlist(FastLength,SlowLength,MACDLength)+1)*3.5);
price = WeightedClose();
//macd25計算式
dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);
macd_s = XAverage(dif_s, MACDLength) ;
osc_s = dif_s - macd_s ;
value1 = ema(close,13); // ma13以下才能空
condition1 = close< value1 and osc_s cross below 0;
condition2 = limit_control <> currentbar;
// 空方進場策略:MACD由正轉負。
if position = 0 and condition1 and condition2 then 
    begin
        setposition(-1);
        limit_control = currentbar;     
    end;
if position = -1 and filled = -1 then 
    begin
        if close < filledavgPrice then lowestprice = close; //採最低價位做為停利(損)起算點
        stopprice = lowestprice + riskpoint;
        //出場策略:同一K線時,以點數控制。不同K線時,三條件擇一出場
        condition3 = osc_s cross above 0 or close > stopprice; 
        if limit_control<>currentbar then 
            if condition3 then setposition(0)    //不同根bar時的停損模式
            else 
                if close>stopprice then setposition(0) ; //同一根bar的停損模式
    end;
//監控
print("lowestprice=",numtostr(lowestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0));

小富 發文於   2024/08/30

謝謝許教授,謝謝!

小富 發文於   2024/08/30

Dear 許教授 & 小編

    回測結果,看持有期數很不錯。但實際測試結果....最低價依舊跑不出來。一進場立即"被"出場,當時的一分鐘K棒也幾乎收在最低點(所以不可能是當根K停損出場)。不知公司內部是否有完整資料環境(tick的值)可做回測? 謝謝

小富

附加文件

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

(1)問題出在進場之後,若close大於filledAvgPrice,則lowestprice不會被賦予數值,它仍然是0,0+35遠低於成交價,所以立即出場。解決方法:預設lowestprice為很大的數值(不可能被close超越):

var: intrabarpersist stopprice(999999);

(2)XQ沒提供Tick頻率的回測。實務上,雲端運算是不可能提供這樣的功能。

小富 發文於   2024/08/31

許教授,

       了解。感謝您的協助,非常感謝,我再試試。

小富

發表回覆
Close