Path: blob/master/examples/Stock_NeurIPS2018_1_Data.ipynb
726 views
Stock NeurIPS2018 Part 1. Data
This series is a reproduction of paper the process in the paper Practical Deep Reinforcement Learning Approach for Stock Trading.
This is the first part of the NeurIPS2018 series, introducing how to use FinRL to fetch and process data that we need for ML/RL trading.
Other demos can be found at the repo of FinRL-Tutorials.
Part 1. Install Packages
Part 2. Fetch data
yfinance is an open-source library that provides APIs fetching historical data form Yahoo Finance. In FinRL, we have a class called YahooDownloader that use yfinance to fetch data from Yahoo Finance.
OHLCV: Data downloaded are in the form of OHLCV, corresponding to open, high, low, close, volume, respectively. OHLCV is important because they contain most of numerical information of a stock in time series. From OHLCV, traders can get further judgement and prediction like the momentum, people's interest, market trends, etc.
Data for a single ticker
Here we provide two ways to fetch data with single ticker, let's take Apple Inc. (AAPL) as an example.
Using yfinance
[*********************100%***********************] 1 of 1 completed
Using FinRL
In FinRL's YahooDownloader, we modified the data frame to the form that convenient for further data processing process. We use adjusted close price instead of close price, and add a column representing the day of a week (0-4 corresponding to Monday-Friday).
[*********************100%***********************] 1 of 1 completed
Shape of DataFrame: (20, 8)
Data for the chosen tickers
Part 3: Preprocess Data
We need to check for missing data and do feature engineering to convert the data point into a state.
Adding technical indicators. In practical trading, various information needs to be taken into account, such as historical prices, current holding shares, technical indicators, etc. Here, we demonstrate two trend-following technical indicators: MACD and RSI.
Adding turbulence index. Risk-aversion reflects whether an investor prefers to protect the capital. It also influences one's trading strategy when facing different market volatility level. To control the risk in a worst-case scenario, such as financial crisis of 2007–2008, FinRL employs the turbulence index that measures extreme fluctuation of asset price.
Hear let's take MACD as an example. Moving average convergence/divergence (MACD) is one of the most commonly used indicator showing bull and bear market. Its calculation is based on EMA (Exponential Moving Average indicator, measuring trend direction over a period of time.)
Part 4: Save the Data
Split the data for training and trading
Save data to csv file
For Colab users, you can open the virtual directory in colab and manually download the files.
For users running on your local environment, the csv files should be at the same directory of this notebook.