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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X