Update random bits and bobs
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
68
src/init.rs
68
src/init.rs
@@ -4,13 +4,18 @@ use crate::{
|
||||
types::alpaca::{self, api, shared::Sort},
|
||||
};
|
||||
use log::{info, warn};
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use time::OffsetDateTime;
|
||||
use tokio::join;
|
||||
|
||||
pub async fn check_account(config: &Arc<Config>) {
|
||||
let account = alpaca::api::incoming::account::get(config, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let account = alpaca::api::incoming::account::get(
|
||||
&config.alpaca_client,
|
||||
&config.alpaca_rate_limiter,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
!(account.status != alpaca::api::incoming::account::Status::Active),
|
||||
@@ -41,7 +46,8 @@ pub async fn rehydrate_orders(config: &Arc<Config>) {
|
||||
let mut after = OffsetDateTime::UNIX_EPOCH;
|
||||
|
||||
while let Some(message) = api::incoming::order::get(
|
||||
config,
|
||||
&config.alpaca_client,
|
||||
&config.alpaca_rate_limiter,
|
||||
&api::outgoing::order::Order {
|
||||
status: Some(api::outgoing::order::Status::All),
|
||||
limit: Some(500),
|
||||
@@ -74,30 +80,52 @@ pub async fn rehydrate_orders(config: &Arc<Config>) {
|
||||
info!("Rehydrated order data.");
|
||||
}
|
||||
|
||||
pub async fn check_positions(config: &Arc<Config>) {
|
||||
pub async fn rehydrate_positions(config: &Arc<Config>) {
|
||||
info!("Rehydrating position data.");
|
||||
|
||||
let positions_future = async {
|
||||
alpaca::api::incoming::position::get(config, None)
|
||||
.await
|
||||
.unwrap()
|
||||
alpaca::api::incoming::position::get(
|
||||
&config.alpaca_client,
|
||||
&config.alpaca_rate_limiter,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|position| (position.symbol.clone(), position))
|
||||
.collect::<HashMap<_, _>>()
|
||||
};
|
||||
|
||||
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);
|
||||
let (mut positions, assets) = join!(positions_future, assets_future);
|
||||
|
||||
for position in positions {
|
||||
if !assets.contains(&position.symbol) {
|
||||
warn!(
|
||||
"Position for unmonitored asset: {}, {} shares.",
|
||||
position.symbol, position.qty
|
||||
);
|
||||
}
|
||||
let assets = assets
|
||||
.into_iter()
|
||||
.map(|mut asset| {
|
||||
if let Some(position) = positions.remove(&asset.symbol) {
|
||||
asset.qty = position.qty_available;
|
||||
} else {
|
||||
asset.qty = 0.0;
|
||||
}
|
||||
asset
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
database::assets::upsert_batch(&config.clickhouse_client, &assets)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
for position in positions.values() {
|
||||
warn!(
|
||||
"Position for unmonitored asset: {}, {} shares.",
|
||||
position.symbol, position.qty
|
||||
);
|
||||
}
|
||||
|
||||
info!("Rehydrated position data.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user