GithubHelp home page GithubHelp logo

worthmining / deep-reinforcement-learning-for-stock-trading-ddpg-algorithm-nips-2018 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ai4finance-foundation/deep-reinforcement-learning-for-stock-trading-ddpg-algorithm-nips-2018

0.0 0.0 0.0 23.81 MB

Practical Deep Reinforcement Learning Approach for Stock Trading. NeurIPS 2018 AI in Finance.

License: MIT License

Jupyter Notebook 29.24% Python 70.76%

deep-reinforcement-learning-for-stock-trading-ddpg-algorithm-nips-2018's Introduction

Practical Deep Reinforcement Learning Approach for Stock Trading

Prerequisites

Python 3.6+ envrionment

Step 1: Install OpenAI Baselines System Packages OpenAI Instruction

Ubuntu

sudo apt-get update && sudo apt-get install cmake libopenmpi-dev python3-dev zlib1g-dev

Mac OS X

Installation of system packages on Mac requires Homebrew. With Homebrew installed, run the following:

brew install cmake openmpi

Step 2: Create and Activate Virtual Environment

Clone the repository to folder /DQN-DDPG_Stock_Trading:

git clone https://github.com/hust512/DQN-DDPG_Stock_Trading.git
cd DQN-DDPG_Stock_Trading

Under folder /DQN-DDPG_Stock_Trading, create a virtual environment

pip install virtualenv

Virtualenvs are essentially folders that have copies of python executable and all python packages. Create a virtualenv called venv under folder /DQN-DDPG_Stock_Trading/venv

virtualenv -p python3 venv

To activate a virtualenv:

source venv/bin/activate

Step 3: Install openAI gym environment under this virtual environment: venv

Tensorflow versions

The master branch supports Tensorflow from version 1.4 to 1.14. For Tensorflow 2.0 support, please use tf2 branch. Refer to TensorFlow installation guide for more details.

  • Install gym and tensorflow packages:
    pip install gym
    pip install gym[atari] 
    pip install tensorflow==1.14
  • Other packages that might be missing:
    pip install filelock
    pip install matplotlib
    pip install pandas

Step 4: Download and Install Official Baseline Package

  • Clone the baseline repository to folder DQN-DDPG_Stock_Trading/baselines:

    git clone https://github.com/openai/baselines.git
    cd baselines
  • Install baselines package

    pip install -e .

Step 5: Testing the installation

Run all unit tests in baselines:

pip install pytest
pytest

A result like '94 passed, 49 skipped, 72 warnings in 355.29s' is expected. Check the OpenAI baselines Issues or stackoverflow if fixes on failed tests are needed.

Step 6: Test OpenAI Atari Pong game

If this works then it's ready to implement the stock trading application

python -m baselines.run --alg=ppo2 --env=PongNoFrameskip-v4 --num_timesteps=1e4 --load_path=~/models/pong_20M_ppo2 --play

A mean reward per episode around 20 is expected.

Step 7: Register Stock Trading Environment under gym

Register the RLStock-v0 environment in folder /DQN-DDPG_Stock_Trading/venv: From

DQN-DDPG_Stock_Trading/gym/envs/__init__.py

Copy following:

register(
    id='RLStock-v0',
    entry_point='gym.envs.rlstock:StockEnv',
)
register(
    id='RLTestStock-v0',
    entry_point='gym.envs.rlstock:StockTestEnv',
)

into the venv gym environment:

/DQN-DDPG_Stock_Trading/venv/lib/python3.6/site-packages/gym/envs/__init__.py

Step 8: Build Stock Trading Environment under gym

  • Copy folder
DQN_Stock_Trading/gym/envs/rlstock

into the venv gym environment folder:

/DQN-DDPG_Stock_Trading/venv/lib/python3.6/site-packages/gym/envs
  • Open
/DQN-DDPG_Stock_Trading/venv/lib/python3.6/site-packages/gym/envs/rlstock/rlstock_env.py 
/DQN-DDPG_Stock_Trading/venv/lib/python3.6/site-packages/gym/envs/rlstock/rlstock_testenv.py

change the import data path in these two files (cd into the rlstock folder and pwd to check the folder path).

Baseline

Replace

/DQN-DDPG_Stock_Trading/baselines/baselines/run.py

with

/DQN-DDPG_Stock_Trading/run.py

Step 9: Training model and Testing

Pre-steps:

Go to folder

/DQN-DDPG_Stock_Trading/

Activate the virtual environment

source venv/bin/activate

Go to the baseline folder

/DQN-DDPG_Stock_Trading/baselines

Train

To train the model, run this

python -m baselines.run --alg=ddpg --env=RLStock-v0 --network=mlp --num_timesteps=1e4

Trade

To see the testing/trading result, run this

python -m baselines.run --alg=ddpg --env=RLStock-v0 --network=mlp --num_timesteps=2e4 --play

The result images are under folder /DQN-DDPG_Stock_Trading/baselines.

(You can tune the hyperparameter num_timesteps to better train the model, note that if this number is too high, then you will face an overfitting problem, if it's too low, then you will face an underfitting problem.)

Compare to our result:

Some Other Commands May Need:

pip3 install opencv-python
pip3 install lockfile
pip3 install -U numpy
pip3 install mujoco-py==0.5.7

Please cite the following paper

Xiong, Z., Liu, X.Y., Zhong, S., Yang, H. and Walid, A., 2018. Practical deep reinforcement learning approach for stock trading, NeurIPS 2018 AI in Finance Workshop.

deep-reinforcement-learning-for-stock-trading-ddpg-algorithm-nips-2018's People

Contributors

bruceyanghy avatar chenqian0168 avatar yangletliu avatar yinchuanll avatar

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.