{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Plotting financial OHLCV ticker data with the matplotlib [mplfinance](https://github.com/matplotlib/mplfinance) package\n`mplfinance` provides matplotlib utilities for the visualization, and visual analysis, of financial data.\nHere we shall plot a [candlestick chart](https://en.wikipedia.org/wiki/Candlestick_chart), along with volumes (V), and different moving averages. The colour of the candlestick indicates the trend of the OHLC (Open, High, Low and Close) data:\n\n![](https://raw.githubusercontent.com/Carl-McBride-Ellis/images_for_kaggle/main/candlestick_image_small.png)","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom datetime import date","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-10T15:03:15.288004Z","iopub.execute_input":"2021-11-10T15:03:15.288401Z","iopub.status.idle":"2021-11-10T15:03:15.316644Z","shell.execute_reply.started":"2021-11-10T15:03:15.288287Z","shell.execute_reply":"2021-11-10T15:03:15.315762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Install the `mplfinance` package","metadata":{}},{"cell_type":"code","source":"!pip -q install mplfinance\nimport mplfinance as mpf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-10T15:03:15.321598Z","iopub.execute_input":"2021-11-10T15:03:15.322016Z","iopub.status.idle":"2021-11-10T15:03:26.304678Z","shell.execute_reply.started":"2021-11-10T15:03:15.321984Z","shell.execute_reply":"2021-11-10T15:03:26.303766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"read in the data provided with the kaggle [G-Research Crypto Forecasting](https://www.kaggle.com/c/g-research-crypto-forecasting) competition","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/g-research-crypto-forecasting/train.csv\")\nasset_details = pd.read_csv(\"../input/g-research-crypto-forecasting/asset_details.csv\")\n\nmapping = dict(asset_details[['Asset_ID', 'Asset_Name']].values)\ntrain[\"Asset name\"] = train[\"Asset_ID\"].map(mapping)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:03:26.306514Z","iopub.execute_input":"2021-11-10T15:03:26.306766Z","iopub.status.idle":"2021-11-10T15:04:26.747774Z","shell.execute_reply.started":"2021-11-10T15:03:26.306736Z","shell.execute_reply":"2021-11-10T15:04:26.746946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### We shall take a look at Bitcoin","metadata":{}},{"cell_type":"code","source":"bitcoin = train.query(\"Asset_ID == 1\").reset_index(drop = True)\nbitcoin['timestamp'] = pd.to_datetime(bitcoin['timestamp'], unit='s')\nbitcoin = bitcoin.set_index('timestamp')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:04:26.749165Z","iopub.execute_input":"2021-11-10T15:04:26.749457Z","iopub.status.idle":"2021-11-10T15:04:27.323946Z","shell.execute_reply.started":"2021-11-10T15:04:26.749420Z","shell.execute_reply":"2021-11-10T15:04:27.323186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"take a quick look at our dataframe","metadata":{}},{"cell_type":"code","source":"bitcoin","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:04:27.326409Z","iopub.execute_input":"2021-11-10T15:04:27.326735Z","iopub.status.idle":"2021-11-10T15:04:27.358280Z","shell.execute_reply.started":"2021-11-10T15:04:27.326695Z","shell.execute_reply":"2021-11-10T15:04:27.357431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"where\n\n* `timestamp` - A timestamp for the minute covered by the row.\n* `Asset_ID` - An ID code for the cryptoasset.\n* `Count` - The number of trades that took place this minute (sometimes simply called trades, or T)\n* `Open` - The USD price at the beginning of the minute.\n* `High` - The highest USD price during the minute.\n* `Low` - The lowest USD price during the minute.\n* `Close` - The USD price at the end of the minute.\n* `Volume` - The number of cryptoasset units traded during the minute.\n* `VWAP` - The volume weighted average price for the minute.\n* `Target` - 15 minute residualized returns. \n \n### Example: Plotting the morning of Wednesday the 14th of April 2021\nWe shall select data from 05:45 a.m. until 07:00 a.m. on Wednesday the 14th of April 2021","metadata":{}},{"cell_type":"code","source":"wednesday_morning = bitcoin.loc['2021-04-14 05:45:00':'2021-04-14 07:00:00']","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:04:27.359556Z","iopub.execute_input":"2021-11-10T15:04:27.360229Z","iopub.status.idle":"2021-11-10T15:04:27.406029Z","shell.execute_reply.started":"2021-11-10T15:04:27.360183Z","shell.execute_reply":"2021-11-10T15:04:27.405290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now produce our plot","metadata":{}},{"cell_type":"code","source":"mpf.plot(wednesday_morning, # the dataframe containing the OHLC (Open, High, Low and Close) data\n         type='candle', # use candlesticks \n         volume=True, # also show the volume\n         mav=(3,6,9), # use three different moving averages\n         figratio=(3,1), # set the ratio of the figure\n         style='yahoo',  # choose the yahoo style\n         title='Bitcoin on Wednesday morning');","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:04:27.407615Z","iopub.execute_input":"2021-11-10T15:04:27.407917Z","iopub.status.idle":"2021-11-10T15:04:28.105867Z","shell.execute_reply.started":"2021-11-10T15:04:27.407879Z","shell.execute_reply":"2021-11-10T15:04:28.105150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### How are the daily markets at the moment? \nLet us take a quick look at how things are going at the moment using the *Yahoo! Finance market data downloader*, [yfinance](https://github.com/ranaroussi/yfinance), to get the daily data","metadata":{}},{"cell_type":"code","source":"!pip -q install yfinance\nimport yfinance as yf","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-10T15:04:28.107298Z","iopub.execute_input":"2021-11-10T15:04:28.107663Z","iopub.status.idle":"2021-11-10T15:04:41.087794Z","shell.execute_reply.started":"2021-11-10T15:04:28.107633Z","shell.execute_reply":"2021-11-10T15:04:41.086700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain the Bitcoin ticker in USD\nbitCoinUSD = yf.Ticker(\"BTC-USD\")\n# save the historical market data to a dataframe\nbitCoinUSD_values = bitCoinUSD.history(start=\"2021-09-21\")\n\n# now plot. For plotting styles see: https://github.com/matplotlib/mplfinance/blob/master/examples/styles.ipynb\nmpf.plot(bitCoinUSD_values,type='candle',volume=True,figratio=(3,1),style='yahoo', title='Bitcoin (USD) from: 21 September 2021');","metadata":{"execution":{"iopub.status.busy":"2021-11-10T15:07:16.565175Z","iopub.execute_input":"2021-11-10T15:07:16.565529Z","iopub.status.idle":"2021-11-10T15:07:17.544656Z","shell.execute_reply.started":"2021-11-10T15:07:16.565491Z","shell.execute_reply":"2021-11-10T15:07:17.543962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The tickers for the cryptocurrencies pertinent to this competition are:\n\n| cryptocurrency | Ticker |\n| --- | --- |\n| Bitcoin Cash     | `BCH-USD` |\n| Binance Coin     | `BNB-USD` |\n| Bitcoin          | `BTC-USD` |\n| EOS.IO           | `EOS-USD` |\n| Ethereum Classic | `ETC-USD` |\n| Ethereum         | `ETH-USD` |\n| Litecoin         | `LTC-USD` |\n| Monero           | `XMR-USD` |\n| TRON             | `TRX-USD` |\n| Stellar          | `XLM-USD` |\n| Cardano          | `ADA-USD` |\n| IOTA             | `MIOTA-USD` |\n| Maker            | `MKR-USD` |\n| Dogecoin         | `DOGE-USD` |\n\n\n### [What happened on 2019-10-16 and 2019-10-23?](https://www.kaggle.com/c/g-research-crypto-forecasting/discussion/285836)\nTaking a look at the dates raised by [Lucas Morin](https://www.kaggle.com/lucasmorin), along with the links by [Alirio Arévalo](https://www.kaggle.com/frogeater):\n\n### [\"Bitcoin Price Falls Under $8K With ‘Ugly’ Scenarios on All Timeframes\"](https://cointelegraph.com/news/bitcoin-price-falls-under-8k-with-ugly-scenarios-on-all-timeframes)","metadata":{}},{"cell_type":"code","source":"october_16 = bitcoin.loc['2019-10-16 06:00:00':'2019-10-16 18:30:00']\nmpf.plot(october_16, # the dataframe containing the OHLC (Open, High, Low and Close) data\n         type='candle', # use candlesticks \n         volume=True, # also show the volume\n         mav=(3,6,9), # use three different moving averages\n         figratio=(3,1), # set the ratio of the figure\n         style='yahoo',  # choose the yahoo style\n         title='2019-10-16: Bitcoin Price Falls Under $8K');","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-10T15:04:42.305559Z","iopub.execute_input":"2021-11-10T15:04:42.308394Z","iopub.status.idle":"2021-11-10T15:04:43.503365Z","shell.execute_reply.started":"2021-11-10T15:04:42.308341Z","shell.execute_reply":"2021-11-10T15:04:43.502524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### [\"3 Likely Reasons Why Bitcoin Price Crashed — And What’s Next for BTC?\"](https://cointelegraph.com/news/3-likely-reasons-why-bitcoin-price-crashed-and-whats-next-for-btc)","metadata":{}},{"cell_type":"code","source":"october_23 = bitcoin.loc['2019-10-23 10:30:00':'2019-10-23 17:00:00']\nmpf.plot(october_23, # the dataframe containing the OHLC (Open, High, Low and Close) data\n         type='candle', # use candlesticks \n         volume=True, # also show the volume\n         mav=(3,6,9), # use three different moving averages\n         figratio=(3,1), # set the ratio of the figure\n         style='yahoo',  # choose the yahoo style\n         title='2019-10-23: Bitcoin Price Crashed');","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-10T15:04:43.505980Z","iopub.execute_input":"2021-11-10T15:04:43.506319Z","iopub.status.idle":"2021-11-10T15:04:44.487976Z","shell.execute_reply.started":"2021-11-10T15:04:43.506270Z","shell.execute_reply":"2021-11-10T15:04:44.487100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Links\n* [mplfinance](https://github.com/matplotlib/mplfinance) on GitHub\n\n### Related notebooks\n* [Probabilistic forecasting using GluonTS: Bitcoin](https://www.kaggle.com/carlmcbrideellis/probabilistic-forecasting-using-gluonts-bitcoin) - rather than providing a single point prediction, also provides a probability distribution as the outcome.\n* [LSTM time series + stock price prediction = FAIL](https://www.kaggle.com/carlmcbrideellis/lstm-time-series-stock-price-prediction-fail) - even though  LSTM (Long short-term memory) artificial neural networks are great, if the data is a random walk, then predictions will fail.\n* [Time series: A simple moving average (MA) model](https://www.kaggle.com/carlmcbrideellis/time-series-a-simple-moving-average-ma-model) - calculate the simple rolling average, which is the mean of the last *n* values, one of the simplest time series models. (Uses `statsmodels.tsa.arima.model`)\n* [Time series decomposition: Naive example](https://www.kaggle.com/carlmcbrideellis/time-series-decomposition-naive-example) - decomposing a time series into three components; the overall trend, the seasonal component, and finally a noise, or residual, term. (Uses `seasonal_decompose` from `statsmodels.tsa.seasonal`)\n* [S&P 500 daily returns: Normal and Cauchy fits](https://www.kaggle.com/carlmcbrideellis/s-p-500-daily-returns-normal-and-cauchy-fits)\n* [Interrupted time series analysis: Causal Impact](https://www.kaggle.com/carlmcbrideellis/interrupted-time-series-analysis-causal-impact)\n* [Causal Forests Double ML example using EconML](https://www.kaggle.com/carlmcbrideellis/causal-forests-double-ml-example-using-econml)","metadata":{}}]}