Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adapting OnPriceChange strategy to OnBarClose

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

    Adapting OnPriceChange strategy to OnBarClose

    Previously, I wrote all my strategies for market replay using OnPriceChange. Now I want to run backtests and thus must adapt my code to OnBarClose. Most of my code follows this structure:

    Code:
    //Long Conditions
    if (Position.MarketPosition == MarketPosition.Flat)
    // Other conditions here
    {
    EnterLong();
    state = 1
    }
    
    //Set TP & SL Targets
    if ((Position.MarketPosition == MarketPosition.Long)
    && (state == 1))
    {
    MyStop = (Low[0] + (-10 * TickSize)) ;
    MyProfit = (Position.AveragePrice + (10 * TickSize)) ;
    state = 2;
    }
    
    //Submit Exit order
    if (Position.MarketPosition == MarketPosition.Long)
    && (state == 2))
    {
    ExitLong();
    }
    
    ​
    This works fine with OnPriceChange BUT I'm guessing it isn't going to work quite the same with OnBarClose because it relies too much on Position. If the code runs once onBarClose then it's highly unlikely an order would fill instantly, thus the last 2 parts of the code would only execute on the close of the next bar, correct?

    What's the best way to adapt this code to work with OnBarClose?

    #2
    Hello several,

    Thank you for your post.

    You can still track MarketPosition with OnBarClose. The only difference is that all logic will only execute once per bar, at the bar close. You cannot execute orders intrabar using OnBarClose. If you wanted to be be able to enter when flat and exit when long or short on the same bar, you would need to use OnPriceChange or OnEachTick.

    To simulate OnPriceChange historically for backtesting, you would need to enable Tick Replay and implement 1-tick intrabar granularity.

    Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

    SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

    TickReplay - https://ninjatrader.com/support/help...ick_replay.htm



    Please let me know if you have any further questions. ​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    128 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    117 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X