这段运行结果是一个框???不可思议


(189) #1
克隆策略
In [2]:
import pandas as pd 
import numpy as np
import talib as tb
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(instruments, start_date, end_date,fields=['open', 'high', 'low', 'close'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池
def get_ratio(df):
    df['rc']=df['close_x']*10000//df['close_y']
    for i in [10,20,30,60]:
        df['rc_%s'%str(i)]=abs(df['rc']-df['rc'].shift(i))
    return
gr_df=df.groupby('instrument_x').apply(get_ratio)
gr_df.tail()
Out[2]:
instrument_x high_x low_x open_x close_x date instrument_y high_y low_y open_y close_y
44503 601919.SHA 7.143742 6.966452 7.039454 7.060311 2017-11-08 000300.SHA 4089.145752 4040.577148 4051.493408 4048.005615
44504 601985.SHA 8.134252 7.990465 8.093170 8.021276 2017-11-08 000300.SHA 4089.145752 4040.577148 4051.493408 4048.005615
44505 601988.SHA 6.480832 6.398796 6.431611 6.480832 2017-11-08 000300.SHA 4089.145752 4040.577148 4051.493408 4048.005615
44506 601989.SHA 11.344260 11.092539 11.344260 11.159664 2017-11-08 000300.SHA 4089.145752 4040.577148 4051.493408 4048.005615
44507 601998.SHA 8.433931 8.312093 8.352705 8.379781 2017-11-08 000300.SHA 4089.145752 4040.577148 4051.493408 4048.005615

右边显示有数据是错的

版主可以复制代码再运行一次


(189) #2

rc_10/rc30等等不见了??


(iQuant) #3

get_ratio要返回对象啊。python 函数知识你可以参阅:[量化学堂-Python编程]函数调用与定义

克隆策略
In [1]:
import pandas as pd 
import numpy as np
import talib as tb
from pandas import Series,DataFrame
In [2]:
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(instruments, start_date, end_date,fields=['open', 'high', 'low', 'close'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#
In [14]:
 def get_ratio(df):
    df['rc']=df['close_x']*10000//df['close_y']
    for i in [10,20,30,60]:
        df['rc_%s'%str(i)]=abs(df['rc']-df['rc'].shift(i))
    return df  # 要返回一个对象
In [29]:
gr_df = df.groupby('instrument_x').apply(get_ratio)
gr_df.tail()
Out[29]:
date instrument_x high_x open_x low_x close_x instrument_y high_y open_y low_y close_y rc rc_10 rc_20 rc_30 rc_60
44503 2017-11-08 601919.SHA 7.143742 7.039454 6.966452 7.060311 000300.SHA 4089.145752 4051.493408 4040.577148 4048.005615 17.0 1.0 1.0 3.0 5.0
44504 2017-11-08 601985.SHA 8.134252 8.093170 7.990465 8.021276 000300.SHA 4089.145752 4051.493408 4040.577148 4048.005615 19.0 0.0 0.0 1.0 1.0
44505 2017-11-08 601988.SHA 6.480832 6.431611 6.398796 6.480832 000300.SHA 4089.145752 4051.493408 4040.577148 4048.005615 16.0 0.0 1.0 1.0 1.0
44506 2017-11-08 601989.SHA 11.344260 11.344260 11.092539 11.159664 000300.SHA 4089.145752 4051.493408 4040.577148 4048.005615 27.0 1.0 1.0 0.0 0.0
44507 2017-11-08 601998.SHA 8.433931 8.352705 8.312093 8.379781 000300.SHA 4089.145752 4051.493408 4040.577148 4048.005615 20.0 1.0 2.0 2.0 3.0