use super::{PreTradeContext, Rejects};
pub trait CheckPreTradeStartPolicy<O, R> {
fn name(&self) -> &str;
fn check_pre_trade_start(&self, ctx: &PreTradeContext, order: &O) -> Result<(), Rejects>;
fn apply_execution_report(&self, report: &R) -> bool;
}
#[cfg(test)]
mod tests {
use crate::core::{
ExecutionReportOperation, FinancialImpact, OrderOperation, WithExecutionReportOperation,
WithFinancialImpact,
};
use crate::param::{AccountId, Asset, Fee, Pnl, Quantity, Side, TradeAmount};
use crate::pretrade::{CheckPreTradeStartPolicy, PreTradeContext, Rejects};
struct StartPolicyNoop;
type TestOrder = OrderOperation;
type TestReport = WithExecutionReportOperation<WithFinancialImpact<()>>;
impl CheckPreTradeStartPolicy<TestOrder, TestReport> for StartPolicyNoop {
fn name(&self) -> &str {
"StartPolicyNoop"
}
fn check_pre_trade_start(
&self,
_ctx: &PreTradeContext,
_order: &TestOrder,
) -> Result<(), Rejects> {
Ok(())
}
fn apply_execution_report(&self, _report: &TestReport) -> bool {
false
}
}
#[test]
fn apply_execution_report_hook_returns_false_for_noop_start_policy() {
let report = WithExecutionReportOperation {
inner: WithFinancialImpact {
inner: (),
financial_impact: FinancialImpact {
pnl: Pnl::from_str("0").expect("pnl must be valid"),
fee: Fee::ZERO,
},
},
operation: ExecutionReportOperation {
instrument: crate::Instrument::new(
Asset::new("AAPL").expect("asset code must be valid"),
Asset::new("USD").expect("asset code must be valid"),
),
account_id: AccountId::from_u64(99224416),
side: Side::Buy,
},
};
assert!(!StartPolicyNoop.apply_execution_report(&report));
}
#[test]
fn required_trait_methods_can_be_invoked_without_side_effects() {
let order = OrderOperation {
instrument: crate::Instrument::new(
Asset::new("AAPL").expect("asset code must be valid"),
Asset::new("USD").expect("asset code must be valid"),
),
account_id: AccountId::from_u64(99224416),
side: Side::Buy,
trade_amount: TradeAmount::Quantity(
Quantity::from_str("1").expect("quantity must be valid"),
),
price: None,
};
assert_eq!(StartPolicyNoop.name(), "StartPolicyNoop");
assert!(StartPolicyNoop
.check_pre_trade_start(&PreTradeContext::new(), &order)
.is_ok());
}
}