Order execution and using decimal
This commit is contained in:
@@ -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(),
|
||||
),
|
||||
|
||||
@@ -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
@@ -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),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ impl Formatted for MarketItem {
|
||||
} else {
|
||||
"\x1b[31m▼\x1b[0m"
|
||||
},
|
||||
format_f64(self.trend.abs())
|
||||
format_f64(self.trend.as_f64().abs())
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user