正常来说我们自己如果想要完成一个完整的量化策略需要解决各种问题,获取数据、分析数据、以及最后根据策略自动选股买进卖出。这些问题相对来说还是比较麻烦的,有的时候券商不给你开放就更麻烦了。所以说我们就可以直接借助国内现有的在线量化投资平台来进行策略的实现。
国内量化投资在线平台:聚宽、优矿等
接下来所有的的策略都是在聚宽平台上运行的,其中主要的逻辑
- 主要框架
- initialize
- handle_data
- ……
- 获取历史数据
- 交易函数
- 回测频率
- 风险指标
- 均线:对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为一条线,就叫做N日移动平均线。
- 移动平均线常用线有5天、10天、30天、60天、120天和240天的指标。
- 5天和10天的是短线操作的参照指标,称做日均线指标;
- 30天和60天的是中期均线指标,称做季均线指标;
- 120天、240天的是长期均线指标,称做年均线指标。
- 金叉:短期均线上穿长期均线
- 死叉:短期均线下穿长期均线
| |
| def initialize(context): |
| |
| set_benchmark('600519.XSHG') |
| |
| set_option('use_real_price', True) |
| |
| set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') |
| |
| |
| |
| g.security = ['600519.XSHG'] |
| g.p1 = 5 |
| g.p2 = 30 |
| |
| |
| def handle_data(context, data): |
| for stock in g.security: |
| |
| |
| df = attribute_history(stock, g.p2) |
| ma10 = df['close'].mean() |
| ma5 = df['close'][-5:].mean() |
| |
| if ma10 > ma5 and stock in context.portfolio.positions: |
| |
| order_target(stock, 0) |
| |
| if ma10 < ma5 and stock not in context.portfolio.positions: |
| |
| order_value(stock, context.portfolio.available_cash * 0.8) |
| |
回测完会发现这个策略虽说是有收益但是相对于大盘收益还是差的远,所以说股市投资要谨慎,选择策略也要谨慎。
| def initialize(context): |
| set_benchmark('000002.XSHG') |
| set_option('use_real_price', True) |
| set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') |
| g.security = get_index_stocks('000002.XSHG') |
| |
| g.q = query(valuation).filter(valuation.code.in_(g.security)) |
| g.N = 20 |
| |
| run_monthly(handle, 1) |
| |
| def handle(context): |
| df = get_fundamentals(g.q)[['code', 'market_cap']] |
| df = df.sort('market_cap').iloc[:g.N,:] |
| |
| to_hold = df['code'].values |
| |
| for stock in context.portfolio.positions: |
| if stock not in to_hold: |
| order_target(stock, 0) |
| |
| to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions] |
| |
| if len(to_buy) > 0: |
| cash_per_stock = context.portfolio.available_cash / len(to_buy) |
| for stock in to_buy: |
| order_value(stock, cash_per_stock) |
同时考虑多个因子
| def initialize(context): |
| set_benchmark('000002.XSHG') |
| set_option('use_real_price', True) |
| set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') |
| g.security = get_index_stocks('000002.XSHG') |
| |
| g.q = query(valuation, indicator).filter(valuation.code.in_(g.security)) |
| g.N = 20 |
| |
| run_monthly(handle, 1) |
| |
| def handle(context): |
| df = get_fundamentals(g.q)[['code', 'market_cap', 'roe']] |
| df['market_cap'] = (df['market_cap'] - df['market_cap'].min()) / (df['market_cap'].max()-df['market_cap'].min()) |
| df['roe'] = (df['roe'] - df['roe'].min()) / (df['roe'].max()-df['roe'].min()) |
| df['score'] = df['roe']-df['market_cap'] |
| |
| df = df.sort('score').iloc[-g.N:,:] |
| |
| to_hold = df['code'].values |
| |
| for stock in context.portfolio.positions: |
| if stock not in to_hold: |
| order_target(stock, 0) |
| |
| to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions] |
| |
| if len(to_buy) > 0: |
| cash_per_stock = context.portfolio.available_cash / len(to_buy) |
| for stock in to_buy: |
| order_value(stock, cash_per_stock) |

