Charts and Plots


Lets have a look at how we can plot charts with our strategies. It's much easier to look at a chart then at a printout of numbers. So learning this skill will help us in subsequent tutorials. The process is quite straight forward! You simply declare a strategy class member of type tsa::chart, and subsequently write data using operator<<.  For this to work, you must:

 

1.Include the following header: "tsa-graphics.h". (This is no longer necessary after library version 3.1)

2.Add a chart::pane to the chart object before inserting any data items/plots. A chart object can have multiple chart panes. A pane is essentially a window with a Y axis on one side.

3.When working with long running simulations, the number of data items becomes too large to easily display on a single chart. Class chart offers various options for breaking the chart into more manageable chunks by printing the chart as a 'chart-book', which is essentially a collection of related charts.

4.In order to control the format for each plot, such as its color and weight, as well as plot type, a number of helper classes exist. 

 

Lets have a look at Tutorial 112:

 

 

(1)

 

 

 

 

 

 

 

(2)

 

 

 

 

 

(3)

 

 

 

 

 

 

 

 

 

 

(4)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(5)

(6)

 

#include "tsa.h"

#include "tsa-graphics.h"

 

using namespace tsa;

 

class my_strategy : public strategy

{

     in_stream sp500;

 

     chart ch;

 

          void on_start(void)   override

          {

                    sp500.connect("sp500.daily");

 

                    ch.title("sp500_chart");

                    ch.width(900);

                    ch.max_bars_per_chart(150);

                    ch.max_charts_to_save(10);

          }

 

          void on_bar_close(void)   override

          {

                    catch_series_bounds_errors(true);

 

                    ch  << chart::pane(350)                                       << "PANE 1: "

                           << plot::ohlc(sp500, color::gold)                      << "SP500"

                           << plot::line(average(sp500.close, 5), color::red)     << "AVG-5"

                           << average(sp500.close, 50) /*auto format*/            << "AVG-10"

                           << plot::line(average(sp500.close, 5) + 15.0, color::auto_color, 4)

                           << plot::line(average(sp500.close, 5) - 15.0, color::purple, 4)

 

                        << chart::pane(150)                                       << "PANE 2: "

                           << plot::area(average(sp500.close, 5)

                                         - average(sp500.close, 10), color::orange)    << "MA-DIFF"

 

                        << chart::pane(150)                                        << "PANE 3: "

                           << plot::bar(sp500.volume[0], color::red)               << "VOL"

                           << plot::line(average(sp500.volume, 5), color::purple, 5);

                    }

          };

 

          fast::database db("output/demo_db", create_db | truncate_db);

 

          import_from_file__SP500_daily(db);

 

          my_strategy s;

          s.name("112_basic");

          s.attach(db);

          s.output_base_path("output");

          s.enable_reports();

          s.auto_open_reports();

 

          s.run("2010-02-01", "2014-03-01");

 

Program Output

 

 

 

 

 

(1)

#include "tsa-graphics.h"

 

lncludes the header required for graphics functionality.

(2)

chart ch;

 

Declares a strategy chart member.

(3)

          ch.title("sp500_chart");

          ch.width(900);

          ch.max_bars_per_chart(150);

          ch.max_charts_to_save(10);

 

The above code sets a few chart properties, such as the width in pixel ( charts::width() ), how many bars should be show on a single chart ( chart::max_bars_per_chart() ), and the maximum number of charts produced altogether. Recall that a chart object is saved as a collection of '.png' image files referred to as a chart-book. Chart books are saved in a dedicated directory inside the strategy's output directory ( see strategy::output_path() ).

For long running simulations, with potentially hundreds of charts to produce, a limit needs to be set on the number of charts to produce and save ( chart::max_charts_to_save() ) to reduce the time it takes to produce the reports and charts after a strategy simulation is completed.

(4)

Here we get to the heart of the charting operation. This following line of code passes both the chart structure, format, as well as data to the chart object:

 

          ch  << chart::pane(350)                                     << "PANE 1: "

                    << plot::ohlc(sp500, color::gold)                      << "SP500"

                    << plot::line(average(sp500.close, 5), color::red)     << "AVG-5"

                    << average(sp500.close, 50) /*auto format*/            << "AVG-10"

                    << plot::line(average(sp500.close, 5) + 15.0, color::auto_color, 4)

                    << plot::line(average(sp500.close, 5) - 15.0, color::purple, 4)

 

          << chart::pane(150)                                         << "PANE 2: "

                    << plot::area(average(sp500.close, 5)

                                         - average(sp500.close, 10), color::orange) << "MA-DIFF"

 

          << chart::pane(150)                                         << "PANE 3: "

                    << plot::bar(sp500.volume[0], color::red)              << "VOL"

                    << plot::line(average(sp500.volume, 5), color::purple, 5);

 

Three panes are inserted. One of height 350 pixel and the other two of height 150 pixel each. In the first pane, we insert the o,h,l,c values, as 'bars', with two moving averages as simple lines, together with some bands around the centre moving average. The character strings are simply optional labels for the most recently added data item.

 

Note that the line inserting the o,h,l,c data could have been written more explicitly as:

 

<< plot::ohlc(sp500.open, sp500.high, sp500.low, sp500.close, color::gold) << ":SP500"

(5)

s.output_base_path("output");

 

This tells the strategy where to write output. Output normally consists of logs, reports and charts. With this particular strategy, no orders were sent or filled, and so there are no performance or trade related reports. Charts are also written to a strategy's output directory. By default this strategy output directory is created in the application's 'current working directory'. This can be changed by passing a desired base path to the strategy's  output_base_path() member.

In this case, the charts are written to the following relative path:

 

output/112_basic/charts

(6)

The easiest way to access the charts that the strategy produced is by telling the strategy to automatically open reports with a call to :

 

                    s.auto_open_reports();

 

The auto_open_reports() member sets a flag to automatically open any reports produced in an HTML browser. A chart-book is also considered a 'report' .