Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multithreading runtime #46

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 83 additions & 78 deletions tests/staticweight_test_multi_strat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use alator::broker::implement::multi::{ConcurrentBroker, ConcurrentBrokerBuilder
use alator::broker::{BrokerCost, Dividend, Quote};
use alator::simcontext::{SimContextMulti, SimContextMultiBuilder};
use alator::types::{CashValue, Frequency, PortfolioAllocation};
use tokio::runtime::Runtime;

fn build_data(clock: Clock) -> DefaultPriceSource {
let price_dist = Uniform::new(90.0, 100.0);
Expand All @@ -35,86 +36,90 @@ fn build_data(clock: Clock) -> DefaultPriceSource {
price_source
}

#[tokio::test]
async fn staticweight_integration_test() {
#[test]
fn staticweight_integration_test() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let initial_cash: CashValue = 100_000.0.into();
let length_in_days: i64 = 1000;
let start_date: i64 = 1609750800; //Date - 4/1/21 9:00:0000
let clock = ClockBuilder::with_length_in_days(start_date, length_in_days)
.with_frequency(&Frequency::Daily)
.build();

let data = build_data(clock.clone());

let mut first_weights: PortfolioAllocation = PortfolioAllocation::new();
first_weights.insert("ABC", 0.5);
first_weights.insert("BCD", 0.5);

let mut second_weights: PortfolioAllocation = PortfolioAllocation::new();
second_weights.insert("ABC", 0.3);
second_weights.insert("BCD", 0.7);

let mut third_weights: PortfolioAllocation = PortfolioAllocation::new();
third_weights.insert("ABC", 0.7);
third_weights.insert("BCD", 0.3);

let mut exchange = ConcurrentExchangeBuilder::new()
.with_price_source(data)
.with_clock(clock.clone())
.build();

let simbrkr_first: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
let rt = Runtime::new()?;
rt.block_on(async {
let initial_cash: CashValue = 100_000.0.into();
let length_in_days: i64 = 1000;
let start_date: i64 = 1609750800; //Date - 4/1/21 9:00:0000
let clock = ClockBuilder::with_length_in_days(start_date, length_in_days)
.with_frequency(&Frequency::Daily)
.build();

let data = build_data(clock.clone());

let mut first_weights: PortfolioAllocation = PortfolioAllocation::new();
first_weights.insert("ABC", 0.5);
first_weights.insert("BCD", 0.5);

let mut second_weights: PortfolioAllocation = PortfolioAllocation::new();
second_weights.insert("ABC", 0.3);
second_weights.insert("BCD", 0.7);

let mut third_weights: PortfolioAllocation = PortfolioAllocation::new();
third_weights.insert("ABC", 0.7);
third_weights.insert("BCD", 0.3);

let mut exchange = ConcurrentExchangeBuilder::new()
.with_price_source(data)
.with_clock(clock.clone())
.build();

let simbrkr_first: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
.await;

let strat_first = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_first)
.with_weights(first_weights)
.with_clock(clock.clone())
.default();

let simbrkr_second: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
.await;

let strat_second = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_second)
.with_weights(second_weights)
.with_clock(clock.clone())
.default();

let simbrkr_third: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
.await;

let strat_third = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_third)
.with_weights(third_weights)
.with_clock(clock.clone())
.default();

let mut sim: SimContextMulti<
Dividend,
Quote,
DefaultPriceSource,
AsyncStaticWeightStrategy<Dividend, DefaultCorporateEventsSource, Quote>,
> = SimContextMultiBuilder::new()
.with_clock(clock.clone())
.with_exchange(exchange)
.add_strategy(strat_first)
.add_strategy(strat_second)
.add_strategy(strat_third)
.init(&initial_cash)
.await;

let strat_first = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_first)
.with_weights(first_weights)
.with_clock(clock.clone())
.default();
sim.run().await;

let simbrkr_second: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
.await;

let strat_second = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_second)
.with_weights(second_weights)
.with_clock(clock.clone())
.default();

let simbrkr_third: ConcurrentBroker<Dividend, DefaultCorporateEventsSource, Quote> =
ConcurrentBrokerBuilder::new()
.with_trade_costs(vec![BrokerCost::Flat(1.0.into())])
.build(&mut exchange)
.await;

let strat_third = AsyncStaticWeightStrategyBuilder::new()
.with_brkr(simbrkr_third)
.with_weights(third_weights)
.with_clock(clock.clone())
.default();

let mut sim: SimContextMulti<
Dividend,
Quote,
DefaultPriceSource,
AsyncStaticWeightStrategy<Dividend, DefaultCorporateEventsSource, Quote>,
> = SimContextMultiBuilder::new()
.with_clock(clock.clone())
.with_exchange(exchange)
.add_strategy(strat_first)
.add_strategy(strat_second)
.add_strategy(strat_third)
.init(&initial_cash)
.await;

sim.run().await;

let _perf = sim.perf(Frequency::Daily);
let _perf = sim.perf(Frequency::Daily);
});
Ok(())
}
Loading