Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Detect the 1st bar where 2 successive but not always consecutive conditions are met

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

    Detect the 1st bar where 2 successive but not always consecutive conditions are met

    Hello

    I’m trying to automate a strategy where it has to evaluate 2 conditions that are mutually exclusive, i.e. the second one is met only after the first one is no longer met (the 2nd is dependent on the 1st), and when this happens and the 2nd condition takes place after the 1st condition, then the strategy generates an Entry signal. So these 2 conditions take place in a successive order BUT these 2 conditions do not necessarily take place consecutively in time, i.e. sometimes once the 1st condition takes place then the 2nd condition takes place in the next bar, some others times the 2nd condition takes place 2 bars after, but the problem I found is where the 2nd condition take place after many bars from the 1st condition, for example, now I visually check a couple of cases where there are 50-70+ bars of separation between the 1st and 2nd condition, and in these cases I see the strategy works as should if you would trade it in a manual way, but now I’m looking for automate those cases where are a considerable separation between the 2 conditions.

    Code:
    ...
    protected override void OnBarUpdate()
    {
       ...
       if ((lineH[0] < lineJ[0] && lineH[0] < lineK[0])  &&  (lineH[1] > lineJ[1] && lineH[1] > lineK[1])  ||  (lineH[2] > lineJ[2] && lineH[2] > lineK[2]))
       {
          //generate an Entry signal
       }
       ...
    }
    ...
    However when we talk about 50+ bars the if-statement condition would be enormous and I want to think that there is a more efficient way where maybe once a first condition is met, then the code keeps evaluating bar per bar (for n bars?) checking until the successive 2nd condition is met even if this takes place 50-100+ later.

    I’ve seen some strategy examples to see if I find a similar case but the strategies I’ve seen normally evaluates their condition for 2-5 bars and done, but the problem in my case is the number of bars between the 2 conditions is not a fixed number and how to handle those cases where it takes some time to meet the 2nd condition.

    I need help about to know how we can handle this kind cases.

    Thank you in advance!


    #2
    Hello futurenow,

    You can use a bool variable that is set when the first condition is true. Then in a second condition require the bool to be true. The second condition would then reset the bool back to false.

    Below is a link to an example.
    Hi, To improve a strategy, I would like the condition to enter a trade to be triggered only after a second crossing happens. Meaning, for instance we have a sthocastics crossing, but the strategy would only trigger when a crossing between 2 emas happen. Would the looking back N bars work? Can it be done within the builder
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    44 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    65 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X