ComponentOne FinancialChart for WPF
Analytics / Indicators / Relative Strength Index
In This Topic
    Relative Strength Index
    In This Topic

    Relative Strength Index (RSI) indicator for FinancialChart is a momentum oscillator, which measures velocity and magnitude of price movements. It compares the upward movements in closing price of an asset to the downward movements over a trading period, and intends to determine strength or weakness of a stock. It fluctuates between 0 and 100. The stocks with strong positive changes have a higher RSI than the stocks with strong negative changes.

    It finds application in comparing the magnitude of recent gains to recent losses, to determine the overbought and oversold conditions of an asset. Stocks are considered overbought when RSI is above 70, and oversold when below 30.

    FinancialChart also enables you to fetch the calculated RSI values at run-time using GetValues() method. This can help in creating alerts in application or maintaining logs while working with dynamic data.

    Notice that the given code snippet uses a class DataService.cs. To see the code, refer to Average True Range. In addition, the sample creates an instance of the RSI class to work with Relative Strength Index.

    Public Class DataService
        Private _companies As New List(Of Company)()
        Private _cache As New Dictionary(Of String, List(Of Quote))()
    
        Private Sub New()
            _companies.Add(New Company() With {
                Key.Symbol = "box",
                Key.Name = "Box Inc"
            })
            _companies.Add(New Company() With {
                Key.Symbol = "fb",
                Key.Name = "Facebook"
            })
        End Sub
    
        Public Function GetCompanies() As List(Of Company)
            Return _companies
        End Function
    
        Public Function GetSymbolData(symbol As String) As List(Of Quote)
            If Not _cache.Keys.Contains(symbol) Then
                Dim path As String = String.Format("FinancialChartExplorer.Resources.{0}.json", symbol)
                Dim stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
                Dim ser = New DataContractJsonSerializer(GetType(Quote()))
                Dim data = DirectCast(ser.ReadObject(stream), Quote())
                _cache.Add(symbol, data.ToList())
            End If
    
            Return _cache(symbol)
        End Function
    
        Shared _ds As DataService
        Public Shared Function GetService() As DataService
            If _ds Is Nothing Then
                _ds = New DataService()
            End If
            Return _ds
        End Function
    End Class
    
    public class DataService
    {
        List<Company> _companies = new List<Company>();
        Dictionary<string, List<Quote>> _cache = new Dictionary<string, List<Quote>>();
    
        private DataService()
        {
            _companies.Add(new Company() { Symbol = "box", Name = "Box Inc" });
            _companies.Add(new Company() { Symbol = "fb", Name = "Facebook" });
        }
    
        public List<Company> GetCompanies()
        {
            return _companies;
        }
    
        public List<Quote> GetSymbolData(string symbol)
        {
            if (!_cache.Keys.Contains(symbol))
            {
                string path = string.Format("FinancialChartExplorer.Resources.{0}.json", symbol);
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
                var ser = new DataContractJsonSerializer(typeof(Quote[]));
                var data = (Quote[])ser.ReadObject(stream);
                _cache.Add(symbol, data.ToList());
            }
    
            return _cache[symbol];
        }
    
        static DataService _ds;
        public static DataService GetService()
        {
            if (_ds == null)
                _ds = new DataService();
            return _ds;
        }
    }
    
    Partial Public Class Indicators
        Inherits UserControl
    
        Private dataService As DataService = DataService.GetService()
        Private rsi As New RSI() With {
            Key.SeriesName = "RSI"
        }
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Public ReadOnly Property Data() As List(Of Quote)
            Get
                Return dataService.GetSymbolData("box")
            End Get
        End Property
    
        Public ReadOnly Property IndicatorType() As List(Of String)
            Get
                Return New List(Of String)() From {
                    "Relative Strength Index"
                }
            End Get
        End Property
    
        Private Sub OnIndicatorTypeSelectionChanged(sender As Object, e As SelectionChangedEventArgs)
            Dim ser As FinancialSeries = Nothing
            If cbIndicatorType.SelectedIndex = 0 Then
                ser = rsi
            End If
    
    
            If ser IsNot Nothing AndAlso Not indicatorChart.Series.Contains(ser) Then
                indicatorChart.BeginUpdate()
                indicatorChart.Series.Clear()
                indicatorChart.Series.Add(ser)
                indicatorChart.EndUpdate()
            End If
        End Sub
    
        Private Sub OnFinancialChartRendered(sender As Object, e As C1.WPF.Chart.RenderEventArgs)
            If indicatorChart IsNot Nothing Then
                indicatorChart.AxisX.Min = DirectCast(financialChart.AxisX, IAxis).GetMin()
                indicatorChart.AxisX.Max = DirectCast(financialChart.AxisX, IAxis).GetMax()
            End If
        End Sub
    End Class
    
    public partial class Indicators : UserControl
    {
    
        DataService dataService = DataService.GetService();
        RSI rsi = new RSI() { SeriesName = "RSI" };
    
        public Indicators()
        {
            InitializeComponent();
        }
    
        public List<Quote> Data
        {
            get
            {
                return dataService.GetSymbolData("box");
            }
        }
    
        public List<string> IndicatorType
        {
            get
            {
                return new List<string>()
                {
                    "Relative Strength Index",
                };
            }
        }
    
        void OnIndicatorTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FinancialSeries ser = null;
            if (cbIndicatorType.SelectedIndex == 0)
                ser = rsi;
    
    
            if (ser != null && !indicatorChart.Series.Contains(ser))
            {
                indicatorChart.BeginUpdate();
                indicatorChart.Series.Clear();
                indicatorChart.Series.Add(ser);
                indicatorChart.EndUpdate();
            }
        }
    
        void OnFinancialChartRendered(object sender, C1.WPF.Chart.RenderEventArgs e)
        {
            if (indicatorChart != null)
            {
                indicatorChart.AxisX.Min = ((IAxis)financialChart.AxisX).GetMin();
                indicatorChart.AxisX.Max = ((IAxis)financialChart.AxisX).GetMax();
            }
        }
    }