在本地写一个以市净率为因子的回测结果

数据库为:mongodb

数据获取包为:tushare(但是获取市净率需要300积分,学生认证后可送1500积分)

                         没有积分可引入聚宽,米筐等一系列量化平台的接口

 

 

代码为:

df = ts.get_hist_data('hs300',start="2017-11-21",end='2017-12-23')      #2017-10-25     2020-4-24
df=df.sort_index()
allmoney=[]
sub=pd.DataFrame()
monlist=[10000,10000,10000,10000,10000,
         10000,10000,10000,10000,10000,
         10000,10000,10000,10000,10000,]      #选15只股,每只股1W元
da=get_trade_days(start_date="2017-11-21",end_date='2017-12-23')

code = []
conn = pymongo.MongoClient()
tdb = conn.platform
post_info = tdb.strategy                                   #连接数据库
#获取每天策略的收益情况
for i in range(len(da)):
    print(i)
    t=0
    if i==0:                      #第一天直接买股
        pb = get_fundamentals(query(valuation.code, valuation.pb_ratio
                                ).filter(valuation.pb_ratio > 0,
                                ).order_by(valuation.pb_ratio.asc()
                                ),date = da[i])
        code=list(pb['code'][:15])
        for j in code:
            s=j[:6]
            hs=ts.get_hist_data(code=s, start=str(da[i]), end=str(da[i]))
            hs['buy']=monlist[t]/hs['open'][0]
            hs['code']=s
            hs['monnum']=t
            hs['state']="have"
            t=t+1
            sub = sub.append(hs)

        allmoney.append(sum(monlist))
        print(sub)
        post_info.insert_many(json.loads(sub.to_json(orient='records')))

    else:                  #非第一天进行买卖操作
        summon=0
        buynum=[]
        numb =[]
        pb = get_fundamentals(query(valuation.code, valuation.pb_ratio
                                    ).filter(valuation.pb_ratio > 0,
                                             ).order_by(valuation.pb_ratio.asc()
                                                        ), date=da[i])
        codesub = list(pb['code'][:15])
        buycode=Jundgebuy(code,codesub)
        sellcode=Jundgesell(code,codesub)
        print(sellcode)
        code=codesub
        for j in sellcode:          #卖股票
            s = j[:6]
            cursor = post_info.find({"code": s,"state":"have"})
            sj=pd.DataFrame(list(cursor))

            post_info.update_many({'code': s}, {'$set': { 'state': "no"}})

            hs = ts.get_hist_data(code=s, start=str(da[i]), end=str(da[i]))

            summon=summon+list(hs['open'])[0]*list(sj['buy'])[0]
            numb=list(sj['monnum'])[0]
            monlist[numb]=list(hs['open'])[0]*list(sj['buy'])[0]

        for j in buycode:      #买股票
            s = j[:6]
            hs = ts.get_hist_data(code=s, start=str(da[i]), end=str(da[i]))
            hs['buy'] = summon/len(buycode) / hs['open'][0]
            hs['code'] = s
            hs['monnum'] = t
            hs['state'] = "have"
            hp=hs.to_json(orient='records')
            post_info.insert_one(json.loads(hp[1:-1]))


        cursor = post_info.find({ "state": "have"})
        sj = pd.DataFrame(list(cursor))
        ncode=sj['code']
        nbuy=sj['buy']
        nsum=0
        for j in range(15):               #计算每只股票的收盘价总金额
            hs = ts.get_hist_data(code=ncode[j], start=str(da[i]),end=str(da[i]))
            nsum=nsum+nbuy[j]*hs['close'][0]

        allmoney.append(nsum)
        print(nsum)
conn.close()
allmoney=[i /36 for i in allmoney]
df['strategy']=allmoney

df[['close', 'open','strategy']].plot(figsize=(15, 8), grid=True)             以所获金额按比例和hs300比较
plt.show()           
print(df['strategy'])  

图的结果为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值