|
|
@@ -1,8 +1,10 @@
|
|
|
+use std::ops::Div;
|
|
|
use std::sync::Arc;
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
-use serde::Deserialize;
|
|
|
+use serde::{Deserialize, Serialize};
|
|
|
use strategy::quant::Quant;
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder, post, get};
|
|
|
+use rust_decimal::Decimal;
|
|
|
use tokio::sync::Mutex;
|
|
|
use tracing::{debug, info};
|
|
|
|
|
|
@@ -12,6 +14,15 @@ struct InputData {
|
|
|
stop: bool,
|
|
|
}
|
|
|
|
|
|
+#[derive(Serialize, Clone)]
|
|
|
+struct AccountInfo {
|
|
|
+ now_balance: Decimal, // 钱包余额
|
|
|
+ unrealized_pn_l: Decimal, // 未实现盈亏
|
|
|
+ pos: Decimal, // 持仓数量
|
|
|
+ entry_price: Decimal, // 开仓价格
|
|
|
+ now_price: Decimal, // 当前价格
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Clone)]
|
|
|
struct Arcs {
|
|
|
running: Arc<AtomicBool>,
|
|
|
@@ -22,7 +33,41 @@ struct Arcs {
|
|
|
#[get("/account")]
|
|
|
async fn get_account(arcs: web::Data<Arcs>) -> impl Responder {
|
|
|
let quant = arcs.quant_arc.lock().await;
|
|
|
- HttpResponse::Ok().body(format!("Value: {}", quant.params.log_level))
|
|
|
+
|
|
|
+ // --------------------------------数据逻辑处理--------------------------------
|
|
|
+ let mut pos = quant.local_position_by_orders.long_pos - quant.local_position_by_orders.short_pos;
|
|
|
+ if !quant.exchange.contains("spot") {
|
|
|
+ pos = quant.local_position.long_pos - quant.local_position.short_pos;
|
|
|
+ }
|
|
|
+ pos.rescale(8);
|
|
|
+
|
|
|
+ let mut entry_price = Decimal::ZERO;
|
|
|
+ if pos.gt(&Decimal::ZERO) {
|
|
|
+ entry_price = quant.local_position_by_orders.long_avg;
|
|
|
+ } else {
|
|
|
+ entry_price = quant.local_position_by_orders.short_avg;
|
|
|
+ }
|
|
|
+ entry_price.rescale(8);
|
|
|
+
|
|
|
+ let mut now_balance = quant.strategy.equity.div(quant.used_pct);
|
|
|
+ now_balance.rescale(4);
|
|
|
+
|
|
|
+ let mut unrealized_pn_l = quant.local_profit;
|
|
|
+ unrealized_pn_l.rescale(4);
|
|
|
+
|
|
|
+ let mut now_price = quant.strategy.mp;
|
|
|
+ now_price.rescale(8);
|
|
|
+
|
|
|
+ // --------------------------------发送到远端--------------------------------
|
|
|
+ let info = AccountInfo{
|
|
|
+ now_balance,
|
|
|
+ unrealized_pn_l,
|
|
|
+ pos,
|
|
|
+ entry_price,
|
|
|
+ now_price
|
|
|
+ };
|
|
|
+ let json_string = serde_json::to_string(&info).unwrap();
|
|
|
+ HttpResponse::Ok().content_type("application/json").body(json_string)
|
|
|
}
|
|
|
|
|
|
// 句柄 POST 请求
|
|
|
@@ -31,7 +76,11 @@ async fn on_change(arcs: web::Data<Arcs>, input: web::Json<InputData>) -> impl R
|
|
|
let mut quant = arcs.quant_arc.lock().await;
|
|
|
debug!(?input);
|
|
|
|
|
|
- HttpResponse::Ok().body(format!("Incremented Value: {}", quant.params.log_level))
|
|
|
+ if input.stop {
|
|
|
+ HttpResponse::Ok().body(format!("程序已收到退出信号,将在10秒后退出。"))
|
|
|
+ } else {
|
|
|
+ HttpResponse::ServiceUnavailable().body(format!("程序没有收到正确的信号:{:?}。", input))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub fn run_server(port: u32, running: Arc<AtomicBool>, quant_arc: Arc<Mutex<Quant>>) {
|