Code Monkey home page Code Monkey logo

trading-bot's Introduction

Overview

This project implements a Stock Trading Bot, trained using Deep Reinforcement Learning, specifically Deep Q-learning. Implementation is kept simple and as close as possible to the algorithm discussed in the paper, for learning purposes.

Introduction

Generally, Reinforcement Learning is a family of machine learning techniques that allow us to create intelligent agents that learn from the environment by interacting with it, as they learn an optimal policy by trial and error. This is especially useful in many real world tasks where supervised learning might not be the best approach due to various reasons like nature of task itself, lack of appropriate labelled data, etc.

The important idea here is that this technique can be applied to any real world task that can be described loosely as a Markovian process.

Approach

This work uses a Model-free Reinforcement Learning technique called Deep Q-Learning (neural variant of Q-Learning). At any given time (episode), an agent abserves it's current state (n-day window stock price representation), selects and performs an action (buy/sell/hold), observes a subsequent state, receives some reward signal (difference in portfolio position) and lastly adjusts it's parameters based on the gradient of the loss computed.

There have been several improvements to the Q-learning algorithm over the years, and a few have been implemented in this project:

  • Vanilla DQN
  • DQN with fixed target distribution
  • Double DQN
  • Prioritized Experience Replay
  • Dueling Network Architectures

Results

Trained on GOOG 2010-17 stock data, tested on 2019 with a profit of $1141.45 (validated on 2018 with profit of $863.41):

Google Stock Trading episode

You can obtain similar visualizations of your model evaluations using the notebook provided.

Some Caveats

  • At any given state, the agent can only decide to buy/sell one stock at a time. This is done to keep things as simple as possible as the problem of deciding how much stock to buy/sell is one of portfolio redistribution.
  • The n-day window feature representation is a vector of subsequent differences in Adjusted Closing price of the stock we're trading followed by a sigmoid operation, done in order to normalize the values to the range [0, 1].
  • Training is prefferably done on CPU due to it's sequential manner, after each episode of trading we replay the experience (1 epoch over a small minibatch) and update model parameters.

Data

You can download Historical Financial data from Yahoo! Finance for training, or even use some sample datasets already present under data/.

Getting Started

In order to use this project, you'll need to install the required python packages:

pip3 install -r requirements.txt

Now you can open up a terminal and start training the agent:

python3 train.py data/GOOG.csv data/GOOG_2018.csv --strategy t-dqn

Once you're done training, run the evaluation script and let the agent make trading decisions:

python3 eval.py data/GOOG_2019.csv --model-name model_GOOG_50 --debug

Now you are all set up!

Acknowledgements

References

trading-bot's People

Contributors

pskrunner14 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

trading-bot's Issues

Updated?

Is this project updated? If it isn't, do you guys know any alternatives?

Insatlling

Hey, I am at second as per written in the readme file but I am getting this error, Can you tell what is this and why is it? How can I resolve it?

versions of my libraries are all up to date as per requirements, I can't understand why it is not working???
altair 4.1.0 coloredlogs 15.0.1 docopt 0.6.2 keras 2.6.0 jupyterlab 3.1.13 numpy 1.19.5 pandas 1.3.2t seaborn 0.11.2 tensorflow 2.6.0 tqdm 4.62.2

When I try to write python train.py data/GOOG.csv data/GOOG_2018.csv --strategy t-dqn on terminal it shows this error
Look at these Images....
image_1

image_2

Error loading script: Script error for "vega-embed" http://requirejs.org/docs/errors.html#scripterror in visualize.ipynb

I ran all the cells from visualize.ipynb, on the last cell it's giving me an error
Last cell: -

chart = visualize(df, history, title=test_stock)
chart

error: -

Error loading script: Script error for "vega-embed" http://requirejs.org/docs/errors.html#scripterror

Something to do with vega-embed, I can't find what is causing the problem. I also tried the URL
http://requirejs.org/docs/errors.html#scripterror it showing something related to javascript. I don't know how to debug this error.

SCRIPT ERROR
§ 7
This occurs when the script.onerror function is triggered in a browser. This usually means there is a JavaScript syntax error or other execution problem running the script. To fix it, examine the script that generated the error in a script debugger.

This error may not show up in IE, just other browsers, and instead, in IE you may see the No define call for ... error when you see "Script error". This is due to IE's quirks in detecting script errors.

Agent.n_iter

I didn't see Agent.n_iter being updated, is that a bug?

Evaluation error

Anytime I run the python3 eval.py data/GOOG_2019.csv --model-name model_GOOG_50 --debug , it shows

No file or directory found at models/model

Why last layer dont use any activation?

Hello, first of all, thanks for sharing.

I was looking your model and I saw something strange. Why are you not using any activation in the last layer?
This problem looks like a classification problem, the bot can choose just one option (sit, buy or sell), so using softmax activation should be better? What do you think?

Allow extra features in training/evaluating

Currently only the Adj Close price is used. The price files that I use already contains extra features like RSI and MA and MACD.

The get_stock_data() method currently only returns the Adj Close price. Would it be possible to add the other features, like RSI and MACD etc ?

New complementary tool

My name is Luis, I'm a big-data machine-learning developer, I'm a fan of your work, and I usually check your updates.

I was afraid that my savings would be eaten by inflation. I have created a powerful tool that based on past technical patterns (volatility, moving averages, statistics, trends, candlesticks, support and resistance, stock index indicators).
All the ones you know (RSI, MACD, STOCH, Bolinger Bands, SMA, DEMARK, Japanese candlesticks, ichimoku, fibonacci, williansR, balance of power, murrey math, etc) and more than 200 others.

The tool creates prediction models of correct trading points (buy signal and sell signal, every stock is good traded in time and direction).
For this I have used big data tools like pandas python, stock market libraries like: tablib, TAcharts ,pandas_ta... For data collection and calculation.
And powerful machine-learning libraries such as: Sklearn.RandomForest , Sklearn.GradientBoosting, XGBoost, Google TensorFlow and Google TensorFlow LSTM.

With the models trained with the selection of the best technical indicators, the tool is able to predict trading points (where to buy, where to sell) and send real-time alerts to Telegram or Mail. The points are calculated based on the learning of the correct trading points of the last 2 years (including the change to bear market after the rate hike).

I think it could be useful to you, to improve, I would like to share it with you, and if you are interested in improving and collaborating I am also willing, and if not file it in the box.

optimization to train faster

/usr/local/lib/python3.5/dist-packages/keras/callbacks/callbacks.py:95: RuntimeWarning: Method (on_train_batch_end) is slow compared to the batch update (0.128259). Check your callbacks.

availabilities

Hi,

Have you ever thought about implementing this system within the world of cryptocurrency?
If not, I will try so during the following weeks. Thanks for this trading-bot

Epochs

Maybe it's me - but how can I reduce the number of Epochs - it runs forever!

Where does the model come from?

Hi, I'm just trying to understand how the model is created I understand that you're repo comes with multiple models for GOOG with different episodes but how were these created? It seems for me after running train.py in the exact way you did except with data for SPY no model is instantiated.

QUESTION - how to save new model?

QUESTION - how to save new model?
when i run a train.py, where it's saved the trained agent/model? i see some file already present in model but i need to made my own

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.