Combine conditions with AND operator


Category:
0
0

Hello Pete:

Had a question on these following conditions.

When i place the condition by itself

low crosses below 37.96

It shows at 6:54 it met the condition.

But when I combined it with the following condition as one, some how it doesn’t works, can you let me know what might be causing it.

2nd condition
close crosses above high from 1 bar ago

low crosses below 37.96 and close crosses above high from 1 bar ago

Thanks.

Attachments:
Marked as spam
Posted by (Questions: 2, Answers: 2)
Asked on July 15, 2022 9:52 am
30 views
0
Private answer

I have a feeling that you don't realize the question you should be asking. Which is "how to offset conditions across multiple bars". Your condition statements are all operating on the same exact candle, whereas your example screenshot shows you are trying to capture a two bar pattern.

I'll try my best to explain this so you can learn to apply this as you learn how to write scripts. I will begin by giving you the plain English interpretation of your conditions:

First, here is your condition:

low crosses below 37.96 and close crosses above high from 1 bar ago

The interpretation is:

TRUE IF the low of the current bar crosses below the fixed value of $37.96 AND the close of the current bar crosses above the high of the previous bar

There you go. Now if you read that carefully you will see that you have created something that does not match the pattern on your screenshot. The only way that condition can evaluate to true is if both conditions occur on the exact same bar. But your screenshot example shows these two conditions are spread across two different bars. So your conditions must be adjusted to include two bars in the pattern.

When building conditions it is much easier if you start with the final condition and work your way backwards on the chart. Watch this:

close crosses above high from 1 bar ago

Got it? That's the second bar of your two bar pattern.

Now lets use that as the anchor point and build the condition for the first bar in your two bar pattern:

low from 1 bar ago crosses below 37.96

The first condition is checking the last bar of your pattern and the second condition is checking the first bar in your pattern. Let's put them together:

close crosses above high from 1 bar ago and low from 1 bar ago crosses below 37.96

Now we have a statement which combines the two conditions and spreads them across the 2 bar pattern you have shown in your example. The differences between my solution and yours are subtle. So study this carefully to make sure you understand. But what happens when the price level changes? And what happens when you have 2-3 bars between the low crossing below the level and the high crossing above the previous high? Your entire approach here is very rigid, and that means you will need to constantly update your code to adjust to changing conditions.

You are using a fixed value for the low crossing value. This condition will only work for those two bars on your chart. This can never be used again without modifications. Making your entire approach nearly useless as a trading tool. And what happens if the cross above the previous high occurs 2 or 3 bars later? Your code would again require modification to suit the ever changing condition on the chart.

If you would like to learn more you will find this topic is applied to a very large percentage of solutions in this forum. The following post deals specifically with the issue you were unable to address:

https://www.hahn-tech.com/ans/how-to-require-a-sequence-of-conditions/

So before you try to learn how to write scripts, you really should be thinking about how to use built-in indicators on the chart which are able to automatically adapt to the changing conditions on the chart. You want to focus on solutions which do not require you to rewrite your conditions every time the chart changes. If you study the solution in the post I linked above you will find that it does exactly these things.

Marked as spam
Posted by (Questions: 37, Answers: 4073)
Answered on July 15, 2022 4:19 pm
0
Hi Pete I had to read few time to understand the solution code as to what it was doing. Thanks for all your help :)
( at July 19, 2022 2:11 pm)