def loadStock(stock):
'''
Use this to dynamically pull a stock:
'''
stockFile =[]
try:
print('Currently Pulling',stock)
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
print('URL', urlToVisit)
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
return stockFile
python类finance()的实例源码
def loadStock(stock):
'''
Use this to dynamically pull a stock:
'''
stockFile =[]
try:
print('Currently Pulling',stock)
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
print('URL', urlToVisit)
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
return stockFile
def loadStock(stock):
'''
Use this to dynamically pull a stock:
'''
stockFile =[]
try:
print('Currently Pulling',stock)
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
print('URL', urlToVisit)
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
return stockFile
def plot_p(df):
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc
fig, ax = plt.subplots()
candlestick2_ohlc(ax,
df['price_open'].values,
df['price_high'].values,
df['price_low'].values,
df['price_close'].values,
width=0.6,
colorup='g',
colordown='r',
alpha=1)
plt.show()
print('Done.')
def save_to_file(df, filename):
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc
fig, ax = plt.subplots()
candlestick2_ohlc(ax,
df['price_open'].values,
df['price_high'].values,
df['price_low'].values,
df['price_close'].values,
width=0.6,
colorup='g',
colordown='r',
alpha=1)
plt.savefig(filename)
plt.close(fig)
def weekday_candlestick(ohlc_data, ax, fmt='%b %d', freq=7, **kwargs):
""" Wrapper function for matplotlib.finance.candlestick_ohlc
that artificially spaces data to avoid gaps from weekends
??????????
fmt: ????
freq: ???????
"""
# Convert data to numpy array
ohlc_data_arr = np.array(ohlc_data)
ohlc_data_arr2 = np.hstack(
[np.arange(ohlc_data_arr[:,0].size)[:,np.newaxis], ohlc_data_arr[:,1:]])
ndays = ohlc_data_arr2[:,0] # array([0, 1, 2, ... n-2, n-1, n])
# Convert matplotlib date numbers to strings based on `fmt`
dates = mdates.num2date(ohlc_data_arr[:,0])
date_strings = []
for date in dates:
date_strings.append(date.strftime(fmt))
# Plot candlestick chart
mpf.candlestick_ohlc(ax, ohlc_data_arr2, **kwargs)
# Format x axis
ax.set_xticks(ndays[::freq])
ax.set_xticklabels(date_strings[::freq], rotation=45, ha='right')
ax.set_xlim(ndays.min(), ndays.max())