Basic Strategy Scheduling
Tutorial 102 builds on tutorial 101 and introduces scheduling and an early exit from the strategy loop.
(1)
(2)
(3)
(4) |
#include "tsa.h"
using namespace tsa;
class my_strategy : public tsa::strategy { void on_start(void) override { strategy::auto_schedule(duration::microseconds(1)); }
void on_bar_close(void) override { std::cout << "bar: " << bar_count() << "\t " << "timestamp: " << timestamp() << std::endl;
if (bar_count() == 10) { exit(); } } };
my_strategy s; s.run("2012-01-01", "2012-01-02"); |
Program Output |
Tutorial: 102 ================================= bar: 1 timestamp: 2012-01-01T00:00:00.0 bar: 2 timestamp: 2012-01-01T00:00:00.000001 bar: 3 timestamp: 2012-01-01T00:00:00.000002 bar: 4 timestamp: 2012-01-01T00:00:00.000003 bar: 5 timestamp: 2012-01-01T00:00:00.000004 (... lines skipped here ...) |
(1) |
using namespace tsa;
'tsa' is the name of the 'Trading System API' namespace. It encompasses everything that you will be using from the library. In (1) we issue a directive to use the tsa namespace. This makes the code somewhat more concise, which is not a bad thing in a tutorial. |
(2) |
strategy::auto_schedule(duration::microseconds(1));
Here we introduce scheduling. Scheduling involves generating the timestamp sequence that represents strategy intervals (bars). In most cases, this sequence will come from a record stream where each record is associated with a timestamp. In this case, the strategy has no such data source and so, as an alternative, we instruct the strategy to auto-generating such a scheduling timestamp sequence. Note that we are passing a microsecond duration argument to auto_schedule(), so the bars produced will be just 1 microsecond apart. A microsecond is the shorted duration supported by the library (more specifically by the library's class date_time ) . The actual time the strategy requires to iterate over a single bar mostly depends on the amount of user logic that needs recalculating at each bar, such as indicators, pattern matching engines, geometric analysis etc. This can all take much longer than 1 microsecond, so the interval duration needs to be set with caution. |
(3) |
There are of course a lot of microsecond intervals in a day. For our purposes we don't want to iterate over billions of intervals and so we use the strategy::bar_count() member in conjunction with the strategy::exit() member to terminate the strategy on the 10th strategy interval.
if (bar_count() == 10) { exit(); }
Note that the exit() member does not terminate the strategy immediately! It merely sets an internal flag that marks the current bar as the last bar. The on_on_bar_close() member will still be evaluated in its entirety. |
(4) |
Alternatively, we could have simply added more resolution to the timespan arguments passed to strategy::run() such as:
s.run("2012-01-01 09:00:00.0", "2012-01-02 09:00:00.000009");
This would have had the same effect as exiting the strategy on 10th bar, but is a little verbose. |
|
|