A neural network-based stock trading algorithm
To be able to get data from Quandl, obtain a Quandl API key and put it in quandl_key.txt
To train:
For a full list of CLI args, call python train_lstm.py --help
Example:
python3 train_lstm.py -e 50 -l 4 -n 1000 WIKI/MMM WIKI/ABT WIKI/ABBV
trains a neural net with 4 recurrent layers with 1000 cells each for 50 epochs, using data from the securities MMM, ABT, and ABBV
To test:
python trade_lstm.py <filename of saved model>. The stock being used to test is currently hard-coded. Proper testing CLI is soon to come.
- Uses technical indicators and price data to predict the direction of stock movement and major price direction changes (peaks/troughs).
- Based on this output, makes decisions on whether to buy/sell (i.e. if the predicted direction of prices is up, go long). Tries to buy at the troughs and sell at the peaks.
- Training algorithm (trainer) is called, parameters of the neural net are specified. (filename here)
- Trainer builds training data with
data_provider.build_train_data, which:- Pulls (daily) price data from Quandl.
- Processes the data, builds indicators, aggregates into Pandas dataframes. (Processed data is cached by default so as to save resources, be careful with big datasets and little disk space.)
- Creates the output labels (i.e. the direction of stock movement, 1 for up/long, 0 for down/short)
- Splits data into training and testing, optionally scaling/shuffling/etc.
- Returns Pandas Dataframes of
train_x,test_xand Series oftrain_yandtest_y.
- Trains neural net as specified on training data, tests on the testing data. Uses Keras to build the neural nets.
- Optionally, saves intermediate training results, outputs accuracy, and/or simulates "live trading" with another stock ticker (preferably one it hasn't seen before).
- Takes parameters including the stock ticker to use, how much cash to start with, the period to test on.
- Builds data as described above. Does not build labels, as this will not be used for training, but rather for simulating "real-life" trading, in which the correct action is obviously not provided.
- Steps through the data day-by-day, making long-short decisions based on the output of the network being tested.
- Outputs yearly results, annualized return percentage and (optionally) individual trade details.