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

Plotting issue

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

    Plotting issue

    Hey,

    I'm quite a Noob when it comes to progamming, but still I don't know why the following Indicator below is not plotted (compilation is not the problem).

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;

    }

    protected override void OnBarUpdate()
    {

    int Innenstab = 1;
    if (Close[0] < High[1]);
    {
    Innenstab = Innenstab - 1;
    }

    Plot0.Set(Innenstab);
    }

    The if statement works, but the plotting only if the "Close" and "High" bars are set to [0]. why is this? what am I doing wrong?

    Being gratefule for your help,

    yours,

    Andrew

    #2
    Originally posted by andrew-aut1 View Post
    Hey,

    I'm quite a Noob when it comes to progamming, but still I don't know why the following Indicator below is not plotted (compilation is not the problem).

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;

    }

    protected override void OnBarUpdate()
    {

    int Innenstab = 1;
    if (Close[0] < High[1]);
    {
    Innenstab = Innenstab - 1;
    }

    Plot0.Set(Innenstab);
    }

    The if statement works, but the plotting only if the "Close" and "High" bars are set to [0]. why is this? what am I doing wrong?

    Being gratefule for your help,

    yours,

    Andrew
    Hi Andrew

    I haven't tried to plot your indicator but two problems spring to mind:

    1) this is maybe the main reason it's not plotting: the first bar on the chart has nothing to calculate on, and when this happens, the ind will never plot - I've made this mistake many times!

    So please allow for this as the first code in OnBarUpdate as below.

    2) may I also suggest you allow for when your condition isn't true, i.e. when Close[0] >= High[1]

    so your code may end up looking something like:

    Code:
    [B]if (CurrentBar < 2)
           return;[/B]
    
     int Innenstab = 1; 
                if (Close[0] < High[1]);
                
                Innenstab = Innenstab - 1; // you don't need curly brackets for just one action
                
               [B] else
    
               [I] do something[/I][/B] // such as Innenstab = Innenstab
    
                Plot0.Set(Innenstab);
    Hope this helps.
    Last edited by arbuthnot; 12-13-2014, 09:07 AM.

    Comment


      #3
      Thx for your help, but it still does not work. Neither it accepts my else statement (always asks for a "while" statement) nor the the plot is correct without it => as it supposed to be. If the following is compiled:

      protected override void OnBarUpdate()
      {
      if (CurrentBar < 2)
      return;

      int Innenstab = 1;
      if (Close[1] < High[2]);
      {

      Innenstab = Innenstab - 1; // you don't need curly brackets for just one action
      }
      Plot0.Set(Innenstab);
      }

      a constant line of 0 is plotted which means that the statement "Innenstab - 1" is applied to every value on the time series regardless of the if condition. Why is that?

      Thx for your help again,

      yours,

      Andrew

      Comment


        #4
        Sorry - I initially posted something which was incorrect. My mind's been on other things today.

        I don't why, but this one has defeated me.

        I hope someone else can help you.
        Last edited by arbuthnot; 12-13-2014, 04:09 PM.

        Comment


          #5
          Hello,

          There is a ; at the end of the if condition. This will stop the condition check in which case every bar will be set to 0 for the Innenstab variable.

          Code:
          if (CurrentBar < 2)
                 return;
          
           int Innenstab = 1; 
          
           if (Close[0] < High[1])            
                      Innenstab = Innenstab - 1; // you don't need curly brackets for just one action
                      
           else
                     do something // such as Innenstab = Innenstab
          
          Plot0.Set(Innenstab);
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Innenstab = Inside Close.

            To detect an inside close you would check whether the close of the current bar lies within the range of the prior bar. To run this comparison, you would need to differentiate between two cases

            -> indicator runs in CalculateOnBarClose = true (meaning: the indicator calculates once after the bar has closed) - in this case you can use the code

            Code:
            if(Close[0] <= High[1] && Close[0] >= Low[1])
            {
                   isInsideCloseBar = true;  
                   Value.Set(1.0);
            }
            else
            {
                   isInsideCloseBar = false; 
                   Value.Set(0.0);
            }
            -> indicator runs in CalculateOnBarClose = false (meaning: the indicator recalculates with each incoming tick) - in this case you can use the code

            Code:
            if(FirstTickOfBar) // only check this once for the prior bar
            {
                   if(Close[1] <= High[2] && Close[1] >= Low[2])
                   {
                          isInsideCloseBar = true;  
                          Value.Set(1, 1.0);  // resets the value for the prior bar
                          Value.Set(0.0); // set the preliminary value for the current bar
                   }
                   else
                          isInsideCloseBar = false; 
            
            }
            Both cases are similar. In the first case NinjaTrader closes the bar, when the first tick of the next bar arrives (event driven). At that stage the indicator value for the closed bar is shown. In the second case you let the indicator calculate with the first tick of each bar to determine the final status of the prior bar (inside close bar or not).

            Putting this together:

            Code:
            protected override void OnBarUpdate()
            {      
                bool isInsideCloseBar = false;
                if (Historical || CalculateOnBarClose)
                {
                    if(CurrentBar < 1)
                    {
                           Value.Set(0.0);
                           return;
                    }
                    else if(Close[0] <= High[1] && Close[0] >= Low[1])
                    {
                           isInsideCloseBar = true;  
                           Value.Set(1.0);
                    }
                    else
                           Value.Set(0.0);
                }
                else if (FirstTickOfBar)
                {
                    if(CurrentBar < 2)
                    {
                          Value.Set(0.0);  
                          return;
                    }
                    else if(Close[1] <= High[2] && Close[1] >= Low[2])
                    {
                          isInsideCloseBar = true;  
                          Value.Set(1, 1.0);
                          Value.Set(0.0);
                    }
            }
            Not tested.
            Last edited by Harry; 12-13-2014, 05:02 PM.

            Comment


              #7
              Originally posted by NinjaTrader_Cal View Post
              Hello,

              There is a ; at the end of the if condition. This will stop the condition check in which case every bar will be set to 0 for the Innenstab variable.

              I believe this is the solution to fix your problem, Andrew.

              The code should be:

              Code:
              if (CurrentBar == 0) return;
              
              int Innenstab = 1; 
              
              if (Close[0] < High[1])  Innenstab = Innenstab - 1; 
              
              Plot0.Set(Innenstab);
              Last edited by ninZa; 12-16-2014, 06:15 PM.
              ninZa
              NinjaTrader Ecosystem Vendor - ninZa.co

              Comment


                #8
                Thx a lot guys!!! works perfectly now. I'm really gratefule. You guys are really making a contribution to this forrum

                Best wishes!

                Andrew

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by geddyisodin, Yesterday, 05:20 AM
                7 responses
                45 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by gbourque, Today, 06:39 AM
                2 responses
                5 views
                0 likes
                Last Post gbourque  
                Started by cre8able, Yesterday, 07:24 PM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by cocoescala, 10-12-2018, 11:02 PM
                6 responses
                939 views
                0 likes
                Last Post Jquiroz1975  
                Started by cmtjoancolmenero, Yesterday, 03:58 PM
                1 response
                17 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X