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

Using variable from a different BarsInProgress

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

    Using variable from a different BarsInProgress

    I am trying to use a variable that was created in BarsInProgress -1 within BarsInProgress-0 but It doesn't appear to be working as i get no chart print.

    My assumption is that in BIP1 the global variable will update and carry through to be used in BIP0 but apparently I'm wrong. What am I doing wrong here?

    Code:
            
    protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                    return;
                
                if (BarsInProgress == 1)
                    // Determine direction of the long term trend
                    if( CUMRSI(BarsArray[1],2,3)[0] > CUMRSI(BarsArray[1],2,3)[1] )
                    {
                        upTrend = true;
                    }
                    else
                    {
                        upTrend = false;
                    } //-----------------------------------------------------------        
                
                if(BarsInProgress == 0)
                
                    if(    upTrend == false)
                        SignalShort.Set(High[0] + 10);
                            
                    if(    upTrend == true)
                        SignalBuy.Set(Low[0] - 10);                 
            }
    Last edited by ShruggedAtlas; 04-08-2015, 08:45 AM.

    #2
    Hello,

    Thank you for the question.

    By global variable I think you are referring to the "Scope" of the variable.

    The scope would need to be either outside the checks for BarsInProgress or outside of OnBarUpdate as a class Scope.

    To use a variable between any BIP or inside your If Statements, you could do the following:

    Code:
    bool upTrend = false;
    			
    if (BarsInProgress == 1)
    {
         if( CUMRSI(BarsArray[1],2,3)[0] > CUMRSI(BarsArray[1],2,3)[1] )
         {
              upTrend = true;
         }
         else
         {
              upTrend = false;
         } 
    } 
    else if(BarsInProgress == 0)
    {
         if(upTrend == false)
         {
               SignalShort.Set(High[0] + 10);
         } 
         else
         {
                SignalBuy.Set(Low[0] - 10);     
         }
    }
    I did notice one item as well with your supplied code, this statement:

    Code:
    [B]if(BarsInProgress == 0)
                
                    if(    upTrend == false)
                        SignalShort.Set(High[0] + 10);[/B]
                            
                    if(    upTrend == true)
                        SignalBuy.Set(Low[0] - 10);
    The first if will only use the second if statement, the 3rd if statement is outside of the scope of the bars in progress check because you are not using curly braces.

    Please look at this simple example that explains what is happening, when you do not use braces you can do one action as the result of the if statement, if you use braces you can do multiple commands because the if statement was true.

    Code:
    if(something == true)
    doOneThing
    
    or
    
    if(something == true)
    {
    doMoreThanOneThing
    doSomethingElse
    }
    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ok this was definitely helpful to clarify however...I'm still not getting a signal.

      I took the bool variable out of Initialize method and put it within the context of OnBarUpdate method and put the curlies in. It's definitely easier to read.

      My assumption at this point is that i should get an overload of prints to the chart, virtually on every single bar however I'm still not getting any prints at all. this makes no sense to me.

      Any ideas?

      here is the code with Properties not included:

      Code:
          
      public class AnRSISwingAlert : Indicator
          {
              #region Variables
              // Wizard generated variables
                  private int extremeHigh = 70; // Default setting for ExtremeHigh
                  private int extremeLow = 30; // Default setting for ExtremeLow
                  private int chartOne = 1440;
                  private int chartTwo = 60;
              // User defined variables (add any user defined variables below)
      
              #endregion
      
      
              protected override void Initialize()
              {
                  Add(PeriodType.Minute, chartOne); //BarsArray[1] - chart 1
                  Add(PeriodType.Minute, chartTwo); //BarsArray[2] - chart 2
                  Add(PeriodType.Minute, 2); //BarsArray[3] - precise entry timeframe
                  Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Block, "SignalShort"));
                  Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Block, "SignalBuy"));
      
      
                  Overlay    = true;
              }
      
              protected override void OnBarUpdate()
              {
      
                  
                  if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                      return;
                  
                  bool upTrend = false;
                  
                  if (BarsInProgress == 1)
                  {
                      // Determine direction of the long term trend
                      if( CUMRSI(BarsArray[1],2,3)[0] > CUMRSI(BarsArray[1],2,3)[1] )
                      {
                          upTrend = true;
                      }
                      else
                      {
                          upTrend = false;
                      } //-----------------------------------------------------------        
                  }
                  else if(BarsInProgress == 0)
                  {
                  
                      if(    upTrend == false)
                      {
                          SignalShort.Set(High[0] + 10);
                      }
                      else
                      {
                          SignalBuy.Set(Low[0] - 10); 
                      }
                  }
              }
      }

      Comment


        #4
        Hello,

        Thank you for the reply.

        For this to debug what is happening I would recommend using Print Statements to check either the variable to see what it is being set to, or also inside your conditions that set the variable.

        Here is a simple example that may help determine what is happening, you will need to open the Tools -> Output window

        Code:
        protected override void OnBarUpdate()
         {
        
                    
              if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                        return;
                    
              bool upTrend = false;
                    
              if (BarsInProgress == 1)
              {
                        // Determine direction of the long term trend
                        if( CUMRSI(BarsArray[1],2,3)[0] > CUMRSI(BarsArray[1],2,3)[1] )
                        {
                            upTrend = true;
                        }
                        else
                        {
                            upTrend = false;
                        } //-----------------------------------------------------------   
                  [B]      Print("BIP 1 upTrend is: " + upTrend);     [/B]
                    }
                    else if(BarsInProgress == 0)
                    {
                    
                        if(    upTrend == false)
                        {
                            SignalShort.Set(High[0] + 10);
                        }
                        else
                        {
                            SignalBuy.Set(Low[0] - 10); 
                        }
                    }
                }
        After running this, you should see in the output window for every bar what upTrend is set to.

        If you see it never becomes true, you would know the CUMRSI check may not be working correctly so you may need to use prints in other places to determine where the error here is.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Didn't have enough data to cover the longer timeframe!

          i answered my own question - I didn't have enough data to cover BIP1 - i only had 20 bars....I needed enough to capture 1440 minute bars. changing to 365 bars solved the problem!!!

          thanks and sorry if you had to spend any time on this last bit of silliness!

          Comment


            #6
            Hello,

            Perfect glad to see this was resolved.

            If you have further questions please let me know.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,609 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