nautilus_analysis/statistics/
profit_factor.rsuse crate::{statistic::PortfolioStatistic, Returns};
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis")
)]
pub struct ProfitFactor {}
impl PortfolioStatistic for ProfitFactor {
type Item = f64;
fn name(&self) -> String {
stringify!(ProfitFactor).to_string()
}
fn calculate_from_returns(&self, returns: &Returns) -> Option<Self::Item> {
if !self.check_valid_returns(returns) {
return Some(f64::NAN);
}
let (positive_returns_sum, negative_returns_sum) =
returns
.values()
.fold((0.0, 0.0), |(pos_sum, neg_sum), &pnl| {
if pnl >= 0.0 {
(pos_sum + pnl, neg_sum)
} else {
(pos_sum, neg_sum + pnl)
}
});
if negative_returns_sum == 0.0 {
return Some(f64::NAN);
}
Some((positive_returns_sum / negative_returns_sum).abs())
}
}
#[cfg(test)]
mod profit_factor_tests {
use std::collections::BTreeMap;
use nautilus_core::nanos::UnixNanos;
use super::*;
fn create_returns(values: Vec<f64>) -> Returns {
let mut new_return = BTreeMap::new();
for (i, value) in values.iter().enumerate() {
new_return.insert(UnixNanos::from(i as u64), *value);
}
new_return
}
#[test]
fn test_empty_returns() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert!(result.unwrap().is_nan());
}
#[test]
fn test_all_positive() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![10.0, 20.0, 30.0]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert!(result.unwrap().is_nan());
}
#[test]
fn test_all_negative() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![-10.0, -20.0, -30.0]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert_eq!(result.unwrap(), 0.0);
}
#[test]
fn test_mixed_returns() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![10.0, -20.0, 30.0, -40.0]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert_eq!(result.unwrap(), 0.6666666666666666);
}
#[test]
fn test_with_zero() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![10.0, 0.0, -20.0, -30.0]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert_eq!(result.unwrap(), 0.2);
}
#[test]
fn test_equal_positive_negative() {
let profit_factor = ProfitFactor {};
let returns = create_returns(vec![20.0, -20.0]);
let result = profit_factor.calculate_from_returns(&returns);
assert!(result.is_some());
assert_eq!(result.unwrap(), 1.0);
}
#[test]
fn test_name() {
let profit_factor = ProfitFactor {};
assert_eq!(profit_factor.name(), "ProfitFactor");
}
}