• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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:
    if(BarsInProgress == 0)
                
                    if(    upTrend == false)
                        SignalShort.Set(High[0] + 10);
                            
                    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;
                        } //-----------------------------------------------------------   
                        Print("BIP 1 upTrend is: " + upTrend);     
                    }
                    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 johnnybegoode, Today, 07:02 PM
            0 responses
            4 views
            0 likes
            Last Post johnnybegoode  
            Started by YevhenShynkarenko, Today, 05:02 PM
            1 response
            11 views
            0 likes
            Last Post YevhenShynkarenko  
            Started by lahabatha, Today, 04:42 PM
            0 responses
            8 views
            0 likes
            Last Post lahabatha  
            Started by explorer101, Today, 03:39 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by faisamtrader, Today, 03:32 PM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X