pub struct FullAccountTracker<M> { /* private fields */ }
Expand description

Keep track of many possible Account performance statistics This can be quite memory intensive, easily reaching beyond 10GB if using tick-by-tick data due to the storage of many returns

Implementations§

source§

impl<M> FullAccountTracker<M>where M: Currency + MarginCurrency + Send,

TODO: create its own risk crate out of these implementations for better reusability and testability

source

pub fn new(starting_wb: M) -> Self

Create a new AccTracker struct

source

pub fn absolute_returns(&self, source: &ReturnsSource) -> &Vec<M>

Vector of absolute returns the account has generated, including unrealized pnl # Parameters source: the sampling interval of pnl snapshots

source

pub fn ln_returns(&self, source: &ReturnsSource) -> &Vec<f64>

Vector of natural logarithmic returns the account has generated, including unrealized pnl # Parameters source: the sampling interval of pnl snapshots

source

pub fn profit_loss_ratio(&self) -> Decimal

Ratio of cumulative trade profit over cumulative trade loss

source

pub fn cumulative_fees(&self) -> M

Cumulative fees paid to the exchange

source

pub fn buy_and_hold_return(&self) -> M

Would be return of buy and hold strategy

source

pub fn sell_and_hold_return(&self) -> M

Would be return of sell and hold strategy

source

pub fn sharpe( &self, returns_source: ReturnsSource, risk_free_is_buy_and_hold: bool ) -> f64

Return the sharpe ratio using the selected returns as source

Parameters:

returns_source: the sampling interval of pnl snapshots risk_free_is_buy_and_hold: if true, it will use the market returns as the risk-free comparison else risk-free rate is zero

TODO: annualized depending on the ReturnsSource

source

pub fn sortino( &self, returns_source: ReturnsSource, risk_free_is_buy_and_hold: bool ) -> f64

Return the Sortino ratio based on daily returns data

Parameters:

returns_source: the sampling interval of pnl snapshots risk_free_is_buy_and_hold: if true, it will use the market returns as the risk-free comparison else risk-free rate is zero

TODO: annualized depending on the ReturnsSource

source

pub fn historical_value_at_risk( &self, returns_source: ReturnsSource, percentile: f64 ) -> f64

Calculate the value at risk using the percentile method on daily returns multiplied by starting wallet balance The time horizon N is assumed to be 1 The literature says if you want a larger N, just multiply by N.sqrt(), which assumes standard normal distribution # Arguments returns_source: the sampling interval of pnl snapshots percentile: value between [0.0, 1.0], smaller value will return more worst case results TODO: annualized depending on the ReturnsSource

source

pub fn historical_value_at_risk_from_n_hourly_returns( &self, n: usize, percentile: f64 ) -> f64

Calculate the historical value at risk from n consequtive hourly return values, This should have better statistical properties compared to using daily returns due to having more samples. Set n to 24 for daily value at risk, but with 24x more samples from which to take the percentile, giving a more accurate VaR

Parameters:

n: number of hourly returns to use percentile: value between [0.0, 1.0], smaller value will return more worst case results

source

pub fn cornish_fisher_value_at_risk( &self, returns_source: ReturnsSource, percentile: f64 ) -> f64

Calculate the cornish fisher value at risk based on daily returns of the account # Arguments returns_source: the sampling interval of pnl snapshots percentile: in range [0.0, 1.0], usually something like 0.01 or 0.05

source

pub fn cornish_fisher_value_at_risk_from_n_hourly_returns( &self, n: usize, percentile: f64 ) -> f64

Calculate the corni fisher value at risk from n consequtive hourly return values This should have better statistical properties compared to using daily returns due to having more samples. Set n to 24 for daily value at risk, but with 24x more samples from which to take the percentile, giving a more accurate VaR

Parameters:

n: number of hourly returns to use percentile: value between [0.0, 1.0], smaller value will return more worst case results

source

pub fn num_trading_days(&self) -> u64

Return the number of trading days

source

pub fn d_ratio(&self, returns_source: ReturnsSource) -> f64

Also called discriminant-ratio, which focuses on the added value of the algorithm It uses the Cornish-Fish Value at Risk (CF-VaR) It better captures the risk of the asset as it is not limited by the assumption of a gaussian distribution It it time-insensitive from: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3927058

Parameters

returns_source: the sampling interval of pnl snapshots

source

pub fn annualized_roi(&self) -> f64

Annualized return on investment as a factor, e.g.: 100% -> 2x

source

pub fn max_drawdown_wallet_balance(&self) -> Decimal

Maximum drawdown of the wallet balance

source

pub fn max_drawdown_total(&self) -> Decimal

Maximum drawdown of the wallet balance including unrealized profit and loss

source

pub fn num_trades(&self) -> i64

Return the number of trades the account made

source

pub fn trade_percentage(&self) -> f64

Return the ratio of executed trades vs total trading opportunities Higher values means a more active trading agent

source

pub fn buy_ratio(&self) -> f64

Return the ratio of buy trades vs total number of trades

source

pub fn turnover(&self) -> M

Return the cumulative turnover denoted in margin currency

source

pub fn total_rpnl(&self) -> M

Return the total realized profit and loss of the account

source

pub fn upnl(&self) -> M

Return the current unrealized profit and loss

source

pub fn win_ratio(&self) -> f64

Return the ratio of winning trades vs all trades

source

pub fn limit_order_fill_ratio(&self) -> f64

Return the ratio of filled limit orders vs number of submitted limit orders

source

pub fn limit_order_cancellation_ratio(&self) -> f64

Return the ratio of limit order cancellations vs number of submitted limit orders

Trait Implementations§

source§

impl<M> AccountTracker<M> for FullAccountTracker<M>where M: Currency + MarginCurrency + Send,

source§

fn update(&mut self, timestamp: u64, price: QuoteCurrency, upnl: M)

Update with each tick, using data provided in update_state method of Exchange. Read more
source§

fn log_rpnl(&mut self, rpnl: M)

Log a realized profit and loss event
source§

fn log_fee(&mut self, fee_in_margin: M)

Log a fee, measured in the margin currency
source§

fn log_limit_order_submission(&mut self)

Log a limit order submission event
source§

fn log_limit_order_cancellation(&mut self)

Log a limit order cancellation event
source§

fn log_limit_order_fill(&mut self)

Log a limit order fill event
source§

fn log_trade(&mut self, side: Side, price: QuoteCurrency, size: M::PairedCurrency)

Log a trade event where some order got filled and the position changed
source§

impl<M: Clone> Clone for FullAccountTracker<M>

source§

fn clone(&self) -> FullAccountTracker<M>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<M: Debug> Debug for FullAccountTracker<M>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<M> Display for FullAccountTracker<M>where M: Currency + MarginCurrency + Send,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M> RefUnwindSafe for FullAccountTracker<M>where M: RefUnwindSafe,

§

impl<M> Send for FullAccountTracker<M>where M: Send,

§

impl<M> Sync for FullAccountTracker<M>where M: Sync,

§

impl<M> Unpin for FullAccountTracker<M>where M: Unpin,

§

impl<M> UnwindSafe for FullAccountTracker<M>where M: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.