Add paper URL support

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-02-14 21:15:27 +00:00
parent 6f85b9b0e8
commit 4b194e168f
16 changed files with 200 additions and 147 deletions

View File

@@ -1,78 +1,21 @@
mod rehydrate;
mod websocket;
use crate::{
config::{Config, ALPACA_TRADING_WEBSOCKET_URL},
database,
config::{Config, ALPACA_WEBSOCKET_URL},
types::alpaca,
};
use futures_util::StreamExt;
use log::warn;
use rehydrate::rehydrate;
use std::{collections::HashSet, sync::Arc};
use std::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 (websocket, _) = connect_async(&*ALPACA_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;
spawn(websocket::run(config, websocket_stream, websocket_sink));
}
pub async fn check_account(config: &Arc<Config>) {
let account = alpaca::api::incoming::account::get(config, None)
.await
.unwrap();
assert!(
!(account.status != alpaca::api::incoming::account::Status::Active),
"Account status is not active: {:?}.",
account.status
);
assert!(
!account.trade_suspend_by_user,
"Account trading is suspended by user."
);
assert!(!account.trading_blocked, "Account trading is blocked.");
assert!(!account.blocked, "Account is blocked.");
if account.cash == 0.0 {
warn!("Account cash is zero, qrust will not be able to trade.");
}
warn!(
"qrust active with {}{}, avoid transferring funds without shutting down.",
account.cash, account.currency
);
}
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);
}
}
}