程序员的量化交易(34)--QuantConnect_Lean如何定义Indicator指标2

简介:


指标(Indicator)由三大关键组件组成:
1. 实现你指标的类
2. 在ACAlgorithm基类中的Helper方法,用于简化对你指标实现的调用。
3. 测试方法用于测试指标的表现。
为了实现一个指标,上述所有组件需要实现。下面的教程会带你浏览这些组件。我们将会使用  AroonOscillator作为案例。我们不会去介绍指标的基本概念。

1. 实现你的指标(Implementing Your Indicator)

你的指标类应该在工程根下的QuantConnect.Indicators项目下实现。它必须是一个单独的类,不能缩写,名如“AroonOscillator.cs”。
第一步
指标必须是下面指标基类的一个派生:
Indicator – 如果你的指标只需要单个值(例如ExponontialMovingAverage),你就可以用Indicator作为基类。
TradeBarIndicator – 如果你的指标需要用到TradeBars,那么你的指标必须是由TradeBarIndicator类扩展而来的。(例如AroonOscillator)。
WindowIndicator - 如果你的指标需要一个rolling window of data(数据的滚动窗口),那么你就要继承这个类。(例如Minimum)。


所有的指标都必须实现下面四个组件,为了在Lean中正常运作。
Constructor – 实现你的构造函数,用于接收和保存必要的指标配置信息。例如Aroon指标需要Aroon Up 和 Aroon Down值。
IsReady – 它是一个Boolean值,用于指示算法是否有了充足的历史价格数据用于算出指标的值。Aroon只有当有了n个周期的样本数据后才能开始计算。
ComputeNextValue()- 用提供的数据,计算和返回下一个指标的值。这里是你实现指标具体算法的地方。
保持可扩展性
Aroon作为案例是非常有代表性的,它有多个属性,Aroon-Up和Aroon-Down。这两个值在计算Aroon的时候都需要用到,但只有delta(up – down)会返回。这个例子中的dela叫做Aroon Percentage(比例)。许多指标会有一个主值(Primary value),多个属性,用于交易决策。


为了处理这些情况-每个属性都应该作为子指标(sub-indicator)。你可以查看下AroonOscillator.cs的源码。Aroon创建了AroonUp,AroonDown指标作为属性。这使得你可以单独的使用这些子指标。
例如下面的代码:
var smaAroonUp = sma.Of( aroon.AroonUp )
就直接用属性(子指标)。


现在我们贴出AroonOscillator的代码:

using QuantConnect.Data.Market;


namespace QuantConnect.Indicators
{
    /// 
    /// The Aroon Oscillator is the difference between AroonUp and AroonDown. The value of this
    /// indicator fluctuats between -100 and +100. An upward trend bias is present when the oscillator
    /// is positive, and a negative trend bias is present when the oscillator is negative. AroonUp/Down
    /// values over 75 identify strong trends in their respective direction.
    /// 
    public class AroonOscillator : TradeBarIndicator
    {
        /// 
        /// Gets the AroonUp indicator
        /// 
        public IndicatorBase AroonUp { get; private set; }


        /// 
        /// Gets the AroonDown indicator
        /// 
        public IndicatorBase AroonDown { get; private set; }


        /// 
        /// Gets a flag indicating when this indicator is ready and fully initialized
        /// 
        public override bool IsReady
        {
            get { return AroonUp.IsReady && AroonDown.IsReady; }
        }


        /// 
        /// Creates a new AroonOscillator from the specified up/down periods.
        /// 
        /// 
The lookback period to determine the highest high for the AroonDown /// The lookback period to determine the lowest low for the AroonUp public AroonOscillator(int upPeriod, int downPeriod) : this(string.Format("AROON({0},{1})", upPeriod, downPeriod), upPeriod, downPeriod) { } /// /// Creates a new AroonOscillator from the specified up/down periods. /// /// The name of this indicator /// The lookback period to determine the highest high for the AroonDown /// The lookback period to determine the lowest low for the AroonUp public AroonOscillator(string name, int upPeriod, int downPeriod) : base(name) { var max = new Maximum(name + "_Max", upPeriod + 1); AroonUp = new FunctionalIndicator(name + "_AroonUp", input => ComputeAroonUp(upPeriod, max, input), aroonUp => max.IsReady, () => max.Reset() ); var min = new Minimum(name + "_Min", downPeriod + 1); AroonDown = new FunctionalIndicator(name + "_AroonDown", input => ComputeAroonDown(downPeriod, min, input), aroonDown => min.IsReady, () => min.Reset() ); } /// /// Computes the next value of this indicator from the given state /// /// The input given to the indicator /// A new value for this indicator protected override decimal ComputeNextValue(TradeBar input) { AroonUp.Update(input.Time, input.High); AroonDown.Update(input.Time, input.Low); return AroonUp - AroonDown; } /// /// AroonUp = 100 * (period - {periods since max})/period /// /// The AroonUp period /// A Maximum indicator used to compute periods since max /// The next input data /// The AroonUp value private static decimal ComputeAroonUp(int upPeriod, Maximum max, IndicatorDataPoint input) { max.Update(input); return 100m * (upPeriod - max.PeriodsSinceMaximum) / upPeriod; } /// /// AroonDown = 100 * (period - {periods since min})/period /// /// The AroonDown period /// A Minimum indicator used to compute periods since min /// The next input data /// The AroonDown value private static decimal ComputeAroonDown(int downPeriod, Minimum min, IndicatorDataPoint input) { min.Update(input); return 100m * (downPeriod - min.PeriodsSinceMinimum) / downPeriod; } /// /// Resets this indicator and both sub-indicators (AroonUp and AroonDown) /// public override void Reset() { AroonUp.Reset(); AroonDown.Reset(); base.Reset(); } } }


2 在QCAlgorithm中安装Helper方法

在你实现了indicator(指标)之后,i需要将它安装到基类中。指标可以手工的创建和更新,不过我们加你使用helper方法,这样后面使用起来会简单很多。
在Aroon的案例中,实现是在AroonOscillator类,但它的helper方法是在QCAlgorithm中。这个helper方法—AROON—总是是大写的,总是返回一个指标实现类的实例对象。
public AroonOscillator AROON(string symbol, int upPeriod, int downPeriod, Resolution? resolution = null)  {
      var name = CreateIndicatorName(symbol, string.Format("AROON({0},{1})", upPeriod, downPeriod), resolution);
      var aroon = new AroonOscillator(name, upPeriod, downPeriod);
      RegisterIndicator(symbol, aroon, resolution);
      return aroon;
}



在helper方法中,你需要创建你指标的一个唯一的名字,使用你指标实现类的构造函数去构造一个指标实例,将其注册(注册后意味着指标的值会随着symbol(指定证券)的价格数据变动而自动的变动)。

3 创建测试方法

3.1 准备测试数据

 将准备好的测试数据放到QuantConnect.Tests\TestData目录下。

3.2 写测试类

在QuantConnect.Tests\Indicators下面。可以拷贝已经有的进行改写。

using System;
using NUnit.Framework;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Tests.Indicators
{
    [TestFixture]
    public class AroonOscillatorTests
    {
        [Test]
        public void ComparesWithExternalData()
        {
            var aroon = new AroonOscillator(14, 14);
            TestHelper.TestIndicator(aroon, "spy_aroon_oscillator.txt", "Aroon Oscillator 14",
                (i, expected) => Assert.AreEqual(expected, (double)aroon.Current.Value, 1e-3));
        }

        [Test]
        public void ResetsProperly()
        {
            var aroon = new AroonOscillator(3, 3);
            aroon.Update(new TradeBar
            {
                Symbol = "SPY",
                Time = DateTime.Today,
                Open = 3m,
                High = 7m,
                Low = 2m,
                Close = 5m,
                Volume = 10
            });
            aroon.Update(new TradeBar
            {
                Symbol = "SPY",
                Time = DateTime.Today.AddSeconds(1),
                Open = 3m,
                High = 7m,
                Low = 2m,
                Close = 5m,
                Volume = 10
            });
            aroon.Update(new TradeBar
            {
                Symbol = "SPY",
                Time = DateTime.Today.AddSeconds(2),
                Open = 3m,
                High = 7m,
                Low = 2m,
                Close = 5m,
                Volume = 10
            });
            Assert.IsFalse(aroon.IsReady);
            aroon.Update(new TradeBar
            {
                Symbol = "SPY",
                Time = DateTime.Today.AddSeconds(3),
                Open = 3m,
                High = 7m,
                Low = 2m,
                Close = 5m,
                Volume = 10
            });
            Assert.IsTrue(aroon.IsReady);

            aroon.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(aroon);
            TestHelper.AssertIndicatorIsInDefaultState(aroon.AroonUp);
            TestHelper.AssertIndicatorIsInDefaultState(aroon.AroonDown);
        }
    }
}


相关文章
|
编译器 数据处理 C#
C#中的异步流:使用IAsyncEnumerable<T>和await foreach实现异步数据迭代
【1月更文挑战第10天】本文介绍了C#中异步流的概念,并通过使用IAsyncEnumerable<T>接口和await foreach语句,详细阐述了如何异步地迭代数据流。异步流为处理大量数据或需要流式处理数据的场景提供了一种高效且非阻塞性的方法,使得开发者能够更优雅地处理并发和数据流问题。
|
4月前
|
API 数据安全/隐私保护 Python
Python如何快速接入聚合数据行情API
聚合数据行情API,指的是一个接口即可提供多个不同交易品种的行情数据查询,这种接口,可以让你同时查询A股、美股、外汇等多种资产的行情数据。
|
6月前
|
物联网
(手把手)在华为云、阿里云搭建自己的物联网MQTT消息服务器,免费IOT平台
本文介绍如何在阿里云搭建自己的物联网MQTT消息服务器,并使用 “MQTT客户端调试工具”模拟MQTT设备,接入平台进行消息收发。
2442 42
|
机器学习/深度学习 存储 PyTorch
PyTorch自定义学习率调度器实现指南
本文将详细介绍如何通过扩展PyTorch的 ``` LRScheduler ``` 类来实现一个具有预热阶段的余弦衰减调度器。我们将分五个关键步骤来完成这个过程。
701 2
|
Java 数据库连接 API
springBoot:后端解决跨域&Mybatis-Plus&SwaggerUI&代码生成器 (四)
本文介绍了后端解决跨域问题的方法及Mybatis-Plus的配置与使用。首先通过创建`CorsConfig`类并设置相关参数来实现跨域请求处理。接着,详细描述了如何引入Mybatis-Plus插件,包括配置`MybatisPlusConfig`类、定义Mapper接口以及Service层。此外,还展示了如何配置分页查询功能,并引入SwaggerUI进行API文档生成。最后,提供了代码生成器的配置示例,帮助快速生成项目所需的基础代码。
703 1
|
数据挖掘 Python
pandas中的groupby函数应用
pandas中的groupby函数应用
164 0
pandas中的groupby函数应用
|
Java 编译器 Scala
IDEA上的Scala环境搭建
本文指导如何搭建Scala开发环境。首先,安装Scala编译器`scala-2.12.10.msi`,通过DOS窗口验证安装成功。然后,在IDEA中,安装Scala插件,创建Maven工程,删除默认包,新建Scala源码包,并在其中创建Scala Object类。接着,配置项目结构,添加Scala SDK,确保Maven、Language Level和Compiler的bytecode版本设置正确。最后,编写并测试基本的Scala代码。
855 2
IDEA上的Scala环境搭建
|
分布式计算 资源调度 Hadoop
Java大数据处理:Spark与Hadoop整合
Java大数据处理:Spark与Hadoop整合
|
SQL Oracle 关系型数据库
SQL SELECT TOP 详解
SQL SELECT TOP 详解
|
Linux
Linux安装bind9搭建自己的域名服务器
Linux安装bind9搭建自己的域名服务器
539 0