{"id":117,"date":"2007-05-09T11:55:10","date_gmt":"2007-05-09T11:55:10","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/05\/09\/time-calculations\/"},"modified":"2007-07-07T19:41:59","modified_gmt":"2007-07-07T19:41:59","slug":"time-calculations","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/05\/09\/time-calculations\/","title":{"rendered":"Time Calculations"},"content":{"rendered":"
Time calculations are needed when placing GAT (Good At Time) and GTD (Good Till Date) orders. The simplest way is to convert the TimeNum to a SecondNum, perform your calculations in Seconds, and then convert the SecondNum back to a TimeNum. The first two functions below will do that for you.<\/p>\n
The next function applies a reference expressed in seconds to your Reference time. This reference would typically be used to Calculate GAT times. You may need this function when you want to apply an offset to a Time Reference and have orders activated automatically during the day without having to be online.<\/p>\n
<\/span>function <\/span>TimeNumToSecondNum<\/span>( <\/span>TimeNumber <\/span>)\r{\r<\/span>Seconds <\/span>= <\/span>int<\/span>(<\/span>TimeNumber<\/span>%<\/span>100<\/span>);\r<\/span>Minutes <\/span>= <\/span>int<\/span>(<\/span>TimeNumber<\/span>\/<\/span>100<\/span>%<\/span>100<\/span>);\r<\/span>Hours <\/span>= <\/span>int<\/span>(<\/span>TimeNumber<\/span>\/<\/span>10000<\/span>%<\/span>100<\/span>);\r<\/span>NumberSecs <\/span>= <\/span>int<\/span>(<\/span>3600<\/span>*<\/span>Hours<\/span>+<\/span>60<\/span>*<\/span>Minutes<\/span>+<\/span>Seconds<\/span>);\rreturn <\/span>NumberSecs<\/span>;<\/span><\/pre>\n<\/span>function <\/span>SecondNumToTimeNum<\/span>( <\/span>SecondNum <\/span>)\r{\r<\/span>Hours <\/span>= <\/span>int<\/span>(<\/span>SecondNum <\/span>\/<\/span>3600<\/span>);\r<\/span>Minutes <\/span>= <\/span>int<\/span>((<\/span>SecondNum <\/span>-<\/span>Hours<\/span>*<\/span>3600<\/span>)\/<\/span>60<\/span>);\r<\/span>Seconds <\/span>= <\/span>int<\/span>((<\/span>SecondNum <\/span>-<\/span>Hours<\/span>*<\/span>3600<\/span>)-<\/span>Minutes<\/span>*<\/span>60<\/span>);\r<\/span>TimeNumber <\/span>= <\/span>Hours<\/span>*<\/span>10000 <\/span>+ <\/span>Minutes<\/span>*<\/span>100 <\/span>+ <\/span>Seconds<\/span>;\rreturn <\/span>TimeNumber<\/span>;\r}<\/span><\/pre>\n<\/span>function <\/span>ApplyOffsetToTimeNum<\/span>( <\/span>RefTimeNumber<\/span>, <\/span>Offset <\/span>)\r{\r<\/span>OffsetSecondNum <\/span>= <\/span>TimeNumToSecondNum<\/span>( <\/span>RefTimeNumber <\/span>) + <\/span>Offset<\/span>;\r<\/span>OffsetTimeNumber <\/span>= <\/span>SecondNumToTimeNum<\/span>( <\/span>OffsetSecondNum <\/span>);\rreturn <\/span>OffsetTimeNumber<\/span>;\r}<\/span><\/pre>\nOffSet <\/span>= <\/span>Param<\/span>(<\/span>"Offset in Seconds"<\/span>,<\/span>0<\/span>,-<\/span>100<\/span>,<\/span>100<\/span>,<\/span>1<\/span>);\r<\/span>RefTimeNum <\/span>= <\/span>ParamTime<\/span>(<\/span>"ReferenceTime"<\/span>,<\/span>Now<\/span>(<\/span>2<\/span>));\r<\/span>CalcTimeNum <\/span>= <\/span>ApplyOffsetToTimeNum<\/span>( <\/span>RefTimeNum<\/span>, <\/span>Offset <\/span>);\r<\/span>Title <\/span>=<\/span>"\\n"<\/span>+\r<\/span>" Reference Time: "<\/span>+<\/span>StrRight<\/span>(<\/span>NumToStr<\/span>(<\/span>DateTimeConvert<\/span>( <\/span>2<\/span>, <\/span>DateNum<\/span>(), <\/span>RefTimeNum<\/span>),<\/span>formatDateTime<\/span>),<\/span>11<\/span>)+<\/span>"\\n"<\/span>+\r<\/span>"Calculated Time: "<\/span>+<\/span>StrRight<\/span>(<\/span>NumToStr<\/span>(<\/span>DateTimeConvert<\/span>( <\/span>2<\/span>, <\/span>DateNum<\/span>(), <\/span>CalcTimeNum <\/span>),<\/span>formatDateTime<\/span>),<\/span>11<\/span>)+<\/span>"\\n"<\/span>;<\/span><\/pre>\nEdited by Al Venosa.<\/p>\n","protected":false},"excerpt":{"rendered":"
Time calculations are needed when placing GAT (Good At Time) and GTD (Good Till Date) orders. The simplest way is to convert the TimeNum to a SecondNum, perform your calculations in Seconds, and then convert the SecondNum back to a TimeNum. The first two functions below will do that for you. The next function applies […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/117"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}