Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculating EMA9 Based on 5-Minute Close Prices and Current Tick Price

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

    Calculating EMA9 Based on 5-Minute Close Prices and Current Tick Price



    Hello NinjaTrader community,

    I'm working on a strategy that primarily operates on a 5-minute bar series, calculating on Calculate.OnBarClose. My goal is to calculate the EMA9 based on the 5-minute close prices and the current tick price, allowing me to adjust my profit target price dynamically when i need to. What I've Tried:
    • I initially tried calculating an EMA9 using a 1-tick data series alongside my 5-minute series. However, I found that the EMA9 values were significantly different from what I expected, appearing much closer to the current instrument price.
    • I also experimented with running my strategy on Calculate.OnEachTick, which provided the desired EMA9 values. However, this approach conflicts with the rest of my strategy logic, which is designed to run on Calculate.OnBarClose.
    Observations:
    • When running on Calculate.OnEachTick, the EMA9 for the 5-minute period was calculated using the last 9 bars and the current tick value.
    • When running on Calculate.OnBarClose, the EMA9 for the 5-minute period was calculated using only the last 9 bar close values, ignoring the current tick.
    Proposed Solution:


    I came up with a solution that involves creating a custom EMA9 indicator class. This class calculates the EMA9 based on the 5-minute close prices and the current tick price. By updating this custom indicator on each tick, I can mimic the behavior of calculating on each tick while keeping my main strategy logic on Calculate.OnBarClose, just like if i were to add an ema9 to the chart manually and assign calculation mode on each tick.


    Here's a snippet of the proposed solution:
    Code:
    public class CustomEma9 : Indicator
    {
        private int period;
        private double multiplier;
    
        public CustomEma9(int period)
        {
            this.period = period;
            this.multiplier = 2.0 / (period + 1);
        }
    
        protected override void OnUpdate()
        {
            double emaValue = (Input[0] - Value[1]) * multiplier + Value[1];
            Value[0] = emaValue;
        }
    }
    ​
    And in my strategy:
    Code:
    private CustomEma9 customEma9;
    
    protected override void OnStateChange()
    {
        // ...
        customEma9 = new CustomEma9(9);
    }
    
    protected override void OnBarUpdate()
    {
        if (BarsInProgress == 3) customEma9.Update(Close[0]); //BarsInProgress == 3 is the tick data
        // Main strategy logic
    }
    ​
    • Is this approach sound, or is there a more elegant or straightforward way to achieve my goal within NinjaTrader?
    • Are there any nuances to how NinjaTrader calculates EMAs that I should be aware of?
    • Is it even possible to calculate an indicator on each tick or timeframe i choose to by adding a secondary bar series to it? (in this case a 1 tick bar series)
    • If all of the above are not possible, is it possible for me to just calculate the indicator inside the strategy using both time frames? if so, can i just create it inside an addon class and then plot it somehow although its not technically under an indicator class?
    • I know this code might not be correct in any way shape or form, but a representation of what i would like to achieve.

    I appreciate any insights or advice you can provide. Thank you for your assistance!

    Best regards, Aviram Y.



    Last edited by Aviram Y; 08-04-2023, 03:50 PM.
    Aviram Y
    NinjaTrader Ecosystem Vendor - Aviram Y

    #2
    Hello Aviram Y,

    For what you described it would normally be suggested to just run the script OnEachTick as that would include the building bar. If you need to still process logic OnbarClose you can surround that code with a condition checking if its the first tick of the bar.


    Comment


      #3
      I see, this is much more elegant indeed, thank you for the service, much appreciated!
      Aviram Y
      NinjaTrader Ecosystem Vendor - Aviram Y

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      559 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 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
      546 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X