bybit/models/historical_volatility_request.rs
1use crate::prelude::*;
2
3/// Represents a request to fetch historical volatility data for a specific cryptocurrency on Bybit.
4/// Historical volatility measures the price fluctuation of an asset over a period, crucial for
5/// assessing risk in perpetual futures trading. In perpetual futures, which are contracts without
6/// an expiration date, volatility data helps traders predict price movements and adjust leverage.
7/// Trading bots use this to optimize entry/exit points or hedge positions.
8#[derive(Clone, Default)]
9pub struct HistoricalVolatilityRequest<'a> {
10 /// The base cryptocurrency (e.g., "BTC" for Bitcoin).
11 /// On Bybit, this specifies the asset for which volatility is calculated. For perpetual futures,
12
13 /// the base coin determines the trading pair (e.g., BTCUSD). Bots must validate this field to
14 /// ensure the pair is supported, as an invalid coin will result in an API error.
15 pub base_coin: Option<Cow<'a, str>>,
16
17 /// The time period for volatility calculation (e.g., "7" for 7 days).
18 /// This defines the lookback period for the volatility metric, typically in days. In perpetual
19 /// futures, shorter periods (e.g., 7 days) are used for short-term trading strategies, while
20 /// longer periods (e.g., 30 days) suit risk management. Bots should handle this flexibly to
21 /// adapt to different trading horizons.
22 pub period: Option<Cow<'a, str>>,
23
24 /// The start time for the data range (e.g., "2023-01-01T00:00:00Z").
25 /// Specifies the beginning of the historical data window. In perpetual futures, accurate
26 /// time ranges are critical for backtesting strategies. Bots must ensure the format complies
27 /// with Bybit’s API (ISO 8601) to avoid errors.
28 pub start: Option<Cow<'a, str>>,
29
30 /// The end time for the data range (e.g., "2023-12-31T23:59:59Z").
31 /// Marks the end of the historical data window. This is essential for defining precise data
32 /// sets in trading algorithms. Bots should validate that `end` is later than `start` to prevent
33 /// invalid requests.
34 pub end: Option<Cow<'a, str>>,
35}
36
37impl<'a> HistoricalVolatilityRequest<'a> {
38 /// Creates a default request with BTC as the base coin.
39 /// Useful for quick initialization in trading bots, but developers should override fields as
40 /// needed for specific strategies.
41 pub fn default() -> HistoricalVolatilityRequest<'a> {
42 HistoricalVolatilityRequest::new(Some("BTC"), None, None, None)
43 }
44 /// Constructs a new request with specified parameters.
45 /// Allows fine-grained control over the volatility query, enabling bots to target specific
46 /// coins and timeframes.
47 pub fn new(
48 base_coin: Option<&'a str>,
49 period: Option<&'a str>,
50 start: Option<&'a str>,
51 end: Option<&'a str>,
52 ) -> HistoricalVolatilityRequest<'a> {
53 HistoricalVolatilityRequest {
54 base_coin: base_coin.map(Cow::Borrowed),
55 period: period.map(Cow::Borrowed),
56 start: start.map(Cow::Borrowed),
57 end: end.map(Cow::Borrowed),
58 }
59 }
60}