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 RDTrader16, Today, 10:19 PM
        0 responses
        3 views
        0 likes
        Last Post RDTrader16  
        Started by gemify, 03-08-2023, 08:02 AM
        9 responses
        148 views
        0 likes
        Last Post culpepper  
        Started by elirion, Today, 10:03 PM
        0 responses
        1 view
        0 likes
        Last Post elirion
        by elirion
         
        Started by RaddiFX, Today, 09:55 PM
        0 responses
        6 views
        0 likes
        Last Post RaddiFX
        by RaddiFX
         
        Started by Trader146, 03-29-2024, 01:22 PM
        4 responses
        25 views
        0 likes
        Last Post Trader146  
        Working...
        X