Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

First occurrence of a condition since another condition

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

    First occurrence of a condition since another condition

    I would like to develop a strategy that requires a way to code for the first instance a particular condition is met since the last occurrence that another (different) condition was met e.g.:

    If (condition 1) is true
    and the occurrence of (condition 1) is the first since the last occurrence of (condition 2)
    then do x

    This would also mean that if condition 1 is met on a subsequent occasion then it would be ignored unless there is a new occurrence of condition 2.

    I hope that is clear.

    I have looked at MRO, LRO and Countlf() but can't figure out if any of these are suitable.

    #2
    This psuedocode should work, I haven't tested it.


    Code:
    private int condition1_met = 0; 
    private int condition2_met = 0; 
    
    
    		protected override void OnStateChange()
    		{
    			if (State == State.Historical) 
    				return; 
    						
    			if (State == State.SetDefaults)
    			{  condition2_met = 0; condition2_met = 0; }
    
    		protected override void OnBarUpdate()
    		{
    if ( condition1==TRUE
    && condition2_met == 1
    && condition1_met == 0 )
    {
    
    logic
    
    condition1_met = 1;
    condition2_met = 0;
    }
    
    if ( condition2 == TRUE 
    && condition1_met == 1
    && condition2_met == 0 )
    {
    
    logic
    
    condition2_met = 1;
    condition1_met = 0;
    
    }

    Comment


      #3
      Hello DJ****burn,

      Thank you for your post and welcome to the NinjaTrader Support Forum!

      sledge provides an example that would essentially only allow for condition 1 to be true if condition two was true prior, and vice versa. This example was designed for NinjaTrader 8 but the same logic for the variables and the OnBarUpdate() would work in NinjaTrader 7.

      Another example would be the following:
      Code:
      if(cond2)
      {
      cond1 = true;
      cond2 = false;
      }
      if(cond1)
      {
      cond1 = false;
      cond2 = true;
      }
      Where cond1 and cond2 are booleans you have defined.

      Comment


        #4
        Thanks, Sledge and PatrickH.

        I'm a complete amateur and your replies are a great help!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        153 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        89 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        133 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        127 views
        1 like
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        107 views
        0 likes
        Last Post CarlTrading  
        Working...
        X