Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Plot Forward?

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

    Plot Forward?

    Hey everyone,

    I have an indicator I am working on that has a plot that I want to see for 10 or so future bars so as the candles are developing I can see the candles relative to the indicator. I also want to use it as somewhat of a projection.

    I tried adding to my series using negative bars ago, like this DataToPlot[-1] = 200; and that is not working and giving me an error.

    I have seen charts do this before, so I know its possible, just not sure how to code it. Can anyone help me out with this?

    Thanks

    #2
    Welcome to the forums ErikY!

    Bars would not exist that are not formed yet, so we can't plot into the future with negative bars ago indexes.

    I would suggest using Displacement to project a plot into the future. If you have other plots that you want to have visually aligned with the current bar, those plots can be assigned values with the same number you used for Displacement for the bars ago index.

    For example:

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description = @"Enter the description for your new custom Indicator here.";
            Name = "MyCustomIndicator13";
            Calculate = Calculate.OnBarClose;
            Displacement = 10;
            AddPlot(Brushes.Orange, "MyPlot");
            AddPlot(Brushes.Orange, "MyPlot2");
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (CurrentBar < 10)
            return;
        Values[0][0] = Close[0];
        Values[1][10] = Close[0];
    }
    Displacement - https://ninjatrader.com/support/help...splacement.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim, thanks for the welcome and thanks for the example. I think I understand whats going on here. So it looks from your example that the displacement works globally for all plots, so that would mean that you have to kind of manage that displacement on every plot?

      I have to play with this code a bit, but this would look to me like MyPlot would take the current values and project them forward 10 bars, and MyPlot2 would set the plot 10 bars ago to the current close, but show it over the current bar because of the displacement.

      Comment


        #4
        Hello ErikY,

        Yes, Displacement is an indicator property that affects all plots in the indicator.

        MyPlot is projected forward because we are assigning plot values at the current bar value and then using Displacement to project it forward.

        MyPlot2 is aligned with the current bar because we are assigning values at the bars ago 10 index and projecting that forward with Displacement.

        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks again Jim. I built a really simple example, and its kind of working, but not exactly as expected.

          I basically setup a parameter that selects the displacement value and I assigned that to Displacement.

          For some reason I don't understand, it seems to be limiting the projection to the right. If I put in 0-5 it works as expected, but 5 up to 100 does not change, it just stays at what looks like 5. Is there a limit on this or am I doing something wrong? Thanks for all the help, much appreciated!


          Code:
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Indicator here.";
          Name = "TestDisplacement";
          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;
          DisplaceBars = 100;
          
          Displacement=DisplaceBars;
          
          AddPlot(Brushes.Blue, "EMAPlot");
          AddPlot(Brushes.DeepSkyBlue, "EMAPlotProject");
          }

          Code:
          protected override void OnBarUpdate()
          {
          //Add your custom indicator logic here.
          
          
          if (CurrentBar >= DisplaceBars)
          {
          EMAPlot[DisplaceBars] = (High[0] + Low[0] + Close[0]) / 3;
          
          for (int i = 1; i <= DisplaceBars; i++)
          {
          EMAPlotProject[i] = (High[0] + Low[0] + Close[0]) / 3;
          }
          }
          }

          Comment


            #6
            Hello ErikY,

            Displacement can be set in the indicators dialog already, I would suggest to simply use that property instead of adding an input for it.

            Please note that State.SetDefaults occurs at the beginning of the script's life and would not be suitable to place to modify properties based on user input. State.Configure or State.DataLoaded would be more suitable.

            To better understand states and the NinjaScript lifecycle, please see the documentation below. It can also help visualize if you add a print for the current state in OnStateChange. For example:

            Code:
            protected override void OnStateChange()
            {
                Print(State);
            }
            OnStateChange - https://ninjatrader.com/support/help...tatechange.htm

            NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm

            I see that you are looping through 1 to DisplaceBars to assign previous EMAPlotProject values. If you want to project forward, assign the plot value for the current bar, and let Displacement project it forward. If you want to the plot to align with the current bar, only assign plot values for the same number of barsAgo as Displacement.

            Demo - https://drive.google.com/file/d/1lNO...w?usp=drivesdk

            For more information on how bar indexing works with OnBarUpdate, please see below. Looping over previous bars would not be necessary unless you want to modify plot values for previous bars.



            We look forward to assisting.
            JimNinjaTrader Customer Service

            Comment


              #7

              Jim, Thanks much for all your help. I discovered I can also use draw.line to do this drawing from current bar out to however many bars I want, relatively easily. I think that will ultimately end up givng me exactly what I need. Thanks again for your help!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by aussugardefender, Today, 01:07 AM
              0 responses
              3 views
              0 likes
              Last Post aussugardefender  
              Started by pvincent, 06-23-2022, 12:53 PM
              14 responses
              238 views
              0 likes
              Last Post Nyman
              by Nyman
               
              Started by TraderG23, 12-08-2023, 07:56 AM
              9 responses
              384 views
              1 like
              Last Post Gavini
              by Gavini
               
              Started by oviejo, Today, 12:28 AM
              0 responses
              4 views
              0 likes
              Last Post oviejo
              by oviejo
               
              Started by pechtri, 06-22-2023, 02:31 AM
              10 responses
              125 views
              0 likes
              Last Post Leeroy_Jenkins  
              Working...
              X