Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to pass real-time value and recalculate the indicator ?

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

    How to pass real-time value and recalculate the indicator ?

    It's a very concrete question. How to read real-time (last value) indicator[0] value an use this value to redraw/recalculate the indicator from the beginning?
    I've tried several methods like to call Custom Indicator and loading in State.DataLoaded / State.Historical but then OnBarUpdate much the bars, so I 'm not getting fixed value on real-time bar, but where the OnBarUpdate is. See the pic for more information. Thanks in advance.


    Click image for larger version

Name:	image.png
Views:	325
Size:	88.8 KB
ID:	1234983​

    #2
    Hello, thanks for writing in. The indicator would need to loop back on previous bars and re-calculate its plot. You can alternatively loop back to prior bars in your host indicator and pass new parameters to the indicator on each bar. You can loop through all the bars like this:

    Code:
    for(int i = CurrentBar; i > 0; i--)
    {
        Print(Close[i]);
    }​

    Comment


      #3
      Didn't knew I can fill Plots backwords... It works! Thanks. Here is it :

      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class LoopBackTest : Indicator
      {
      
      private ADX _adx;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Indicator here.";
      Name = "LoopBackTest";
      Calculate = Calculate.OnBarClose;
      IsOverlay = true;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
      //See Help Guide for additional information.
      IsSuspendedWhileInactive = true;
      
      AddPlot(new Stroke(Brushes.Red, 3), PlotStyle.Line, "_loopValue");
      }
      else if (State == State.Configure)
      {
      }
      else
      if (State == State.Historical)
      {
      _adx = ADX(14);
      }
      }
      
      double real_time_adx;
      protected override void OnBarUpdate()
      {
      // doing hithing until we reach recent bar
      if ( CurrentBar == Count-1)
      {
      //getting the value we need
      real_time_adx = _adx[0];
      
      // back loop to fill Plot with values
      for(int i = CurrentBar; i>0; i--)
      {
      _loopValue[i] = High[i] + real_time_adx;
      
      }
      
      }
      }
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> _loopValue
      {
      get { return Values[0]; }
      }
      }
      }
      But, is it possible this method to reduce platform performance, it will redo all bars on every tick if I update OnTick or OnPriceChange?

      Comment


        #4
        I thing this way is better :
        Code:
        double real_time_adx;
        [B]bool isFirstRun = true;[/B]
                protected override void OnBarUpdate()
                {
                    // doing nothing until we reach recent bar
                    if ( CurrentBar == Count-1 && isFirstRun )
                    {
                        //getting the value we need
                        real_time_adx = _adx[0];
        
                        // back loop to fill Plot with values
                        for(int i = CurrentBar; i>0; i--)
                        {
                            _loopValue[i] = High[i] + real_time_adx;
        
                        }
        
        [B]isFirstRun = false;[/B]
        
                    }
        
                    // then moving on OnBarUpdate - real-time
                    if(State == State.Realtime)
                        _loopValue[0] = High[0] + real_time_adx;
        
        
                }​

        Comment


          #5
          Hi, thanks for the follow up. There are many different ways of doing this. The way I showed might be the least efficient way of doing it, but you can always optimize the idea especially if the indicator needs to run OnEachTick.

          Comment


            #6
            Thanks Chris, yes I tried this method, and it works for a limited number of cases. Haven't got time yet, but I'll try to use a Dispatcher at the first run and first to get the value of external indicator at the recent bar, then normally to go over all bars. I hope this will be better, I'll post the result later

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            637 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            366 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            107 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            569 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            571 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X