0
我正在嘗試使用來自雅虎的每月庫存數據來分析模式。出於某種原因,該計劃在特定股票(ATVI)的數據框中吐出的每月回報與實際雅虎網站的回報不匹配。我比較了2015年的月度回報,並列出了平均增加和減少的列數以及每個數字的發生次數。Python - Pandas Dataframe - 與數據源不匹配
雅虎鏈接:https://finance.yahoo.com/q/hp?s=ATVI&a=00&b=1&c=2015&d=11&e=31&f=2015&g=m
我的代碼:
from datetime import datetime
from pandas_datareader import data, wb
import pandas_datareader.data as web
import pandas as pd
from pandas_datareader._utils import RemoteDataError
import csv
import sys
import os
import time
class MonthlyChange(object):
months = { 0:'JAN', 1:'FEB', 2:'MAR', 3:'APR', 4:'MAY',5:'JUN', 6:'JUL', 7:'AUG', 8:'SEP', 9:'OCT',10:'NOV', 11:'DEC' }
def __init__(self,month):
self.month = MonthlyChange.months[month-1]
self.sum_of_pos_changes=0
self.sum_of_neg_changes=0
self.total_neg=0
self.total_pos=0
def add_change(self,change):
if change < 0:
self.sum_of_neg_changes+=change
self.total_neg+=1
elif change > 0:
self.sum_of_pos_changes+=change
self.total_pos+=1
def get_data(self):
if self.total_pos == 0:
return (self.month,0.0,0,self.sum_of_neg_changes/self.total_neg,self.total_neg)
elif self.total_neg == 0:
return (self.month,self.sum_of_pos_changes/self.total_pos,self.total_pos,0.0,0)
else:
return (self.month,self.sum_of_pos_changes/self.total_pos,self.total_pos,self.sum_of_neg_changes/self.total_neg,self.total_neg)
for ticker in ['ATVI']:
try:
data = web.DataReader(ticker.strip('\n'), "yahoo", datetime(2015,01,1), datetime(2015,12,31))
data['ymd'] = data.index
year_month = data.index.to_period('M')
data['year_month'] = year_month
first_day_of_months = data.groupby(["year_month"])["ymd"].min()
first_day_of_months = first_day_of_months.to_frame().reset_index(level=0)
last_day_of_months = data.groupby(["year_month"])["ymd"].max()
last_day_of_months = last_day_of_months.to_frame().reset_index(level=0)
fday_open = data.merge(first_day_of_months,on=['ymd'])
fday_open = fday_open[['year_month_x','Open']]
lday_open = data.merge(last_day_of_months,on=['ymd'])
lday_open = lday_open[['year_month_x','Open']]
fday_lday = fday_open.merge(lday_open,on=['year_month_x'])
monthly_changes = {i:MonthlyChange(i) for i in range(1,13)}
for index,ym, openf,openl in fday_lday.itertuples():
month = ym.strftime('%m')
month = int(month)
diff = (openf-openl)/openf
monthly_changes[month].add_change(diff)
changes_df = pd.DataFrame([monthly_changes[i].get_data() for i in monthly_changes],columns=["Month","Avg Inc.","Inc","Avg.Dec","Dec"])
print ticker
print changes_df
我在數據幀相匹配的回報我會從中獲取想要的平均增長/平均跌幅列使用上面張貼的網站的每月數據。 Return =(open-close)/ open – RageAgainstheMachine
請參閱'Open' /'Close'的平均差異更新。 – Stefan
@ Stefan,謝謝你的幫助。說實話,我不知道把你的代碼放到哪裏。是否有可能修改我發佈的代碼?另外,我希望根據當月第一天的開盤和本月最後一天的收盤價得到回報(對不起,如果以前不清楚) return =(上一天關閉 - 第一天開放)/第一天開放 然後取每個月的平均值 - >如果期限爲2年,那麼(2014年7月+ 2015年7月)/ 2 =平均回報 – RageAgainstheMachine