Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

real-time backtesting

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

    real-time backtesting

    Hi!

    I created a strategy with two timeframes (primary is on 1-tick, secondary on 1 Min) on the same instrument what allows me to update the value of a dynamic stop-loss (takes its value from a custom indicator) every tick.

    This is needed, because the StopLoss should take the maximum value of the indicator ever reached (calculated on every tick) since a position was entered.

    In Market Replay environment the strategy behaves like expected without any problems.

    But when I start a backtest with the same setup, I don't get any results and the log-tab shows me following message:

    Failed to call mehtod "Initialize" for strategy "AATrailProb"; "Position" property can not be accessed from within "Initialize" method.

    1. What does this mean?
    2. In general: Is my logic right that I should get "near to live-trading"-behaviour of my strategy when I backtest this strategy calculating indicator values on a timeframe that should represent the live-trading-primary-bar-intervall (eg 1 Min) but letting the strategy run on a one-tick-primary-timeframe when backtesting?

    I'm using the Position-method for resetting purposes within the strategy:
    Here is my code:

    Code:
     [INDENT]#region Initialize
    protected override void Initialize()
    
    {
    Add(PeriodType.Minute, xminutes);
    
    
    CalculateOnBarClose = true;
    
    StopValueSCalc = new DataSeries(this);
    StopValueLCalc = new DataSeries(this);
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    /// 
    #endregion
    
    
    #region OnBarUpdate
    
    protected override void OnBarUpdate()
    
    { 
    if (Historical)
    return; 
    if (CurrentBar < 10)
    return;
    
    
    if (BarsInProgress == 1)
    {
    StopValueSCalc.Set(ATRTrailingStop(10, 1, 20, 1).ATRTrailup[0]);
    StopValueLCalc.Set(ATRTrailingStop(10, 1, 20, 1).ATRTraildown[0]);
    ResetSLL = ATRTrailingStop(10, 1, 20, 1).ATRTraildown[0];
    ResetSLS = ATRTrailingStop(10, 1, 20, 1).ATRTrailup[0];
    }
    
    
    if (BarsInProgress == 0)
    { 
    #region LONG
    
    
    if (Closes[0][0] > Highs[0][1])
    {
    EnterLong(DefaultQuantity, "ENTERLONG");
    }
    
    if (Positions[1].MarketPosition == MarketPosition.Long)
    {
    StopValueL = Math.Max(ResetSLL, MAX(StopValueLCalc, BarsSinceEntry())[0]);
    SetStopLoss("ENTERLONG", CalculationMode.Price, StopValueL, false);
    }
     
    
    if (Positions[1].MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss("ENTERLONG", CalculationMode.Price, ResetSLL, false);
    }
     
    
    #endregion
    thx for help!!
    [/INDENT]

    #2
    - please make sure you are on latest 6.5.0.8 or 6.0.1000.9
    - somehow you access the Position property directly or indirectly in the Initialize method in your code. This is not allowed. Please revisit your code
    - any backtest is different than the "real" thing. Please read more here why: http://www.ninjatrader-support.com/H...sBacktest.html

    Comment


      #3
      Hi Dierk!

      1.) I updated from 6.0.1000.7 to 6.0.1000.9 but I still get the same error in the log tab.

      2.) you wrote
      "- somehow you access the Position property directly or indirectly in the Initialize method in your code. This is not allowed."

      If you take a look at my code, you can see, that I only access the values of the custom dataseries "StopValueSCalc" and "StopValueLCalc", which have to be defined within the Initialize-Method, havn't they?
      Anyway I moved the DataSeries-definition to the beginning of the OnBarupdate-Section and changed the code in the Initialize-section to:

      Code:
       
      {
      Add(PeriodType.Minute, 1);
       
       
      CalculateOnBarClose = true;
       
       
      }
      and I also tried the variations with "Position" instead of "Positions[1]" but without any change of result.

      I still get the same error message after starting the backtest-procedure. Does that mean, the Position-method cannot be used within a MTF-Strategy?

      3.) Of course I already read the chapter in the NT-help-section and some other articles on backtesting-problems in general.

      Historical tick-data is provided by my data-supplier so I try to come as close as possible to real-time-conditions (of course there are systemical restrictions).

      The Market-Replay-Tool of NT is the only alternative to this, though I don't have enough replay data yet to get valid results. Also optimization of input parameters of a strategy can only be done on historical basis economically (instead of trying several set ups manually).

      Would you agree that my approach of a 1-Tick-MTF-Strategy is right concerning that target or would you suggest something completely different? I'd be glad for just a little push in the right direction!

      thx

      Comment


        #4
        Could you please mail be your complete strategy and a screenshot of any settings needed to reproduce the problem to "dierk AT ninjatrader DOT com"? Thanks

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X