{"id":1004,"date":"2007-08-18T14:07:23","date_gmt":"2007-08-18T14:07:23","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/08\/18\/real-time-gap-trading-demo-session-timing\/"},"modified":"2007-10-19T13:00:10","modified_gmt":"2007-10-19T13:00:10","slug":"real-time-gap-trading-demo-session-timing","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/08\/18\/real-time-gap-trading-demo-session-timing\/","title":{"rendered":"Gap-Trading demo, Session Timing Example"},"content":{"rendered":"
This post shows how to add intraday session timing to the Real-Time Gap-Trading (RTGT) system developed in the previous post. However, before tackling session timing, there are a few things you should be aware about:<\/p>\n
In real-time trading there are many functions that are timed with reference to your system’s clock. It is therefore imperative that you always synchronize your computer clock with an Internet timeserver before each trading session. <\/p>\n
Data Timestamps can be aligned to either the start or the end of the base period. The code developed here uses time of FIRST tick inside bar<\/i>, i.e., the data timestamp returns the time at the start of the bar. This is when the first quote for the new period arrives and defines the Open for the new bar. Click Tools -> Preferences -> Intraday to set this option: <\/p>\n
<\/p>\n
During backtesting the timing resolution will be equal to the periodicity set in AA Settings. If you are comparing Backtester signals with the signals displayed on your chart, you must make sure that the AA and Chart use the same periodicity. <\/p>\n
During Bar Replay the timing resolution will be equal to the greater of the base interval of your database or the Step Interval selected in the Bar-Replay window. <\/p>\n
During Backtesting and Bar-Replay, AmiBroker will refer to the timestamp to know how prices change over time. Hence, you will have no choice but to time events, such as session timing, with reference to the data timestamp. <\/p>\n
When trading from an Indicator, the data timestamp is rounded to the selected chart-periodicity, i.e., if you display a 1-minute chart, the timing resolution will be one minute. This means you cannot implement delays based in seconds using the data timestamps of a minute database. This is why most functions in a real-time trading system use the computer clock as reference. <\/p>\n
You can, with caution, use either the data timestamp or the system’s clock to control your session. However, since the data timestamp is dependent on the arrival of new quotes (ignoring data padding), using data timestamp could be unreliable. If you want higher timing resolution, you could create a 5-second database. However, this means working with slow backfills and slow AFL executions, due to lengthy data histories. To keep things simple, we will use a one-minute database. <\/p>\n
<\/b> <\/p>\n
When you are developing real-time trading systems, it is often handy, even essential sometimes, to develop several code versions. Typically, these might include: <\/p>\n
1) A version for Backtesting and Bar-Reply. This version would use the TimeNum function for timing.
\n2) A Real-Time trading version. This version would use the system clock (Now()) for timing and would be highly optimized for AFL execution speed.
\n3) A DebugView version. This is a useful intermediate development stage that lets you run your system in real-time without any TWS interfacing; it logs trades to DebugView instead of sending them to the TWS. <\/p>\n
ParamTime() input statements are used to set the start- and end-time of the trading session. The code below is only intended for preliminary system evaluation using the Backtester: hence, is uses time-numbers for session timing. <\/p>\n
The StartOfSession and EndOfSession variables are triggers (they last only one bar). They are used to initialize processes at the start of the session and clean up processes at the end of the session. The InSessionTime variable is True during trading and is used to control processes that must be turned On\/Off depending on whether you are trading or not. These processes will be covered in future posts. <\/p>\n
A TimeFrame Parameter has been added to help visualize how the system works in different timeframes. To see the equity for different timeframes, just drag the slider to see the system response to any timeframe from 1 minute to 1 hour. Having added Session Timing you can now explore whether this system is more profitable during certain hours of the trading day. I suggest you perform an individual backtest on you favorite watchlist; you may be surprised to find that with some systems there is no need to trade all day. More…<\/a><\/p>\n For debugging purposes, you can turn On\/Off a colored ribbon to display the Start- (Green), End- (Red) and In-Session (Yellow) variables. <\/p>\n For convenience the code below includes all previously developed parts. Just copy to an Indicator formula window, and click Apply. <\/p>\n<\/span><<\/span>p<\/span>><\/span>_SECTION_BEGIN<\/span>(<\/span>"SESSION TIMING"<\/span>); \r<<\/span>p<\/span>><\/span>TimeFrame<\/span>= <\/span>Param<\/span>(<\/span>"Chart Timeframe (min)"<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>60<\/span>,<\/span>1<\/span>)*<\/span>60<\/span>; <<\/span>p<\/span>><\/span>TimeFrameSet<\/span>(<\/span>TimeFrame<\/span>); <<\/span>p<\/span>><\/span>TN <\/span>= <\/span>TimeNum<\/span>(); <<\/span>p<\/span>><\/span>ParamStartTime <\/span>= <\/span>ParamTime<\/span>(<\/span>"Session Start"<\/span>,<\/span>"09:30:00"<\/span>); <<\/span>p<\/span>><\/span>ParamEndTime <\/span>= <\/span>ParamTime<\/span>(<\/span>"Session End"<\/span>,<\/span>"15:59:00"<\/span>); <<\/span>p<\/span>><\/span>InSessionTime <\/span>= <\/span>TN <\/span>&<\/span>gt<\/span>;= <\/span>ParamStartTime <\/span>AND <\/span>TN <\/span>&<\/span>lt<\/span>;= <\/span>ParamEndTime<\/span>; <<\/span>p<\/span>><\/span>StartOfSession <\/span>= <\/span>InSessionTime <\/span>&<\/span>gt<\/span>; <\/span>Ref<\/span>(<\/span>InSessionTime<\/span>,-<\/span>1<\/span>); <<\/span>p<\/span>><\/span>EndOfSession <\/span>= <\/span>InSessionTime <\/span>&<\/span>lt<\/span>; <\/span>Ref<\/span>(<\/span>InSessionTime<\/span>,-<\/span>1<\/span>); <<\/span>p<\/span>><\/span>InsessionTime <\/span>= <\/span>InSessionTime <\/span>OR <\/span>EndOfSession<\/span>;\r<<\/span>p<\/span>><\/span>_SECTION_END<\/span>();\r\r<<\/span>p<\/span>><\/span>Buy <\/span>= <\/span>O <\/span>&<\/span>lt<\/span>; <\/span>Ref<\/span>(<\/span>L<\/span>,-<\/span>1<\/span>) AND <\/span>InsessionTime<\/span>; <<\/span>p<\/span>><\/span>BuyPrice <\/span>= <\/span>O<\/span>; <<\/span>p<\/span>><\/span>Sell <\/span>= <\/span>L <\/span>&<\/span>lt<\/span>; <\/span>Ref<\/span>(<\/span>L<\/span>,-<\/span>1<\/span>) OR <\/span>EndOfSession<\/span>; <<\/span>p<\/span>><\/span>SellPrice <\/span>= <\/span>C<\/span>; <<\/span>p<\/span>><\/span>Short <\/span>= <\/span>O <\/span>&<\/span>gt<\/span>; <\/span>Ref<\/span>(<\/span>H<\/span>,-<\/span>1<\/span>) AND <\/span>InsessionTime<\/span>; <<\/span>p<\/span>><\/span>ShortPrice <\/span>= <\/span>O<\/span>; <<\/span>p<\/span>><\/span>Cover <\/span>= <\/span>H <\/span>&<\/span>gt<\/span>; <\/span>Ref<\/span>(<\/span>H<\/span>,-<\/span>1<\/span>) OR <\/span>EndOfSession<\/span>; <<\/span>p<\/span>><\/span>CoverPrice <\/span>= <\/span>C<\/span>; <<\/span>p<\/span>><\/span>SetTradeDelays<\/span>(<\/span>0<\/span>,<\/span>0<\/span>,<\/span>0<\/span>,<\/span>0<\/span>); <<\/span>p<\/span>><\/span>SetOption<\/span>(<\/span>"CommissionMode"<\/span>,<\/span>3<\/span>); <<\/span>p<\/span>><\/span>SetOption<\/span>(<\/span>"CommissionAmount"<\/span>, <\/span>0.005<\/span>); <<\/span>p<\/span>><\/span>E<\/span>=<\/span>Equity<\/span>(<\/span>1<\/span>); <<\/span>p<\/span>><\/span>TradeEquity <\/span>= <\/span>ValueWhen<\/span>(<\/span>Sell <\/span>OR <\/span>Cover<\/span>,<\/span>E<\/span>); \r\r<<\/span>p<\/span>><\/span>