简体   繁体   中英

How to draw r = (20 day moving average / 20 weeks moving average) graph using cufflinks in python 3.x?

I know how to draw MA20 an MA50 using python3 and cufflinks.

import pandas as pd 
import yfinance as yf
df_twtr = yf.download('TWR', 
                       start='2015-01-01', 
                       end='2017-12-31',
                       progress=False,
                       auto_adjust=True)
import cufflinks as cf
from plotly.offline import iplot, init_notebook_mode

cf.go_offline()
init_notebook_mode()
qf = cf.QuantFig(df_twtr, title="TWR Price", 
                 legend='top', name='TWR')
qf.add_volume()
qf.add_sma(periods=20, column='Close', color='red')
qf.add_sma(periods=50, color='green') 
qf.iplot()

How can I draw another metric ie r = (20 day moving average / 20 week moving average) and add them to the same plot? if cufflinks cannot do that, any other method or package is fine for me.

I found the answer. I used pandas to do that. here is the solution just in case somebody needs it:

# get data from yahoo
df = pdr.get_data_yahoo('VOO', start=start_date, end=end_date)

ma50d=df['Close'].rolling(50).mean()
ma50w=df['Close'].rolling(50*7).mean()
ratio1=ma50d.divide(ma50w, fill_value=0)

plt.plot(ratio1,label= 'Voo')
plt.legend(loc='best')
plt.title('VOO')
#plt.yscale("log") #logarithmic plot
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM