{"id":1316,"date":"2015-11-29T00:19:16","date_gmt":"2015-11-28T23:19:16","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1316"},"modified":"2016-01-29T00:32:10","modified_gmt":"2016-01-28T23:32:10","slug":"how-to-execute-part-of-the-formula-only-when-new-bar-is-added","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/11\/29\/how-to-execute-part-of-the-formula-only-when-new-bar-is-added\/","title":{"rendered":"How to execute part of the formula only when new bar is added"},"content":{"rendered":"
In realtime conditions we may be interested in executing some parts of our formula only once per bar, when a new bar is created (e.g. for auto-trading purposes or just for notification). To do that – we would need to identify the very moment when new bar appears.<\/p>
This can be done using static variables to record the timestamp of the most recent bar, then comparing current reading with the recorded value. Once the difference is detected – we can conditionally run our code and update the recorded time info.<\/p>
Such an approach will work if we use timestamps that don’t change with each tick, so preferred option is to use Start Time of Interval<\/strong> for timestamp display (for daily and higher intervals we should unmark “override” box):<\/p> <\/p> Then we can use the following code (this sample formula will just play a ding.wav system sound when the new bar is detected):<\/p> Newer AmiBroker versions (>5.60) can use this for reading last bar timestamp (this is faster than using DateTime() function). In realtime conditions we may be interested in executing some parts of our formula only once per bar, when a new bar is created (e.g. for auto-trading purposes or just for notification). To do that – we would need to identify the very moment when new bar appears.This can be done using static variables to […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[55,94,95],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1316"}],"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=1316"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1316\/revisions"}],"predecessor-version":[{"id":1319,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1316\/revisions\/1319"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/span>\/\/ read last bar date\/time
<\/span>lastBartime <\/span>= <\/span>LastValue<\/span>( <\/span>DateTime<\/span>() );
<\/span>\/\/ we use per-symbol variable
\/\/ you may consider to add GetChartID() key if you want
\/\/ to use the formula in multiple charts shown at the same time
<\/span>varName <\/span>= <\/span>Name<\/span>() + <\/span>"_lastdt"<\/span>;
<\/span>\/\/ read recorded date\/time from last execution
<\/span>recordedTimestamp <\/span>= <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>varName <\/span>) );
<\/span>\/\/ code runs conditionally only when new bar is detected
<\/span>if( <\/span>lastBarTime <\/span>!= <\/span>recordedTimestamp <\/span>)
{
<\/span>\/\/ record new bar datetime
<\/span>StaticVarSet<\/span>( <\/span>varName<\/span>, <\/span>lastBartime <\/span>);
<\/span>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\/\/ main code here
<\/span>PlaySound<\/span>( <\/span>"c:\\\\windows\\\\media\\\\ding.wav" <\/span>);
<\/span>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
<\/span>}
<\/span>\/\/ sample indicator code
<\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorDefault<\/span>, <\/span>styleBar <\/span>)<\/code>lastBartime <\/span>= <\/span>Status<\/span>(<\/span>"lastbarend"<\/span>)<\/code>","protected":false},"excerpt":{"rendered":"