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 CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    52 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    31 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    194 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Started by CaptainJack, 04-24-2026, 11:07 PM
    0 responses
    355 views
    0 likes
    Last Post CaptainJack  
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    274 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X