April 28, 2007
Session Timing
True System Automation means you program your trading system to start and stop at designated times while you go play golf, right?
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.
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.
function BlinkText( Text ) { ABAxisColor = colorWhite; if( Status("redrawaction") ) { BlinkState= StaticVarGet("BlinkState"); if( IsNull( BlinkState) ) StaticVarSet("BlinkState", False); if( BlinkState ) { Text = EncodeColor(colorBrightGreen)+Text+EncodeColor(ABAxisColor); StaticVarSet("BlinkState", False); } else { Text = EncodeColor(ABAxisColor)+Text; StaticVarSet("BlinkState", True); } } return Text; }
RequestTimedRefresh( 1 ); DisableSessionTiming = ParamToggle("Auto Session Timing","ENABLED|DISABLED",1); ParamDateNumber = ParamDate("Date", Now(1), 0); ParamStartTime = ParamTime("Start","09:30:00"); ParamEndTime = ParamTime("End","15:59:00"); RTTimeNumber = Now(4); RTDateNumber = Now(3); InSessionDate = RTDateNumber == ParamDateNumber; PreSessionTime = RTTimeNumber < ParamStartTime; PostSessionTime = RTTimeNumber > ParamEndTime; InSessionTime = NOT ( PreSessionTime OR PostSessionTime ); PrevInSession = StaticVarGet("InSession"); if( DisableSessionTiming ) InSession = 1; else InSession = InSessionDate AND InSessionTime; StartSessionTrigger = LastValue(InSession) > PrevInSession; EndSessionTrigger = LastValue(InSession) < PrevInSession; StaticVarSet("InSession", InSession); OutOfSessionColor = ParamColor("Out of Session",colorBlack); Plot( NOT InSession,"",colorBlack,styleArea|styleOwnScale|styleNoLabel,0,1); Title = "\n"+BlinkText( Now(0) )+"\n"+ "Session Status: "+ WriteIf( DisableSessionTiming, "Session Timing Disabled", WriteIf( NOT InSessionDate,"Out of Session date, ","")+ WriteIf(PreSessionTime AND InSessionDate,"Waiting for Start, ","")+ WriteIf(StartSessionTrigger, "Start Trigger, ","")+ WriteIf(InSessionTime, "In Progress, ","")+ WriteIf(EndSessionTrigger, "End Trigger, ","")+ WriteIf(PostSessionTime, "Completed",""));
Edited by Al Venosa
Filed by Herman at 9:28 pm under Real-Time AFL Programming
Comments Off on Session Timing