-
Notifications
You must be signed in to change notification settings - Fork 425
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
Read event data in parallel to backtest #124
Draft
garyttierney
wants to merge
3
commits into
nkaz001:master
Choose a base branch
from
garyttierney:parallel-loading-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::{io, io::ErrorKind}; | ||
use std::iter::Peekable; | ||
use bus::{Bus, BusIntoIter, BusReader}; | ||
use tracing::{error, info, info_span}; | ||
|
||
use crate::backtest::{ | ||
data::{read_npy_file, read_npz_file, Data, NpyDTyped}, | ||
BacktestError, | ||
}; | ||
|
||
#[derive(Copy, Clone)] | ||
pub enum EventBusMessage<EventT: Clone> { | ||
Item(EventT), | ||
EndOfData, | ||
} | ||
|
||
pub struct EventBusReader<EventT: Clone + Send + Sync> { | ||
reader: Peekable<BusIntoIter<EventBusMessage<EventT>>>, | ||
} | ||
|
||
impl<EventT: Clone + Send + Sync> EventBusReader<EventT> { | ||
pub fn new(reader: BusReader<EventBusMessage<EventT>>) -> Self { | ||
Self { | ||
reader: reader.into_iter().peekable() | ||
} | ||
} | ||
|
||
pub fn peek(&mut self) -> Option<&EventT> { | ||
self.reader.peek().and_then(|ev| match ev { | ||
EventBusMessage::Item(item) => Some(item), | ||
EventBusMessage::EndOfData => None, | ||
}) | ||
} | ||
|
||
pub fn next(&mut self) -> Option<EventT> { | ||
self.reader.next().and_then(|ev| match ev { | ||
EventBusMessage::Item(item) => Some(item), | ||
EventBusMessage::EndOfData => None, | ||
}) | ||
} | ||
} | ||
|
||
pub trait TimestampedEventQueue<EventT> { | ||
fn next_event(&mut self) -> Option<EventT>; | ||
|
||
fn peek_event(&mut self) -> Option<&EventT>; | ||
|
||
fn event_time(value: &EventT) -> i64; | ||
} | ||
|
||
pub trait EventConsumer<EventT> { | ||
fn is_event_relevant(event: &EventT) -> bool; | ||
|
||
fn process_event(&mut self, event: EventT) -> Result<(), BacktestError>; | ||
} | ||
|
||
fn load_data<EventT: NpyDTyped + Clone + Send>( | ||
filepath: String, | ||
) -> Result<Data<EventT>, BacktestError> { | ||
let data = if filepath.ends_with(".npy") { | ||
read_npy_file(&filepath)? | ||
} else if filepath.ends_with(".npz") { | ||
read_npz_file(&filepath, "data")? | ||
} else { | ||
return Err(BacktestError::DataError(io::Error::new( | ||
ErrorKind::InvalidData, | ||
"unsupported data type", | ||
))); | ||
}; | ||
|
||
Ok(data) | ||
} | ||
|
||
#[tracing::instrument(skip_all)] | ||
pub fn replay_events_to_bus<EventT: NpyDTyped + Clone + Send + 'static>( | ||
mut bus: Bus<EventBusMessage<EventT>>, | ||
mut sources: Vec<String>, | ||
) { | ||
for source in sources.drain(..) { | ||
let source_load_span = info_span!("load_data", source = &source); | ||
let _source_load_span = source_load_span.entered(); | ||
|
||
let data = load_data::<EventT>(source); | ||
|
||
match data { | ||
Ok(data) => { | ||
info!( | ||
records = data.len(), | ||
"found {} events in data source", | ||
data.len() | ||
); | ||
|
||
for row in 0..data.len() { | ||
bus.broadcast(EventBusMessage::Item(data[row].clone())); | ||
} | ||
} | ||
Err(e) => { | ||
error!("encountered error loading data source: {}", e); | ||
// TODO: handle as an error. | ||
break; | ||
} | ||
} | ||
} | ||
|
||
bus.broadcast(EventBusMessage::EndOfData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use std::{ | |
collections::HashMap, | ||
io::{Error as IoError, ErrorKind}, | ||
rc::Rc, | ||
sync::Arc, | ||
}; | ||
|
||
use uuid::Uuid; | ||
|
@@ -60,7 +61,7 @@ where | |
/// Provides a data cache that allows both the local processor and exchange processor to access the | ||
/// same or different data based on their timestamps without the need for reloading. | ||
#[derive(Clone, Debug)] | ||
pub struct Cache<D>(Rc<RefCell<HashMap<String, CachedData<D>>>>) | ||
pub struct Cache<D>(Arc<RefCell<HashMap<String, CachedData<D>>>>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover from the initial prototype, needs reverted. |
||
where | ||
D: POD + Clone; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably won't cause any problems (because this happens in SPMC and we're an SPSC structure), but I think I'll put up some docs anyway.