羊驼反转策略(修改版)
来源:https://uqer.io/community/share/566c0e3cf9f06c6c8a91ceec
start = '2015-01-01'
end = '2015-12-01'
capital_base = 1000000
refresh_rate = 5
benchmark = 'HS300'
freq = 'd'
universe = set_universe('HS300')
import numpy as np
import pandas as pd
def initialize(account):
account.stocks_num=10
def handle_data(account):
if account.stocks_num==10:
account.stocks_num=1
keylist=[]
data=DataAPI.MktStockFactorsOneDayGet(tradeDate=account.current_date,secID=account.universe,ticker=u"",field=['secID','REVS10'],pandas="1")
keylist=data.dropna().sort(columns='REVS10',ascending=False).tail(10)['secID'].values.tolist()
for i in keylist:
order(i,100000/account.referencePrice[i])
else:
sellist=[]
replacelist=[]
keylist=[]
for key in account.valid_secpos.keys():
keylist.append(key)
sell=DataAPI.MktStockFactorsOneDayGet(tradeDate=account.current_date,secID=keylist,ticker=u"",field=['secID','REVS10'],pandas="1")
sellist.append(sell.min()['secID'])
replace=DataAPI.MktStockFactorsOneDayGet(tradeDate=account.current_date,secID=universe,ticker=u"",field=['secID','REVS10'],pandas="1")
replace=replace.set_index('secID').drop(keylist).dropna()
replace=replace.sort(columns='REVS10',ascending=False).tail(1).reset_index()['secID'].values.tolist()
keylist.remove(sellist[0])
replacelist=replacelist+replace
keylist.append(replacelist[0])
for stk in sellist:
order_to(stk, 0)
for stk in replacelist:
order(stk,account.cash/account.referencePrice[stk])