Improve outgoing Alpaca API types
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -108,7 +108,14 @@ async fn handle_asset_status_message(
|
||||
.await
|
||||
.send(tungstenite::Message::Text(
|
||||
to_string(&websocket::outgoing::Message::Subscribe(
|
||||
create_websocket_market_message(thread_type, symbols),
|
||||
match thread_type {
|
||||
ThreadType::Bars(_) => {
|
||||
websocket::outgoing::subscribe::Message::new_market(symbols)
|
||||
}
|
||||
ThreadType::News => {
|
||||
websocket::outgoing::subscribe::Message::new_news(symbols)
|
||||
}
|
||||
},
|
||||
))
|
||||
.unwrap(),
|
||||
))
|
||||
@@ -140,7 +147,14 @@ async fn handle_asset_status_message(
|
||||
.await
|
||||
.send(tungstenite::Message::Text(
|
||||
to_string(&websocket::outgoing::Message::Unsubscribe(
|
||||
create_websocket_market_message(thread_type, symbols),
|
||||
match thread_type {
|
||||
ThreadType::Bars(_) => {
|
||||
websocket::outgoing::subscribe::Message::new_market(symbols)
|
||||
}
|
||||
ThreadType::News => {
|
||||
websocket::outgoing::subscribe::Message::new_news(symbols)
|
||||
}
|
||||
},
|
||||
))
|
||||
.unwrap(),
|
||||
))
|
||||
@@ -154,17 +168,3 @@ async fn handle_asset_status_message(
|
||||
|
||||
message.response.send(()).unwrap();
|
||||
}
|
||||
|
||||
fn create_websocket_market_message(
|
||||
thread_type: ThreadType,
|
||||
symbols: Vec<String>,
|
||||
) -> websocket::outgoing::subscribe::Message {
|
||||
match thread_type {
|
||||
ThreadType::Bars(_) => websocket::outgoing::subscribe::Message::Market(
|
||||
websocket::outgoing::subscribe::MarketMessage::new(symbols),
|
||||
),
|
||||
ThreadType::News => websocket::outgoing::subscribe::Message::News(
|
||||
websocket::outgoing::subscribe::NewsMessage::new(symbols),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@@ -3,11 +3,14 @@ use crate::{
|
||||
config::{Config, ALPACA_CRYPTO_DATA_URL, ALPACA_NEWS_DATA_URL, ALPACA_STOCK_DATA_URL},
|
||||
database,
|
||||
types::{
|
||||
alpaca::{api, Source},
|
||||
alpaca::{
|
||||
api::{self, outgoing::Sort},
|
||||
Source,
|
||||
},
|
||||
news::Prediction,
|
||||
Asset, Bar, Class, News, Subset,
|
||||
},
|
||||
utils::{duration_until, last_minute, FIFTEEN_MINUTES, ONE_MINUTE},
|
||||
utils::{duration_until, last_minute, remove_slash_from_pair, FIFTEEN_MINUTES, ONE_MINUTE},
|
||||
};
|
||||
use backoff::{future::retry, ExponentialBackoff};
|
||||
use futures_util::future::join_all;
|
||||
@@ -251,14 +254,31 @@ async fn execute_backfill_bars(
|
||||
app_config
|
||||
.alpaca_client
|
||||
.get(&data_url)
|
||||
.query(&api::outgoing::bar::Bar::new(
|
||||
vec![symbol.clone()],
|
||||
ONE_MINUTE,
|
||||
fetch_from,
|
||||
fetch_to,
|
||||
10000,
|
||||
next_page_token.clone(),
|
||||
))
|
||||
.query(&match thread_type {
|
||||
ThreadType::Bars(Class::UsEquity) => api::outgoing::bar::Bar::UsEquity {
|
||||
symbols: vec![symbol.clone()],
|
||||
timeframe: ONE_MINUTE,
|
||||
start: Some(fetch_from),
|
||||
end: Some(fetch_to),
|
||||
limit: Some(10000),
|
||||
adjustment: None,
|
||||
asof: None,
|
||||
feed: Some(app_config.alpaca_source),
|
||||
currency: None,
|
||||
page_token: next_page_token.clone(),
|
||||
sort: Some(Sort::Asc),
|
||||
},
|
||||
ThreadType::Bars(Class::Crypto) => api::outgoing::bar::Bar::Crypto {
|
||||
symbols: vec![symbol.clone()],
|
||||
timeframe: ONE_MINUTE,
|
||||
start: Some(fetch_from),
|
||||
end: Some(fetch_to),
|
||||
limit: Some(10000),
|
||||
page_token: next_page_token.clone(),
|
||||
sort: Some(Sort::Asc),
|
||||
},
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
@@ -325,15 +345,16 @@ async fn execute_backfill_news(
|
||||
app_config
|
||||
.alpaca_client
|
||||
.get(&data_url)
|
||||
.query(&api::outgoing::news::News::new(
|
||||
vec![symbol.clone()],
|
||||
fetch_from,
|
||||
fetch_to,
|
||||
50,
|
||||
true,
|
||||
false,
|
||||
next_page_token.clone(),
|
||||
))
|
||||
.query(&api::outgoing::news::News {
|
||||
symbols: vec![remove_slash_from_pair(&symbol)],
|
||||
start: Some(fetch_from),
|
||||
end: Some(fetch_to),
|
||||
limit: Some(50),
|
||||
include_content: Some(true),
|
||||
exclude_contentless: Some(false),
|
||||
page_token: next_page_token.clone(),
|
||||
sort: Some(Sort::Asc),
|
||||
})
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
@@ -399,7 +420,7 @@ async fn execute_backfill_news(
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let backfill = (news[0].clone(), symbol.clone()).into();
|
||||
let backfill = (news.last().unwrap().clone(), symbol.clone()).into();
|
||||
database::news::upsert_batch(&app_config.clickhouse_client, news).await;
|
||||
database::backfills::upsert(&app_config.clickhouse_client, &thread_type, &backfill).await;
|
||||
|
||||
|
@@ -102,9 +102,8 @@ async fn handle_parsed_websocket_message(
|
||||
match message {
|
||||
websocket::incoming::Message::Subscription(message) => {
|
||||
let symbols = match message {
|
||||
websocket::incoming::subscription::Message::Market(message) => message.bars,
|
||||
websocket::incoming::subscription::Message::News(message) => message
|
||||
.news
|
||||
websocket::incoming::subscription::Message::Market { bars, .. } => bars,
|
||||
websocket::incoming::subscription::Message::News { news } => news
|
||||
.into_iter()
|
||||
.map(|symbol| add_slash_to_pair(&symbol))
|
||||
.collect(),
|
||||
|
Reference in New Issue
Block a user