Programmer's Quantitative Trading (--quantconnect_lean) How to define Indicator indicator 2

Source: Internet
Author: User
Tags quantconnect

Reprint need to indicate source: http://blog.csdn.net/minimicall,http://cloudtrade.top/


The indicator (Indicator) consists of three key components:
1. The class that implements your indicator
2. helper methods in the Acalgorithm base class to simplify calls to your indicator implementations.
3. The test method is used to test the performance of the indicator.
To implement an indicator, all of the above components need to be implemented. The following tutorials will take you through these components. We will use Aroonoscillator as a case. We are not going to introduce the basic concepts of indicators.

1. Implementing your indicator (implementing Your Indicator) Your indicator class should be implemented under the Quantconnect.indicators project under the project root. It must be a separate class, not abbreviated, named "AroonOscillator.cs".
The first step
The indicator must be a derivation of the following indicator base class:
indicator– If your indicator requires only a single value (for example, exponontialmovingaverage), you can use Indicator as the base class.
tradebarindicator– If your indicator needs to use tradebars, then your indicator must be extended by the Tradebarindicator class. (e.g. Aroonoscillator).
Windowindicator<t>-If your indicator requires a rolling window of data (the scrolling windows of the database), then you will inherit this class. (e.g. minimum).


All indicators must implement the following four components in order to function properly in lean.
Constructor– implements your constructor to receive and save the necessary indicator configuration information. For example Aroon indicators require Aroon up and Aroon down values.
isready– It is a Boolean value that indicates whether the algorithm has sufficient historical price data to calculate the value of the indicator. Aroon can only be calculated if there are sample data for n cycles.
Computenextvalue ()-Calculates and returns the value of the next indicator with the provided data. Here is where you implement the specific algorithm of the indicator.
Maintain scalability
Aroon as a case is very representative, it has a number of properties, aroon-up and Aroon-down. Both values need to be used when calculating aroon, but only the Delta (Up–down) is returned. The Dela in this example is called Aroon Percentage (scale). Many indicators will have a primary value (Primary value), multiple attributes, for trading decisions.


In order to handle these conditions-each attribute should be used as a sub-indicator (sub-indicator). You can view the source code of the next AroonOscillator.cs. Aroon created the Aroonup,aroondown indicator as an attribute. This allows you to use these sub-indicators separately.
For example, the following code:
var smaaroonup = SMA. Of (Aroon. Aroonup)
Directly with attributes (sub-indicators).


Now we post the Aroonoscillator code:

Using Quantconnect.data.market;namespace quantconnect.indicators{//<summary>///The Aroon oscillator is th E difference between Aroonup and Aroondown. The value of this///indicator fluctuats between-100 and +100. An upward trend bias was present when the oscillator//is positive, and a negative trend bias was present when the Osci Llator is negative.    Aroonup/down//values over-identify strong trends in their respective direction. </summary> public class Aroonoscillator:tradebarindicator {//<summary>//Gets T He aroonup indicator//</summary> public indicatorbase<indicatordatapoint> aroonup {get; priv Ate set; }//<summary>//Gets the Aroondown indicator///</summary> public Indicatorba        Se<indicatordatapoint> Aroondown {get; private set;} <summary>//Gets a flag indicating when this indicator are ready and FUlly initialized//</summary> public override bool IsReady {get {return aroonup.i Sready && Aroondown.isready;        }}///<summary>//Creates a new aroonoscillator from the specified up/down periods. </summary>//<param name= "Upperiod" >the lookback period to determine the highest high for the Ar oondown</param>//<param name= "Downperiod" >the lookback period to determine the lowest low for the Ar oonup</param> public aroonoscillator (int upperiod, int. downperiod): This (string.        Format ("AROON ({0},{1})", Upperiod, Downperiod), Upperiod, downperiod) {}//<summary>        Creates a new aroonoscillator from the specified up/down periods. </summary>//<param name= "name" >the name of this indicator</param>//<param Nam E= "Upperiod" >the lookback period to determine tHe highest high for the aroondown</param>//<param name= "Downperiod" >the lookback period to determine            The lowest aroonup</param> public aroonoscillator (string name, int upperiod, int downperiod)            : base (name) {var max = new Maximum (name + "_max", Upperiod + 1); Aroonup = new Functionalindicator<indicatordatapoint> (name + "_aroonup", input + = Computearoonup (u Pperiod, Max, input), Aroonup = max. IsReady, () = max.            Reset ());            var min = new Minimum (name + "_min", Downperiod + 1); Aroondown = new Functionalindicator<indicatordatapoint> (name + "_aroondown", input + = Computearoon Down (downperiod, Min, input), Aroondown = Min. IsReady, () = Min.        Reset ()); }//<summary>//computes the next value of this Indicator from the given State///</summary>//<param name= "input" >the input given to the indicator& lt;/param>//<returns>a new value for this indicator</returns> protected override decimal C Omputenextvalue (Tradebar input) {aroonup.update (input. Time, input.            High); Aroondown.update (input. Time, input.            Low);        return aroonup-aroondown;        }///<summary>//Aroonup = + * (Period-{periods since Max})/period//</summary> <param name= "Upperiod" >the aroonup period</param>//<param name= "Max" >a Maximum Indi Cator used to compute periods since max</param>//<param name= "input" >the next input DATA&LT;/PARAM&G        T <returns>the aroonup value</returns> private static decimal computearoonup (int upperiod, Maximum ma x, Indicatordatapoint input) {max. Update (input);           Return 100m * (Upperiod-max.        Periodssincemaximum)/upperiod; }///<summary>//Aroondown = * (Period-{periods since min})/period//</summary&gt        ; <param name= "Downperiod" >the aroondown period</param>//<param name= "min" >a Minimum indicat        or used to compute periods since min</param>//<param name= "input" >the next input data</param> <returns>the aroondown value</returns> private static decimal computearoondown (int Downperio D, Minimum min, indicatordatapoint input) {min.            Update (input); Return 100m * (downperiod-min.        Periodssinceminimum)/downperiod; }///<summary>//resets this indicator and both sub-indicators (Aroonup and Aroondown)///&            lt;/summary> public override void Reset () {aroonup.reset ();            Aroondown.reset (); Base.        Reset (); }    }}



2 Installing helper methods in Qcalgorithm after you have implemented the indicator (indicator), I need to install it into the base class. Indicators can be created and updated manually, but we add you to the helper method, which is a lot easier to use later.
In the case of Aroon, the implementation is in the Aroonoscillator class, but its helper method is in Qcalgorithm. This helper method-aroon-is always uppercase and always returns an instance object of the indicator implementation class.
Public Aroonoscillator AROON (string symbol, int upperiod, int downperiod, Resolution? Resolution = null)  {      var nam E = createindicatorname (symbol, String. Format ("AROON ({0},{1})", Upperiod, Downperiod), resolution);      var aroon = new Aroonoscillator (name, Upperiod, downperiod);      Registerindicator (symbol, aroon, resolution);      return Aroon;}



In the helper method, you need to create a unique name for your indicator, using the constructor of your indicator implementation class to construct an instance of the indicator, registering it (meaning that the value of the indicator will change automatically as the price data of the symbol (the specified security) changes).
3 Creating a test method 3.1 Prepare the test data to put the prepared test data into the Quantconnect.tests\testdata directory.
3.2 Write the test class below quantconnect.tests\indicators. You can copy an existing rewrite.

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", (i, expected) = Asse Rt. 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 = 5            M, 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); }    }}


Programmer's Quantitative Trading (--quantconnect_lean) How to define Indicator indicator 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.