Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtesting with best available time resolution

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Backtesting with best available time resolution

    I've have a strategy which makes decision in 5-min resolution. My data is one-minute bar data, so I want to use these for simulation.
    Following this sample, I've configured my default bars to 5min, and my secondary to 1min. Then, by adding some debug prints, I've noticed that:
    • The entry orders are sent and filled in the timeframe of 1 min - as expected
    • The Stoploss and Profit target are filled in the 5 minutes time frame, resulting in inaccuracy


    In the log excerpt below, you can see that the limit order was placed at 18:05 (OK), Filled at 18:08 (OK), and Stop-Limited at 18:10 (wrong - should be 18:12).

    You can find my code below:
    Code:
    protected override void Initialize()
            {
                Add(PeriodType.Minute, 1);
                CalculateOnBarClose = true;
    
                SetProfitTarget(CalculationMode.Ticks, 2);
                SetStopLoss(CalculationMode.Ticks, 2);
            }
    protected override void OnBarUpdate()
            {
                if (BarsInProgress == 0)
                {                
                    entryLimit = Close[0];
                    EnterLongLimit(1, true, 1, entryLimit, "Enter long limit: 1min");
                }
    
                Print(String.Format("[{0}] {1} {2}: LP = {3}, O = {4}, H = {5}, C = {6}, L = {7}",
                        (BarsInProgress == 0) ? 5 : 1,
                        Time[0].ToString(@"dd/MM/yyyy HH:mm:ss"),
                        "OnBarUpdate",
                        entryLimit,
                        Open[0],
                        High[0],
                        Close[0],
                        Low[0]
                        ));
            }
    And the log excerpt:
    [5] 03/01/2011 18:05:00 OnBarUpdate: LP = 1.3344, O = 1.335, H = 1.3351, C = 1.3344, L = 1.3341
    [1] 03/01/2011 18:05:00 Fill.Buy 1 Limit @ 1.3344: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3345, NL = 1.3344
    [1] 03/01/2011 18:05:00 OnBarUpdate: LP = 1.3344, O = 1.3343, H = 1.3344, C = 1.3344, L = 1.3343
    [1] 03/01/2011 18:05:00 Fill.Buy 1 Limit @ 1.3344: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3345, NL = 1.3344
    [1] 03/01/2011 18:06:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3345, C = 1.3345, L = 1.3344
    [1] 03/01/2011 18:06:00 Fill.Buy 1 Limit @ 1.3344: FP = 0, NO = 1.3344, NH = 1.3344, NC = 1.3344, NL = 1.3344
    [1] 03/01/2011 18:07:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3344, C = 1.3344, L = 1.3344
    [1] 03/01/2011 18:07:00 Fill.Buy 1 Limit @ 1.3344: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3345, NL = 1.3344
    [1] 03/01/2011 18:08:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3345, C = 1.3345, L = 1.3344
    [1] 03/01/2011 18:08:00 Fill.Buy 1 Limit @ 1.3344: FP = 1.3344, NO = 1.3344, NH = 1.3344, NC = 1.3344, NL = 1.3343
    [5] 03/01/2011 18:05:00 OnExecution: Ex = Enter long limit: 1min
    [5] 03/01/2011 18:05:00 OnPositionUpdate: Pr = 1.3344, Pos = Long
    [5] 03/01/2011 18:05:00 Fill.Sell 1 Stop @ 1.3342: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3344, NL = 1.3343
    [5] 03/01/2011 18:05:00 Fill.Sell 1 Limit @ 1.3346: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3344, NL = 1.3343
    [1] 03/01/2011 18:09:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3344, C = 1.3344, L = 1.3343
    [5] 03/01/2011 18:05:00 Fill.Sell 1 Stop @ 1.3342: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3344, NL = 1.3343
    [5] 03/01/2011 18:05:00 Fill.Sell 1 Limit @ 1.3346: FP = 0, NO = 1.3344, NH = 1.3345, NC = 1.3344, NL = 1.3343
    [5] 03/01/2011 18:10:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3345, C = 1.3344, L = 1.3343
    [5] 03/01/2011 18:10:00 Fill.Sell 1 Stop @ 1.3342: FP = 1.3342, NO = 1.3343, NH = 1.3345, NC = 1.3345, NL = 1.3342
    [5] 03/01/2011 18:10:00 OnExecution: Ex = Stop loss
    [5] 03/01/2011 18:10:00 OnPositionUpdate: Pr = 0, Pos = Flat
    [1] 03/01/2011 18:10:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3344, C = 1.3344, L = 1.3344
    [1] 03/01/2011 18:11:00 OnBarUpdate: LP = 1.3344, O = 1.3343, H = 1.3344, C = 1.3344, L = 1.3343
    [1] 03/01/2011 18:12:00 OnBarUpdate: LP = 1.3344, O = 1.3343, H = 1.3344, C = 1.3344, L = 1.3342
    [1] 03/01/2011 18:13:00 OnBarUpdate: LP = 1.3344, O = 1.3344, H = 1.3344, C = 1.3344, L = 1.3344
    [1] 03/01/2011 18:14:00 OnBarUpdate: LP = 1.3344, O = 1.3343, H = 1.3345, C = 1.3345, L = 1.3342
    Last edited by boaza; 08-22-2011, 07:22 AM.

    #2
    Hello,

    Thank you for your post.

    From the description in your post, these results are expected. Since you are running this on a 5 minute time, you will only see your stops and targets updated on the 5 minute time frame. Backtesting is Calculate On Bar Close True in all cases.

    Since your entry order is sent on the 1 minute time frame, you will see these the entry order filled in minute intervals (8:08), however since the primary data series for the backtest is a 5 minute series, and your stops and targets are set in Initialized, you will only see these update in a 5 minute interval.

    I'm including a link to our reference sample on 'Separating logic to either calculate once on bar close or on every tick':



    Please let me know if you have any questions.
    MatthewNinjaTrader Product Management

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by love2code2trade, Yesterday, 01:45 PM
    4 responses
    28 views
    0 likes
    Last Post love2code2trade  
    Started by funk10101, Today, 09:43 PM
    0 responses
    7 views
    0 likes
    Last Post funk10101  
    Started by pkefal, 04-11-2024, 07:39 AM
    11 responses
    37 views
    0 likes
    Last Post jeronymite  
    Started by bill2023, Yesterday, 08:51 AM
    8 responses
    44 views
    0 likes
    Last Post bill2023  
    Started by yertle, Today, 08:38 AM
    6 responses
    26 views
    0 likes
    Last Post ryjoga
    by ryjoga
     
    Working...
    X