Saturday 4 February 2017

3: My First Tobaggan Algo

In this lecture we'll simply run a backtest without much explanation as to why things work the way they do. I will talk more about this later. See if you can understand what's going on and have a look at  the code if you need to. I assume you have looked at the previous two posts. Have fun.

Go to the file

lesson1.py 

and at the bottom of the file find this:
if __name__=='__main__':
    run_csv()

python lesson1.py

You'll notice that it does something and finally stop. So what's happening here?
First, have a look at the algo definition by finding in the file the following lines:

def initialize_csv(context):
    csv_fetcher(context,'data/MSFT.csv','signals')


def handle_data_csv(context):
    if random.randint(0,100) == 23:
        order(context,'MSFT',1)
    if random.randint(0,100) == 23:
        order(context,'AAPL',1)

def run_csv():
    data = CSVDataFactory(['data/MSFT.csv','data/AAPL.csv'])()
    strategy = Strategy(config,
                        handle_data=handle_data_csv,
                        initialize=initialize_csv)
    strategy.run(data, 
                 start=datetime(2012,1,1),
                 finish=datetime(2014,12,12))

This is all Toboggan needs to run. Let's take this apart a little. The crucial bit is our run_csv()function and the first line says:

data = CSVDataFactory(['data/MSFT.csv','data/AAPL.csv'])()

This line instantiates our data source, which is a bunch of sample csv files that live in the data/directory of the Toboggan repo. CSVDataFactory is a class in the data_factory.py file.  I'll talk about the data factory in detail later. What you need to know for now is that the funny parenthesis at the end of the command call a function within the class that creates a data object that can be used by the algo engine for processing. If the notion of objects, classes and functions slightly confuses you, it's not really that important for now. So don't worry too much. 

After we created a data object, we instantiate the strategy with 

strategy = Strategy(config,handle_data=handle_data_csv,initialize=initialize_csv)

When we do this we pass in several other objects, namely config, handle_data and initialise

config is a file that contains configuration values for the strategy and this file lives in the config folder. In many cases, algos have a long list of configuration parameters and it's advisable to separate them from the rest of the code, so the code does not change when we want to run a bunch of other values. 

With initialise we can set the initial conditions for our simulation. In our case we fetch some external data with a fetcher. This will be explained later. In this algo, the fetcher doesn't actually do anything. So, our initialize() function is effectively empty. 

Finally, we have handle_data() which is called every time a new tick comes in. This is where we define what the algo needs to do. In our case, we just order a random number of either AAPL or MSFT shares whenever our if-condition is true. 

You notice that there is a mysterious context variable. This is kind of the donkey that you can attach all sorts of baggage to and it will hardly ever complain. More on this in the next blog. 

Finally, we run our algo with 

strategy.run(data,start=datetime(2012,1,1),finish=datetime(2014,12,12))

run() is a function that is part of the strategy object. It processes block data (rather than streaming data). Block data are simply lines that are read into memory at once as a block. The Strategy() class lives  in the engine.py file. Have a look if you like. I will explain more on that later. 

Finally you'll see some lines like this:
Order Loaded AAPL 2014-10-29 00:00:00

(datetime.datetime(2014, 10, 30, 0, 0), 'ORDERING instrument:', 'AAPL', ' quantity:', 0, ' price:', 106.98, ' positions:', 4, 'pnl:', 144.70000000000002)

Here, the algo tells us that we've submitted some trades with "Order Loaded" and executed them on the next available bar. In order for these trades to show up, we open the file config_lesson1.py in the config directory and set PRINT_TRADES = True.

Easy! Please leave your comments. Again, if you have trouble installing Python packages, please try to solve this yourself and, by all means, use Stackoverflow.

2: Getting Started With Toboggan



Toboggan runs on Python2.7. I know this is a bit old-school but so am I 😉 
To get it to run on Python3.x all it needs is to change the print statements. Perhaps I will change this in the near future.

To download the code, open a terminal and run:

git clone https://github.com/rodler/toboggan.git

Then type:

make run

or, if you are on Windows (I'm a Linux geek, sorry) and type:

python lesson1.py

This should run one of the examples in the code.  You will probably see some screen outputs and in the following posts I will explain all of the examples in detail. 

For now, feel happy that you got the system to run. 

If you're on Python3.x you may want to install Python2.7 in a virtual environment. There is a good article here. Just replace


mkvirtualenv -p python3 marketcrush

with 


mkvirtualenv -p python2 toboggan

And all the other "marketcrush" with Toboggan
The rest should be the same. Too easy...

If you are missing any Python packages such as Numpy or Matplotlib, just install them with 

sudo pip install numpy

or drop the sudo if you are in a virtual environment. Leave a comment if you have any issues.
Please be aware that my time is very limited, so I probably won't be able to help you with basic computer issues. Refer to Stackoverflow for help. This is how I learned.

1: Welcome To Toboggan Algo Trading


This blog discusses the alto-trading and backtesting platform Toboggan. I've been involved in algorithmic trading for many years and this is a little side-project of mine. It is still early stages with the project but it already has taken some nice shape.

Here is the link to Github.

Toboggan is written in Python. There are already a few open-source backtesting environments in Python: Zipline, Pyalgotrade, QSTrader. There are also some C-based platforms such as Zorro and QuantConnect.
All of them are  pretty good. Personally, I really like the Quantopian environment with Zipline and if you have ever used it you will notice that Toboggan looks quite similar.
The Quantopian guys worked really hard to build an amazing online tool and I have no intention to get into competition with them. While there online environment is excellent, there open-source version has been a bit neglected. So, with Toboggan I try to address some of the issues that I could not really resolve with Zipline such as:


  • Running a range of data formats
  • Running live paper trading
  • Optimisation and walk-forward optimising
  • Line-by-line loading for large data files
  • Execution-sensitive trading (bid-ask spreads)
  • Connections to live markets
  • Simplicity and "hackability"
I hope to address these points in Slackline. Unlike Quantopian, it does not provide market data (which is a crucial part of trading). It also does not have a vibrant community and awesome lecture series. For all these things please check out the Quantopian website. 

However, if you've been checking out other backtesters and left a bit frustrated, Slackline may be for you. Check it out, if you like. 

Happy trading.