Add asset status management

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-02-09 15:43:42 +00:00
parent 76bf2fddcb
commit dee21d5324
8 changed files with 249 additions and 15 deletions

View File

@@ -2,7 +2,7 @@ use super::ThreadType;
use crate::{
config::Config,
database,
types::{alpaca::websocket, news::Prediction, Bar, News},
types::{alpaca::websocket, news::Prediction, Bar, Class, News},
utils::add_slash_to_pair,
};
use async_trait::async_trait;
@@ -222,6 +222,7 @@ async fn handle_websocket_message(
struct BarsHandler {
config: Arc<Config>,
subscription_message_constructor: fn(Vec<String>) -> websocket::outgoing::subscribe::Message,
}
#[async_trait]
@@ -230,7 +231,7 @@ impl Handler for BarsHandler {
&self,
symbols: Vec<String>,
) -> websocket::outgoing::subscribe::Message {
websocket::outgoing::subscribe::Message::new_market(symbols)
(self.subscription_message_constructor)(symbols)
}
async fn handle_parsed_websocket_message(
@@ -291,11 +292,40 @@ impl Handler for BarsHandler {
.await
.unwrap();
}
websocket::incoming::Message::Success(_) => {}
websocket::incoming::Message::Status(message) => {
debug!(
"Received status message for {}: {}.",
message.symbol, message.status_message
);
match message.status {
websocket::incoming::status::Status::TradingHalt
| websocket::incoming::status::Status::VolatilityTradingPause => {
database::assets::update_status_where_symbol(
&self.config.clickhouse_client,
&message.symbol,
false,
)
.await
.unwrap();
}
websocket::incoming::status::Status::Resume
| websocket::incoming::status::Status::TradingResumption => {
database::assets::update_status_where_symbol(
&self.config.clickhouse_client,
&message.symbol,
true,
)
.await
.unwrap();
}
_ => {}
}
}
websocket::incoming::Message::Error(message) => {
error!("Received error message: {}.", message.message);
}
websocket::incoming::Message::News(_) => unreachable!(),
_ => unreachable!(),
}
}
}
@@ -396,20 +426,26 @@ impl Handler for NewsHandler {
.await
.unwrap();
}
websocket::incoming::Message::Success(_) => {}
websocket::incoming::Message::Error(message) => {
error!("Received error message: {}.", message.message);
}
websocket::incoming::Message::Bar(_) | websocket::incoming::Message::UpdatedBar(_) => {
unreachable!()
}
_ => unreachable!(),
}
}
}
pub fn create_handler(thread_type: ThreadType, config: Arc<Config>) -> Box<dyn Handler> {
match thread_type {
ThreadType::Bars(_) => Box::new(BarsHandler { config }),
ThreadType::Bars(Class::UsEquity) => Box::new(BarsHandler {
config,
subscription_message_constructor:
websocket::outgoing::subscribe::Message::new_market_us_equity,
}),
ThreadType::Bars(Class::Crypto) => Box::new(BarsHandler {
config,
subscription_message_constructor:
websocket::outgoing::subscribe::Message::new_market_crypto,
}),
ThreadType::News => Box::new(NewsHandler { config }),
}
}