> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baibaibot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Realtime Trend Data

> Subscribe to realtime trend data from various providers.

<img noZoom src="https://mintcdn.com/baibai/Q0FEDFNQDcX1j3TO/images/separator.jpg?fit=max&auto=format&n=Q0FEDFNQDcX1j3TO&q=85&s=5350c337aa3613424b48079d872b63e6" width="721" height="5" data-path="images/separator.jpg" />

## Possible trend data providers

<CardGroup cols={3}>
  <Card title="Social Media" icon="hashtag">
    • X/Twitter accounts

    • YouTube channels

    • Telegram channels

    • Reddit subreddits
  </Card>

  <Card title="News / Feeds" icon="comments">
    • Financial news websites

    • RSS feeds
  </Card>

  <Card title="External Alerts/Signals" icon="signal-stream">
    • TradingView alerts

    • Custom alerts via Webhook
  </Card>
</CardGroup>

## Using realtime trend data providers

<Note>
  You can register multiple trend data providers.
</Note>

Here is an example that demonstrates how to use the SDK to register to multiple trend data providers:

#### Register the trend data providers in the init function of your signal:

```php theme={null}
// register a trend data provider for Elon Musk's X/Twitter account
$this->_elonMuskX = $this->addTrendDataProvider("elonMuskX", new XTrendDataProvider("elonmusk"));
// register a trend data provider for Telegram channel CatfishcallsbyPoe
$this->_catfishcallsByPoeTelegram = $this->addTrendDataProvider("catfishcallsByPoeTelegram", new TelegramTrendDataProvider("CatfishcallsbyPoe"));
```

#### Use the trend data in the getSignalInfo function of your signal:

```php theme={null}
function getSignalInfo(TradeContext $tradeContext):?TradeSignalInfo
{
	if ($this->_elonMuskX->hasNewData())
	{
		$xPost = $this->_elonMuskX->getLatestContent();

		if (str_contains($xPost, "DOGE"))
		{
			// do something
		}
	}

	if ($this->_catfishcallsByPoeTelegram->hasNewData())
	{
		$telegramMessage = $this->_catfishcallsByPoeTelegram->getLatestContent();

		if (str_contains($telegramMessage, "BAI"))
		{
			// do something
		}
	}

	...
}
```

#### Full example class

```php theme={null}
class MultiTrendDataExample extends UserTradeSignalBase
{
	private ?XTrendDataProvider $_elonMuskX = null;
	private ?TelegramTrendDataProvider $_catfishcallsByPoeTelegram = null;

	protected function init(?ParamValues $params = null)
	{
		// register a trend data provider for Elon Musk's X/Twitter account
		$this->_elonMuskX = $this->addTrendDataProvider("elonMuskX", new XTrendDataProvider("elonmusk"));
		// register a trend data provider for Telegram channel CatfishcallsbyPoe
		$this->_catfishcallsByPoeTelegram = $this->addTrendDataProvider("catfishcallsByPoeTelegram", new TelegramTrendDataProvider("CatfishcallsbyPoe"));
	}

	function getSignalInfo(TradeContext $tradeContext):?TradeSignalInfo
	{
		if ($this->_elonMuskX->hasNewData())
		{
			$xPost = $this->_elonMuskX->getLatestContent();

			if (str_contains($xPost, "DOGE"))
			{
				// do something
			}
		}

		if ($this->_catfishcallsByPoeTelegram->hasNewData())
		{
			$telegramMessage = $this->_catfishcallsByPoeTelegram->getLatestContent();

			if (str_contains($telegramMessage, "BAI"))
			{
				// do something
			}
		}

		...
	}
}
```

<img noZoom src="https://mintcdn.com/baibai/Q0FEDFNQDcX1j3TO/images/separator-small.png?fit=max&auto=format&n=Q0FEDFNQDcX1j3TO&q=85&s=8a26d422be09963b7e375935747563ae" width="721" height="1" data-path="images/separator-small.png" />

## SDK Classes

We currently support the following trend data providers:

```php theme={null}
new XTrendDataProvider(twitterUsername); // hasNewData() triggers on new tweets
new YouTubeTrendDataProvider(channelName); // hasNewData() triggers on new video uploads
new TelegramTrendDataProvider(channelName); // hasNewData() triggers on new messages
new RedditTrendDataProvider(subredditName); // hasNewData() triggers on new posts
new WebsiteTrendDataProvider(websiteUrl); // hasNewData() is not supported. Call getLatestContent() to crawl the website's content
new RSSTrendDataProvider(feedUrl); // hasNewData() triggers on new feed items
new TradingViewTrendDataProvider(alertName); // hasNewData() triggers on new alerts
new WebhookTrendDataProvider(webhookUrl); // hasNewData() triggers on new webhook calls
```

### Class interface

```php theme={null}
interface ITrendDataProvider
{
	// returns true if new data is available
	function hasNewData():bool;

	// returns the latest content
	// NOTE: there could be multiple new contents available since the last call to hasNewData().
	// Use getPreviousContentId() to get the content id of the last fetched content and getContent(x) to iterate over all new contents if needed.
	function getLatestContent():string;

	// returns the content id of the getLatestContent() content
	function getLatestContentId():int

	// returns the content id of the last call to hasNewData()
	function getPreviousContentId():string;

	// returns the content of the specified id
	function getContent(int $id):int;
}
```

<Note>
  Missing a data provider you would like to use? Feel free to <a href="mailto:contact@baibaibot.com">contact us</a>. Adding a new trend data provider is easy due to our plugin based technology.
</Note>
