企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Contrarian strategy > 来源:https://uqer.io/community/share/5545ff8df9f06c1c3d68802f Contrarian strategy similar with Momentum strategy ```py import pandas as pd start = '2010-01-01' # 回测起始时间 end = '2015-01-01' # 回测结束时间 benchmark = 'SH50' # 策略参考标准 universe = set_universe('SH50') capital_base = 100000 # 起始资金 longest_history = 40 # handle_data 函数中可以使用的历史数据最长窗口长度 refresh_rate = 1 # 调仓频率,即每 refresh_rate 个交易日执行一次 handle_data() 函数 def initialize(account): # 初始化虚拟账户状态 pass def handle_data(account): # 每个交易日的买入卖出指令 returndata = {'symbol':[], 'ret':[]} history_data = account.get_attribute_history('closePrice',40) for s in account.universe: returndata['symbol'].append(s) returndata['ret'].append(history_data[s][-1] / history_data[s][0]) returndatanew = pd.DataFrame(returndata).sort(columns = 'ret').reset_index() returndatanew = returndatanew[0:len(returndatanew)/5] buylist = returndatanew['symbol'].tolist() for cur in account.valid_secpos: if cur not in buylist: order_to(cur,0) for sym in buylist: if sym not in account.valid_secpos: order_to(sym,300) ``` ![](https://box.kancloud.cn/2016-07-30_579cbdabb132b.jpg) worse than Momentum strategy