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

C# Newbie -- Helpful Tip When Renaming Plots

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

    C# Newbie -- Helpful Tip When Renaming Plots

    This was a bit of a hair-puller.

    I generated a new indicator because I needed more plot lines for various purposes.

    One of my plots was named "CustomOne" because I figured I could just change the name later when I needed to, right?

    Well, if you go into the section that has them defined and do that, first -- the change doesn't show up until you close ninjatrader and re-open it, and second, once you do and try to use your fancy new plot name, it will just shrug its shoulders and say "name does not exist in this context" or some irritating thing like that. (You also have to change the name in the AddPlot function call, which is what I did.)

    How to solve?

    Well, you have to index this thing.

    If your plot value was say, 4, then to access it and set its value you need to use a statement like:

    Code:
    Values[4][0] = 0;
    And NOT the name as you may be used to doing:

    Code:
    MyNewPlotName[0] = 0;
    This was extremely irritating so I hope it helps someone. The label in the data box will be correct when you open it for the chart, you just can't seem to use the new name in your code at all. Fun...
    Last edited by TallTim; 12-23-2022, 12:14 PM.

    #2
    I'll bite -- let's discuss your tip.

    To do so, let's use @Bollinger.cs,
    Ready?

    Inside State.SetDefaults, we find,

    AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerUpperBand);
    AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerMiddleBand);
    AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerLowerBand);


    ​Notice the order -- plot for 'upper' band is first, then 'middle' band, and last is 'lower' band -- this is important.

    The indexes for the Values series, ya know, Values[0], Values[1], and Values[2], these come from
    the sequence of how the AddPlots statements are arranged. That is, the order of AddPlots is important.

    Series are zero-based.
    The '0' in 'Values[0]' is the plot series for the 1st call to AddPlot.
    The '1' in 'Values[1]' is the plot series for the 2nd call to AddPlot.
    etc etc.

    See that?
    The upper band plot is stored at Values[0] because its AddPlot is first.

    -=o=-

    Ok, next up, take a gander at OnBarUpdate,

    Upper[0] = sma0 + NumStdDev * stdDev0;
    Middle[0] = sma0;
    Lower[0] = sma0 - NumStdDev * stdDev0;


    Woah, woah, were did those short names come from?
    What allows you to use 'Upper', 'Middle', and 'Lower' as series names?
    (Remember, these are 'series', do not speak of them as 'arrays' -- that is wrong.)

    Well, those names are not magic.
    Scroll down, look at the Properties region,

    Code:
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Upper
    {
        get { return Values[0]; }
    }
    ​
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Middle
    {
        get { return Values[1]; }
    }
    
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Lower
    {
        get { return Values[2]; }
    }
    These properties are the glue that allows you to use Upper/Middle/Lower
    as series names. I've reordered them, showing them in order, but you'll notice
    that inside @Bollinger.cs they're actually organized in a yucky hodge-podgey
    kind of way. Doesn't matter.

    -=o=-

    What's my point?

    If you want to access your new plot by a specific name, then you'll need to
    setup a property to do that -- accessing a plot series by it's 'name' doesn't
    come for free, you use the features of C# to make a property precisely to
    give it a nickname to make it convenient to use.

    The fact that your property name and the plot name (specified in AddPlot)
    are the same, well, that would be considered a coincidence.

    I mean, if you want to access your plot series using the name MyNewPlotName,
    you must define an appropriate property, like this,

    Code:
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> MyNewPlotName
    {
        get { return Values[4]; }
    }
    That's the glue, that's how you do it -- with a property.
    The 'getter' is all you need here.

    (Careful, Values[4] is the reference to the 5th plot series, aka, the
    5th call to AddPlot. Remember, all arrays in C#, as well as all series,
    are zero-based.)

    Final thoughts:
    It's not really a tip or trick, it's just the most convenient way to
    access a plot series
    in C#. But yeah, if you don't know how to do it,
    you might feel kinda stuck, not knowing the 'trick' behind it.

    Make sense?

    Last edited by bltdavid; 12-24-2022, 02:23 PM.

    Comment


      #3
      Maybe I should mention this, too:

      Looking at AddPlot, you'll note that the last argument is always
      a string -- this is the name of the plot.

      But that string doesn't really mean anything.

      That is, say your new custom plot is defined like this,

      // Adds a blue Dash-Line style plot with 5pixel width and 50% opacity
      AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Dash, 5, 50), PlotStyle.Line, "MyNewPlotName");​


      The code,

      MyNewPlotName[0] = ...some value...

      does not come for free.

      You must also add a custom property,

      Code:
      [Browsable(false)]
      [XmlIgnore()]
      public Series<double> MyNewPlotName
      {
          get { return Values[0]; }
      }​
      The index '0' in 'Values[0]' represents the first call to AddPlot.

      If your custom plot is the 5th AddPlot, you'd use 'Values[4]' instead.

      -=o=-

      What's my point?

      I mean,
      the variable name MyNewPlotName ... where does it come from?

      It looks like the association is created just by using the string "MyNewPlotName"
      as the last argument to AddPlot -- but nope, that string doesn't mean squat.

      The association, aka, the glue, comes from the property being defined with the
      same name as the string in the last argument to AddPlot.

      Yep, that's the glue.

      Just my 2˘.

      Last edited by bltdavid; 12-24-2022, 12:03 AM.

      Comment


        #4
        Originally posted by bltdavid View Post
        Looking at AddPlot, you'll note that the last argument is always
        a string -- this is the name of the plot.

        But that string doesn't really mean anything.
        Ok, well, to be specific ...

        The name you supply in AddPlot is the name you
        see later in the property grid -- otherwise, I'm not
        sure that string does much for you.

        I mean, you could setup plot names that are numbered, like this,

        AddPlot(Brushes.Goldenrod, "1. Upper");
        AddPlot(Brushes.Goldenrod, "2. Middle");
        AddPlot(Brushes.Goldenrod, "3. Lower");

        Obviously, in code, these strings are illegal as variable names.


        In summary, that name string is used by NT in the property grid, but
        otherwise doesn't do much for you, the NinjaScript developer.
        Last edited by bltdavid; 12-24-2022, 12:02 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rjbtrade1, 11-30-2023, 04:38 PM
        2 responses
        75 views
        0 likes
        Last Post DavidHP
        by DavidHP
         
        Started by Stanfillirenfro, Today, 07:23 AM
        3 responses
        12 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by FitSpressoHonest, Today, 09:14 AM
        0 responses
        2 views
        0 likes
        Last Post FitSpressoHonest  
        Started by Davide999, 05-18-2023, 03:55 AM
        4 responses
        557 views
        1 like
        Last Post kcwasher  
        Started by rexsole, Today, 08:39 AM
        2 responses
        8 views
        0 likes
        Last Post NinjaTrader_Erick  
        Working...
        X