Skip to main content

Using OHLCV realtime market data

You can register multiple OHLCV data sources accross different timeframes and exchanges.
Using multiple OHLCV data sources can help you build more complex signals. As an example in the crypto market, usually when BTC goes up/down, the altcoins follow that trend. By using multiple OHLCV data sources, you can build a signal that takes into account the BTC price movement and the altcoin price movement.
Here is an example that demonstrates how to use the SDK to register to multiple OHLCV data sources:

Register the OHLCV data sources in the init function of your signal:

// register a 5 minute OHLCV data source for the default exchange and asset pair the signal runs on (e.g. AAPL on IBKR) with a 5 entry history
$this->addOHLCVData("min5", OHLCVDataPeriod::Min5, 5);
// register a 1 minute OHLCV data source for the ETH/USDT pair on Binance with a 10 entry history
$this->addOHLCVDataSeries("eth_min1", OHLCVDataFetcherBinance::Id, CryptoAsset::USDT(), CryptoAsset::ETH(), OHLCVDataPeriod::Min1, 10);

Use the OHLCV data in the getSignalInfo function of your signal:

function getSignalInfo(TradeContext $tradeContext):?TradeSignalInfo
{
	foreach ($tradeContext->dataSeries as $key => $dataSeries)
	{
		if (!$dataSeries)
		{
			// we have no data for this data series
			continue;
		}

		// do something with the data series
		$ohlcvEntry = $dataSeries->data[0];

		// do something with the OHLCV entry
		// $ohlcvEntry->openTime;
		// $ohlcvEntry->closeTime;
		// $ohlcvEntry->low;
		// $ohlcvEntry->high;
		// $ohlcvEntry->open;
		// $ohlcvEntry->close;
		// $ohlcvEntry->volume;
		// $ohlcvEntry->previousEntry()->close;
	}

	...
}

Full example class

class MultiOHLCVDataExample extends UserTradeSignalBase
{
	protected function init(?ParamValues $params = null)
	{
		// NOTE: we could use the $params variable to pass the entry lengths for the OHLCV data sources and other parameters that are hard-coded in this example
		// register a 5 minute OHLCV data source for the default exchange and asset pair the signal runs on (e.g. AAPL on IBKR) with a 5 entry history
		$this->addOHLCVData("min5", OHLCVDataPeriod::Min5, 5);
		// register a 1 minute OHLCV data source for the ETH/USDT pair on Binance with a 10 entry history
		$this->addOHLCVDataSeries("eth_min1", OHLCVDataFetcherBinance::Id, CryptoAsset::USDT(), CryptoAsset::ETH(), OHLCVDataPeriod::Min1, 10);
	}

	function getSignalInfo(TradeContext $tradeContext):?TradeSignalInfo
	{
		foreach ($tradeContext->dataSeries as $key => $dataSeries)
		{
			if (!$dataSeries)
			{
				// we have no data for this data series
				continue;
			}

			// do something with the data series
			$ohlcvEntry = $dataSeries->data[0];

			// do something with the OHLCV entry
			// $ohlcvEntry->openTime;
			// $ohlcvEntry->closeTime;
			// $ohlcvEntry->low;
			// $ohlcvEntry->high;
			// $ohlcvEntry->open;
			// $ohlcvEntry->close;
			// $ohlcvEntry->volume;
			// $ohlcvEntry->previousEntry()->close;
		}

		return null;
	}
}