• 【量化】五日均价策略


    g.security = '600570.SS'
    
    def initialize(context):
        pass
    
    def handle_data(context, data):
        security = g.security
        sid = symbol(g.security)
        
        # 取得过去五天的平均价格
        average_price = data[sid].mavg(5)
    
        # 取得上一时间点价格
        current_price = data[sid]['close']
        
        # 取得当前的现金
        cash = context.portfolio.cash
        
        # 如果上一时间点价格高出五天平均价1%, 则全仓买入
        if current_price > 1.01*average_price:
            # 用所有 cash 买入股票
            order_value(symbol(g.security), cash)
            log.info('buy %s' % g.security)
        # 如果上一时间点价格低于五天平均价, 则空仓卖出
        elif current_price < average_price and context.portfolio.positions[sid].amount > 0:
            # 卖出所有股票,使这只股票的最终持有量为0
            order_target(symbol(g.security), 0)
            log.info('sell %s' % g.security)
        # 画出上一时间点价格
        record(stock_price=current_price)
  • 相关阅读:
    012 字典
    011 递归
    010 函数与闭包
    009 格式化
    000 机器学习的概念原理
    008 元组
    007 列表
    005 Numpy的基本操作
    071 SparkStreaming与SparkSQL集成
    070 DStream中的transform和foreachRDD函数
  • 原文地址:https://www.cnblogs.com/jzm17173/p/5345735.html
Copyright © 2020-2023  润新知