• 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

When to use for or while loops

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

    When to use for or while loops

    Hey there again guys,

    I am trying to write a loop and having some issues. What I am trying to do is evaluate if the ADX had a specific value within the last X number of bars. If it does then continue with the rest of the code. Below is a sample of what I am trying to do but not with much luck. What I am trying to do is if the first statement is true then the ADX is evaluate on period ADXPeriod for ADXLookback bars to see if it is >= ADXThreshold. If at any time it is then I want it to go onto the next piece with Bollinger. The other problem I run into is if I just want to evaluate the current bar [0] there is no way to enter 0 in a parameter in the indicator if I have OnBarClose = false. It auto saves as 1.



    if
    (DM(DM5Period).DiPlus[0] >= DM(DM5Period).DiMinus[0])
    for ( int i=0; i < ADXLookback; i++)
    {
    if (ADX(ADXPeriod)[i] >= ADXThreshold);
    }
    if (Bollinger(NumStdDev,PeriodB).Upper[0] >= KeltnerChannel(OffsetMultiplier, PeriodKel).Upper[0])

    #2
    Hello erik949,

    Thank you for your post.

    For the loop you could incorporate a bool into the loop to ensure the if (ADX(ADXPeriod)[i] >= ADXThreshold); has returned true then process the rest of the code. For example:
    Code:
            #region Variables		
            private bool greatThanThres = false;
    
    ...
    
    		protected override void OnBarUpdate()
    		{
    			if(DM(DM5Period.DiPlus[0]>=DM(DM5Period).DiMinus[0]))
    			{
    				greatThanThres = false;
    				for(int i = 0; i < ADXLookback; i++)
    				{
    					if(ADX(ADXPeriod)[i] >= ADXThreshold)
    					{
    						greatThanThres = true;
    					}
    				}
    				if(greatThanThres)
    				{
    					if (Bollinger(NumStdDev,PeriodB).Upper[0] >= KeltnerChannel(OffsetMultiplier, PeriodKel).Upper[0])
    					{
    						//Do Something
    					}
    				}
    			}	
    		}
    What variable is set to 1 by default when using CalculateOnBarClose = true? Can you provide a sample of your code to test on my end?

    I look forward to your response.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by erik949 View Post
      Hey there again guys,

      I am trying to write a loop and having some issues. What I am trying to do is evaluate if the ADX had a specific value within the last X number of bars. If it does then continue with the rest of the code. Below is a sample of what I am trying to do but not with much luck. What I am trying to do is if the first statement is true then the ADX is evaluate on period ADXPeriod for ADXLookback bars to see if it is >= ADXThreshold. If at any time it is then I want it to go onto the next piece with Bollinger. The other problem I run into is if I just want to evaluate the current bar [0] there is no way to enter 0 in a parameter in the indicator if I have OnBarClose = false. It auto saves as 1.



      if
      (DM(DM5Period).DiPlus[0] >= DM(DM5Period).DiMinus[0])
      for ( int i=0; i < ADXLookback; i++)
      {
      if (ADX(ADXPeriod)[i] >= ADXThreshold);
      }
      if (Bollinger(NumStdDev,PeriodB).Upper[0] >= KeltnerChannel(OffsetMultiplier, PeriodKel).Upper[0])
      A for loop is awfully inefficient for what you are trying to do.

      You have streaming data, so take advantage of it, and test each bar as it streams for exceeding the ADXTheshold, and then record/update the last bar on which it occurred. When you want to use it as a filter, you can then test if the occurrence bar is within the desired lookback period.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by vantojo, 03-14-2020, 10:36 AM
      8 responses
      63 views
      0 likes
      Last Post xTrader1  
      Started by nctpar, Yesterday, 02:13 PM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_ChrisSch  
      Started by webus, Today, 12:02 AM
      0 responses
      8 views
      0 likes
      Last Post webus
      by webus
       
      Started by jeronymite, Yesterday, 09:26 PM
      0 responses
      12 views
      0 likes
      Last Post jeronymite  
      Started by ttodua, 08-08-2020, 06:45 AM
      2 responses
      17 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X