{"id":1066,"date":"2015-02-10T17:03:56","date_gmt":"2015-02-10T22:03:56","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1066"},"modified":"2015-02-11T17:04:46","modified_gmt":"2015-02-11T22:04:46","slug":"checking-relationship-between-multiple-moving-averages","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/02\/10\/checking-relationship-between-multiple-moving-averages\/","title":{"rendered":"Checking relationship between multiple moving averages"},"content":{"rendered":"

When we compare the positions of several lines against one another, we need to remember about using correct operators, so our AFL statement return correct results. Let us consider a set of moving averages using 10, 20, 30 and 40 periods, drawn with the following code:<\/p>MA10 <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>10 <\/span>);
<\/span>MA20 <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>20 <\/span>);
<\/span>MA30 <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>30 <\/span>);
<\/span>MA40 <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>40 <\/span>);

<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>);
<\/span>Plot<\/span>( <\/span>MA10<\/span>, <\/span>"MA10"<\/span>, <\/span>colorRed <\/span>);
<\/span>Plot<\/span>( <\/span>MA20<\/span>, <\/span>"MA10"<\/span>, <\/span>colorBlue <\/span>);
<\/span>Plot<\/span>( <\/span>MA30<\/span>, <\/span>"MA10"<\/span>, <\/span>colorGreen <\/span>);
<\/span>Plot<\/span>( <\/span>MA40<\/span>, <\/span>"MA10"<\/span>, <\/span>colorgrey40 <\/span>)<\/code>

\"Multiple<\/p>

When we want to specify a condition where MA10 is highest of all of the lines, above MA20, which is above MA30 and with MA40 on the very bottom – we can NOT<\/strong> simply write:<\/p>condition <\/span>= <\/span>MA10 <\/span>> <\/span>MA20 <\/span>> <\/span>MA30 <\/span>> <\/span>MA40<\/span>; <\/span>\/\/ WRONG - NOT what we really want, but no syntax erro<\/code>

It may seem strange that such statement is accepted without an error but it is actually syntactically correct. This is because of the fact that True<\/strong> and False<\/strong> are represented by numbers 1 and 0 respectively, so all comparisons actually have numerical value that allows such statement to be evaluated and yield numeric result. The above statement is evaluated from left to right and would be an equivalent of:<\/p>condition <\/span>= ( ( <\/span>MA10 <\/span>> <\/span>MA20 <\/span>) > <\/span>MA30 <\/span>) > <\/span>MA40<\/span>; <\/span>\/\/ again WRON<\/code>

Using > operator will return an array of True<\/strong> or False<\/strong> values (1 or 0). Therefore – the result of MA10 > MA20 comparison (which is True<\/strong>, that is equal to 1) would be then compared to MA30, resulting in checking 1 > MA30, then if such condition returns False<\/strong> (i.e. 0 ), we would end up with 0 > MA40 comparison that would return False<\/strong> (0) as the final output. This is of course not what we want to get.<\/p>

That is why we should use AND operator instead, because we want to check several conditions being met at the same time, that is:

MA10 is above MA20
AND \/\/ must use AND\/OR to combine multiple conditions
MA20 is above MA30
AND \/\/ must use AND\/OR to combine multiple conditions
MA30 is above MA40
<\/code><\/p>

Therefore, we should write the AFL statement the following way:<\/p>condition <\/span>= <\/span>MA10 <\/span>> <\/span>MA20 <\/span>AND <\/span>MA20 <\/span>> <\/span>MA30 <\/span>AND <\/span>MA30 <\/span>> <\/span>MA40<\/span>; <\/span>\/\/ correct way of checking multiple condition<\/code>

So as a general guideline – if you have multiple boolean (yes\/no) conditions that you want to combine into single rule, you need to use AND operator between conditions if you want True result when all conditions are met at the same time.<\/p>

In a similar way, if you want a True result when any one (or more) of multiple conditions is met, then you need to use OR operator.<\/p>condition <\/span>= <\/span>MA10 <\/span>> <\/span>MA20 <\/span>OR <\/span>MA20 <\/span>> <\/span>MA30<\/span>; <\/span>\/\/ this will give True result if any one condition (or both) is me<\/code>","protected":false},"excerpt":{"rendered":"

When we compare the positions of several lines against one another, we need to remember about using correct operators, so our AFL statement return correct results. Let us consider a set of moving averages using 10, 20, 30 and 40 periods, drawn with the following code:MA10 = MA( Close, 10 );MA20 = MA( Close, 20 );MA30 = MA( Close, 30 );MA40 = MA( Close, 40 );Plot( Close, "Close", colorDefault, styleBar );Plot( MA10, "MA10", colorRed );Plot( MA20, "MA10", colorBlue );Plot( MA30, "MA10", colorGreen );Plot( MA40, "MA10", colorgrey40 )When we want to specify a condition where MA10 is […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1066"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"predecessor-version":[{"id":1069,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1066\/revisions\/1069"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}