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

looking for alternative syntax to isrising-isfalling

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

    looking for alternative syntax to isrising-isfalling

    good day to everyone,


    searching through nt8's help guides i found this example below of how to create an indicator that will change colors depending on its current values.


    this code will compile and will work as intended to, however i have tried to change the
    (IsRising(Upper)) logic to if ((Upper[0]) > (Upper[1])) and other similar structures and then the code will indeed compile but nothing will be plotted and the sma will just disappear.

    i want to use a structure like ((Upper[0]) > (Upper[1])) because it allows for more flexibility as the comparison periods can be changed and even optimized. ¿does anyone know how could i create such a logic for nt8?

    thanks, regards.


    Code:
    protected override void OnStateChange()
    {
      if(State == State.SetDefaults)
      {
        Name = "Example Indicator";   
            // Add two plots
          AddPlot(Brushes.Blue, "Upper");
          AddPlot(Brushes.Orange, "Lower");
        }
    }
     
    protected override void OnBarUpdate()
    {
        // Sets values to our two plots
        Upper[0] = SMA(High, 20)[0];
        Lower[0] = SMA(Low, 20)[0];
     
        // Color the Upper plot based on plot value conditions
        if (IsRising(Upper))
            PlotBrushes[0][0] = Brushes.Blue;
        else if (IsFalling(Upper))
            PlotBrushes[0][0] = Brushes.Red;
        else
            PlotBrushes[0][0] = Brushes.Yellow;
     
        // Color the Lower plot based on plot value conditions
        if (Rising(Lower))
            PlotBrushes[1][0] = Brushes.Blue;
        else if (IsFalling(Lower))
            PlotBrushes[1][0] = Brushes.Red;
        else
            PlotBrushes[1][0] = Brushes.Yellow;
    }
     
    public Series<double> Upper
    {
      get { return Values[0]; }
    }
      
    public Series<double> Lower
    {
      get { return Values[1]; }
    }

    #2
    Hello rtwave,

    Thanks for your post.

    The only thing I see incorrect in your code (and it would prevent compiling) is this statement: if (Rising(Lower)) The Rising should IsRising.

    I recreated your code and it plots just fine. I did have to set the property (In State.SetDefaults) of IsOverlay to true, to have the plots show in the main panel as otherwise, it would have created an added panel with only the plots.
    Reference: https://ninjatrader.com/support/help...?isoverlay.htm

    Please see attached screenshot.
    Attached Files
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul,


      thanks.


      the code i posted previously was copied and pasted straight from nt8's help guides so it is surprising that it does include a significant error as you point out.


      anyway, it is only when i replace isrising - isfalling with ((Upper[0]) > (Upper[1])) that the code will compile but will not plot. i would like to use a version with an structure more or less as follows, and it is this following syntax that does not work properly:


      Code:
      
      [COLOR=#0000ff]protected[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]override[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#ffffff] [/COLOR]OnStateChange()
      {
      [COLOR=#ffffff]   [/COLOR][COLOR=#0000ff]if[/COLOR](State[COLOR=#ffffff] [/COLOR]==[COLOR=#ffffff] [/COLOR]State.SetDefaults)
      [COLOR=#ffffff]   [/COLOR]{
      [COLOR=#ffffff]      [/COLOR]Name[COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR][COLOR=#800000]"Example Indicator"[/COLOR];[COLOR=#ffffff]   [/COLOR]
      [COLOR=#ffffff]          [/COLOR][COLOR=#008000]// Add two plots[/COLOR]
      [COLOR=#ffffff]        [/COLOR]AddPlot(Brushes.Blue,[COLOR=#ffffff] [/COLOR][COLOR=#800000]"Upper"[/COLOR]);
      [COLOR=#ffffff]        [/COLOR]AddPlot(Brushes.Orange,[COLOR=#ffffff] [/COLOR][COLOR=#800000]"Lower"[/COLOR]);
      [COLOR=#ffffff]     [/COLOR]}
      }
       
      [COLOR=#0000ff]protected[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]override[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#ffffff] [/COLOR]OnBarUpdate()
      {
      [COLOR=#ffffff]     [/COLOR][COLOR=#008000]// Sets values to our two plots[/COLOR]
      [COLOR=#ffffff]     [/COLOR]Upper[[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]SMA(High,[COLOR=#ffffff] [/COLOR][COLOR=#ff6600]20[/COLOR])[[COLOR=#ff6600]0[/COLOR]];
      [COLOR=#ffffff]     [/COLOR]Lower[[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]SMA(Low,[COLOR=#ffffff] [/COLOR][COLOR=#ff6600]20[/COLOR])[[COLOR=#ff6600]0[/COLOR]];
       
      [COLOR=#ffffff]     [/COLOR][COLOR=#008000]// Color the Upper plot based on plot value conditions[/COLOR]
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#ffffff] [/COLOR]((Upper[0]) > (Upper[1]))
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]0[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Blue;
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]else[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#ffffff] [/COLOR]((Upper[0]) < (Upper[1]))
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]0[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Red;
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]else[/COLOR]
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]0[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Yellow;
       
      [COLOR=#ffffff]     [/COLOR][COLOR=#008000]// Color the Lower plot based on plot value conditions[/COLOR]
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#ffffff] [/COLOR]((Lower[0]) > (Lower[1]))
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]1[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Blue;
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]else[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#ffffff] [/COLOR][COLOR=#ffffff][/COLOR]((Lower[0]) < (Lower[1]))
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]1[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Red;
      [COLOR=#ffffff]     [/COLOR][COLOR=#0000ff]else[/COLOR]
      [COLOR=#ffffff]          [/COLOR]PlotBrushes[[COLOR=#ff6600]1[/COLOR]][[COLOR=#ff6600]0[/COLOR]][COLOR=#ffffff] [/COLOR]=[COLOR=#ffffff] [/COLOR]Brushes.Yellow;
      }
       
      [COLOR=#0000ff]public[/COLOR][COLOR=#ffffff] [/COLOR]Series<[COLOR=#0000ff]double[/COLOR]>[COLOR=#ffffff] [/COLOR]Upper
      {
      [COLOR=#ffffff]   [/COLOR]get[COLOR=#ffffff] [/COLOR]{[COLOR=#ffffff] [/COLOR][COLOR=#0000ff]return[/COLOR][COLOR=#ffffff] [/COLOR]Values[[COLOR=#ff6600]0[/COLOR]];[COLOR=#ffffff] [/COLOR]}
      }
      [COLOR=#ffffff]   [/COLOR]
      [COLOR=#0000ff]public[/COLOR][COLOR=#ffffff] [/COLOR]Series<[COLOR=#0000ff]double[/COLOR]>[COLOR=#ffffff] [/COLOR]Lower
      {
      [COLOR=#ffffff]   [/COLOR]get[COLOR=#ffffff] [/COLOR]{[COLOR=#ffffff] [/COLOR][COLOR=#0000ff]return[/COLOR][COLOR=#ffffff] [/COLOR]Values[[COLOR=#ff6600]1[/COLOR]];[COLOR=#ffffff] [/COLOR]}
      }

      Comment


        #4
        Hello rtwave,

        Thanks for your reply and clarification.

        With NinjaTrader8 we have implemented the helpguide as an on-line document which means we can facilitate documentation changes much quicker. I have advised our documentation group and they will correct and update the helpguide, thanks for advising.

        Your condition code (example:(Upper[0]) > (Upper[1])) is checking the current bar and then one bar back. When the indicator loads it will generate a run-time error which can be seen in the "log" tab of the control center. It will reference an index error.

        To prevent the code from accessing a bar that is not yet loaded, please use a statement such as:
        if (CurrentBar < 1) return; Make sure this statement is placed before the first use of [1] so that the code below it will not execute until you have two bars processed/loaded. Reference: https://ninjatrader.com/support/help...currentbar.htm Also see attached example.
        Attached Files
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Paul,


          thanks again.

          you have correctly identified the issue and the fix you suggest works perfectly. now the indicator plots as intended.

          thanks.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by rdtdale, Today, 01:02 PM
          0 responses
          1 view
          0 likes
          Last Post rdtdale
          by rdtdale
           
          Started by alifarahani, Today, 09:40 AM
          3 responses
          15 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by RookieTrader, Today, 09:37 AM
          4 responses
          18 views
          0 likes
          Last Post RookieTrader  
          Started by PaulMohn, Today, 12:36 PM
          0 responses
          5 views
          0 likes
          Last Post PaulMohn  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          40 views
          0 likes
          Last Post love2code2trade  
          Working...
          X