• 量化交易指标函数整理(持续更新)



    # 根据不同的时间段设置滑点与手续费 def set_slip_fee(context): # 将滑点设置为0 set_slippage(FixedSlippage(0)) # 根据不同的时间段设置手续费 dt=context.current_dt if dt>datetime.datetime(2013,1, 1): set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) elif dt>datetime.datetime(2011,1, 1): set_commission(PerTrade(buy_cost=0.001, sell_cost=0.002, min_cost=5)) elif dt>datetime.datetime(2009,1, 1): set_commission(PerTrade(buy_cost=0.002, sell_cost=0.003, min_cost=5)) else: set_commission(PerTrade(buy_cost=0.003, sell_cost=0.004, min_cost=5))

      

    计算指数移动平均线
    # 计算指数移动平均线数据
    # 输入:股票代码-字符串,移动指数平均线天数-整数,data
    # 输出:今天和昨天的移动指数平均数-浮点数
    def get_EMA(security_code,days,data):
        # 如果只有一天的话,前一天的收盘价就是移动平均
        if days==1:
        # 获得前两天的收盘价数据,一个作为上一期的移动平均值,后一个作为当期的移动平均值
            t = attribute_history(security_code, 2, '1d', ('close'))
            return t['close'][-2],t['close'][-1]
        else:
        # 如果全局变量g.EMAs不存在的话,创建一个字典类型的变量,用来记录已经计算出来的EMA值
            if 'EMAs' not in dir(g):
                g.EMAs={}
            # 字典的关键字用股票编码和天数连接起来唯一确定,以免不同股票或者不同天数的指数移动平均弄在一起了
            key="%s%d" %(security_code,days)
            # 如果关键字存在,说明之前已经计算过EMA了,直接迭代即可
            if key in g.EMAs:
                #计算alpha值
                alpha=(days-1.0)/(days+1.0)
                # 获得前一天的EMA(这个是保存下来的了)
                EMA_pre=g.EMAs[key]
                # EMA迭代计算
                EMA_now=EMA_pre*alpha+data[security_code].close*(1.0-alpha)
                # 写入新的EMA值
                g.EMAs[key]=EMA_now
                # 给用户返回昨天和今天的两个EMA值
                return (EMA_pre,EMA_now)
            # 如果关键字不存在,说明之前没有计算过这个EMA,因此要初始化
            else:
                # 获得days天的移动平均
                ma=get_MA(security_code,days) 
                # 如果滑动平均存在(不返回NaN)的话,那么我们已经有足够数据可以对这个EMA初始化了
                if not(isnan(ma)):
                    g.EMAs[key]=ma
                    # 因为刚刚初始化,所以前一期的EMA还不存在
                    return (float("nan"),ma)
                else:
                    # 移动平均数据不足days天,只好返回NaN值
                    return (float("nan"),float("nan"))
    

      

    • 计算移动平均线
    • # 计算移动平均线数据
      # 输入:股票代码-字符串,移动平均线天数-整数
      # 输出:算术平均值-浮点数
      def get_MA(security_code,days):
      # 获得前days天的数据,详见API
      a=attribute_history(security_code, days, '1d', ('close'))
      # 定义一个局部变量sum,用于求和
      sum=0
      # 对前days天的收盘价进行求和
      for i in range(1,days+1):
      sum+=a['close'][-i]
      # 求和之后除以天数就可以的得到算术平均值啦
      return sum/days

  • 相关阅读:
    Serialization and deserialization are bottlenecks in parallel and distributed computing, especially in machine learning applications with large objects and large quantities of data.
    Introduction to the Standard Directory Layout
    import 原理 及 导入 自定义、第三方 包
    403 'Forbidden'
    https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
    These interactions can be expressed as complicated, large scale graphs. Mining data requires a distributed data processing engine
    mysqldump --flush-logs
    mysql dump 参数
    mysql dump 参数
    如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
  • 原文地址:https://www.cnblogs.com/medik/p/11108650.html
Copyright © 2020-2023  润新知