This project uses reinforcement learning on stock market and agent tries to learn trading. The goal is to check if the agent can learn to read tape. The project is dedicated to hero in life great Jesse Livermore.

Overview

Reinforcement-trading

This project uses Reinforcement learning on stock market and agent tries to learn trading. The goal is to check if the agent can learn to read tape. The project is dedicated to hero in life great Jesse Livermore and one of the best human i know Ryan Booth https://github.com/ryanabooth.

One Point to note, the code inside tensor-reinforcement is the latest code and you should be reading/running if you are interested in project. Leave other directories, I am not working on them for now
. To read my thought journal during ongoing development https://github.com/deependersingla/deep_trader/blob/master/deep_thoughts.md

Before this I have used RL here: http://somedeepthoughtsblog.tumblr.com/post/134793589864/maths-versus-computation

Now I run a company on RL trading, so I can't answer questions related to the project.

Steps to reproduce DQN

a) cd tensor-reinforcement
b) Copy data from https://drive.google.com/file/d/0B6ZrYxEMNGR-MEd5Ti0tTEJjMTQ/view and https://drive.google.com/file/d/0B6ZrYxEMNGR-Q0YwWWVpVnJ3YmM/view?usp=sharing into tensor-reinforcement directory.
b) Create a directory saved_networks inside tensor_reinforcement for saving networks.
c) python dqn_model.py

Steps to reproduce PG

a) cd tensor-reinforcement
b) Create a directory saved_networks inside tensor_reinforcement for saving networks.
c) python pg_model.py

For the first iteration of the project

Process:
Intially I started by using Chainer for the project for both supervised and reinforcement learning. In middle of it AlphaGo (https://research.googleblog.com/2016/01/alphago-mastering-ancient-game-of-go.html) came because of it I shifted to read Sutton book on RL (https://webdocs.cs.ualberta.ca/~sutton/book/the-book.html), AlphaGo and related papers, David Silver lectures (http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching.html, they are great).

I am coming back to project after some time a lot has changed. All the cool kids even DeepMind (the gods) have started using TensorFlow. Hence, I am ditching Chainer and will use Tensorflow from now. Exciting times ahead.

Policy network

I will be starting with simple feed-forward network. Though, I am also inclined to use convolutional network reason, they do very well when the minor change in input should not change ouput. For example: In image recognizition, a small pixel values change doesn't meam image is changed. Intutively stocks numbers look same to me, a small change should not trigger a trade but again the problem here comes with normalization. With normalization the big change in number will be reduced to a very small in inputs hence its good to start with feed-forward.

Feed-forward

I want to start with 2 layer first, yes that just vanilla but lets see how it works than will shift to more deeper network. On output side I will be using a sigmoid non-linear function to get value out of 0 and 1. In hidden layer all neurons will be RELU. With 2 layers, I am assuming that first layer w1 can decide whether market is bullish, bearish and stable. 2nd layer can then decide what action to take based on based layer.

Training

I will run x episode of training and each will have y time interval on it. Policy network will have to make x*y times decision of whether to hold, buy or short. After this based on our reward I will label every decison whether it was good/bad and update network. I will again run x episode on the improved network and will keep doing it. Like MCTS where things average out to optimality our policy also will start making more positive decision and less negative decision even though in training we will see policy making some wrong choices but on average it will work out because we will do same thing million times.

Episodic

I plan to start with episodic training rather than continous training. The major reason for this is that I will not have to calculate reward after every action which agent will make which is complex to do in trading, I can just make terminal reward based on portfolio value after an entire episode (final value of portfolio - transaction cost occur inside the episode - initial value of portfolio). The other reason for doing it that I believe it will motivate agent to learn trading on episodes, which decreases risk of any outlier events or sentiment change in market.

This also means that I have to check the hypothesis on:
a) Episodes of different length
b) On different rewards terminal reward or rewards after each step inside an episode also.
As usual like every AI projects, there will be a lot of hit and trial. I should better write good code and store all results properly so that I can compare them to see what works and what don't. Ofcourse the idea is to make sure agent remain profitable while trading.

More info here https://docs.google.com/document/d/12TmodyT4vZBViEbWXkUIgRW_qmL1rTW00GxSMqYGNHU/edit

Data sources

  1. For directly running this repo, use this data source and you are all setup: https://drive.google.com/open?id=0B6ZrYxEMNGR-MEd5Ti0tTEJjMTQ
  2. Nifty Data: https://drive.google.com/folderview?id=0B8e3dtbFwQWUZ1I5dklCMmE5M2M&ddrp=1%20%E2%81%A0%E2%81%A0%E2%81%A0%E2%81%A09:05%20PM%E2%81%A0%E2%81%A0%E2%81%A0%E2%81%A0%E2%81%A0
  3. Nifty futures:http://www.4shared.com/folder/Fv9Jm0bS/NSE_Futures
  4. Google finance
  5. Interative Brokers, I used IB because I have an account with them.

For reading on getting data using IB https://www.interactivebrokers.com/en/software/api/apiguide/tables/historical_data_limitations.htm https://www.interactivebrokers.com/en/software/api/apiguide/java/historicaldata.htm symbol: stock -> STK, Indices -> IND

Reinforcement learning resources

https://github.com/aikorea/awesome-rl , this is enough if you are serious

Comments
  • code run error

    code run error

    I copy data from https://drive.google.com/file/d/0B6ZrYxEMNGR-MEd5Ti0tTEJjMTQ/view and https://drive.google.com/file/d/0B6ZrYxEMNGR-Q0YwWWVpVnJ3YmM/view?usp=sharing into tensor-reinforcement directory.

    and run python dqn_model.py and get error:

    Traceback (most recent call last): File "/Users/Wilson/github/deep_trader/tensor-reinforcement/dqn_model.py", line 12, in from train_stock import * File "/Users/Wilson/github/deep_trader/tensor-reinforcement/train_stock.py", line 23, in supervised_y_data = episodic_data.make_supervised_data(data, data_dict) File "/Users/Wilson/github/deep_trader/tensor-reinforcement/episodic_data.py", line 100, in make_supervised_data supervised_data.append(episode_supervised_data(episode, data_dict)) File "/Users/Wilson/github/deep_trader/tensor-reinforcement/episodic_data.py", line 86, in episode_supervised_data prices.append(data_average_price(data_dict, iteration)) File "/Users/Wilson/github/deep_trader/tensor-reinforcement/episodic_data.py", line 92, in data_average_price data = data_dict[key] KeyError: '297956b2300474fda50a2a6b1d41a714'

    opened by tubu 10
  • code run error

    code run error

    when I run python dqn_model.py in tensor-reinforcement,i got an error(I have mkdir saved_networks,and get data.pk land data_dict.pkl in tensor-reinforcement): Traceback (most recent call last): File "/Users/nicefilm/Documents/code/python/deep-trader/tensor-reinforcement/dqn_model.py", line 11, in from train_stock import * File "/Users/nicefilm/Documents/code/python/deep-trader/tensor-reinforcement/train_stock.py", line 23, in supervised_y_data = episodic_data.make_supervised_data(data, data_dict) File "/Users/nicefilm/Documents/code/python/deep-trader/tensor-reinforcement/episodic_data.py", line 100, in make_supervised_data supervised_data.append(episode_supervised_data(episode, data_dict)) File "/Users/nicefilm/Documents/code/python/deep-trader/tensor-reinforcement/episodic_data.py", line 87, in episode_supervised_data prices.append(data_average_price(data_dict, iteration)) File "/Users/nicefilm/Documents/code/python/deep-trader/tensor-reinforcement/episodic_data.py", line 92, in data_average_price data = data_dict[list_md5_string_value(data)] KeyError: '297956b2300474fda50a2a6b1d41a714'

    opened by 4575759ww 7
  • Could not find old network weights

    Could not find old network weights

    Could not find old network weights 0 Traceback (most recent call last): File "dqn_model.py", line 251, in main() File "dqn_model.py", line 183, in main agent.perceive(state,action,reward,next_state,done) File "dqn_model.py", line 91, in perceive self.train_Q_network() File "dqn_model.py", line 128, in train_Q_network self.saver.save(self.session, 'saved_networks/' + 'network' + '-dqn', global_step = self.time_step) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1037, in save {self.saver_def.filename_tensor_name: checkpoint_file}) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 340, in run run_metadata_ptr) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 564, in _run feed_dict_string, options, run_metadata) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 637, in _do_run target_list, options, run_metadata) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 659, in _do_call e.code) tensorflow.python.framework.errors.NotFoundError: saved_networks/network-dqn-3000.tempstate1294467586467070884 [[Node: save/save = SaveSlices[T=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/save/tensor_names, save/save/shapes_and_slices, Variable, Variable/Adam, Variable/Adam_1, Variable_1, Variable_1/Adam, Variable_1/Adam_1, Variable_2, Variable_2/Adam, Variable_2/Adam_1, Variable_3, Variable_3/Adam, Variable_3/Adam_1, beta1_power, beta2_power)]] Caused by op u'save/save', defined at: File "dqn_model.py", line 251, in main() File "dqn_model.py", line 167, in main agent = DQN(data_dictionary) File "dqn_model.py", line 41, in init self.saver = tf.train.Saver() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 832, in init restore_sequentially=restore_sequentially) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 500, in build save_tensor = self._AddSaveOps(filename_tensor, vars_to_save) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 197, in _AddSaveOps save = self.save_op(filename_tensor, vars_to_save) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 149, in save_op tensor_slices=[vs.slice_spec for vs in vars_to_save]) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/io_ops.py", line 172, in _save tensors, name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_io_ops.py", line 341, in _save_slices name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 661, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2154, in create_op original_op=self._default_original_op, op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1154, in init self._traceback = _extract_stack()

    opened by aaronzhudp 5
  • code run problem

    code run problem

    when I run python python dqn_model.py in tensor-reinforcement,i got an error(I have mkdir saved_networks,and get data.pk land data_dict.pkl in tensor-reinforcement): Traceback (most recent call last): File "dqn_model.py", line 11, in <module> from train_stock import * File "/home/deep_trader/tensor-reinforcement/train_stock.py", line 23, in <module> supervised_y_data = episodic_data.make_supervised_data(data, data_dict) File "/home/deep_trader/tensor-reinforcement/episodic_data.py", line 99, in make_supervised_data supervised_data.append(episode_supervised_data(episode, data_dict)) File "/home/deep_trader/tensor-reinforcement/episodic_data.py", line 86, in episode_supervised_data prices.append(data_average_price(data_dict, iteration)) File "/home/deep_trader/tensor-reinforcement/episodic_data.py", line 91, in data_average_price data = data_dict[list_md5_string_value(data)] KeyError: '297956b2300474fda50a2a6b1d41a714'

    opened by HubFire 2
  • DQN Strategy

    DQN Strategy

    I run the code as directed and get this error which I am not able to work through. Please help me with this. Thanks

    C:\Users\Aakash Juneja\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) Traceback (most recent call last): File "dqn_model.py", line 8, in from train_stock import * File "C:\Study Project (CEERI)\tensor_RL\train_stock.py", line 21, in data = episodic_data.load_data("data.pkl",episode=10) File "C:\Study Project (CEERI)\tensor_RL\episodic_data.py", line 71, in load_data data = load_file_data(file) File "C:\Study Project (CEERI)\tensor_RL\episodic_data.py", line 76, in load_file_data data = six.moves.cPickle.load(myFile) UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: ordinal not in range(128)

    opened by codnoob 1
  • Do you want to calculate high + low / 2 for average price?

    Do you want to calculate high + low / 2 for average price?

    episodic_data.py#L39 temp = [data[2], data[3], data[4], data[5],data[8]]

    episodic_data.py#L56 average_price = sum(temp[0:-2]) / float(len(temp[0:-2])) # (high + low + close ) /3 ?

    Do you want to calculate high + low / 2 for average price? If yes, maybe line56 of episodic_data.py should be changed to as below... average_price = sum(temp[1:-3]) / float(len(temp[1:-3]))

    Right?

    image

    opened by aaronzhudp 1
  • Fix broken headings in Markdown files

    Fix broken headings in Markdown files

    GitHub changed the way Markdown headings are parsed, so this change fixes it.

    See bryant1410/readmesfix for more information.

    Tackles bryant1410/readmesfix#1

    opened by bryant1410 0
  • Singleton array array(<map object at 0x00000190A1D75EF0>, dtype=object) cannot be considered a valid collection

    Singleton array array(, dtype=object) cannot be considered a valid collection

    Microsoft Windows [Version 10.0.17134.285] (c) 2018 Microsoft Corporation. All rights reserved. Clink v0.4.9 [git:2fd2c2] Copyright (c) 2012-2016 Martin Ridgers http://mridgers.github.io/clink

    Python 3.6 Anaconda

    D:\Machine Learning\deep_trader-master\deep_trader-master\tensor-reinforcement>python dqn_model.py C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\h5py_init_.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) Traceback (most recent call last): File "dqn_model.py", line 11, in from train_stock import * File "D:\Machine Learning\deep_trader-master\deep_trader-master\tensor-reinforcement\train_stock.py", line 25, in x_train, x_test, y_train, y_test = train_test_split(data, supervised_y_data, test_size=0.10, random_state=123) File "C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\model_selection_split.py", line 1689, in train_test_split arrays = indexable(*arrays) File "C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\utils\validation.py", line 206, in indexable check_consistent_length(*result) File "C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\utils\validation.py", line 177, in check_consistent_length lengths = [_num_samples(X) for X in arrays if X is not None] File "C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\utils\validation.py", line 177, in lengths = [_num_samples(X) for X in arrays if X is not None] File "C:\AnacondaPython3_6\DeepLearning_Udemy\lib\site-packages\sklearn\utils\validation.py", line 126, in _num_samples " a valid collection." % x) TypeError: Singleton array array(<map object at 0x00000190A1D75EF0>, dtype=object) cannot be considered a valid collection.

    opened by GenesisGupta 1
  • UnicodeDecodeError

    UnicodeDecodeError

    when I run it, I encounter the following error:

    Warning (from warnings module): File "C:\Users\ben\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\cross_validation.py", line 41 "This module will be removed in 0.20.", DeprecationWarning) DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. Traceback (most recent call last): File "C:\Users\ben\Documents\python\deep_trader\tensor-reinforcement\dqn_model.py", line 11, in from train_stock import * File "C:\Users\ben\Documents\python\deep_trader\tensor-reinforcement\train_stock.py", line 22, in data = episodic_data.load_data("data.pkl",episode=10) File "C:\Users\ben\Documents\python\deep_trader\tensor-reinforcement\episodic_data.py", line 77, in load_data data = load_file_data(file) File "C:\Users\ben\Documents\python\deep_trader\tensor-reinforcement\episodic_data.py", line 82, in load_file_data data = six.moves.cPickle.load(myFile) UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: ordinal not in range(128)

    Can someone help me out here?

    opened by yeshengyi 5
  • can not install adium-theme-ubuntu in `requirements.txt'

    can not install adium-theme-ubuntu in `requirements.txt'

    Collecting adium-theme-ubuntu==0.3.4 (from -r requirements.txt (line 1))
      Could not find a version that satisfies the requirement adium-theme-ubuntu==0.3.4 (from -r requirements.txt (line 1)) (from versions: )
    No matching distribution found for adium-theme-ubuntu==0.3.4 (from -r requirements.txt (line 1))
    
    opened by dancju 2
  • pg_model vs pg_stock_model

    pg_model vs pg_stock_model

    What is the difference between these two files and which one should I use in order to train a deep gradient policy RL agent ?

    I tried to run pg_model with several NN architecture but I always have a convergence of the reward around 200, have you tried it before ?

    opened by Nicolas99-9 0
  • Tensorflow version?

    Tensorflow version?

    I get the following error when I run dqn_model.py:

    AttributeError: 'module' object has no attribute 'scalar_summary'
    

    What version of tensorflow should I be using with this. I got this error with version 1.2.1.

    opened by MatthewCochrane 1
Owner
Deepender Singla
Works at @niveshi. Before @accredible. Simple and nice guy.
Deepender Singla
A Deep Reinforcement Learning Framework for Stock Market Trading

DQN-Trading This is a framework based on deep reinforcement learning for stock market trading. This project is the implementation code for the two pap

null 61 Jan 1, 2023
Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! Very tiny! Stock Market Financial Technical Analysis Python library . Quant Trading automation or cryptocoin exchange

MyTT Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! to Stock Market Financial Technical Analysis Python

dev 34 Dec 27, 2022
Trading Gym is an open source project for the development of reinforcement learning algorithms in the context of trading.

Trading Gym Trading Gym is an open-source project for the development of reinforcement learning algorithms in the context of trading. It is currently

Dimitry Foures 535 Nov 15, 2022
Only works with the dashboard version / branch of jesse

Jesse optuna Only works with the dashboard version / branch of jesse. The config.yml should be self-explainatory. Installation # install from git pip

Markus K. 8 Dec 4, 2022
This project provides a stock market environment using OpenGym with Deep Q-learning and Policy Gradient.

Stock Trading Market OpenAI Gym Environment with Deep Reinforcement Learning using Keras Overview This project provides a general environment for stoc

Kim, Ki Hyun 769 Dec 25, 2022
Use deep learning, genetic programming and other methods to predict stock and market movements

StockPredictions Use classic tricks, neural networks, deep learning, genetic programming and other methods to predict stock and market movements. Both

Linda MacPhee-Cobb 386 Jan 3, 2023
:boar: :bear: Deep Learning based Python Library for Stock Market Prediction and Modelling

bulbea "Deep Learning based Python Library for Stock Market Prediction and Modelling." Table of Contents Installation Usage Documentation Dependencies

Achilles Rasquinha 1.8k Jan 5, 2023
Technical experimentations to beat the stock market using deep learning :chart_with_upwards_trend:

DeepStock Technical experimentations to beat the stock market using deep learning. Experimentations Deep Learning Stock Prediction with Daily News Hea

Keon 449 Dec 29, 2022
Trading and Backtesting environment for training reinforcement learning agent or simple rule base algo.

TradingGym TradingGym is a toolkit for training and backtesting the reinforcement learning algorithms. This was inspired by OpenAI Gym and imitated th

Yvictor 1.1k Jan 2, 2023
Deep Reinforcement Learning based Trading Agent for Bitcoin

Deep Trading Agent Deep Reinforcement Learning based Trading Agent for Bitcoin using DeepSense Network for Q function approximation. For complete deta

Kartikay Garg 669 Dec 29, 2022
Narya API allows you track soccer player from camera inputs, and evaluate them with an Expected Discounted Goal (EDG) Agent

Narya The Narya API allows you track soccer player from camera inputs, and evaluate them with an Expected Discounted Goal (EDG) Agent. This repository

Paul Garnier 121 Dec 30, 2022
Stock-history-display - something like a easy yearly review for your stock performance

Stock History Display Available on Heroku: https://stock-history-display.herokua

LiaoJJ 1 Jan 7, 2022
A resource for learning about deep learning techniques from regression to LSTM and Reinforcement Learning using financial data and the fitness functions of algorithmic trading

A tour through tensorflow with financial data I present several models ranging in complexity from simple regression to LSTM and policy networks. The s

null 195 Dec 7, 2022
Forecasting directional movements of stock prices for intraday trading using LSTM and random forest

Forecasting directional movements of stock-prices for intraday trading using LSTM and random-forest https://arxiv.org/abs/2004.10178 Pushpendu Ghosh,

Pushpendu Ghosh 270 Dec 24, 2022
Creating Artificial Life with Reinforcement Learning

Although Evolutionary Algorithms have shown to result in interesting behavior, they focus on learning across generations whereas behavior could also be learned during ones lifetime.

Maarten Grootendorst 49 Dec 21, 2022
This is a simple backtesting framework to help you test your crypto currency trading. It includes a way to download and store historical crypto data and to execute a trading strategy.

You can use this simple crypto backtesting script to ensure your trading strategy is successful Minimal setup required and works well with static TP a

Andrei 154 Sep 12, 2022
A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)

gym-mtsim: OpenAI Gym - MetaTrader 5 Simulator MtSim is a simulator for the MetaTrader 5 trading platform alongside an OpenAI Gym environment for rein

Mohammad Amin Haghpanah 184 Dec 31, 2022
NeoPlay is the project dedicated to ESport events.

NeoPlay is the project dedicated to ESport events. On this platform users can participate in tournaments with prize pools as well as create their own tournaments.

null 3 Dec 18, 2021