指標和策略出訊不同

  •   90 
  • 最後發表   灰熊  2025 六月 17
灰熊 發文於   2025/06/16

教授您好:我的指標和策略程式碼幾乎相同,但跑出的箭頭卻不太相同,想再麻煩教授幫看一下,謝謝

交易標的:台指期

交易概念:1.採用日週期KD作為判斷買賣的方向,KD向上為作多,向下為作空
                2.當多方趨勢:則碰下軌道買進,出場條件為碰到上軌道且K<80則出場,或跌破中軌道出場,停損500點
                3當空方趨勢:則碰上軌道作空,出場條件為碰到下軌道且K>20則出場,或拉過中軌道出場,停損500點
                

 

排序方式: 標準 | 最新
灰熊 發文於   2025/06/16

input: KD_Length(9, "日線KD天數");

input: RSVt(2, "RSVt權數");

input: Kt(2, "Kt權數");

input: BB_Length(20, "布林通道天數");

input: BB_StdDev(2, "布林通道標準差倍數");

input: StopLoss(500, "停損(點)");

 

 

var: day_rsv(0), day_k(0), day_d(0); // 日線KD指標

var: min60_rsv(0), min60_k(0), min60_d(0); // 60分鐘KD指標

var: bb_upper(0), bb_middle(0), bb_lower(0); // 60分鐘布林通道

var: long_condition(false), short_condition(false); // 進場條件

var: intrabarpersist stoploss_price(0); // 停損價格

var: flag(0); // 持倉狀態(0:無持倉, 1:多單, -1:空單)

 

setBackBar(1000);

 

// 計算日線KD指標

xfMin_Stochastic("D", KD_Length, RSVt, Kt, day_rsv, day_k, day_d);

 

// 計算60分鐘KD指標

xfMin_Stochastic("60", KD_Length, RSVt, Kt, min60_rsv, min60_k, min60_d);

 

// 計算60分鐘布林通道

bb_upper = bollingerband(Close, BB_Length, BB_StdDev);

bb_middle = average(Close, BB_Length);

bb_lower = bollingerband(Close, BB_Length, -1 * BB_StdDev);

 

// 檢查停損點是否設定

if StopLoss = 0 then raiseruntimeerror("請設定停損(點)");

 

 

 

 

//========================================

// 布林通道多單進場條件

long_condition = day_k > day_d // 日線K > D

and low<= bb_lower+20; // 60分鐘價格觸及布林下軌

  

//布林通道 空單進場條件

short_condition = day_k < day_d // 日線K < D

  and high>= bb_upper-20; // 60分鐘價格觸及布林上軌

   

 

 

// 繪製訊號

if flag=0 and long_condition then begin

    Plot1(Low, "多單進場");

flag=1;

end;

if flag=0 and short_condition then begin

    Plot2(High, "空單進場");

flag=-1;

end;

if flag = 1 then begin 

    if  (Close >= bb_upper and min60_k < 80) or (close cross under bb_middle ) then begin//1.碰上軌道出場,2.未碰到上軌道就下彎且破中軌道,為了保本先出場

    Plot3(High, "多單出場");

flag=0;

end;

end;

if flag = -1 then begin

    if  (Close <= bb_lower and min60_k > 20) or (close cross above bb_middle) then begin

    Plot4(Low, "空單出場");

flag=0;

end;

end;

灰熊 發文於   2025/06/16

交易策略程式碼

input: KD_Length(9, "日線KD天數");

input: RSVt(2, "RSVt權數");

input: Kt(2, "Kt權數");

input: BB_Length(20, "布林通道天數");

input: BB_StdDev(2, "布林通道標準差倍數");

input: StopLoss(500, "停損(點)");

 

var: day_rsv(0), day_k(0), day_d(0); // 日線KD指標

var: min60_rsv(0), min60_k(0), min60_d(0); // 60分鐘KD指標

var: bb_upper(0), bb_middle(0), bb_lower(0); // 60分鐘布林通道

var: long_condition(false), short_condition(false); // 進場條件

var: intrabarpersist stoploss_price(0); // 停損價格

var: flag(0); // 持倉狀態(0:無持倉, 1:多單, -1:空單)

 

// 計算日線KD指標

xfMin_Stochastic("D", KD_Length, RSVt, Kt, day_rsv, day_k, day_d);

 

// 計算60分鐘KD指標

xfMin_Stochastic("60", KD_Length, RSVt, Kt, min60_rsv, min60_k, min60_d);

 

// 計算60分鐘布林通道

bb_upper = bollingerband(Close, BB_Length, BB_StdDev);

bb_middle = average(Close, BB_Length);

bb_lower = bollingerband(Close, BB_Length, -1 * BB_StdDev);

 

// 檢查停損點是否設定

if StopLoss = 0 then raiseruntimeerror("請設定停損(點)");

 

setBackBar(1000);;

value4=MTM(80)-MTM_MA(80);

 

// 布林通道多單進場條件

long_condition = day_k > day_d // 日線K > D

and low<= bb_lower+20; // 60分鐘價格觸及布林下軌

  

//布林通道 空單進場條件

short_condition = day_k < day_d // 日線K < D

  and high>= bb_upper-20; // 60分鐘價格觸及布林上軌

  

// 進場邏輯

if Position = 0  and long_condition then begin

        SetPosition(1); // 買進1張

        flag = 1; // 標記多單

        stoploss_price = FilledAvgPrice - StopLoss; // 設定多單停損價格

    end;

if Position = 0  and short_condition then begin

        SetPosition(-1); // 賣出1張

        flag = -1; // 標記空單

        stoploss_price = FilledAvgPrice + StopLoss; // 設定空單停損價格

    end;

 

 

// 多單出場與停損邏輯

if  Position = 0 and  Filled = 1 then begin

    // 停利條件:觸及布林上軌且60分K值<80

    if (Close >= bb_upper and min60_k < 80) or (close cross under bb_middle )  then begin

        SetPosition(0); // 停利出場

        flag = 0;

        stoploss_price = 0;

    end;

    // 停損條件

    if Close <=  stoploss_price then begin

        SetPosition(0); // 停損出場

        flag = 0;

        stoploss_price = 0;

    end;

end;

 

// 空單出場與停損邏輯

if Position = -1 and  Filled = -1 then begin

    // 停利條件:觸及布林下軌且60分K值>20

    if (Close <= bb_lower and min60_k > 20) or (close cross above bb_middle) then begin

        SetPosition(0); // 停利出場

        flag = 0;

        stoploss_price = 0;

    end;

    // 停損條件

    if Close >= stoploss_price then begin

        SetPosition(0); // 停損出場

        flag = 0;

        stoploss_price = 0;

    end;

end;

虎科大許教授 發文於   2025/06/17

當你空手且出現作多訊號,送出買進委託時,當下是抓不到庫存成本(需要先成交才抓得到),若成交了,又會因為position=0擋住,無法進入到IF裡面計算多單停損價格。空手出現作空訊號的情況也是一樣,無法計算空單停損價格。

發表回覆
Close