pub trait IndicatorRef:
Clone
+ Send
+ Sync
+ 'static {
// Required methods
fn key(&self) -> String;
fn required_indicators(&self) -> Vec<(String, Indicator)>;
fn value(&self, ctx: &StrategyContext<'_>) -> Option<f64>;
fn prev_value(&self, ctx: &StrategyContext<'_>) -> Option<f64>;
}Expand description
A reference to a value that can be compared in conditions.
This is the building block for creating conditions. Each indicator reference knows:
- Its unique key for storing computed values
- What indicators it requires
- How to retrieve its value from the strategy context
§Implementing Custom References
ⓘ
use finance_query::backtesting::refs::IndicatorRef;
#[derive(Clone)]
struct MyCustomRef {
period: usize,
}
impl IndicatorRef for MyCustomRef {
fn key(&self) -> String {
format!("my_custom_{}", self.period)
}
fn required_indicators(&self) -> Vec<(String, Indicator)> {
vec![(self.key(), Indicator::Sma(self.period))]
}
fn value(&self, ctx: &StrategyContext) -> Option<f64> {
ctx.indicator(&self.key())
}
fn prev_value(&self, ctx: &StrategyContext) -> Option<f64> {
ctx.indicator_prev(&self.key())
}
}Required Methods§
Sourcefn key(&self) -> String
fn key(&self) -> String
Unique key for storing computed values in the context.
This key is used to look up pre-computed indicator values
in the StrategyContext::indicators map.
Sourcefn required_indicators(&self) -> Vec<(String, Indicator)>
fn required_indicators(&self) -> Vec<(String, Indicator)>
Required indicators to compute this reference.
Returns a list of (key, Indicator) pairs that must be pre-computed by the backtest engine before the strategy runs.
Sourcefn value(&self, ctx: &StrategyContext<'_>) -> Option<f64>
fn value(&self, ctx: &StrategyContext<'_>) -> Option<f64>
Get the value at current candle index from context.
Sourcefn prev_value(&self, ctx: &StrategyContext<'_>) -> Option<f64>
fn prev_value(&self, ctx: &StrategyContext<'_>) -> Option<f64>
Get the value at the previous candle index.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.