請問我要如何寫出以下的小型台指期貨高頻自動交易?因為交易回測發現進出點位差很多?

  •   353 
  • 最後發表   neverdie62  2025 二月 04
neverdie62 發文於   2025/01/25

請問我要如何修正以下的高頻自動交易?我希望在上行的BBI線趨勢中,若股價在BBI線之上,每靠近BBI線十點以內,買入一口做多,若在下行的BBI線趨勢中,若股價在BBI線之下,也是每靠近BBI線十點以內,賣出一口做空,然後不管做多做空,都獲利十點之後停利。若發生虧損十點之後停損,然後買賣完成後再依照相同邏輯再進場。

input:a1(3,"第一根均線天期"),a2(6,"第二根均線天期"),a3(12,"第三根均線天期"),a4(24,"第四根均線天期"),n(2,"格局期數");

input:_angle(5,"BBI均線角度"),m(20,"ATR(5)閾值"),_priceThreshold(10,"價格閾值"),_stopProfit(15,"停利點數"),_stopLoss(10,"停損點數");

variable:BBI(0),EBBI(0);

BBI=(average(close,a1)+average(close,a2)+average(close,a3)+average(close,a4))/4;

EBBI=(Xaverage(close,a1)+Xaverage(close,a2)+Xaverage(close,a3)+Xaverage(close,a4))/4;

 

value1=EBBI-BBI;//趨勢強弱

condition1=LinearRegAngle(BBI,3)>_angle AND trueAll(BBI>BBI[1],n);//多方格局

condition2=LinearRegAngle(BBI,3)<-_angle AND trueAll(BBI<BBI[1],n);//空方格局

condition3=value1>0;//多方趨勢

condition4=value1<0;//空方趨勢

condition5=time >= 084500 and time < 133500;//日盤

condition6=time >= 150000 or time < 045000;//夜盤

condition7=time >= 133500 and time <= 134500;//日盤出清

condition8=time >= 045000 and time <= 050000;//夜盤出清 

 

if barfreq <> "Min" or barinterval <> 5 then RaiseRuntimeError("請設定頻率為5分鐘");

 

if condition5 or condition6 then begin

// 進場條件

 if position = 0 and filled = 0 then begin

  if condition1 and condition3 and ATR(5)>m  and close > BBI and close <= BBI + _priceThreshold then begin

     SetPosition(1,BBI + _priceThreshold);// 買進 1 張做

  end;

   if condition2 and condition4 and ATR(5)>m and close < BBI and close >= BBI - _priceThreshold then begin

     SetPosition(-1,BBI - _priceThreshold); // 賣出 1 張做空

  end;

 end;

// 出場條件

  if position = 1 and filled = 1 then begin

   if close >= filledAvgPrice+ _stopProfit then SetPosition(0,filledAvgPrice+ _stopProfit );// 多頭停利       

   if close <= filledAvgPrice - _stopLoss then SetPosition(0,filledAvgPrice - _stopLoss);// 多頭停損

  end;

  if position = -1 and filled = -1 then begin

   if close <= filledAvgPrice - _stopProfit then SetPosition(0,filledAvgPrice - _stopProfit);// 空頭停利 

   if close >= filledAvgPrice + _stopLoss then SetPosition(0,filledAvgPrice + _stopLoss);// 空頭停損

  end;

end;

if (condition7 or condition8) and filled <> 0  then setposition(0);

對了,我是要盤中一達到條件就買賣交易,不是等K棒收完再判斷,所以是要用逐筆洗價的,請看如何改才對?謝謝

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

這個程式執行上有什麼問題嗎?

XS小編 發文於   2025/02/04

Hello neverdie62,

 

要麻煩您詳細描述下遇到的問題和使用情境,小編才好給建議。

 

另外由於腳本中有使用到指數移動平均,故會需要較多的資料讀取筆數。

就上面的範例來看,設定為 (24 + 1) * 4 = 100 筆會比較適合。

 

至於回測的成交價格會以搓合邏輯為主,因此可能和委託價不同。

細節可參考 回測成交價搓合

發表回覆
Close