Order execution and using decimal

This commit is contained in:
2026-07-29 02:28:54 +02:00
parent 2696618ca0
commit e0230425ec
7 changed files with 72 additions and 17 deletions
+2 -3
View File
@@ -102,9 +102,8 @@ impl Engine {
entry_price: USD(position
.position
.entry_px
.map(|px| px.as_f64())
.unwrap_or(0.0)),
profit: USD(position.position.unrealized_pnl.as_f64()),
.unwrap_or_default()),
profit: USD(position.position.unrealized_pnl),
})
.collect(),
),
+52 -2
View File
@@ -1,4 +1,7 @@
use hypersdk::hypercore::{self, CandleInterval, Subscription, WebSocket};
use hypersdk::hypercore::{
self, BatchOrder, CandleInterval, OrderRequest, OrderTypePlacement, Subscription, TimeInForce,
WebSocket,
};
use pulse_sdk::prelude::*;
use std::{
collections::HashSet,
@@ -178,7 +181,54 @@ impl StrategyEngine {
engine.terminal_server.log_raw(log).await?;
}
Some(RiskMessage::Signal(signal)) => {}
Some(RiskMessage::Signal(RiskSignal::Approve(signal))) => {
let client = hypercore::mainnet();
let accounts = engine.accounts.lock().await;
if let Some(acc) = accounts.get_active() {
let order = BatchOrder {
orders: vec![OrderRequest {
asset: 0,
is_buy: matches!(signal.kind, Direction::Buy),
limit_px: signal.price.0,
sz: signal.size,
reduce_only: false,
order_type: OrderTypePlacement::Limit {
tif: TimeInForce::Gtc,
},
cloid: Default::default(),
}],
grouping: hypercore::OrderGrouping::Na,
builder: None,
};
let nonce = chrono::Utc::now().timestamp_millis() as u64;
match client
.place(&acc.private_key.0, order, nonce, None, None)
.await
{
Ok(_) => {}
Err(e) => {
engine
.terminal_server
.error("Engine::order", &e.to_string())
.await?;
}
}
} else {
engine
.terminal_server
.error("Engine::order", "Unable to get active account")
.await?;
}
}
Some(RiskMessage::Signal(RiskSignal::Reject {
signal,
rejection_confidence,
reason,
})) => {}
}
}
}
+6 -4
View File
@@ -1,13 +1,14 @@
use hypersdk::Decimal;
use pulse_sdk::prelude::*;
use serde_json::Value;
use std::collections::HashMap;
fn number(value: &Value, field: &str) -> Result<f64, String> {
fn number(value: &Value, field: &str) -> Result<Decimal, String> {
let raw = value[field]
.as_str()
.ok_or_else(|| format!("asset context field {field} must be a string"))?;
raw.parse::<f64>()
raw.parse::<Decimal>()
.map_err(|error| format!("could not parse asset context field {field} ({raw}): {error}"))
}
@@ -56,7 +57,7 @@ pub async fn fetch_watch_list(
let previous_day_price = number(context, "prevDayPx")?;
let volume_24h = number(context, "dayNtlVlm")?;
if previous_day_price <= 0.0 {
if previous_day_price.is_zero() || previous_day_price.is_sign_negative() {
return Err(format!("{symbol} has an invalid previous-day price"));
}
@@ -66,7 +67,8 @@ pub async fn fetch_watch_list(
symbol: Symbol(symbol.to_owned()),
price: USD(price),
volume_24h: USD(volume_24h),
trend: ((price / previous_day_price) - 1.0) * 100.0,
trend: ((price / previous_day_price) - <Decimal as From<i32>>::from(1))
* <Decimal as From<i32>>::from(100),
},
);
}