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

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 cmtjoancolmenero, Yesterday, 03:58 PM
        5 responses
        28 views
        0 likes
        Last Post cmtjoancolmenero  
        Started by gbourque, Today, 06:39 AM
        2 responses
        14 views
        0 likes
        Last Post gbourque  
        Started by rexsole, Today, 08:39 AM
        0 responses
        4 views
        0 likes
        Last Post rexsole
        by rexsole
         
        Started by trilliantrader, Yesterday, 03:01 PM
        3 responses
        31 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by Brevo, Today, 01:45 AM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X