關於交易時段日夜盤接續問題

  •   171 
  • 最後發表   顆顆  2022 七月 13
顆顆 發文於   2022/06/28

請教小幫手

我的策略是用在小台全日15分K  在盤中的買進賣出訊號都有執行

但今天剛好在最後13:30出訊號  應該在下一根K棒開盤價15:00時會下指令買進

不過程式認定是13:45當時剛好收盤  於是出現下列訊息

想請問  有什麼辦法可以讓13:45的買進訊號在15:00執行

// MACD 黃金交叉 (dif向上穿越macd)
//
input: FastLength(12), SlowLength(65), MACDLength(15);
variable: difValue(0), macdValue(0), oscValue(0);
SetTotalBar(200);
SetInputName(1, "DIF短期期數");
SetInputName(2, "DIF長期期數");
SetInputName(3, "MACD期數");
MACD(weightedclose(), FastLength, SlowLength, MACDLength, difValue, macdValue, oscValue);

input: profit_point(0, "停利(點)");
input: loss_point(30, "停損(點)");
var: 
    long_condition(false),          { 做多 }
    short_condition(false);         { 做空 }

long_condition = difValue cross above macdValue and close > ema(close,20); 
short_condition = difValue cross below macdValue and close < ema(close,20);


if Position = 0 and long_condition then begin
    SetPosition(1);             { 買進1張 }
end;

if Position = 1 and Filled = 1 then begin
    { 依照成本價格設定停損/停利 }
    var: intrabarpersist stoploss_point(0);

    { 計算停損價格 }
    if stoploss_point = 0 then begin
        stoploss_point = FilledAvgPrice - loss_point;
    end;

    { 如果價格上漲的話, 則往上挪動停損價格. 停損價格只會越來越高 }
    if Close > FilledAvgPrice then begin
        if Close - loss_point > stoploss_point then begin
            stoploss_point = Close - loss_point;
        end;    
    end;    

    if profit_point > 0 and Close >= FilledAvgPrice + profit_point then begin
        { 停利 }
        SetPosition(0);
        stoploss_point = 0;
    end else if Close <= stoploss_point then begin
        { 停損 }
        SetPosition(0);
        stoploss_point = 0;
    end;
end;



if Position = 0 and short_condition then begin
    SetPosition(-1);            { 做空賣出1張 }
end;

if Position = -1 and Filled = -1 then begin
    { 依照成本價格設定停損/停利 }
    var: intrabarpersist stoploss_point1(0);

    { 計算停損價格 }
    if stoploss_point1 = 0 then begin
        stoploss_point1 = FilledAvgPrice + loss_point;
    end;

    { 如果價格下跌的話, 則往下挪動停損價格. 停損價格只會越來越低 }
    if Close < FilledAvgPrice then begin
        if Close + loss_point < stoploss_point1 then begin
            stoploss_point1 = Close + loss_point;
        end;    
    end;    

    if profit_point > 0 and Close <= FilledAvgPrice - profit_point then begin
        { 停利 }
        SetPosition(0);
        stoploss_point1 = 0;
    end else if Close >= stoploss_point1 then begin
        { 停損 }
        SetPosition(0);
        stoploss_point1 = 0;
    end;
end;

排序方式: 標準 | 最新
XQ小幫手 發文於   2022/07/04

Hello 顆顆,

 

您可以用 if time = 150000 and long_condition[1] or long_condition 的方式,這樣在 15:00 這根Bar 運算時,只要前一根Bar (13:30 ~ 13:45) 或當根Bar (15:00 ~ 15:15) 符合條件的話就會買進。

另外,如果您使用逐筆洗價,並搭配 long_condition1[1] 這種方式就可以在夜盤開盤後的第二次洗價,因上一根 (日盤結束那根) 條件符合而下委託進場。

 

顆顆 發文於   2022/07/08

另外請教一個問題:

這次我修改了一些程式碼,因為想要即時停損,我改成逐筆洗價,並且把進出場改成long_condition[1]和short_condition[1]。

不過有時打到停損太快,剛進場之後的當根K棒就停損出場。

部位歸零後,因為狀況還是符合long_condition[1]和short_condition[1],導致又馬上進場一次,回測似乎沒有看到這個情形。請問該如何描述才能停損後不要再馬上進場?

XQ小幫手 發文於   2022/07/13

Hello 顆顆,

 

您可以用變數來記錄觸發的K棒序號,如果序號有改變才能進場。

舉例來說:

input: _hold(3, "等待期數");

var:intrabarpersist _count(0);

 

condition1 = 進場條件;

condition2 = 出場條件;

if condition1 and currentbar >= (_count + _hold) then setposition(1, market);

if condition2 then begin

    setposition(0, market);

    _count = currentbar;

    end;

這樣至少要到出場後的第三根Bar策略才能再度進場。

發表回覆
Close