Here is my python sample for scraping some finance data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
import datetime as dt
from mplfinance.original_flavor import candlestick_ohlc
import warnings
warnings.filterwarnings("ignore")
# fix_yahoo_finance is used to fetch data
import yfinance as yf
yf.pdr_override()
# input
symbol = 'AAPL'
start = '2018-01-01'
end = '2020-12-24'
# Read data
df = yf.download(symbol,start,end)
# View Columns
df.head()
df['Absolute_Return'] = 100 * (df['Adj Close'] - df['Adj Close'].shift(1))/df['Adj Close'].shift(1)
df.head(20)
fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot(2, 1, 1)
ax1.plot(df['Adj Close'])
ax1.set_title('Stock '+ symbol +' Closing Price')
ax1.set_ylabel('Price')
ax2 = plt.subplot(2, 1, 2)
ax2.plot(df['Absolute_Return'] , label='Absolute Return', color='red')
#ax2.axhline(y=0, color='blue', linestyle='--')
#ax2.axhline(y=0.5, color='darkblue')
#ax2.axhline(y=-0.5, color='darkblue')
ax2.grid()
ax2.set_ylabel('Absolute Return')
ax2.set_xlabel('Date')
ax2.legend(loc='best')
Problem is, if I put them in Jupyter notebook and run block by block, everything works fine. But when I put them in a normal .py file then run it, it stops in halfway, right at
df = yf.download(symbol,start,end)
Anyone please explain that for me?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…