Git Product home page Git Product logo

teach-machine-to-trade's Introduction

Teach Machine to Trade

This repo has code for the post: Teach Machine to Trade

Dependencies

Python 2.7. To install all the libraries, run pip install -r requirements.txt

Table of content

  • agent.py: a Deep Q learning agent
  • envs.py: a simple 3-stock trading environment
  • model.py: a multi-layer perceptron as the function approximator
  • utils.py: some utility functions
  • run.py: train/test logic
  • requirement.txt: all dependencies
  • data/: 3 csv files with IBM, MSFT, and QCOM stock prices from Jan 3rd, 2000 to Dec 27, 2017 (5629 days). The data was retrieved using Alpha Vantage API

How to run

To train a Deep Q agent, run python run.py --mode train. There are other parameters and I encourage you look at the run.py script. After training, a trained model as well as the portfolio value history at episode end would be saved to disk.

To test the model performance, run python run.py --mode test --weights <trained_model>, where <trained_model> points to the local model weights file. Test data portfolio value history at episode end would be saved to disk.

teach-machine-to-trade's People

Contributors

shuaiw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

teach-machine-to-trade's Issues

TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'

While trying to run the code: https://github.com/ShuaiW/teach-machine-to-trade/blob/master/run.py
with: python run.py --mode train

I get the error:

Traceback (most recent call last):
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 141, in make_shape
shape = tensor_shape.as_shape(v)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 946, in as_shape
return TensorShape(shape)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 541, in init
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 541, in
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 482, in as_dimension
return Dimension(value)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 37, in init
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "run.py", line 38, in
agent = DQNAgent(state_size, action_size)
File "C:\Users\i\Q-Learning-for-Trading-master\agent.py", line 18, in init
self.model = mlp(state_size, action_size)
File "C:\Users\i\Q-Learning-for-Trading-master\model.py", line 11, in mlp
model.add(Dense(n_neuron_per_layer, input_dim=n_obs, activation=activation))
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 161, in add
name=layer.name + '_input')
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
input_tensor=tensor)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\input_layer.py", line 87, in init
name=self.name)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 517, in placeholder
x = tf.placeholder(dtype, shape=shape, name=name)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1734, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5925, in placeholder
shape = _execute.make_shape(shape, "shape")
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 143, in make_shape
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'.


I am using python 3.7 instead of python 2.7

Thanks in advance

IndexError: tuple index out of range

I have met an error as follows:

File "C:\Users\User\Desktop\research_project\run.py", line 63, in
next_state, reward, done, info = env._step(action)
File "C:\Users\User\Desktop\research_project\env.py", line 73, in _step
self._trade(action)
File "C:\Users\User\Desktop\research_project\env.py", line 97, in _trade
action_vec = action_combo[action]
IndexError: tuple index out of range

May I know how to fix it?

Probable "look-ahead" by using StandardScaler on total test data

Hi Shuai,

thanks for your time preparing and sharing this nice example of RL applied to finance. I'm learning a lot by exploring the code.

I've noticed that during data pre-processing you normalize the data using StandardScaler. Two questions come to my mind:

  1. Are you aware that by creating a StandardScaler based on min-max over all the test data you are introducing "look-ahead", meaning your model will include information that in production mode would not be available to you?

  2. It feels quite strange that you normalize the number of stocks owned by some "magic number" as you write in the code. Is there any rationale behind this number?

Thanks a lot!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.