Table of Contents

Indicators

S# provides over 70 technical analysis indicators as standard. This allows not to create the necessary indicators from scratch, but to use finished ones. In addition, you can create your own indicators, taking existing ones as a basis, as shown in the Custom indicator. section. All the base classes for working with indicators, as well as the indicators themselves, are located in the StockSharp.Algo.Indicators namespace.

Connecting the indicator to the robot

  1. At the very beginning, you need to create an indicator. The indicator is created, just like a regular .NET object:

    var longSma = new SimpleMovingAverage { Length = 80 };
    
  2. Next, you need to fill it with data. For example, this could be the candle closing price:

    foreach (var candle in candles)
     longSma.Process(candle);
    

    The indicator accepts an IIndicatorValue. at the input. Some of the indicators operate with a prime number, such as SimpleMovingAverage. Others require a full candle, such as MedianPrice. Therefore, input values must be cast to either DecimalIndicatorValue or CandleIndicatorValue. The resulting indicator value works according to the same rules as the input value.

  3. The resulting and the input value of the indicator have the IIndicatorValue.IsFinal, property, which indicates that the value is final and the indicator will not change at a given point in time. For example, the SimpleMovingAverage indicator is generated by the candle closing price, but at the current moment the final candle closing price is unknown and is changing. In this case, the resulting IIndicatorValue.IsFinal value will be 'false'. If the finished candle is passed to the indicator, then the input and resulting values of IIndicatorValue.IsFinal will be 'true'.

  4. To get the current indicator value, the IIndicatorValue.GetValue<T> method is used:

    // calculate the new position relative to each other
    var isShortLessThenLong = ShortSma.GetCurrentValue() < LongSma.GetCurrentValue();
    // if there was an intersection
    if (_isShortLessThenLong != isShortLessThenLong)
    {
     // if short is less than long, then sale, otherwise, purchase.
     var direction = isShortLessThenLong ? Sides.Sell : Sides.Buy;
     // register the order
     var volume = Position == 0 ? Volume : Position.Abs().Min(Volume) * 2;
     var price = candle.ClosePrice + ((direction == Sides.Buy ? Security.PriceStep : -Security.PriceStep) ?? 1);
        RegisterOrder(this.CreateOrder(direction, price, volume));
     // remember the current position relative to each other
     _isShortLessThenLong = isShortLessThenLong;
    }
    
  5. All indicators have a BaseIndicator.IsFormed, property, which indicates whether the indicator is ready for use. For example, the SimpleMovingAverage indicator has a period, and until the indicator processes the number of candles equal to the indicator period, the indicator will be considered not ready for use. And the BaseIndicator.IsFormed property will be 'false'.