Skip to content

A program to predict the stock market. ML Algorithms: Random Forest, Decision Trees ans also a CNN (TensorFlow) were implemented and their performance compared.

Notifications You must be signed in to change notification settings

AdityaChavan/Stock-Market-Trader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock-Market-Trader

A program to create a strategy to trade in the stock market. ML Algorithms: Random Forest, Decision Trees and also a Convolutional Neural Network (TensorFlow) were implemented and their performance compared. The result indicates that the predicted strategy outperforms just buying a stock and holding it. The best algorithm, the CNN outputs a strategy that would have multiplied the portfolio 6.7 times in 4 years time, compared to 2.1 times by buying the stock.

Shown below are the outputs of the program: alt text

We trade based on the stock Apple (NYSE:AAPL).

The red line, the stock price, represents our portfolio if we would have if we bought 1000 shares of AAPL and held it till the end.

The other lines represent trading strategies given by the ML Algorithms, limited to a maximum of having or shorting 1000 shares.(Higher returns are better)

The Cumulative returns (End Price/ Start price) provided over the time period are:
CNN: 6.6926
Decision Trees: 4.1891
Random Forest: 3.5388
Benchmark AAPL: 2.0926

How to frame the trading problem as a Machine Learning problem:

We limit our problem to 3 states:
having 1000 shares (LONG) (we benefit if the price goes UP),
selling 1000 shares (SHORT) (we benifit if the price goes DOWN),
just holding (0 shares).

We dont give the stock prices directly to the algorithm. We calculate 3 stock indicators (described below) based on the stock price.

Input: Stock Indicators
Output: Order (+1: Buy 1000 shares, 0: Hold, -1: Sell 1000 shares)

We limit our portfolio to having 1000 shares or shorting 1000 shares. Goodwill is assumed to be infinite. Impact is considered minimal. 0 Commissions.
Thus output is limited to +2 and -2 max.

We use the indicators as the training data (X). The Y array for the training set is a look forward to whether the stock went up or not. In other words, the Y is a 3 column matrix with for each of the 3 outcomes: shorting, selling or holding the stock for that day. We can postulate that similar indicators at any time will lead to similar results in the future. Based on our results we can conclude that the assumption holds. A big factor is selecting the indicators. They must accurately reflect the current state of the stock, its past and expected future values as well as be normalized. Here are the 3 indicators that I used:

Indicators:


Simple Moving Average: (SMA):
This is a N day average of the stock. It reflects the average of the previous N days that the stock traded for.
Bolinger Bands Percentage (BBP):
This indicates the standard deviation of the stock from its average value. More info here: BB%.
Momentum (RSI):
We notice that when a stock price is going up (or down), it will maintain the trend for a certain number of days before reversing directions. We can use the slope of the stock price as an indicator to trade. RSI.

##Algorithms:
Random Forest:
20 trees, Max Leaf Size: 5

Decision trees:
Max Leaf size: 5

CNN: Implemented in Tensorflow (tflearn)
2D Convolution >> Max Pool >> Fully Connected >> Dropout >> Fully Connected >> SoftMax
Activation: Relu
Optimizer: Adam
Learning Rate: 1e-2

Training Time: 2004-2007 Testing Time: 2008-2011

###Other stats from results:

CR:(Cumulative Return) ADR:(average Daily Return) SDDR:(Standard Deviation of Daily Return) SR: (Sharpe Ratio)
Random Forest
CR: 3.5388 ADR: 0.0016 SDDR: 0.0125 SR: 2.0058

Decision Trees
CR: 4.1891 ADR: 0.0017 SDDR: 0.012 SR: 2.254

CNN
CR: 6.6926 ADR: 0.0021 SDDR: 0.0154 SR: 2.2105

Benchmark (AAPL):
CR: 2.0926 ADR: 0.0464 SDDR: 3.0535 SR: 0.2411

References:

Georgia Tech CS 7646 under Prof. Tucker Balch
Udacity Machine Learning for trading
Dataset:
Kaggle - Huge Stock Market Dataset

How to run:

  1. Clone repo
  2. go to StrategyLearner folder
  3. PYTHONPATH=../:. python experiment1.py

Written in Python 2.7 Dependencies: Numpy, Pandas, TensorFlow, TFLearn, Matplotlib

Disclaimer: Do not trade based on this strategy.

About

A program to predict the stock market. ML Algorithms: Random Forest, Decision Trees ans also a CNN (TensorFlow) were implemented and their performance compared.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages