Price Alerts
Overview
The FCLite API provides a robust platform for trading on financial markets, but it currently lacks native support for price alerts.
Price alerts are essential for monitoring specific conditions (e.g., price thresholds) and triggering notifications when those conditions are met.
Your Opportunity: Custom Price Alerts Extend FCLite by creating custom price alert functionality:
- Price Change Listeners: Implement mechanisms to listen for real-time price changes in instruments.
- Threshold Conditions: Define conditions (e.g., price above/below a threshold) that trigger alerts.
- Notification Methods: Support browser alerts, email notifications, SMS, or push notifications.
- User Preferences: Allow users to configure their preferred alert settings.
Benefits of Price Alerts: + Monitor market movements efficiently. + Execute trading strategies based on real-time data. + Stay informed without constant manual monitoring.
Integration Steps: + Integrate your custom price alert logic with the existing FCLite API. + Extend methods to handle additional parameters related to conditions, thresholds, and notification methods. + Rigorously test your implementation.
Remember that while enhancing FCLite, security and privacy standards must be maintained. Your custom price alert feature can significantly enhance the trading experience for FCLite users.
Lets go through the steps of how to create a price alert.
Step 1
Price Change Listeners
Wait for Real-Time Prices:
Please refer to the article Get a price update for an instrument for instructions on how to do this.
In that article you will see that we create an instance of the OffersWorker class.
const offersWorker = new OffersWorker();
Then we subscribe to the listeners that are triggered when prices change or update
// Subscribe to state changes
session.getOffersManager().subscribeStateChange(new offersStateListener());
// Subscribe to offer changes
session.getOffersManager().subscribeOfferChange(new offersChangeListener());
// Optionally update the initial state
offersWorker.updateState(session.getOffersManager().getState());
Inside the OffersChangeListener class is where you should handle price updates and compare the old and new prices.
Lets look in more detail:
- OffersChangeListener Class: The OffersChangeListener class is responsible for receiving updates related to offers (which include price changes). When an offer's price changes, this listener is notified.
- Handling Price Updates: Within the OffersChangeListener, you can implement logic to compare the old (previous) price with the new price. Based on your predefined conditions, you can then trigger alerts or take other actions.
Here is an example of how you might implement this:
class OffersChangeListener {
constructor() {
// Initialize any necessary state or variables
this.previousPrices = {}; // Map offerId to previous price
}
// Called when an offer's price changes
onOfferChange(offer) {
const offerId = offer.getOfferId();
const newPrice = offer.getAsk(); // Assuming you're interested in the ask price
// Retrieve the previous price (if available)
const prevPrice = this.previousPrices[offerId];
if (prevPrice !== undefined) {
if (newPrice > prevPrice) {
console.log(`Price increased for offer ${offerId}: ${prevPrice} to ${newPrice}`);
// Trigger your alert (e.g., send a notification)
// ...
} else if (newPrice < prevPrice) {
console.log(`Price decreased for offer ${offerId}: ${prevPrice} to ${newPrice}`);
// Trigger another type of alert
// ...
} else {
console.log(`Price remained unchanged for offer ${offerId}: ${newPrice}`);
// Handle unchanged price scenario
// ...
}
}
// Update the previous price for the next comparison
this.previousPrices[offerId] = newPrice;
}
}
// Example usage:
const offersChangeListener = new OffersChangeListener();
// Subscribe the listener to offer changes
session.getOffersManager().subscribeOfferChange(offersChangeListener);
Step 2
Next we need to define threshold conditions for price alerts. The following is just a basic example, however this can get quite complicated.
const currentPrice = 60; // Replace with the actual current price
const thresholdPrice = 55; // Set your desired threshold price
if (currentPrice > thresholdPrice) {
console.log(`Price is above the threshold (${thresholdPrice}). Alert triggered!`);
// You can replace the console.log with your preferred alert mechanism (e.g., notifications).
} else if (currentPrice < thresholdPrice) {
console.log(`Price is below the threshold (${thresholdPrice}). Alert triggered!`);
// Handle the condition when the price is below the threshold.
} else {
console.log(`Price is exactly at the threshold (${thresholdPrice}). No action needed.`);
// Handle the case when the price matches the threshold.
}
Step 3
Notification Methods
We won't go into the notification examples as they will be different for each programming language and method that you are using, however the following are some common methods.
Browser Alerts:
Use browser notifications to alert the user when conditions are met. Show a pop-up notification in the user's browser.
Email Alerts:
Send an email to the user with details of the triggered alert. Integrate with an email service (e.g., SendGrid, SMTP).
SMS Alerts:
Send SMS notifications to the user's registered phone number. Integrate with an SMS gateway service.
| Table of Contents | |
|---|---|
| Get Price Updates for an Instrument The article teaches how to receive price updates for an instrument. |
|
| Get Historical Prices for an Instrument The article teaches how to receive historical prices for an instrument. |