Skip to content

Commit

Permalink
dev: add docstring and refactor names
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaz001 committed Apr 24, 2024
1 parent e489ded commit ee1ce74
Show file tree
Hide file tree
Showing 26 changed files with 79 additions and 55 deletions.
6 changes: 3 additions & 3 deletions rust/src/backtest/assettype.rs
Expand Up @@ -7,14 +7,14 @@ pub trait AssetType {
fn equity(&self, price: f32, balance: f64, position: f64, fee: f64) -> f64;
}

/// The common type of asset.
/// The common type of asset where the contract's notional value is linear to the quote currency.
#[derive(Clone)]
pub struct LinearAsset {
contract_size: f64,
}

impl LinearAsset {
/// Constructs [`LinearAsset`].
/// Constructs an instance of `LinearAsset`.
pub fn new(contract_size: f64) -> Self {
Self { contract_size }
}
Expand All @@ -37,7 +37,7 @@ pub struct InverseAsset {
}

impl InverseAsset {
/// Constructs [`InverseAsset`].
/// Constructs an instance of `InverseAsset`.
pub fn new(contract_size: f64) -> Self {
Self { contract_size }
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/backtest.rs
Expand Up @@ -10,7 +10,7 @@ use crate::{
Error,
},
depth::{HashMapMarketDepth, MarketDepth},
ty::{Event, OrdType, Order, Side, TimeInForce},
types::{Event, OrdType, Order, Side, TimeInForce},
BuildError,
Interface,
};
Expand Down
15 changes: 14 additions & 1 deletion rust/src/backtest/mod.rs
Expand Up @@ -12,16 +12,29 @@ use crate::{
state::State,
},
depth::MarketDepth,
ty::Event,
types::Event,
BuildError,
};

/// Asset types
pub mod assettype;

/// The backtester
pub mod backtest;

/// Latency and queue position models
pub mod models;

/// OrderBus implementation
pub mod order;

/// Local and exchange models
pub mod proc;

/// The data reader
pub mod reader;

/// The state, such as position, cash, and fees.
pub mod state;

mod evs;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/models/latencies.rs
@@ -1,4 +1,4 @@
use crate::{backtest::reader::Data, ty::Order};
use crate::{backtest::reader::Data, types::Order};

/// Provides the order entry latency and the order response latency.
pub trait LatencyModel {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/models/queue.rs
Expand Up @@ -2,7 +2,7 @@ use std::marker::PhantomData;

use crate::{
depth::MarketDepth,
ty::{Order, Side},
types::{Order, Side},
};

/// Provides an estimation of the order's queue position.
Expand Down
4 changes: 2 additions & 2 deletions rust/src/backtest/order.rs
Expand Up @@ -4,7 +4,7 @@ use std::{
rc::Rc,
};

use crate::ty::Order;
use crate::types::Order;

/// Provides a bus for transporting backtesting orders between the exchange and the local model
/// based on the given timestamp.
Expand All @@ -21,7 +21,7 @@ impl<Q> OrderBus<Q>
where
Q: Clone,
{
/// Constructs [`OrderBus`].
/// Constructs an instance of `OrderBus`.
pub fn new() -> Self {
Self {
order_list: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/proc/local.rs
Expand Up @@ -25,7 +25,7 @@ use crate::{
Error,
},
depth::MarketDepth,
ty::{Event, OrdType, Order, Side, Status, TimeInForce, BUY, SELL},
types::{Event, OrdType, Order, Side, Status, TimeInForce, BUY, SELL},
};

pub struct Local<AT, Q, LM, MD>
Expand Down
4 changes: 2 additions & 2 deletions rust/src/backtest/proc/nopartialfillexchange.rs
Expand Up @@ -28,12 +28,12 @@ use crate::{
Error,
},
depth::{MarketDepth, INVALID_MAX, INVALID_MIN},
ty::{Event, Order, Side, Status, TimeInForce, BUY, SELL},
types::{Event, Order, Side, Status, TimeInForce, BUY, SELL},
};

/// The exchange model without partial fills.
///
/// Support order types: [`crate::ty::OrdType::Limit`]
/// Support order types: [`crate::types::OrdType::Limit`]
/// Support time-in-force: [`TimeInForce::GTC`], [`TimeInForce::GTX`]
///
/// *Conditions for Full Execution*
Expand Down
4 changes: 2 additions & 2 deletions rust/src/backtest/proc/partialfillexchange.rs
Expand Up @@ -28,12 +28,12 @@ use crate::{
Error,
},
depth::{MarketDepth, INVALID_MAX, INVALID_MIN},
ty::{Event, Order, Side, Status, TimeInForce, BUY, SELL},
types::{Event, Order, Side, Status, TimeInForce, BUY, SELL},
};

/// The exchange model with partial fills.
///
/// Support order types: [`crate::ty::OrdType::Limit`]
/// Support order types: [`crate::types::OrdType::Limit`]
/// Support time-in-force: [`TimeInForce::GTC`], [`TimeInForce::FOK`], [`TimeInForce::IOC`],
/// [`TimeInForce::GTX`]
///
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/proc/proc.rs
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::{
backtest::{state::StateValues, Error},
depth::MarketDepth,
ty::{Event, OrdType, Order, Side, TimeInForce},
types::{Event, OrdType, Order, Side, TimeInForce},
};

/// Provides local-specific interaction.
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/reader.rs
Expand Up @@ -11,7 +11,7 @@ use std::{

use crate::{
backtest::Error,
ty::{BUY, DEPTH_CLEAR_EVENT, DEPTH_EVENT, DEPTH_SNAPSHOT_EVENT, SELL, TRADE_EVENT},
types::{BUY, DEPTH_CLEAR_EVENT, DEPTH_EVENT, DEPTH_SNAPSHOT_EVENT, SELL, TRADE_EVENT},
};

pub const EXCH_EVENT: i64 = 1 << 31;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/backtest/state.rs
@@ -1,4 +1,4 @@
use crate::{backtest::assettype::AssetType, ty::Order};
use crate::{backtest::assettype::AssetType, types::Order};

#[derive(Debug)]
pub struct StateValues {
Expand Down
14 changes: 7 additions & 7 deletions rust/src/connector/binancefutures/mod.rs
Expand Up @@ -23,7 +23,7 @@ use crate::{
},
get_precision,
live::AssetInfo,
ty::{Error, ErrorType, LiveEvent, Order, OrderResponse, Position, Status},
types::{Error, ErrorKind, LiveEvent, Order, OrderResponse, Position, Status},
BuildError,
};

Expand Down Expand Up @@ -265,7 +265,7 @@ impl Connector for BinanceFutures {
if let Err(error) = client.cancel_all_orders(symbol).await {
error!(?error, %symbol, "Couldn't cancel all open orders.");
ev_tx
.send(LiveEvent::Error(Error::with(ErrorType::OrderError, error)))
.send(LiveEvent::Error(Error::with(ErrorKind::OrderError, error)))
.unwrap();
error_count += 1;
continue 'connection;
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Connector for BinanceFutures {
// 1000 indicates user data stream starting error.
ev_tx
.send(LiveEvent::Error(Error::with(
ErrorType::Custom(1000),
ErrorKind::Custom(1000),
error,
)))
.unwrap();
Expand Down Expand Up @@ -341,14 +341,14 @@ impl Connector for BinanceFutures {
error!(?error, "A connection error occurred.");
ev_tx
.send(LiveEvent::Error(Error::with(
ErrorType::ConnectionInterrupted,
ErrorKind::ConnectionInterrupted,
error,
)))
.unwrap();
} else {
ev_tx
.send(LiveEvent::Error(Error::new(
ErrorType::ConnectionInterrupted,
ErrorKind::ConnectionInterrupted,
)))
.unwrap();
}
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Connector for BinanceFutures {
.unwrap();
}

tx.send(LiveEvent::Error(Error::with(ErrorType::OrderError, error)))
tx.send(LiveEvent::Error(Error::with(ErrorKind::OrderError, error)))
.unwrap();
}
}
Expand Down Expand Up @@ -468,7 +468,7 @@ impl Connector for BinanceFutures {
.unwrap();
}

tx.send(LiveEvent::Error(Error::with(ErrorType::OrderError, error)))
tx.send(LiveEvent::Error(Error::with(ErrorKind::OrderError, error)))
.unwrap();
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/binancefutures/msg/mod.rs
Expand Up @@ -4,7 +4,7 @@ use serde::{
Deserializer,
};

use crate::ty::{OrdType, Side, Status, TimeInForce};
use crate::types::{OrdType, Side, Status, TimeInForce};

pub mod rest;
pub mod stream;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/binancefutures/msg/rest.rs
Expand Up @@ -9,7 +9,7 @@ use super::{
from_str_to_tif,
from_str_to_type,
};
use crate::ty::{OrdType, Side, Status, TimeInForce};
use crate::types::{OrdType, Side, Status, TimeInForce};

#[derive(Deserialize, Debug)]
#[serde(untagged)]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/binancefutures/msg/stream.rs
Expand Up @@ -8,7 +8,7 @@ use super::{
from_str_to_tif,
from_str_to_type,
};
use crate::ty::{OrdType, Side, Status, TimeInForce};
use crate::types::{OrdType, Side, Status, TimeInForce};

#[derive(Deserialize, Debug)]
pub struct Stream {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/binancefutures/ordermanager.rs
Expand Up @@ -9,7 +9,7 @@ use tracing::{debug, error};

use crate::{
connector::binancefutures::{msg::rest::OrderResponse, BinanceFuturesError},
ty::{Order, Status},
types::{Order, Status},
};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/binancefutures/rest.rs
Expand Up @@ -18,7 +18,7 @@ use crate::{
BinanceFuturesError,
},
live::AssetInfo,
ty::{AsStr, OrdType, Order, Side, Status, TimeInForce},
types::{AsStr, OrdType, Order, Side, Status, TimeInForce},
};

#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions rust/src/connector/binancefutures/ws.rs
Expand Up @@ -24,7 +24,7 @@ use crate::{
ordermanager::OrderManager,
},
live::AssetInfo,
ty::{self, Depth, LiveEvent, Order, OrderResponse, Position, Status, BUY, SELL},
types::{self, Depth, LiveEvent, Order, OrderResponse, Position, Status, BUY, SELL},
};

fn parse_depth(
Expand Down Expand Up @@ -213,7 +213,7 @@ pub async fn connect(
.ok_or(BinanceFuturesError::AssetNotFound)?;
ev_tx.send(
LiveEvent::Trade(
ty::Trade {
types::Trade {
asset_no: asset_info.asset_no,
exch_ts: data.transaction_time * 1_000_000,
local_ts: Utc::now().timestamp_nanos_opt().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connector/mod.rs
@@ -1,6 +1,6 @@
use std::sync::mpsc::Sender;

use crate::ty::{LiveEvent, Order};
use crate::types::{LiveEvent, Order};

pub mod binancefutures;

Expand Down
5 changes: 3 additions & 2 deletions rust/src/depth/btreemarketdepth.rs
Expand Up @@ -3,10 +3,10 @@ use std::collections::BTreeMap;
use super::{ApplySnapshot, MarketDepth, INVALID_MAX, INVALID_MIN};
use crate::{
backtest::reader::Data,
ty::{Event, BUY, SELL},
types::{Event, BUY, SELL},
};

/// Market depth implementation based on an B-Tree map.
/// L2 Market depth implementation based on a B-Tree map.
///
/// If feed data is missing, it may result in the crossing of the best bid and ask, making it
/// impossible to restore them to the most recent values through natural refreshing.
Expand All @@ -21,6 +21,7 @@ pub struct BTreeMarketDepth {
}

impl BTreeMarketDepth {
/// Constructs an instance of `BTreeMarketDepth`.
pub fn new(tick_size: f32, lot_size: f32) -> Self {
Self {
tick_size,
Expand Down
5 changes: 3 additions & 2 deletions rust/src/depth/hashmapmarketdepth.rs
Expand Up @@ -3,10 +3,10 @@ use std::collections::{hash_map::Entry, HashMap};
use super::{ApplySnapshot, MarketDepth, INVALID_MAX, INVALID_MIN};
use crate::{
backtest::reader::Data,
ty::{Event, BUY, SELL},
types::{Event, BUY, SELL},
};

/// HashMap-based Market Depth
/// L2 Market depth implementation based on a hash map.
///
/// This is considered more robust than a BTreeMap-based Market Depth. This is because in the
/// BTreeMap-based approach, missing depth feeds can lead to incorrect best bid or ask prices.
Expand Down Expand Up @@ -49,6 +49,7 @@ fn depth_above(depth: &HashMap<i32, f32>, start: i32, end: i32) -> i32 {
}

impl HashMapMarketDepth {
/// Constructs an instance of `HashMapMarketDepth`.
pub fn new(tick_size: f32, lot_size: f32) -> Self {
Self {
tick_size,
Expand Down
4 changes: 2 additions & 2 deletions rust/src/depth/mod.rs
@@ -1,4 +1,4 @@
use crate::{backtest::reader::Data, ty::Event};
use crate::{backtest::reader::Data, types::Event};

mod btreemarketdepth;
mod hashmapmarketdepth;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait MarketDepth {
timestamp: i64,
) -> (i32, i32, i32, f32, f32, i64);

/// Clears the market depth. If the `side` is neither [crate::ty::BUY] nor [crate::ty::SELL],
/// Clears the market depth. If the `side` is neither [crate::types::BUY] nor [crate::types::SELL],
/// both sides are cleared. In this case, `clear_upto_price` is ignored.
fn clear_depth(&mut self, side: i64, clear_upto_price: f32);

Expand Down

0 comments on commit ee1ce74

Please sign in to comment.