{"id":747,"date":"2014-11-28T17:07:05","date_gmt":"2014-11-28T22:07:05","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=747"},"modified":"2014-12-04T08:18:28","modified_gmt":"2014-12-04T13:18:28","slug":"how-to-restrict-trading-to-certain-hours-of-the-day","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/11\/28\/how-to-restrict-trading-to-certain-hours-of-the-day\/","title":{"rendered":"How to restrict trading to certain hours of the day"},"content":{"rendered":"

In order to include time-based conditions in the back-testing code – we can use TimeNum() function to check the time-stamp of given bar and use it as input for any time-based conditions.
http:\/\/www.amibroker.com\/f?timenum<\/a><\/p>

In order to code a strategy that triggers trades only in certain hours of the day, 9:30-11:00 in this example, we can use the following approach (code uses simple MACD crossovers to generate signals):<\/p>tn <\/span>= <\/span>TimeNum<\/span>();
<\/span>startTime <\/span>= <\/span>93000<\/span>; <\/span>\/\/ start in HHMMSS format
<\/span>endTime <\/span>= <\/span>110000<\/span>;  <\/span>\/\/ end in HHMMSS format
<\/span>timeOK <\/span>= <\/span>tn <\/span>>= <\/span>startTime <\/span>AND <\/span>tn <\/span><= <\/span>endTime<\/span>;

<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() ) AND <\/span>timeOK<\/span>;
<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() ) AND <\/span>timeOK<\/span>; <\/code>

It is also possible to force an exit signal after 11:00 to avoid overnight positions:<\/p>tn <\/span>= <\/span>TimeNum<\/span>();
<\/span>startTime <\/span>= <\/span>93000<\/span>; <\/span>\/\/ start in HHMMSS format
<\/span>endTime <\/span>= <\/span>110000<\/span>;  <\/span>\/\/ end in HHMMSS format
<\/span>timeOK <\/span>= <\/span>tn <\/span>>= <\/span>startTime <\/span>AND <\/span>tn <\/span><= <\/span>endTime<\/span>;

<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() ) AND <\/span>timeOK<\/span>;
<\/span>Sell <\/span>= (<\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() ) AND <\/span>timeOK<\/span>) OR <\/span>Cross<\/span>( <\/span>tn<\/span>, <\/span>endTime <\/span>); <\/code>","protected":false},"excerpt":{"rendered":"

In order to include time-based conditions in the back-testing code – we can use TimeNum() function to check the time-stamp of given bar and use it as input for any time-based conditions.http:\/\/www.amibroker.com\/f?timenumIn order to code a strategy that triggers trades only in certain hours of the day, 9:30-11:00 in this example, we can use the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/747"}],"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=747"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/747\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}