Add order/position management
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
mod rehydrate;
|
||||
mod websocket;
|
||||
|
||||
use crate::{
|
||||
config::{Config, ALPACA_TRADING_WEBSOCKET_URL},
|
||||
database,
|
||||
types::alpaca,
|
||||
};
|
||||
use futures_util::StreamExt;
|
||||
use log::warn;
|
||||
use rehydrate::rehydrate;
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
use tokio::spawn;
|
||||
use tokio_tungstenite::connect_async;
|
||||
|
||||
pub async fn run(config: Arc<Config>) {
|
||||
let (websocket, _) = connect_async(ALPACA_TRADING_WEBSOCKET_URL).await.unwrap();
|
||||
let (mut websocket_sink, mut websocket_stream) = websocket.split();
|
||||
|
||||
alpaca::websocket::trading::authenticate(&config, &mut websocket_sink, &mut websocket_stream)
|
||||
.await;
|
||||
alpaca::websocket::trading::subscribe(&mut websocket_sink, &mut websocket_stream).await;
|
||||
|
||||
rehydrate(&config).await;
|
||||
check_positions(&config).await;
|
||||
|
||||
spawn(websocket::run(config, websocket_stream, websocket_sink));
|
||||
}
|
||||
|
||||
pub async fn check_positions(config: &Arc<Config>) {
|
||||
let positions_future = async {
|
||||
alpaca::api::incoming::position::get(config, None)
|
||||
.await
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let assets_future = async {
|
||||
database::assets::select(&config.clickhouse_client)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|asset| asset.symbol)
|
||||
.collect::<HashSet<_>>()
|
||||
};
|
||||
|
||||
let (positions, assets) = tokio::join!(positions_future, assets_future);
|
||||
|
||||
for position in positions {
|
||||
if !assets.contains(&position.symbol) {
|
||||
warn!("Position for unmonitored asset: {:?}", position.symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user