Skip to content

Data provider

andriypohorilko edited this page Apr 14, 2022 · 5 revisions

DefaultCandleDataProvider and DataManager are objects that simplify data manipulation in your app.

DataManager is responsible for storing data and accessing it by the DataSourceId. First, you register your ID, then put some values under it. Later, you can access those values where needed.

DefaultCandleDataProvider is responsible for adding candlesticks. It has xValues, openValues, highValues, lowValues, closeValues and volumeValues along with corresponding id's that you can use to get access to those values later.

Let's take a look, at how the data flow works on the RSIStudy example.

  1. At the beginning, create a DefaultCandleDataProvider instance and set it to SciFinanceChart.candleDataProvider property. DefaultCandleDataProvider registers all candlesticks IDs in the DataManager and stores corresponding values under the hood.
val chart = findViewById<SciFinanceChart>(R.id.financeChart)
val candleDataProvider = DefaultCandleDataProvider()
chart.candleDataProvider = candleDataProvider
  1. Then, use dataProvider to fill the data manager with some candles:
// Assume you get some candles from a server
for (candlestick in candles) {
    dataProvider.xValues.addTime(candlestick.openTime)
    dataProvider.openValues.add(candlestick.open)
    dataProvider.highValues.add(candlestick.high)
    dataProvider.lowValues.add(candlestick.low)
    dataProvider.closeValues.add(candlestick.close)
    dataProvider.volumeValues.add(candlestick.volume)
}
  1. When we create our Studies, we specify Values ID's, that we will need to get access to. Since RSIStudy need xValues and close values we pass as a constructor parameters corresponding IDs, that have been registered previously in our DefaultCandleDataProvider - DataSourceId("xValues") and DataSourceId("close"):
class RSIStudy(
    pane: PaneId,
    id: StudyId = StudyId.uniqueId("RSI"),
    xValuesId: DataSourceId = DataSourceId("xValues"),
    yValuesId: DataSourceId = DataSourceId("close"),
) : CandleStudyBase(pane, id) {
    ...
}
  1. Next we send yValuesId to the RSIIndicator. It gets yValues from the DataManager and uses it to perform RSI calculations. When the job is done it saves the outputValues to the data manager under specific rsiOutputId. Here is how it looks in code in short form:
// create rsiIndicator with yValuesId and rsiOutputId
val rsiIndicator = TALibIndicatorProvider.RSIIndicator(defaultPeriod, yValuesId, rsiOutputId)

// register outputValues under yValuesId in DataManager
dataManager.registerYValuesSource(rsiOutputId, outputValues)

// calculate RSI and write computed values to the outputValues
core.rsi(startIndex, endIndex, inputValues, period.value, outStart, outLength, outputValues)
  1. In order to send RSI values to the chart we create a LineFinanceSeries with our xValuesId and rsiOutputId alongside with other parameters. FinanceSeries gets corresponding values from the DataManager and adds it to the renderableSeries.dataSeries:
// create rsiSeries with xValuesId and rsiIndicator.outputId
val rsiSeries = LineFinanceSeries(R.string.rsiIndicatorName, xValuesId, rsiIndicator.outputId, yAxisId)

// get values from the DataManager
val xValues = dataManager.getXValues(xValues.value)
val yValues = dataManager.getYValues(yValues.value)

// Add values to the dataSeries
dataSeries.append(xValues, yValues)

That's it. The rest is handled by the SciChart library and finally, our nice blue RSI line appears on the screen:

RSI-14-close

NOTE: Most likely, you'd want to display some real-time data, coming from the WebSocket. To see it in action, please try our SciTrader Android and iOS app.

SciTrader Real-Time