{"id":60,"date":"2007-04-28T21:28:43","date_gmt":"2007-04-28T21:28:43","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/04\/28\/at-session-timing\/"},"modified":"2007-10-19T12:58:09","modified_gmt":"2007-10-19T12:58:09","slug":"at-session-timing","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/04\/28\/at-session-timing\/","title":{"rendered":"Session Timing"},"content":{"rendered":"
True System Automation means you program your trading system to start and stop at designated times while you go play golf, right?<\/p>\n
This means that in addition to having a Master Enable\/Disable switch you need to automate the start and stop of your trading session. For example, you might want to start your session at 9:30 AM and have it automatically end at 4:00 PM.<\/p>\n
The code below uses the ParamDate() and ParamTime() to set the session start and stop dates\/times, and compares these parameters to the system Date and Time that are returned by the Now() function. It generates various session-states that are displayed in the Title, when you incorporate this code into your system you will use these states to perform specific actions, like Close all Positions, Cancel all Orders, etc. The BlinkText() is not really needed but was thrown in for fun.<\/p>\n
<\/span>function <\/span>BlinkText<\/span>( <\/span>Text <\/span>)\r{\r<\/span>ABAxisColor <\/span>= <\/span>colorWhite<\/span>;\rif( <\/span>Status<\/span>(<\/span>"redrawaction"<\/span>) )\r{\r<\/span>BlinkState<\/span>= <\/span>StaticVarGet<\/span>(<\/span>"BlinkState"<\/span>);\rif( <\/span>IsNull<\/span>( <\/span>BlinkState<\/span>) ) <\/span>StaticVarSet<\/span>(<\/span>"BlinkState"<\/span>, <\/span>False<\/span>);\rif( <\/span>BlinkState <\/span>)\r{\r<\/span>Text <\/span>= <\/span>EncodeColor<\/span>(<\/span>colorBrightGreen<\/span>)+<\/span>Text<\/span>+<\/span>EncodeColor<\/span>(<\/span>ABAxisColor<\/span>);\r<\/span>StaticVarSet<\/span>(<\/span>"BlinkState"<\/span>, <\/span>False<\/span>);\r}\relse\r{\r<\/span>Text <\/span>= <\/span>EncodeColor<\/span>(<\/span>ABAxisColor<\/span>)+<\/span>Text<\/span>;\r<\/span>StaticVarSet<\/span>(<\/span>"BlinkState"<\/span>, <\/span>True<\/span>);\r}\r}\rreturn <\/span>Text<\/span>;\r}<\/span><\/pre>\nRequestTimedRefresh<\/span>( <\/span>1 <\/span>);\r<\/span>DisableSessionTiming <\/span>= <\/span>ParamToggle<\/span>(<\/span>"Auto Session Timing"<\/span>,<\/span>"ENABLED|DISABLED"<\/span>,<\/span>1<\/span>);\r<\/span>ParamDateNumber <\/span>= <\/span>ParamDate<\/span>(<\/span>"Date"<\/span>, <\/span>Now<\/span>(<\/span>1<\/span>), <\/span>0<\/span>);\r<\/span>ParamStartTime <\/span>= <\/span>ParamTime<\/span>(<\/span>"Start"<\/span>,<\/span>"09:30:00"<\/span>);\r<\/span>ParamEndTime <\/span>= <\/span>ParamTime<\/span>(<\/span>"End"<\/span>,<\/span>"15:59:00"<\/span>);\r<\/span>RTTimeNumber <\/span>= <\/span>Now<\/span>(<\/span>4<\/span>);\r<\/span>RTDateNumber <\/span>= <\/span>Now<\/span>(<\/span>3<\/span>);\r<\/span>InSessionDate <\/span>= <\/span>RTDateNumber <\/span>== <\/span>ParamDateNumber<\/span>;\r<\/span>PreSessionTime <\/span>= <\/span>RTTimeNumber <\/span>&<\/span>lt<\/span>; <\/span>ParamStartTime<\/span>;\r<\/span>PostSessionTime <\/span>= <\/span>RTTimeNumber <\/span>&<\/span>gt<\/span>; <\/span>ParamEndTime<\/span>;\r<\/span>InSessionTime <\/span>= <\/span>NOT <\/span>( <\/span>PreSessionTime <\/span>OR <\/span>PostSessionTime <\/span>);\r<\/span>PrevInSession <\/span>= <\/span>StaticVarGet<\/span>(<\/span>"InSession"<\/span>);\rif( <\/span>DisableSessionTiming <\/span>) <\/span>InSession <\/span>= <\/span>1<\/span>;\relse <\/span>InSession <\/span>= <\/span>InSessionDate <\/span>AND <\/span>InSessionTime<\/span>;\r<\/span>StartSessionTrigger <\/span>= <\/span>LastValue<\/span>(<\/span>InSession<\/span>) &<\/span>gt<\/span>; <\/span>PrevInSession<\/span>;\r<\/span>EndSessionTrigger <\/span>= <\/span>LastValue<\/span>(<\/span>InSession<\/span>) &<\/span>lt<\/span>; <\/span>PrevInSession<\/span>;\r<\/span>StaticVarSet<\/span>(<\/span>"InSession"<\/span>, <\/span>InSession<\/span>);\r<\/span>OutOfSessionColor <\/span>= <\/span>ParamColor<\/span>(<\/span>"Out of Session"<\/span>,<\/span>colorBlack<\/span>);\r<\/span>Plot<\/span>( <\/span>NOT InSession<\/span>,<\/span>""<\/span>,<\/span>colorBlack<\/span>,<\/span>styleArea<\/span>|<\/span>styleOwnScale<\/span>|<\/span>styleNoLabel<\/span>,<\/span>0<\/span>,<\/span>1<\/span>);\r<\/span>Title <\/span>= <\/span>"\\n"<\/span>+<\/span>BlinkText<\/span>( <\/span>Now<\/span>(<\/span>0<\/span>) )+<\/span>"\\n"<\/span>+\r<\/span>"Session Status: "<\/span>+\r<\/span>WriteIf<\/span>( <\/span>DisableSessionTiming<\/span>, <\/span>"Session Timing Disabled"<\/span>,\r<\/span>WriteIf<\/span>( <\/span>NOT InSessionDate<\/span>,<\/span>"Out of Session date, "<\/span>,<\/span>""<\/span>)+\r<\/span>WriteIf<\/span>(<\/span>PreSessionTime <\/span>AND <\/span>InSessionDate<\/span>