Giter VIP home page Giter VIP logo

q-trader's Introduction

Q-Trader

An implementation of Q-learning applied to (short-term) stock trading. The model uses n-day windows of closing prices to determine if the best action to take at a given time is to buy, sell or sit.

As a result of the short-term state representation, the model is not very good at making decisions over long-term trends, but is quite good at predicting peaks and troughs.

Results

Some examples of results on test sets:

!^GSPC 2015 S&P 500, 2015. Profit of $431.04.

BABA_2015 Alibaba Group Holding Ltd, 2015. Loss of $351.59.

AAPL 2016 Apple, Inc, 2016. Profit of $162.73.

GOOG_8_2017 Google, Inc, August 2017. Profit of $19.37.

Running the Code

To train the model, download a training and test csv files from Yahoo! Finance into data/

mkdir model
python train ^GSPC 10 1000

Then when training finishes (minimum 200 episodes for results):

python evaluate.py ^GSPC_2011 model_ep1000

References

Deep Q-Learning with Keras and Gym - Q-learning overview and Agent skeleton code

q-trader's People

Contributors

edwardhdlu 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  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

q-trader's Issues

IndexError in train.py

Hey, could someone help me with my problem? What am i overseeing? :)

stock_name, window_size, episode_count = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])

IndexError: list index out of range

Kernel died, restarting

Minor mistype

Hey Edward...I was going to issue apull request, but it is probably faster to just make the quick edit to the readme yourself. At the bottom you state to "mkdir model" when in-fact I believe it should be "mkdir models" as that is the path your existing code references. Just thought I would let you know. Thanks for putting this together. Very interesting!!

no convergence

after run python train.py GSPC 10 2000 there is non convergence in the P&L results

image

Trying to implement in tfjs... can anyone explain this bit of code?

I'm trying to implement this in tensorflow js but i'm running into some issue with these lines in agent.py:

target_f = self.model.predict(state)
target_f[0][action] = target
self.model.fit(state, target_f, epochs=1, verbose=0)

What does target_f means? final target?

What is target[action] ? Why are we setting target_f to the predict and then assingning it something else?

thanks,
Tiago

Multiple buy before any sell

Hi, I found that there maybe multiple buying if the action == 1 is true, which means the program will record multiple buy before any sell could happen. And there is no limit of number of "buy" could be allowed. This is very different from real life situation. Am my understanding correct? However, when I tried to limit the "buy" to 1 if the system already hold a long position. I found the result during training is not stable and most of the time is negative. Do I miss something? Many thanks in advance.

Some possible optimization

I may be wrong but doesn't
mini_batch.append(self.memory.popleft())
do better job than
mini_batch.append(self.memory[i])
in

def expReplay(self, batch_size):
	mini_batch = []
	l = len(self.memory)
	for i in xrange(l - batch_size + 1, l):
                 mini_batch.append(self.memory[i])

It is much faster too.

error when running training

after i run the training program, I get "the program is running, do you want to kill it?" window. I click "okay" and it dies.

I have apple macbook pro laptop.

any thoughts?

No action at all (zero output) when evaluating a training result.

It is an interesting project, and I tried to it on my computer based on your readme. This is what I did.
mkdir models
python train.py ^GSPC 20 100
python evaluate.py ^GSPC_2011 model_ep100
And I got following output in evaluation

/Users/username/Library/Python/2.7/lib/python/site-packages/h5py/init.py:36: 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
Using TensorFlow backend.
2018-01-17 07:29:54.049861: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA

^GSPC_2011 Total Profit: $0.00

The agent did not do any thing to the test data set...
I know 100 training episodes is not enough to produce meaningful result. But I expect insufficient training would yield some bad strategy to loss money rather than no action at all.

My OS is macOS High Sierra. Do you think it is the problem of python environment or just too few training? Have you had such problem before?
Thanks!

how to draw the graph of the stock

hey~
your code is very interesting i like it
but i want to know how to draw the graph of the stock price
i run the code however it didn't draw the graph as you post on the github

Dead link

There is a dead link at the bottom of the README file, in the reference section.

Training time and async/await

What is the python training time for this? With default settings 10 window and 1000 episodes, on what hardware?

Also, in Python is the fit() sequential? i mean, the code stops until model.fit() is done?

I'm trying to port this to tfjs and the code rushes through the for loop of the episodes and the data length but doesn't "wait" for the model.fit(). Now i'm using async/await for it to wait for the model.fit() but it's taking a great amount of time. Can someone shed some light?

HELP

Standard library module invoked user code during import; breakpoints disabled for invoked code.

Cannot use GPU for training..

Hi, i installed tensorflow-gpu and are able to use GPU for training on other reinforcement training projects (GPU has about 80% or above). However, I tried to specify the GPU but still the utilization of the GPU is still around 5-10%. Does anyone have experience? Thanks in advance.

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
import keras

Regards,
Lam

please sir solve my problem

I understand your code , please give me idea how we plot graph according to x-y axis.

What we are taken in X-axis and Y-axis.

No directory

lines = open("data/" + key + ".csv", "r").read().splitlines()

FileNotFoundError: [Errno 2] No such file or directory: 'data/GSPC.csv'

i got this error

tenroflow 1.10.0
python 3.6.4
w7.
I maked some changes in code couse parenthes was missing

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.