Click or drag to resize

How To develop an Indicator

An indicator is a calculation of different market matrix and is plotted on a chart (or in a separate panel) as a series to analyze the market. ArthaChitra natively comes with a set of popular indicators. However if the user wishes to develop his/her own logic into an indicator then ArthaChitra provides a platform to do so.

The main calculations are stored in the Values property which is an array of ISeriesT, where T is a double. The Values property is created by calling the AddPlot method. The AddPlot method can be called only when the indicator State is in Initialize State.

If the user chooses to draw a horizontal line then he/she can do so by calling the AddLine method. Like the AddPlot method, the AddLine method can be only called when the indicator is in initialized state.

Understanding State

During the life cycle of an indicator it goes through different stages or processes. In each stage the indicator performs a set of jobs. In which state the indicator s in is defined by the State property. Whenever the state gets changed it raises the method OnStateChange. The different states which an indicator goes through are:

  • Initialize - Here the plot values and line values are defined.
  • StartUp - User should initialize indicators, series here.
  • Historical - When historical data is being processed.
  • RealTime - When incoming ticks are being processed in realtime.
  • Finalize - When the indicator encounters an error or is being finalized (disposed off). All clear up codes should be placed here.
Abstract methods

OnStateChange

The OnStateChanged method is called whenever the State of the indicator is changed. Please refer here to know more about the State enumeration

Example
C#
protected override void OnStateChange()
{
    //your code here
}

OnBarUpdate

The main business logic, pertaining to how the series plot values are computed, generally resides in the OnBarUpdate method. The OnBarUpdate method is called whenever a new tick comes in. How this method is called depends on the Calculate property. If calculate property is set to OnEachTick then this method is called on each incoming tick. If the Calculate property is set as OnPriceChange then OnBarUpdate will be called when the previous price differs from the last price.

Example
C#
protected override void OnBarUpdate()
{
    //your code here
}
Add reference Indicators

To add reference to another indicator please use the AddIndicator method.

Example
C#
//reference the SMA
SMA sma = AddIndicator<SMA>(new object[] { 14 });
Example
C#
//If you want to call reference the Avg series of the MacD indicator
ISeries<double> macdAvg = AddIndicator<MacD>(new object[]{ 14, 26, 9 }).Avg;

Note: new object[]{} is the parameter values of the specific indicators constructor.

The above method however pose a challange as in absense of intellisense defining the exact constractor parameter can often result in ambiguity. To avoid this developer might consider defining a custom methods in the AddOrGetIndicator class to access the indicator. Users then can easily call those methods to access that indicator. The below codes demonstrates how to define the SMA indicator and how it is accessed by other indicators.

Example
C#
public static partial class AddOrGetIndicator
{
    public static SMA SMA(SharpScriptBase owner, int period)
    {
        return SMA(owner, owner.Input, period);
    }

    public static SMA SMA(SharpScriptBase owner, ISeries<double> input, int period)
    {
        return CacheIndicator<SMA>(owner, input, new object[]{ period });
    }

}

Users can access the same as:

Example
C#
SMA sma = AddOrGetIndicator.SMA(this, 14);    //calls a 14 peirod simple moving average

A referenced indicator must be initialized when the scripts is in StartUp state.

Example
C#
    //declare a private field
    private SMA sma;

protected override void OnStateChange()
{
    switch (base.State)
    {
            case State.Finalize:
                break;
            case State.Initialize:
                break;
            case State.StartUp:
                //initialize the indicator
                this.sma = AddOrGetIndicator.SMA(this, 14);
                break;
            default:
                break;
    }
}

protected override void OnBarUpdate()
{
    //now refer it as below
    double smaValue = sma[0];

    //other codes
}
How to add an Drawing/Chart object

You can add a drawing object via a SharpScript code. For example the below code will add a line from the current running bar to the last 10 (ten) bars back at closing price.

Example
C#
if (CurrentBar > 10)
{
    Draw.Line(this, "tag", Time[10], Low[10], Time[0], High[0], new Pen(Brushes.Orange, 2), isBarsPanel: false);
}

If the same tag name is used then the existing chartObject will be replaced. If you wish to create multiple chartObjects then please use unique tag names for the chartObjects

A drawing object can be drawn both on the indicator panel and the bars panel. In the above code, for example, if the param isBarsPanel is set to true then the drawing object will be rendered on the bars panel.

Configuring properties via GUI using Xaml templates
See Also