Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator below value for n bars

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

    Indicator below value for n bars

    Hi,

    Can anyone help me with this. I'm trying to code a strategy that only takes signals if indicatorline1 was below indicatorline2, for the last n bars.

    Instead of writing n lines of code for comaring, I thought I should use a loop.

    Maybe someone has already done something like this and can give me some help.

    Thanks,

    Marco

    #2
    Hello Marco,
    You can use the MRO function to do it. For example the below code checks for is low greater than SMA(14) in the last 10 bars.
    Code:
    int i = MRO(delegate {return Low[0] > SMA(14)[0];}, 1, 9);
    Please refer here to know more


    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Tahnk you Joydeep!

      I read that the MRO function doesn't work in multi time frame strategies. However I would like to enter in a lower time frame if supplementary conditions are met.

      Is there a workaround for this?

      Marco

      Comment


        #4
        Hello Marco,
        In case of multi-series strategy loop is the way to go.

        C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.


        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Would this enter a trade if the EMA(6) was below the SMA(14) for the last 20 handles?

          Code:
          int Counter = 0;
                                          
                      
                      while (EMA(6)[Counter] < SMA(14)[Counter]) 
                      {
                          
                      Counter = Counter + 1;
                      if (Counter > 20)
                              {
                                  
                                  EnterLong(DefaultQuantity, "");
                                  Counter = 0;
                                  
                              }
                          
                      }

          Comment


            #6
            Hello Marco,
            Please try the below code to do it
            Code:
            int count = 0;
            while (count < 20)
            {
            	if (CurrentBar >= count && EMA(6)[count] < SMA(14)[count])
            	{
            		//do something
            		EnterLong();
            		break;	//exit the loop
            	}
            	
            	count++;
            }
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Joydeep,

              This doenst trigger any trades on my end.

              I'll try a little myself and come back to you if needed. Thanks

              Comment


                #8
                Just for the records:

                I finally use a simple counter.

                Code:
                OnBarUpdate()
                
                if (BarsInProgress == 1)
                {
                if (EMA(6)[0] < SMA(14)[0])
                	{
                		count++;
                	}
                
                if (EMA(6)[0] > SMA(14)[0])
                	{
                		count = 0;
                	}
                }
                In the primary Timeframe I simply look if count has reached 20.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                648 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X