|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Fri Nov 27 08:09:11 2020 |
| 4 | +
|
| 5 | +@author: Tin |
| 6 | +""" |
| 7 | +# Plot Line Chart |
| 8 | +import pandas as pd # Dataframe Library |
| 9 | +import matplotlib.pyplot as plt # Plot Chart Library |
| 10 | + |
| 11 | +pd.set_option('max_columns', None) # To show all columns |
| 12 | + |
| 13 | +import yfinance as yf |
| 14 | +yf.pdr_override() |
| 15 | + |
| 16 | + |
| 17 | +# input |
| 18 | +symbol = 'AAPL' |
| 19 | +start = '2019-01-01' |
| 20 | +end = '2020-01-01' |
| 21 | + |
| 22 | + |
| 23 | +# dataframe |
| 24 | +data = yf.download(symbol,start,end) |
| 25 | + |
| 26 | +data['VolumePositive'] = data['Open'] < data['Adj Close'] |
| 27 | + |
| 28 | +print('Line Chart') |
| 29 | +fig = plt.figure(figsize=(14,10)) |
| 30 | +ax1 = plt.subplot(3, 1, 1) |
| 31 | +ax1.plot(data['Adj Close']) |
| 32 | +ax1.set_title('Stock '+ symbol +' Closing Price') |
| 33 | +ax1.set_ylabel('Price') |
| 34 | +ax1.legend(loc='best') |
| 35 | + |
| 36 | +ax2 = plt.subplot(3, 1, 2) |
| 37 | +ax2.plot(data['Volume'], label='Volume') |
| 38 | +ax2.grid() |
| 39 | +ax2.legend(loc='best') |
| 40 | +ax2.set_ylabel('Volume') |
| 41 | + |
| 42 | +ax3 = plt.subplot(3, 1, 3) |
| 43 | +ax3v = ax3.twinx() |
| 44 | +colors = data.VolumePositive.map({True: 'g', False: 'r'}) |
| 45 | +ax3v.bar(data.index, data['Volume'], color=colors, alpha=0.4) |
| 46 | +ax3.set_ylabel('Volume') |
| 47 | +ax3.grid() |
| 48 | +ax3.set_xlabel('Date') |
| 49 | + |
| 50 | + |
| 51 | +# Candlestick |
| 52 | +print('Candlestick') |
| 53 | +from matplotlib import dates as mdates |
| 54 | +dfc = data.copy() |
| 55 | +dfc['VolumePositive'] = dfc['Open'] < dfc['Adj Close'] |
| 56 | +#dfc = dfc.dropna() |
| 57 | +dfc = dfc.reset_index() |
| 58 | +dfc['Date'] = pd.to_datetime(dfc['Date']) |
| 59 | +dfc['Date'] = dfc['Date'].apply(mdates.date2num) |
| 60 | +dfc.head() |
| 61 | + |
| 62 | +from mpl_finance import candlestick_ohlc |
| 63 | + |
| 64 | +fig = plt.figure(figsize=(14,10)) |
| 65 | +ax1 = plt.subplot(3, 1, 1) |
| 66 | +candlestick_ohlc(ax1,dfc.values, width=0.5, colorup='g', colordown='r', alpha=1.0) |
| 67 | +ax1.xaxis_date() |
| 68 | +ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y')) |
| 69 | +ax1.grid(True, which='both') |
| 70 | +ax1.minorticks_on() |
| 71 | +ax1v = ax1.twinx() |
| 72 | +colors = dfc.VolumePositive.map({True: 'g', False: 'r'}) |
| 73 | +ax1v.bar(dfc.Date, dfc['Volume'], color=colors, alpha=0.4) |
| 74 | +ax1v.axes.yaxis.set_ticklabels([]) |
| 75 | +ax1v.set_ylim(0, 3*data.Volume.max()) |
| 76 | +ax1.set_title('Stock '+ symbol +' Closing Price') |
| 77 | +ax1.set_ylabel('Price') |
| 78 | + |
| 79 | +ax2 = plt.subplot(3, 1, 2) |
| 80 | +ax2.plot(data['Volume'], label='Volume') |
| 81 | +ax2.grid() |
| 82 | +ax2.legend(loc='best') |
| 83 | +ax2.set_ylabel('Volume') |
| 84 | + |
| 85 | +ax3 = plt.subplot(3, 1, 3) |
| 86 | +ax3v = ax3.twinx() |
| 87 | +colors = data.VolumePositive.map({True: 'g', False: 'r'}) |
| 88 | +ax3v.bar(data.index, data['Volume'], color=colors, alpha=0.4) |
| 89 | +ax3.set_ylabel('Volume') |
| 90 | +ax3.grid() |
| 91 | +ax3.set_xlabel('Date') |
0 commit comments