Separate trading rules by date

Greetings! I’m attempting to use different Buy/Sell statements depending on whether the date is before or after a specified date. The code below ignores the first set of rules and only processes the second set of rules (dateChk==2). I assume this has to do with the way Ami processes the Buy and Sell stmts. I’m wondering if there is another way that might work, or if I’m missing something in my code. Any thoughts are appreciated. Thank you!

dateChk = IIf(DateNum()<1220103,1,2);

//  2017-2022 rules

Buy = (Cross(ROC1x,7.91)  AND dateChk==1);;;//

Sell = (Cross(ROC1x,3.28)  AND dateChk==1);;



// post 2022 rules

Buy = (Cross(ROC1,-5.28) AND dateChk==2);

Sell = (Cross(ROC1,2.44) AND dateChk==2);

You are reassigning the Buy and Sell variables, so the second assignment will supersede the first. This would happen with any variable name you used, not just the built-in Buy and Sell signal arrays.

Try something like this instead:

Buy = IIF(dateChk == 1, (Cross(ROC1x,7.91), Cross(ROC1,-5.28));
Sell = IIF(dateChk == 1, Cross(ROC1x,3.28), (Cross(ROC1,2.44));

Alternatively, you could do it like this:

Buy = (Cross(ROC1x,7.91) AND dateChk==1) OR
      (Cross(ROC1,-5.28) AND dateChk==2);

Sell = (Cross(ROC1x,3.28) AND dateChk==1) OR
       (Cross(ROC1,2.44) AND dateChk==2);

2 Likes

Thank you Matt. That was most helpful and appreciated.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.