Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot falls behind

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

    Plot falls behind

    Hi,
    I have a strategy which adds an additional 30min data series and my custom indicator which should consume that data series. The problem is now, that the plot of my indicator "falls" behind of chart.

    Click image for larger version

Name:	Screenshot 2022-04-30 115050.png
Views:	243
Size:	41.0 KB
ID:	1199365

    Here is my simple test strategy:

    Code:
    public class MyTestStrategy : Strategy
    {
    private MyTestIndicator myTestIndicator;
    private const int Secondary = 1;
    protected override void OnStateChange()
    {[INDENT]if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyTestStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;[/INDENT]
     
    }
    else if (State == State.Configure)
    {[INDENT]AddDataSeries(BarsPeriodType.Minute, 30);[/INDENT]
     }
    else if (State == State.DataLoaded)
    {
              myTestIndicator = MyTestIndicator(BarsArray[Secondary]);
              AddChartIndicator(myTestIndicator);
    }
    }
    
    protected override void OnBarUpdate()
    {
        //Add your custom strategy logic here.
        if (BarsInProgress == 0 && CurrentBar < BarsRequiredToPlot) return;
    
        if(BarsInProgress == Secondary)
       {
            var result = myTestIndicator[0];
       }
    }
    }
    My test indicator:

    Code:
    public class MyTestIndicator : Indicator
    {
       private SMA sma10;
       private double _result = 0;
       protected override void OnStateChange()
       {
          if (State == State.SetDefaults)
          {
             Description = @"Enter the description for your new custom Indicator here.";
              Name = "MyTestIndicator";
              Calculate = Calculate.OnBarClose;
              IsOverlay = false;
              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(Brushes.Orange, "TestPlot");
              AddLine(Brushes.DarkCyan, 0.5, "Strong Buy");
              AddLine(Brushes.DarkCyan, 0.1, "Buy");
              AddLine(Brushes.DarkCyan, -0.1, "Sell");
              AddLine(Brushes.DarkCyan, -0.5, "Strong sell");
        }
       else if (State == State.Configure)
       {
    
       }
       else if (State == State.DataLoaded)
       {
       sma10 = SMA(10);
       }
    }
    
    protected override void OnBarUpdate()
    {
       //Add your custom indicator logic here.
       TestPlot[0] = _result;
    
       if (CurrentBar < 2)
       {
          return;
       }
    
       var signals = new List<double>
       {
          GetMovingAverageSignal(sma10[0]),
       };
    
       _result = signals.Average();
    }
    
    private int GetMovingAverageSignal(double value)
    {
    if (value < Close[0]) return 1;
    if (value > Close[0]) return -1;
    return 0;
    }
    
    public override void OnCalculateMinMax()
    {
    MinValue = -1;
    MaxValue = 1;
    }
    
    #region Properties
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> TestPlot
    {
    get { return Values[0]; }
    }
    #endregion
    
    }
    }

    How could I fix this?
    Thx,
    Sven

    #2
    Hello SvenB,

    TestPlot[0] is being set to _result; at the top of OnBarUpdate(). _result is going to have the previous bars value because it is set at the end of OnBarUpdate. If you want the currently calculated value set on the current bar, set TestPlot[0] at the bottom of OnBarUpdate().

    If you want a series update before the bar closes, use the Calculate OnPriceChange or OnEachTIck settings. (In historical this would also require TickReplay)


    If a plot is being set from a larger time frame, for the in-between bars where no value is set, set to the previous bars value to continue the plot line forward.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      after a long time, I'm coming back to this problem.

      Click image for larger version

Name:	Screenshot 2022-09-05 211551.png
Views:	139
Size:	692.5 KB
ID:	1214413

      Originally posted by NinjaTrader_ChelseaB View Post
      If a plot is being set from a larger time frame, for the in-between bars where no value is set, set to the previous bars value to continue the plot line forward.
      https://ninjatrader.com/support/foru...196#post820196
      The problem is, I don't understand how this could be done. My indicator uses the 30min timeframe. I feed the secondary Bars into my indicator. How could my indicator draw more points? It gets called every 30min only.

      Why does my indicator not draw on the correct x-axis position? I don't get it.
      And in my strategy I see no way to influence how the indicator gets drawn. I'm not able to feed more data into it or something like that.



      Thx,
      Sven
      Last edited by SvenB; 09-05-2022, 01:18 PM.

      Comment


        #4
        Hello Sven,

        The example I have provided a link to demonstrates setting the plot value on the current bar to the value of the previous bar (to carry it forward and draw a line) when there is value for the current bar due to the higher time frame.

        Why does my indicator not draw on the correct x-axis position?
        The X axis is the Time axis that is horizontal. If there no line, there is no continue plot value being set. Print the values on each bar to understand why
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Sorry, I still don't get it. If I print BarsInProgress in the OnBarUpdate of my indicator, it is always "0". Which is understandable because there is only one time series provided to the indicator (30min).
          So how can I carry it forward? There are no in-between updates where I could provide data to "interpolate" a line.

          Could you please outline the idea in the code shown in my initial post? That would be awesome!

          Thx,
          Sven


          Comment


            #6
            Hello Sven,

            I may have misunderstood what you are trying to achieve.

            In post one you have stated:
            I have a strategy which adds an additional 30min data series
            Is this still the case?
            Have you added a series with AddDataSeries()?

            If so, there are two series, not just one. The primary series is BarsInProgress 0. The added series is BarsInProgress 1.
            If you are not getting prints for BarsInProgress 1, this would imply you have not added a series with AddDataSeries()..
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yes, I had the same idea/solution after reading https://ninjatrader.com/de/support/h...nstruments.htm again.
              I only fed my 30min time series via constructor into my indicator. which was wrong. Solution is to feed the primary time series (2000tick) into my indicator and let the indicator add the 30min time series itself.
              And as described in the manuals, the strategy still needs to subscribe to the 30min time series.

              And voila, two different bar updates and I can provide points in between.

              Click image for larger version

Name:	Screenshot 2022-09-06 181400.png
Views:	138
Size:	202.8 KB
ID:	1214524

              Thx,
              Sven




              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              71 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              143 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              76 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              47 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              51 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X