羊驼反转策略
来源:https://uqer.io/community/share/566982a4f9f06c6c8a91b7a2
start = '2011-04-01'
end = '2015-12-01'
capital_base = 1000000
refresh_rate = 10
benchmark = 'HS300'
freq = 'd'
universe = set_universe('HS300')
import numpy as np
import pandas as pd
data=DataAPI.MktStockFactorsOneDayGet(tradeDate='20110331',secID=universe,ticker=u"",field=['secID','REVS10'],pandas="1")
buylist=data.dropna().sort(columns='REVS10',ascending=False).tail(10)['secID'].values.tolist()
def initialize(account):
account.stocks_num=10
def handle_data(account):
if account.stocks_num==10:
global buylist
account.universe=buylist
hist_prices = account.get_attribute_history('closePrice', 1)
for i in account.universe:
order(i,100000/hist_prices[i][0])
account.stocks_num=1
sellist=[]
replacelist=[]
sell=DataAPI.MktStockFactorsOneDayGet(tradeDate=account.current_date,secID=account.universe,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(buylist)
replace=replace.dropna().sort(columns='REVS10',ascending=False).tail(1).reset_index()['secID'].values.tolist()
replacelist.append(replace)
account.universe.remove(sell.min()['secID'])
account.universe=account.universe+replacelist[0]
hist_prices = account.get_attribute_history('closePrice', 1)
for stk in sellist:
order_to(stk,0)
for stk in account.universe:
order(stk,account.cash/10/hist_prices[stk][0])