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

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.

    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 usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      22 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      20 views
      0 likes
      Last Post Pattontje  
      Started by flybuzz, 04-21-2024, 04:07 PM
      17 responses
      230 views
      0 likes
      Last Post TradingLoss  
      Working...
      X