Version 24.2 (latest)

Custom Indicators

The XamFinancialChart control allows you to define custom financial indicators to display in the Indicator Pane.

In this topic

This topic contains the following sections:

Configuring Custom Indicators

In the XamFinancialChart, you can enable custom financial indicators by adding names for them to the CustomIndicatorNames property and performing calculations for them in the ApplyCustomIndicators event.

Code Snippet

The following code example shows how to set up and calculate two custom indicators, one featuring the Simple Moving Average (SMA) and one displaying random values:

In XAML:

<ig:XamFinancialChart x:Name="chart"
    ItemsSource="{Binding}"
    ApplyCustomIndicators="ApplyCustomIndicators" />

In C#:

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();

    var names = new ObservableCollection<string>();
    names.Add("SMA 100");
    names.Add("Random");
    chart.CustomIndicatorNames = names;
}

private void ApplyCustomIndicators(object sender, FinancialChartCustomIndicatorArgs e)
{
    var SMAValues = e.IndicatorInfo.SupportingCalculations.SMA.Strategy.Invoke(e.IndicatorInfo.DataSource.CloseColumn, 100);
    int length = e.IndicatorInfo.DataSource.IndicatorColumn.Count;
    switch (e.Index)
    {
        case 0:
            for (int i = 0; i < length; i++)
                e.IndicatorInfo.DataSource.IndicatorColumn[i] = SMAValues.ToList()[i];
            break;
        case 1:
            Random rand = new Random();
            for (int i = 0; i < length; i++)
                e.IndicatorInfo.DataSource.IndicatorColumn[i] = rand.Next();
            break;
    }
}

In Visual Basic:

Public Sub New()
    MyBase.New()
    InitializeComponent()
    Me.DataContext = New ViewModel

    Dim names = New ObservableCollection(Of String)
    names.Add("SMA 100")
    names.Add("Random")
    chart.CustomIndicatorNames = names
End Sub

Private Sub ApplyCustomIndicators(ByVal sender As Object, ByVal e As FinancialChartCustomIndicatorArgs)
    Dim SMAValues = e.IndicatorInfo.SupportingCalculations.SMA.Strategy.Invoke(e.IndicatorInfo.DataSource.CloseColumn, 100)
    Dim length As Integer = e.IndicatorInfo.DataSource.IndicatorColumn.Count
    Select Case (e.Index)
        Case 0
            Dim i As Integer = 0
            Do While (i < length)
                e.IndicatorInfo.DataSource.IndicatorColumn(i) = SMAValues.ToList(i)
                i = (i + 1)
            Loop

        Case 1
            Dim rand As Random = New Random
            Dim i As Integer = 0
            Do While (i < length)
                e.IndicatorInfo.DataSource.IndicatorColumn(i) = rand.Next()
                i = (i + 1)
            Loop

    End Select

End Sub
financialchart wpf custom indicators.png

Related Content