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

disabling parameter for a secondary series without error / problem for code lovers

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

    disabling parameter for a secondary series without error / problem for code lovers


    hello everyone,

    I found for anyone a problem to solve (you are welcome XD), for who likes brain/coding exercices :

    Attached is a MTF indicator sample that plots one Sma for each added DataSeries ( 2 added series + the Primary = 3 series).

    The goal is to allow the user -from the indicator window- to on/off switch the plots, AND/OR the added series (implying an on/off switch of the plots too).
    We could add the series by default whithout any enabling parameter, and just on/off switch the plots only, but instead we try to have the option to eventually not add another series at all (for performance purposes for example).

    In the sample below, if we enable all parameters, the plots are functioning great.
    But if we disable isSc1On ("is scale 1 on"), then we cause an outside the bounds of the array error.
    This is due to the Closes[1] disapearance, causing Closes[2] to become Closes[1]. (but Closes[2] is not written Closes[1]... )

    Would anyone be able to tell and show us a trick, to make the disabling of a secondary an available option ?
    Is this just not possible ?

    Attached Files

    #2
    Hello Amedeus,

    Thank you for your note.

    Yes, it'd be possible. There's a couple spots you'll need to change in your logic to get it working, though.

    First, you'll need to change the order of your plots because otherwise you're going to get weird values returned if you turn off plots 1 or 2 because down in the parameters those are returning Values[0] and Values[1] and if they're removed the primary series would be trying to return Values[2] when that doesn't exist.

    Code:
     #region // AddPlots //
    
    if (isSc0On == true)
    {
    AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Solid, 1), PlotStyle.Line, "sma0");
    };
    if (isSc1On == true)
    {
    AddPlot(new Stroke(Brushes.Coral, DashStyleHelper.Solid, 1), PlotStyle.Line, "sma1");
    };
    if (isSc2On == true)
    {
    AddPlot(new Stroke(Brushes.Aquamarine, DashStyleHelper.Solid, 1), PlotStyle.Line, "sma2");
    };
    #endregion // AddPlots //
    And then in your properties make sure to swap around the Values[index] values (I've omitted other properties here since those don't need changing):

    Code:
    [Browsable(false)] [XmlIgnore] public Series<double> sma2 { get { return Values[[B]2[/B]]; } }
    
    [Browsable(false)] [XmlIgnore] public Series<double> sma1 { get { return Values[[B]1[/B]]; } }
    
    [Browsable(false)] [XmlIgnore] public Series<double> sma0 { get { return Values[[B]0[/B]]; } }
    Next, for the issue with Closes, you'll need to add a bit of extra logic in State.DataLoaded for those different scenarios for whether these are turned on or off:

    Code:
    else if (State == State.DataLoaded)
    {
    if (isSc1On == true && isSc2On == true)
    {
    Sma1 = SMA(Closes[1], 20);
    Sma2 = SMA(Closes[2], 20);
    }
    else if (isSc1On == true && isSc2On == false)
    {
    Sma1 = SMA(Closes[1], 20);
    }
    else if (isSc1On == false && isSc2On == true)
    {
    Sma2 = SMA(Closes[1], 20);
    }
    if (isSc0On == true)
    {
    Sma0 = SMA(Closes[0], 20);
    };
    }
    The last thing you'd need to address would be the CurrentBars checks, because if isSc1On is false, the sma2 series would be CurrentBars[1] not CurrentBars[2]:

    Code:
    // CurrentBars Checks
    if (isSc0On == true)
    {
    if ( CurrentBars[0] < Loockback ) {return;};
    if ( CurrentBars[0] < 1 ) {return;};
    };
    if (isSc1On == true && isSc2On == true)
    {
    if ( CurrentBars[1] < Loockback ) {return;};
    if ( CurrentBars[1] < 1 ) {return;};
    if (CurrentBars[2] < Loockback) { return; };
    if (CurrentBars[2] < 1) { return; };
    };
    if (isSc1On == false && isSc2On == true)
    {
    if ( CurrentBars[1] < Loockback ) {return;};
    if ( CurrentBars[1] < 1 ) {return;};
    };
    I'm attaching a revised script illustrating these changes.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      hey Kate, thank you very much.

      I get it now : we implement with conditons for each possible scenarios by adjusting the inputs of the indicators and the CurrentBars checks consequentially.

      Now about the plots values order that you have me change :
      I had them backwards in order to have the sma0 plot -when enabled- always on top of the others plots (the code reading it always last).
      Do we have to let this go, or there coud be a solution ?
      Last edited by Amedeus; 03-10-2022, 05:41 PM.

      Comment


        #4
        Hello Amedeus,

        Thank you for your reply.

        Not really a way around that unfortunately since the Values index for each would change and try to return the wrong series, causing an error the other way around.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi Kate,

          Got it, thank you very much.

          Have a great day.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by bortz, 11-06-2023, 08:04 AM
          47 responses
          1,607 views
          0 likes
          Last Post aligator  
          Started by jaybedreamin, Today, 05:56 PM
          0 responses
          9 views
          0 likes
          Last Post jaybedreamin  
          Started by DJ888, 04-16-2024, 06:09 PM
          6 responses
          19 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Jon17, Today, 04:33 PM
          0 responses
          6 views
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          16 views
          0 likes
          Last Post Javierw.ok  
          Working...
          X