I've written the code for showing MACD indicator graph. what I did is here:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv(#stock_chart)
df = df.rename(columns={"<DTYYYYMMDD>":"Date"})
df['Date'] = pd.to_datetime(df['Date'].astype(str), format="%Y%m%d")
print(df.head())
#seting index for graph
df = df.set_index(df['Date'].values)
print(df.head())
which my dataframe's head is :
#Macd
S_term = 12
L_term = 26
s_ema = df.close.ewm(span=S_term, adjust=False).mean()
l_ema = df.close.ewm(span=L_term, adjust=False).mean()
#calculate MACD
macd = s_ema - l_ema
signal_term = 9
signal_line = macd.ewm(span= signal_term, adjust=False).mean()
#plot the graphs
plt.figure(figsize=(100,10)) #width = 12.2in, height = 4.5
plt.plot(df.index, macd, label='macd', color = 'red')
plt.plot(df.index, signal_line, label='Signal Line', color='blue')
plt.tight_layout()
plt.xticks(rotation=0)
plt.legend(loc='upper left')
plt.show()
finally , I got to this :
Anyway , the problem is that despite row index labels are like this : 2020-08-19 or "%Y%m%d"
, graph labels are incomplete and if I reorder it , the data overlaps on each other . what do I have to do ?