Order execution and using decimal
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use hypersdk::Decimal;
|
||||||
|
|
||||||
use crate::units::{Direction, Symbol, USD};
|
use crate::units::{Direction, Symbol, USD};
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
@@ -20,7 +22,7 @@ pub struct Signal {
|
|||||||
pub symbol: String,
|
pub symbol: String,
|
||||||
pub kind: Direction,
|
pub kind: Direction,
|
||||||
pub confidence: f32,
|
pub confidence: f32,
|
||||||
pub size: f64,
|
pub size: Decimal,
|
||||||
pub price: USD,
|
pub price: USD,
|
||||||
pub take_profit: USD,
|
pub take_profit: USD,
|
||||||
pub stop_loss: USD,
|
pub stop_loss: USD,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::{
|
|||||||
plugin::{RiskManifest, StrategyManifest},
|
plugin::{RiskManifest, StrategyManifest},
|
||||||
units::{Symbol, USD, Volatility},
|
units::{Symbol, USD, Volatility},
|
||||||
};
|
};
|
||||||
use hypersdk::hypercore::CandleInterval;
|
use hypersdk::{Decimal, hypercore::CandleInterval};
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum TerminalServerMessage {
|
pub enum TerminalServerMessage {
|
||||||
@@ -39,7 +39,7 @@ pub enum TerminalClientMessage {
|
|||||||
pub struct MarketItem {
|
pub struct MarketItem {
|
||||||
pub symbol: Symbol,
|
pub symbol: Symbol,
|
||||||
pub price: USD,
|
pub price: USD,
|
||||||
pub trend: f64,
|
pub trend: Decimal,
|
||||||
pub volume_24h: USD,
|
pub volume_24h: USD,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
use hypersdk::Decimal;
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Symbol(pub String);
|
pub struct Symbol(pub String);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct USD(pub f64);
|
pub struct USD(pub Decimal);
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
@@ -25,10 +27,10 @@ impl std::fmt::Display for Symbol {
|
|||||||
|
|
||||||
impl std::fmt::Display for USD {
|
impl std::fmt::Display for USD {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
if self.0 > 0.0 {
|
if self.0.is_sign_positive() {
|
||||||
write!(f, "\x1b[32m${}\x1b[0m", format_f64(self.0))
|
write!(f, "\x1b[32m${}\x1b[0m", format_f64(self.0.as_f64()))
|
||||||
} else {
|
} else {
|
||||||
write!(f, "\x1b[31m${}\x1b[0m", format_f64(self.0))
|
write!(f, "\x1b[31m${}\x1b[0m", format_f64(self.0.as_f64()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,9 +102,8 @@ impl Engine {
|
|||||||
entry_price: USD(position
|
entry_price: USD(position
|
||||||
.position
|
.position
|
||||||
.entry_px
|
.entry_px
|
||||||
.map(|px| px.as_f64())
|
.unwrap_or_default()),
|
||||||
.unwrap_or(0.0)),
|
profit: USD(position.position.unrealized_pnl),
|
||||||
profit: USD(position.position.unrealized_pnl.as_f64()),
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.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 pulse_sdk::prelude::*;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
@@ -178,7 +181,54 @@ impl StrategyEngine {
|
|||||||
engine.terminal_server.log_raw(log).await?;
|
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 pulse_sdk::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::HashMap;
|
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]
|
let raw = value[field]
|
||||||
.as_str()
|
.as_str()
|
||||||
.ok_or_else(|| format!("asset context field {field} must be a string"))?;
|
.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}"))
|
.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 previous_day_price = number(context, "prevDayPx")?;
|
||||||
let volume_24h = number(context, "dayNtlVlm")?;
|
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"));
|
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()),
|
symbol: Symbol(symbol.to_owned()),
|
||||||
price: USD(price),
|
price: USD(price),
|
||||||
volume_24h: USD(volume_24h),
|
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 {
|
} else {
|
||||||
"\x1b[31m▼\x1b[0m"
|
"\x1b[31m▼\x1b[0m"
|
||||||
},
|
},
|
||||||
format_f64(self.trend.abs())
|
format_f64(self.trend.as_f64().abs())
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user