在线平台与量化投资

在线平台与量化投资

​ 正常来说我们自己如果想要完成一个完整的量化策略需要解决各种问题,获取数据、分析数据、以及最后根据策略自动选股买进卖出。这些问题相对来说还是比较麻烦的,有的时候券商不给你开放就更麻烦了。所以说我们就可以直接借助国内现有的在线量化投资平台来进行策略的实现。

国内量化投资在线平台:聚宽优矿

常见量化策略

接下来所有的的策略都是在聚宽平台上运行的,其中主要的逻辑

  • 主要框架
    • initialize
    • handle_data
    • ……
  • 获取历史数据
  • 交易函数
  • 回测频率
    • 按天回测
    • 按分钟回测
  • 风险指标

1、双均线策略

  • 均线:对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为一条线,就叫做N日移动平均线。
  • 移动平均线常用线有5天、10天、30天、60天、120天和240天的指标。
    • 5天和10天的是短线操作的参照指标,称做日均线指标;
    • 30天和60天的是中期均线指标,称做季均线指标;
    • 120天、240天的是长期均线指标,称做年均线指标。
  • 金叉:短期均线上穿长期均线
  • 死叉:短期均线下穿长期均线
# 初始化操作
def initialize(context):
    # 设定贵州茅台为基准,参数为股票代码
    set_benchmark('600519.XSHG')
    # True为开启动态复权模式,使用真实价格交易
    set_option('use_real_price', True)
    # 股票类交易手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
    set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

    # 定义一个全局变量, 保存要操作的股票
    # 000002(股票:万科A)
    g.security = ['600519.XSHG']
    g.p1 = 5
    g.p2 = 30

# 每个交易日自动运行一次
def handle_data(context, data):
    for stock in g.security:
        # 金叉:如果5日均线大于10日均线并且不持仓
        # 死叉:如果5日均线小于10日均线并且持仓
        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)
    # record(ma5=ma5, ma10=ma10)

回测完会发现这个策略虽说是有收益但是相对于大盘收益还是差的远,所以说股市投资要谨慎,选择策略也要谨慎。

2、因子选股策略

  • 因子:标准
    • 增长率,市值,ROE,……
  • 选股策略:
    • 选取该因子最大(或最小)的N只股票持仓
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)

3、多因子选股策略

同时考虑多个因子

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)

4、均值回归策略

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术