Expand description
§Alerts scheduler and manager for trading and price alerts
This module provides the functionality to manage and schedule alerts related to trading and price changes. It includes structures for handling alert data, configurations, database interactions, and utilities.
§Getting Started
To use the Resend Email Library, add it to your Cargo.toml:
[dependencies]
trading_alerts = "0.1.0"§Features
- Fetching real-time prices.
- Database Interactions.
- Alert Management.
- Hash Generation.
- Robust error handling for network and API errors.
§Fetching real-time prices
We can fetch real-time prices of any FX symbol using the Xylex API by providing the symbol. Or any other API that provides real-time prices.
The following example demonstrates how to fetch the real-time price of the AUD/CAD symbol using the Xylex API.
§Example
use trade_alerts::data::XylexApi;
#[tokio::main]
async fn main() {
// Initialize the Xylex API client
let xylex_api_result = XylexApi::new_env().await;
// Check if the API client was initialized successfully
let xylex_api = match xylex_api_result {
Ok(api) => api,
Err(e) => {
eprintln!("{:?}", e);
return;
}
};
// Define the symbol for which to fetch the real-time price
let symbol = "aud/cad";
// Fetch the real-time price for the symbol
match xylex_api.request_real_time_price(symbol).await {
Ok(price) => println!("Real-time price for {}: {}", symbol, price),
Err(e) => eprintln!("{:?}", e),
};
}§Database Interactions
We can interact with the Supabase database to store and manage alerts. If you need a Rust SDK for Supabase, you can use the supabase-rs crate.
§Supported Operations
- Configuring the table structure for alerts
- Adding alerts to the database
- Fetching alerts by user ID
- Fetching alerts by hash
We generate an internal unique hash for each alert, which is used to identify and manage alerts.
§Examples
§Prerequisites
To use the Supabase Client, you need to set the initialize the client.
// Initialize Supabase client
let supabase = match Supabase::new_env().await {
Ok(client) => client,
Err(e) => {
eprintln!("{}", e);
return;
},
};§Configuration for tables
We need to setup all the table names so we can route everything accordingly
// Define a TableConfig
let config: TableConfig = TableConfig::new(
"alerts".to_string(),
"hash".to_string(),
"price_level".to_string(),
"user_id".to_string(),
"symbol".to_string(),
);§Add an alert
We first need to create an alert and then add it to the database.
// Create a new alert
let alert: Alert = Alert::new(
Hash { hash: "unique_hash_string".to_string() },
1.2345, // price level
"aud/chf".to_string(), // symbol
"user1234".to_string() // user ID
);
// Adding an alert
match supabase.add_alert(alert.clone(), config.clone()).await {
Ok(_) => println!("Alert added successfully"),
Err(e) => eprintln!("{}", e),
};§Fetch hashes by user ID
// Fetching hashes by user ID
match supabase.fetch_hashes_by_user_id(&alert.user_id, config.clone()).await {
Ok(hashes) => println!("Fetched hashes: {:?}", hashes),
Err(e) => eprintln!("{}", e),
};§Fetch alert details
// Fetching details by hash
match supabase.fetch_details_by_hash(&alert.hash.hash, &config).await {
Ok(details) => println!("Fetched details: {:?}", details),
Err(e) => eprintln!("{}", e),
}; §Alert Management
We assume that the Table config is already set up in this example and your supabase client is initialized.
#[tokio::main]
async fn main() {
// Initialize XylexApi
let xylex_api = match XylexApi::new_env().await {
Ok(api) => api,
Err(e) => {
eprintln!("{}", e);
return;
},
};
let symbols: HashSet<&str> = [
"aud/chf", "eur/usd"
].iter().cloned().collect();
match xylex_api.fetch_prices_for_symbols(
symbols
).await {
Ok(prices) => println!("Prices: {:?}", prices),
Err(e) => eprintln!("{}", e),
};
// Check and delete triggered alerts
match xylex_api.check_and_fetch_triggered_alert_hashes(
&supabase,
&config
).await {
Ok(triggered_hashes) => {
if triggered_hashes.is_empty() {
println!("No triggered alerts.");
return;
}
match xylex_api.delete_triggered_alerts_by_hashes(
&supabase,
&config,
triggered_hashes
).await {
Ok(_) => println!("Successfully deleted triggered alerts"),
Err(e) => eprintln!("{}", e),
}
},
Err(e) => eprintln!("{}", e),
};
}§Hash Generation
use trade_alerts::HashComponents;
let components: HashComponents = HashComponents::new(
100.0,
"user123".to_string(),
"AAPL".to_string()
);
let hash = components.generate_hash().await;
println!("Generated Hash: {}", hash);§Handling Success and Errors
-
Success::SupabaseSuccess: Success outcomes for Supabase operations.Success::XylexApiSuccess: Success outcomes for Xylex API operations.
-
Error::SupabaseError: Errors related to Supabase operations.Error::TableConfigError: Errors related to table configuration.Error::XylexApiError: Errors related to Xylex API operations.
Notes:
- Not all methods are covered in the examples above. Please refer to the documentation for more details.
- This library uses the supabase_rs crate for interacting with the Supabase database.
- Contact us on Discord for any questions or support: @hadi_xlx or @floris_xlx.
Upcoming:
- More detailed examples and use cases.
- Better custom errors with more detailed and linear messaging.
Modules§
- alert
- Module for managing trading and price alerts.
- data
- Data management for incoming price data feeds
- db
- Databasing module for the pricing alerts
- errors
- Error handling and logging module for various service interactions.
- success
- Module for handling and logging success outcomes from different APIs.
- utils
- Utilities for working with Alerts
Structs§
- Alert
- Represents an alert for a specific user intrested in a particular symbol at a certain price level with a unique hash.
- Hash
- A structure representing a hash value associated with an alert.
- Hash
Components - Represents the components used to generate a hash for an alert.