yellowtail
2018-11-07 21:12:32 +08:00
import tushare as ts
import pandas as pd
import pymongo
import json
% matplotlib inline
data = ts.get_today_ticks("000089")
szjc2=json.loads(data.to_json(orient='records'))
ljt1107=pymongo.MongoClient('local',27017)
db=ljt1107['szjc2']
col=db['1107']
col.insert(szjc2)
print data
df=pd.DataFrame()
df['price']=data['price']
df['change']=df['price']-df['price'].shift(1)
df
df['3']=df['price'].rolling(window=3,center=False).mean()
df['13']=df['price'].rolling(window=13,center=False).mean()
df['34']=df['price'].rolling(window=34,center=False).mean()
df
df=df.dropna()
df['pos']=0
df['tst']=0
df['tst1']=0
df['tst'][df['3']>=df['13']] = 10000
df['tst1'][df['13']>=df['34']]= 10000
df['pos'][df['tst1']==df['tst']] = 10000
#print type(df['3']<=df['13'])
df['pos'][df['3']<=df['13']] = -10000
df['pos']=df['pos'].shift(1).fillna(0)
df['pnl']=df['change']*df['pos']
df['fee']=0
df['fee'][df['pos']!=df['pos'].shift(1)]=df['price']*20000*0
df['netpnl']=df['pnl']-df['fee']
df
df['cumpnl']=df['netpnl'].cumsum()
df['cumpnl'].plot()
print df