Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Calum committed Jun 25, 2024
1 parent 7aaec2f commit a5f1c31
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 58 deletions.
2 changes: 1 addition & 1 deletion example_clients/alator/benches/sim_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use alator::broker::uist::UistBrokerBuilder;
use alator::broker::{BrokerCost, CashOperations, SendOrder, Update};
use criterion::{criterion_group, criterion_main, Criterion};
use std::collections::HashMap;

use alator::strategy::staticweight::StaticWeightStrategyBuilder;
use rotala::http::uist::uistv1_client::{TestClient, UistClient};
Expand Down
29 changes: 8 additions & 21 deletions example_clients/alator/src/broker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
//! with optional [BrokerCost].
use std::{
collections::HashMap, error::Error, fmt::{Display, Formatter}, ops::Deref
collections::HashMap,
error::Error,
fmt::{Display, Formatter},
ops::Deref,
};

use log::info;
Expand Down Expand Up @@ -248,12 +251,7 @@ impl BrokerCost {

//Returns a valid trade given trading costs given a current budget
//and price of security
pub fn trade_impact(
&self,
gross_budget: &f64,
gross_price: &f64,
is_buy: bool,
) -> (f64, f64) {
pub fn trade_impact(&self, gross_budget: &f64, gross_price: &f64, is_buy: bool) -> (f64, f64) {
let mut net_budget = *gross_budget;
let mut net_price = *gross_price;
match self {
Expand Down Expand Up @@ -612,9 +610,7 @@ pub trait BrokerOperations<O: BrokerOrder, Q: BrokerQuote>:
let positions = self.get_positions();
let mut sell_orders: Vec<O> = Vec::new();
for ticker in positions {
let position_value = self
.get_position_value(&ticker)
.unwrap_or(0.0);
let position_value = self.get_position_value(&ticker).unwrap_or(0.0);
//Position won't generate enough cash to fulfill total order
//Create orders for selling 100% of position, continue
//to next position to see if we can generate enough cash
Expand Down Expand Up @@ -742,9 +738,7 @@ pub trait BrokerOperations<O: BrokerOrder, Q: BrokerQuote>:
};

for symbol in target_weights.keys() {
let curr_val = self
.get_position_value(symbol)
.unwrap_or(0.0);
let curr_val = self.get_position_value(symbol).unwrap_or(0.0);
//Iterating over target_weights so will always find value
let target_val = total_value * target_weights.get(symbol).unwrap();
let diff_val = target_val - curr_val;
Expand Down Expand Up @@ -888,12 +882,7 @@ impl StrategySnapshot {
}
}

pub fn real(
date: DateTime,
portfolio_value: f64,
net_cash_flow: f64,
inflation: f64,
) -> Self {
pub fn real(date: DateTime, portfolio_value: f64, net_cash_flow: f64, inflation: f64) -> Self {
Self {
date,
portfolio_value,
Expand All @@ -902,5 +891,3 @@ impl StrategySnapshot {
}
}
}


42 changes: 12 additions & 30 deletions example_clients/alator/src/broker/uist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use rotala::http::uist::uistv1_client::{BacktestId, UistClient};
use crate::{broker::BrokerOrder, strategy::staticweight::StaticWeightBroker};

use super::{
BrokerCost, BrokerEvent, BrokerOperations, BrokerState, BrokerStates, CashOperations, Clock, DateTime, Portfolio, PortfolioHoldings, Quote, SendOrder, Update
BrokerCost, BrokerEvent, BrokerOperations, BrokerState, BrokerStates, CashOperations, Clock,
DateTime, Portfolio, PortfolioHoldings, Quote, SendOrder, Update,
};

type UistBrokerEvent = BrokerEvent<Order>;
Expand Down Expand Up @@ -141,9 +142,7 @@ impl<C: UistClient> SendOrder<Order> for UistBroker<C> {
OrderType::MarketSell | OrderType::LimitSell | OrderType::StopSell => quote.bid,
};

if let Err(_err) =
self.client_has_sufficient_cash::<OrderType>(&order, &price)
{
if let Err(_err) = self.client_has_sufficient_cash::<OrderType>(&order, &price) {
info!(
"BROKER: Unable to send {:?} order for {:?} shares of {:?} to exchange",
order.get_order_type(),
Expand Down Expand Up @@ -192,11 +191,9 @@ impl<C: UistClient> SendOrder<Order> for UistBroker<C> {
let symbol = order.get_symbol().to_string();
if let Some(position) = self.pending_orders.get(order.get_symbol()) {
let existing = *position + order_effect;
self.pending_orders
.insert(symbol, existing);
self.pending_orders.insert(symbol, existing);
} else {
self.pending_orders
.insert(symbol, order_effect);
self.pending_orders.insert(symbol, order_effect);
}
info!(
"BROKER: Successfully sent {:?} order for {:?} shares of {:?} to exchange",
Expand Down Expand Up @@ -262,8 +259,7 @@ impl<C: UistClient> Update for UistBroker<C> {
if updated_pending == 0.0 {
self.pending_orders.remove(&trade.symbol);
} else {
self.pending_orders
.insert(trade.symbol, updated_pending);
self.pending_orders.insert(trade.symbol, updated_pending);
}

self.last_seen_trade += 1;
Expand Down Expand Up @@ -542,9 +538,7 @@ mod tests {
let cash = brkr.get_cash_balance();
assert!(cash < 100_000.0);

let qty = brkr
.get_position_qty("ABC")
.unwrap_or(0.0);
let qty = brkr.get_position_qty("ABC").unwrap_or(0.0);
assert_eq!(qty, 495.00);
}

Expand Down Expand Up @@ -673,19 +667,15 @@ mod tests {

//Missing live quote for BCD
brkr.check().await;
let value = brkr
.get_position_value("BCD")
.unwrap_or(f64::from(0.0));
let value = brkr.get_position_value("BCD").unwrap_or(f64::from(0.0));
println!("{:?}", value);
//We test against the bid price, which gives us the value exclusive of the price paid at ask
assert!(value == 10.0 * 100.0);

//BCD has quote again
brkr.check().await;

let value1 = brkr
.get_position_value("BCD")
.unwrap_or(f64::from(0.0));
let value1 = brkr.get_position_value("BCD").unwrap_or(f64::from(0.0));
println!("{:?}", value1);
assert!(value1 == 12.0 * 100.0);
}
Expand Down Expand Up @@ -782,9 +772,7 @@ mod tests {
let res = brkr.send_order(Order::market_buy("ABC", 50.0));
assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
assert_eq!(
*brkr
.get_holdings_with_pending()
.get("ABC").unwrap_or(&0.0),
*brkr.get_holdings_with_pending().get("ABC").unwrap_or(&0.0),
50.0
);
brkr.check().await;
Expand All @@ -794,10 +782,7 @@ mod tests {
let res = brkr.send_order(Order::market_sell("ABC", 10.0));
assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
assert_eq!(
*brkr
.get_holdings_with_pending()
.get("ABC")
.unwrap_or(&0.0),
*brkr.get_holdings_with_pending().get("ABC").unwrap_or(&0.0),
40.0
);
brkr.check().await;
Expand All @@ -807,10 +792,7 @@ mod tests {
let res = brkr.send_order(Order::market_buy("ABC", 50.0));
assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
assert_eq!(
*brkr
.get_holdings_with_pending()
.get("ABC")
.unwrap_or(&0.0),
*brkr.get_holdings_with_pending().get("ABC").unwrap_or(&0.0),
90.0
);
brkr.check().await;
Expand Down
3 changes: 2 additions & 1 deletion example_clients/alator/src/strategy/staticweight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::marker::PhantomData;
use log::info;

use crate::broker::{
BrokerCashEvent, BrokerOperations, BrokerOrder, BrokerQuote, BrokerStates, CashOperations, Clock, Portfolio, SendOrder, StrategySnapshot, Update
BrokerCashEvent, BrokerOperations, BrokerOrder, BrokerQuote, BrokerStates, CashOperations,
Clock, Portfolio, SendOrder, StrategySnapshot, Update,
};
use crate::perf::{BacktestOutput, PerformanceCalculator};
use crate::schedule::{DefaultTradingSchedule, TradingSchedule};
Expand Down
2 changes: 1 addition & 1 deletion example_clients/alator/tests/staticweight_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use alator::broker::uist::UistBrokerBuilder;
use alator::broker::BrokerCost;

use alator::strategy::staticweight::{StaticWeightStrategyBuilder, PortfolioAllocation};
use alator::strategy::staticweight::{PortfolioAllocation, StaticWeightStrategyBuilder};
use rotala::http::uist::uistv1_client::{TestClient, UistClient};
use rotala::input::penelope::Penelope;

Expand Down
5 changes: 1 addition & 4 deletions rotala/src/http/jura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ impl AppState {
}
}

pub fn tick(
&mut self,
backtest_id: BacktestId,
) -> Option<TickResult> {
pub fn tick(&mut self, backtest_id: BacktestId) -> Option<TickResult> {
if let Some(backtest) = self.backtests.get_mut(&backtest_id) {
if let Some(dataset) = self.datasets.get(&backtest.dataset_name) {
let mut has_next = false;
Expand Down

0 comments on commit a5f1c31

Please sign in to comment.