All Projects → matlab-deep-learning → reinforcement_learning_financial_trading

matlab-deep-learning / reinforcement_learning_financial_trading

Licence: other
MATLAB example on how to use Reinforcement Learning for developing a financial trading model

Programming Languages

matlab
3953 projects

Projects that are alternatives of or similar to reinforcement learning financial trading

Quarto
A working example of the Quarto board game using Elm and Netlify. An exploration of game development, OSS, and functional programming.
Stars: ✭ 15 (-84.04%)
Mutual labels:  example
SeatLayout
A seat selection library for Android with an example for selecting seats for flights, sports venue, theatres, etc
Stars: ✭ 30 (-68.09%)
Mutual labels:  example
hugo-bare-min-theme
A bare minimum theme for Hugo (https://gohugo.io) to help develop and debug Hugo sites -- https://hugo-bare-min.netlify.com/,
Stars: ✭ 71 (-24.47%)
Mutual labels:  example
play-scala-chatroom-example
Play chatroom with Scala API
Stars: ✭ 43 (-54.26%)
Mutual labels:  example
riot realworld example app
Exemplary real world application built with Riot.js v6 🖐👍
Stars: ✭ 16 (-82.98%)
Mutual labels:  example
scenic asteroids
A toy Asteroids clone written in Elixir with the Scenic UI library
Stars: ✭ 42 (-55.32%)
Mutual labels:  example
PoC-Examples
This repository contains synthesizable examples which use the PoC-Library.
Stars: ✭ 27 (-71.28%)
Mutual labels:  example
Hello-GLUT
A very simple "Hello World!" GLUT application demonstrating how to write OpenGL applications in C with MinGW and MSVC.
Stars: ✭ 27 (-71.28%)
Mutual labels:  example
Discord-Bot-TypeScript-Template
Discord bot - A discord.js bot template written with TypeScript.
Stars: ✭ 86 (-8.51%)
Mutual labels:  example
iOS ARkit2 Multiusers
An example implemented multiplayer experience in ARKit2
Stars: ✭ 19 (-79.79%)
Mutual labels:  example
example-orbitdb-todomvc
TodoMVC with OrbitDB
Stars: ✭ 17 (-81.91%)
Mutual labels:  example
widgets playground
Showcase example for https://github.com/therecipe/qt
Stars: ✭ 50 (-46.81%)
Mutual labels:  example
db2-samples
Db2 application code, configuration samples, and other examples
Stars: ✭ 56 (-40.43%)
Mutual labels:  example
todo-graphql-example
Example Todo app on top of json-graphql-server
Stars: ✭ 20 (-78.72%)
Mutual labels:  example
haxe
Qt binding for Haxe | Showcase example for https://github.com/therecipe/qt
Stars: ✭ 21 (-77.66%)
Mutual labels:  example
Algorithmic-Trading
I have been deeply interested in algorithmic trading and systematic trading algorithms. This Repository contains the code of what I have learnt on the way. It starts form some basic simple statistics and will lead up to complex machine learning algorithms.
Stars: ✭ 47 (-50%)
Mutual labels:  algorithmic-trading
rest-api-endpoints
🌾 WordPress REST API endpoints
Stars: ✭ 31 (-67.02%)
Mutual labels:  example
api-examples
Plesk API-RPC usage examples
Stars: ✭ 79 (-15.96%)
Mutual labels:  example
play-java-ebean-example
Example Play application showing Java with Ebean
Stars: ✭ 54 (-42.55%)
Mutual labels:  example
learning-python
notes and codes while learning python
Stars: ✭ 71 (-24.47%)
Mutual labels:  example

Reinforcement Learning For Financial Trading 📈

How to use Reinforcement learning for financial trading using Simulated Stock Data using MATLAB.

Setup

To run:

  1. Open RL_trading_demo.prj
  2. Open workflow.mlx (MATLAB Live Script preferred) or workflow.m (MATLAB script viewable in GitHub)
  3. Run workflow.mlx

Environment and Reward can be found in: myStepFunction.m

Requires

Overview

The goal of the Reinforcement Learning agent is simple. Learn how to trade the financial markets without ever losing money.

Note, this is different from learn how to trade the market and make the most money possible.

Reinforcement Learning for Financial Trading

Lets apply some of the terminology and concepts of teaching a reinforcement learning agent to trade.

  • The agent in this case study is the computer.
  • It will observe financial market indicators (states).
  • The financial market is the environment.
  • The actions it can take are buy, hold sell.
  • It needs to learn which action to take and when (policy).
  • It learns by understanding what is a good trade or a bad trade via rewards.

Case Study

Our environment consists of 3 stocks, $20000 cash & 15 years of historical data:

Stocks are:

Actions (buy, sell ,hold) for 3 stocks = 27 total actions

The States being observed are:

  • Stocks Owned
  • Price Different when Bought
  • Cash In Hand
  • Price change from yesterday
  • % Price change from 2 days ago
  • % Price change from 7 days ago
  • % Price change from average price of 7 days ago

Strategy

  • Given 3 stocks
  • Try to find the best time to buy, sell, or hold each stock
  • If selling a stock, sell all of it.
  • If buying a stock, buy the maximum allowed given cash in hand.

Reward

The reward system was what took the most time to develop and required several iterations.

The details are listed, but to simplify

A good reward is given when a trade results in a profit and a stock is bought/held/sold at the right time. For example buying a stock on the way up.

The reverse goes for giving a penalty except for one thing.

A large penalty is given if ANY trade of the 3 stocks is determined as a bad trade. WHY? In the first iteration of the reward system, this was not there. What was observed is that the agent quickly learnt the best of the 3 stocks to trade and ignored the other 2.

  • A good reward is given when
    • A selling trade results in profit
    • A selling trade occurs with momentum
      • I.e. selling when prices start to fall -Holding a stock results in the portfolio value increasing
  • A bad reward is given when
    • Selling results in a negative profit
    • A selling trade occurs against momentum
      • I.e. selling when prices are increasing, buying when prices are falling
    • Holding a stock results in the portfolio value decreasing
    • A good reward is overwritten to be bad if any illogical trade occurs
      • I.e. sell a stock you don’t have or buy when you don’t had cash

Training

  • Based 12years of data
  • 3000 episodes
  • ~80hrs to train

Here is an overview of how long it took to learn. You might think 80hrs is a long time. But remember, how long do you think it takes a human to learn how to trade successfully over 12 years?

Results

Highlights

  • 100 Simulations of 3 years:
    • Average profit - ~$30k
    • 0 simulations returned negative
    • But most did not outperform individual stocks

Histogram of 100 Simulations

Best Simulation

Worst Simulation

The histogram shows that for 100 simulations, not once did the agent ever lose money. So the goal was achieved!

However, you can see that the range varies quite a bit. If you inspect the plots on the right, just buying and holding 1 stock would returned a profit just as good if not better than the agent.

BUT – It’s easy to judge retrospectively. The agent was trading each day as it occurred. None of the simulations resulted in a loss.

Further Improvements

The case study did ignore some common things to consider when trading the market. Here a few areas that could improve the performance, and make the trained agent more robust:

  • Include Transaction costs
  • Cover the Hi/Lo spread
  • Refined reward system
  • Compare different agents

Conclusion

The aim of this example was to show:

  • What reinforcement learning is
  • How it can be applied to trading the financial markets
  • Leave a starting point for financial professionals to use and enhance using their own domain expertise.

For more information on Reinforcement Learning in MATLAB:

Free Reinforcement Learning Onramp - No downloads, or installation, just your browser and you!

Download a free trial

Getting Started with Reinforcement Learning (YouTube series)

Copyright 2020 The MathWorks, Inc.

View Reinforcement Learning for Financial Trading on File Exchange

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].