{"id":1107,"date":"2015-01-27T14:05:26","date_gmt":"2015-01-27T19:05:26","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1107"},"modified":"2015-02-12T14:18:50","modified_gmt":"2015-02-12T19:18:50","slug":"detecting-n-th-occurrence-of-a-condition-using-modulus-operator","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/01\/27\/detecting-n-th-occurrence-of-a-condition-using-modulus-operator\/","title":{"rendered":"Detecting N-th occurrence of a condition using modulus operator"},"content":{"rendered":"

Modulus (%) is an operator that returns the reminder from integer division. It is very helpful to create counters that wrap-around at user-specified N.<\/p>

In order to define a condition, which returns True every Nth bar, the easiest way is just to use % (modulus) operator. If we apply modulus to consecutive numbers such as BarIndex() – then calculating the reminder from integer division of barindex by N will return 0 every Nth bar (on bars that are divisible by N). We can use the following exploration to demonstrate that:<\/p>n <\/span>= <\/span>7<\/span>;
<\/span>bi <\/span>= <\/span>BarIndex<\/span>();
<\/span>condition <\/span>= <\/span>bi <\/span>% <\/span>n <\/span>== <\/span>0<\/span>;

<\/span>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>bi<\/span>, <\/span>"BarIndex"<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>bi <\/span>% <\/span>n <\/span>, <\/span>"Div by 7 remainder "<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>IIF<\/span>( <\/span>condition<\/span>, <\/span>'T'<\/span>, <\/span>'F' <\/span>), <\/span>"Condition"<\/span>,
           <\/span>formatChar<\/span>, <\/span>colorDefault<\/span>, <\/span>IIf<\/span>( <\/span>condition<\/span>, <\/span>colorYellow<\/span>, <\/span>colorDefault <\/span>) )<\/code>

\"Modulus<\/p>

Since the remainder from division by 7 will equal zero only for the multiples of 7, then we will have our condition True every 7th bar (as marked in the above exploration results with T letter on yellow background).<\/p>

Using the same technique we can also count occurrences of certain criteria and then apply the % operator. For example – let us say we want to test a rotational strategy, where we rotate our portfolio every 2nd Monday. To detect such condition in our code we need to first identify all Monday bars, then count them and use % operator to divide such count by 2.<\/p>

The following exploration shows the calculations of the condition we look for:<\/p>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>DayOfWeek<\/span>(), <\/span>"Day of week"<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>Mon<\/span>, <\/span>"Monday"<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>countMon<\/span>, <\/span>"Monday counter"<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>countMon <\/span>% <\/span>2 <\/span>== <\/span>0<\/span>, <\/span>"division by 2 remainder"<\/span>, <\/span>1 <\/span>);
<\/span>AddColumn<\/span>( <\/span>rotation<\/span>, <\/span>"condition"<\/span>, <\/span>1<\/span>, <\/span>colorDefault<\/span>, <\/span>IIf<\/span>( <\/span>rotation<\/span>, <\/span>colorYellow<\/span>, <\/span>colorDefault<\/span>) )<\/code>

\"Modulus<\/p>

Here is the formula showing how to code these technique for the rotational back-test:<\/p>SetBacktestMode<\/span>( <\/span>backtestRotational <\/span>);
<\/span>posScore <\/span>= <\/span>100 <\/span>- <\/span>RSI<\/span>(); <\/span>\/\/ sample scoring indicator
<\/span>SetPositionSize<\/span>( <\/span>10<\/span>, <\/span>spsShares <\/span>); <\/span>\/\/ sample position sizing

<\/span>Mon <\/span>= <\/span>DayOfWeek<\/span>() == <\/span>1<\/span>; <\/span>\/\/ identify Mondays
<\/span>countMon <\/span>= <\/span>Cum<\/span>( <\/span>Mon <\/span>); <\/span>\/\/ count Mondays

\/\/ rotate only on Monday, every 2nd one
<\/span>rotation <\/span>= <\/span>Mon <\/span>AND (<\/span>countMon <\/span>% <\/span>2 <\/span>== <\/span>0 <\/span>);
<\/span>PositionScore <\/span>= <\/span>IIf<\/span>( <\/span>rotation<\/span>, <\/span>posScore<\/span>, <\/span>scoreNoRotate <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

Modulus (%) is an operator that returns the reminder from integer division. It is very helpful to create counters that wrap-around at user-specified N.In order to define a condition, which returns True every Nth bar, the easiest way is just to use % (modulus) operator. If we apply modulus to consecutive numbers such as BarIndex() […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1107"}],"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=1107"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1111,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1107\/revisions\/1111"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}