零、动量策略VS反转策略
1、实现代码
# 导入函数库 import jqdata import pandas as pd import numpy as np import datetime import math def initialize(context): set_option('use_real_price', True) set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') g.benchmark = '000300.XSHG' g.N = 10 set_benchmark(g.benchmark) run_monthly(handle, 1) def handle(context): stock = get_index_stocks('000300.XSHG') df_close = history(30,field='close',security_list=list(stock)).T df_close['ret']=(df_close.iloc[:,-1]-df_close.iloc[:,0])/df_close.iloc[:,0] sorted_stocks = df_close.sort('ret',ascending=True).index tohold = sorted_stocks[:g.N] for stock in context.portfolio.positions: if stock not in tohold: order_target_value(stock, 0) tobuy = [stock for stock in tohold if stock not in context.portfolio.positions] if len(tobuy) > 0: print('Buying') cash = context.portfolio.available_cash / len(tobuy) cash_every_stock = cash / len(tobuy) for stock in tobuy: order_value(stock, cash_every_stock)
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/135655.html