Open in Colab: https://colab.research.google.com/github/ryanraba/stocksml/blob/master/docs/quick_start.ipynb


Installation

Normal installation from the command line (assuming you have Python3 installed)

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install stocksml

From a Jupyter notebook environment such as Google Colab

!pip install stocksml

Quick Start

Quick demonstration using included sample data sources.

[1]:
 import os
 os.system("pip install stocksml")
 print('installed stocksml')
installed stocksml
[2]:
from stocksml import Demo

Demo(notebook=True)
_images/quick_start_3_0.png

2021-02-01   buy market order for   24 shares of vixm at    0.0  ->  bought   24 shares at   41.6  ($0.9, $1000.0,   43.2,   41.6,   41.6,   41.9 )
2021-02-01  sell  limit order for   24 shares of vixm at   40.9  ->    sold   24 shares at   41.6  ($1000.0, $1000.0,   43.2,   41.6,   41.6,   41.9 )
2021-02-02   buy market order for   24 shares of vixm at    0.0  ->  bought   24 shares at   41.2  ($11.4, $1000.0,   41.3,   40.6,   41.2,   40.7 )
2021-02-02  sell  limit order for   24 shares of vixm at   42.6
2021-02-03  sell market order for   24 shares of vixm at    0.0  ->    sold   24 shares at   40.2  ($977.4, $977.4,   40.5,   39.9,   40.2,   39.9 )
2021-02-03   buy market order for   24 shares of vixm at    0.0  ->  bought   24 shares at   40.2  ($11.4, $977.4,   40.5,   39.9,   40.2,   39.9 )
2021-02-03  sell  limit order for   24 shares of vixm at   43.4
2021-02-04  sell market order for   24 shares of vixm at    0.0  ->    sold   24 shares at   39.5  ($959.2, $959.2,   39.8,   39.2,   39.5,   39.7 )
2021-02-04   buy market order for   24 shares of vixm at    0.0  ->  bought   24 shares at   39.5  ($11.4, $959.2,   39.8,   39.2,   39.5,   39.7 )
2021-02-04  sell  limit order for   24 shares of vixm at   40.4
2021-02-05  sell market order for   24 shares of vixm at    0.0  ->    sold   24 shares at   39.4  ($958.0, $958.0,   40.0,   39.4,   39.4,   39.9 )
2021-02-05   buy market order for   24 shares of vixm at    0.0  ->  bought   24 shares at   39.4  ($11.4, $958.0,   40.0,   39.4,   39.4,   39.9 )
2021-02-05  sell  limit order for   24 shares of vixm at   40.6
---------- liquidate ----------
2021-02-05  sell market order for   24 shares of vixm at    0.0  ->    sold   24 shares at   39.9  ($968.1, $968.1,   40.0,   39.4,   39.4,   39.9 )
---------- result = $968.1 at 0.932 of baseline ----------

Load Data

[3]:
from stocksml import LoadData, BuildData

# load symbols and build a symbol dataframe
sdf, symbols = LoadData(symbols=['SPY','BND'])

# convert symbol dataframe to a feature dataframe
fdf = BuildData(sdf)

fdf.head()
building BND data...
building SPY data...
[3]:
bnd0 bnd1 bnd2 bnd3 bnd4 spy0 spy1 spy2 spy3 spy4
date
2017-01-03 -0.001654 -0.000511 -0.001190 -0.000778 0.371118 -0.004692 -0.003802 -0.003938 -0.003082 0.018743
2017-01-04 0.009508 0.018398 0.053818 0.006910 0.080335 0.028361 0.045359 0.013076 0.026660 0.391944
2017-01-05 0.109609 0.010800 0.025270 0.051719 0.385050 -0.010775 -0.007476 0.015081 -0.007054 0.026528
2017-01-06 -0.043183 0.003252 0.021451 -0.041545 -0.413196 0.037203 0.008074 0.003648 0.014804 0.152411
2017-01-09 0.012214 0.010019 0.011997 0.024802 -0.107640 -0.028913 0.010770 0.007136 -0.019585 -0.380552

Build a Model

Define a model and create a set of 2 or more with corresponding training data.

[4]:
from stocksml import BuildModel

models, dx = BuildModel(fdf, len(symbols), layers=[('rnn',32),('dnn',64),('dnn',32)], count=2)

models[0].summary()
Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input (InputLayer)              [(None, 5, 10)]      0
__________________________________________________________________________________________________
rnn_0 (SimpleRNN)               (None, 32)           1376        input[0][0]
__________________________________________________________________________________________________
dnn_1 (Dense)                   (None, 64)           2112        rnn_0[0][0]
__________________________________________________________________________________________________
dnn_2 (Dense)                   (None, 32)           2080        dnn_1[0][0]
__________________________________________________________________________________________________
action (Dense)                  (None, 5)            165         dnn_2[0][0]
__________________________________________________________________________________________________
symbol (Dense)                  (None, 2)            66          dnn_2[0][0]
__________________________________________________________________________________________________
limit (Dense)                   (None, 1)            33          dnn_2[0][0]
==================================================================================================
Total params: 5,832
Trainable params: 5,832
Non-trainable params: 0
__________________________________________________________________________________________________

Learn a Strategy

After creating a set of adversarial models and the corresponding training data formatted for them, StocksML is ready to learn a new trading strategy. This is done in an unsupervised manner, meaning no truth data is provided.

The algorithm begins with each model making random guesses. When one model successfully guesses a sequence of trades that results in superior performance (i.e. makes money or beats a benchmark), that model’s strategy is “learned” by the unsuccessful model. This continues for a set period of iterations or until it appears that the models are no longer learning anything useful.

The LearnStrategy function displays a live plot of various metrics to illustrate the learning process and help inform when a good stopping point might be.

[5]:
from stocksml import LearnStrategy

LearnStrategy(models, sdf, dx, symbols, 'SPY', 5, 500, True)
_images/quick_start_9_0.png

Examine the Strategy

Once a trading strategy has been learned, it can be applied to different points in time across the available market data to see what it does and how it performs.

To avoid overfitting, it would be wise to examine strategy performance on data that wasn’t used for training.

[6]:
from stocksml import ExamineStrategy

ExamineStrategy(models[0], sdf, dx, symbols, '2021-02-01', days=5, baseline='SPY')

2021-02-01   buy market order for    2 shares of  spy at    0.0  ->  bought    2 shares at  376.2  ($247.5, $1000.0,  377.3,  370.4,  373.7,  376.2 )
2021-02-02  sell  limit order for    2 shares of  spy at  397.7
2021-02-03  sell market order for    2 shares of  spy at    0.0  ->    sold    2 shares at  382.4  ($1012.4, $1012.4,  383.7,  380.5,  382.4,  381.9 )
2021-02-03   buy market order for    2 shares of  spy at    0.0  ->  bought    2 shares at  382.4  ($247.5, $1012.4,  383.7,  380.5,  382.4,  381.9 )
2021-02-03  sell  limit order for    2 shares of  spy at  390.0
2021-02-04  sell market order for    2 shares of  spy at    0.0  ->    sold    2 shares at  383.0  ($1013.5, $1013.5,  386.2,  382.0,  383.0,  386.2 )
2021-02-04   buy market order for    2 shares of  spy at    0.0  ->  bought    2 shares at  383.0  ($247.5, $1013.5,  386.2,  382.0,  383.0,  386.2 )
2021-02-04  sell  limit order for    2 shares of  spy at  389.9
2021-02-05  sell  limit order for    2 shares of  spy at  346.2  ->    sold    2 shares at  388.2  ($1023.9, $1023.9,  388.5,  386.1,  388.2,  387.7 )
2021-02-05   buy market order for   11 shares of  bnd at    0.0  ->  bought   11 shares at   87.0  ($67.4, $1023.9,   87.1,   87.0,   87.1,   87.0 )
---------- liquidate ----------
2021-02-05  sell market order for   11 shares of  bnd at    0.0  ->    sold   11 shares at   87.0  ($1023.9, $1023.9,   87.1,   87.0,   87.1,   87.0 )
---------- result = $1023.9 at 0.986 of baseline ----------