|
@@ -1,16 +1,15 @@
|
|
|
use std::cmp::{max, min};
|
|
use std::cmp::{max, min};
|
|
|
use std::collections::HashMap;
|
|
use std::collections::HashMap;
|
|
|
use std::ops::{Div, Mul};
|
|
use std::ops::{Div, Mul};
|
|
|
-// use std::str::FromStr;
|
|
|
|
|
use chrono::Utc;
|
|
use chrono::Utc;
|
|
|
|
|
+use reqwest::Client;
|
|
|
use rust_decimal::Decimal;
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
|
|
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
|
|
|
use rust_decimal_macros::dec;
|
|
use rust_decimal_macros::dec;
|
|
|
|
|
+use serde_json::Value;
|
|
|
use crate::model::{LocalPosition, OrderInfo};
|
|
use crate::model::{LocalPosition, OrderInfo};
|
|
|
use crate::utils;
|
|
use crate::utils;
|
|
|
use tracing::{info, error, warn};
|
|
use tracing::{info, error, warn};
|
|
|
-// use reqwest::{Client};
|
|
|
|
|
-// use tokio::spawn;
|
|
|
|
|
use tokio::time::Instant;
|
|
use tokio::time::Instant;
|
|
|
use global::params::Params;
|
|
use global::params::Params;
|
|
|
use standard::{OrderCommand};
|
|
use standard::{OrderCommand};
|
|
@@ -97,6 +96,7 @@ pub struct Strategy {
|
|
|
|
|
|
|
|
pub trade_close_dist: Decimal, //
|
|
pub trade_close_dist: Decimal, //
|
|
|
pub trade_open_dist: Decimal, //
|
|
pub trade_open_dist: Decimal, //
|
|
|
|
|
+ pub prev_shwo_open_dist: Decimal,
|
|
|
|
|
|
|
|
pub ref_index: usize, //
|
|
pub ref_index: usize, //
|
|
|
pub predict: Decimal, //
|
|
pub predict: Decimal, //
|
|
@@ -193,6 +193,7 @@ impl Strategy {
|
|
|
close_dist: vec![],
|
|
close_dist: vec![],
|
|
|
trade_close_dist: params.close,
|
|
trade_close_dist: params.close,
|
|
|
trade_open_dist: params.open,
|
|
trade_open_dist: params.open,
|
|
|
|
|
+ prev_shwo_open_dist: params.open,
|
|
|
ref_index: 0,
|
|
ref_index: 0,
|
|
|
predict: Default::default(),
|
|
predict: Default::default(),
|
|
|
predict_alpha: Default::default(),
|
|
predict_alpha: Default::default(),
|
|
@@ -471,10 +472,47 @@ impl Strategy {
|
|
|
// debug!(?command);
|
|
// debug!(?command);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 获取外部开单距离
|
|
|
|
|
+ pub async fn get_open_dist(&mut self) -> Decimal {
|
|
|
|
|
+ let url = "http://assist.skyfffire.com:9999";
|
|
|
|
|
+
|
|
|
|
|
+ // 创建 HTTP 客户端
|
|
|
|
|
+ let client = Client::new();
|
|
|
|
|
+
|
|
|
|
|
+ // 发送 GET 请求
|
|
|
|
|
+ let response = client.get(url)
|
|
|
|
|
+ .send()
|
|
|
|
|
+ .await.unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ if response.status().is_success() {
|
|
|
|
|
+ let response_text = response.text().await.unwrap();
|
|
|
|
|
+ let rst: Value = serde_json::from_str(response_text.as_str()).unwrap();
|
|
|
|
|
+ let is_success = rst.as_bool().unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ return if is_success {
|
|
|
|
|
+ let delta_sum = Decimal::from_f64(rst["delta_sum"].as_f64().unwrap()).unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ let mut open = delta_sum / Decimal::TWO;
|
|
|
|
|
+ open.rescale(6);
|
|
|
|
|
+
|
|
|
|
|
+ open
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Decimal::ZERO
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Decimal::ZERO
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 生成各类挂单价格,原文是gen_dist
|
|
// 生成各类挂单价格,原文是gen_dist
|
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
|
- pub fn generate_dist(&mut self) {
|
|
|
|
|
- let open = self.trade_open_dist;
|
|
|
|
|
|
|
+ pub async fn generate_dist(&mut self) {
|
|
|
|
|
+ let network_open = self.get_open_dist().await;
|
|
|
|
|
+ let open = if network_open == Decimal::ZERO {
|
|
|
|
|
+ self.trade_open_dist
|
|
|
|
|
+ } else {
|
|
|
|
|
+ network_open
|
|
|
|
|
+ };
|
|
|
let close = self.trade_close_dist;
|
|
let close = self.trade_close_dist;
|
|
|
let pos_rate = vec![self.long_hold_rate, self.short_hold_rate];
|
|
let pos_rate = vec![self.long_hold_rate, self.short_hold_rate];
|
|
|
let ref_bp = self.ref_bp;
|
|
let ref_bp = self.ref_bp;
|
|
@@ -482,6 +520,13 @@ impl Strategy {
|
|
|
let predict = self.predict;
|
|
let predict = self.predict;
|
|
|
let grid = self.grid;
|
|
let grid = self.grid;
|
|
|
|
|
|
|
|
|
|
+ // 打印当前参数
|
|
|
|
|
+ let rise = self.prev_shwo_open_dist / open;
|
|
|
|
|
+ if rise < dec!(0.5) || rise > dec!(2) {
|
|
|
|
|
+ info!("开仓距离发生改变:{}->{}", self.prev_shwo_open_dist, open);
|
|
|
|
|
+ self.prev_shwo_open_dist = open;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 平仓相关
|
|
// 平仓相关
|
|
|
let mut mp = (ref_bp + ref_ap) * dec!(0.5);
|
|
let mut mp = (ref_bp + ref_ap) * dec!(0.5);
|
|
|
let mut buy_start = mp;
|
|
let mut buy_start = mp;
|
|
@@ -1243,7 +1288,7 @@ impl Strategy {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 在满足条件后,返回非空command,否则返回一个空的command。原文的onTime。
|
|
// 在满足条件后,返回非空command,否则返回一个空的command。原文的onTime。
|
|
|
- pub fn on_tick(&mut self,
|
|
|
|
|
|
|
+ pub async fn on_tick(&mut self,
|
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
|
local_position: &LocalPosition,
|
|
local_position: &LocalPosition,
|
|
|
agg_market: &Vec<Decimal>,
|
|
agg_market: &Vec<Decimal>,
|
|
@@ -1274,7 +1319,7 @@ impl Strategy {
|
|
|
// 刷新多空双方持仓比例
|
|
// 刷新多空双方持仓比例
|
|
|
self._pos_rate();
|
|
self._pos_rate();
|
|
|
// 生成开仓、平仓等相关价格
|
|
// 生成开仓、平仓等相关价格
|
|
|
- self.generate_dist();
|
|
|
|
|
|
|
+ self.generate_dist().await;
|
|
|
|
|
|
|
|
// 下单指令处理逻辑
|
|
// 下单指令处理逻辑
|
|
|
self._cancel_open(&mut command, local_orders); // 撤单命令处理
|
|
self._cancel_open(&mut command, local_orders); // 撤单命令处理
|
|
@@ -1362,20 +1407,23 @@ impl Strategy {
|
|
|
mod tests {
|
|
mod tests {
|
|
|
use rust_decimal::Decimal;
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal_macros::dec;
|
|
use rust_decimal_macros::dec;
|
|
|
|
|
+ use tracing::info;
|
|
|
use global::params::Params;
|
|
use global::params::Params;
|
|
|
use crate::strategy::Strategy;
|
|
use crate::strategy::Strategy;
|
|
|
|
|
|
|
|
- #[test]
|
|
|
|
|
- fn on_time_test() {
|
|
|
|
|
|
|
+ #[tokio::test]
|
|
|
|
|
+ async fn on_time_test() {
|
|
|
global::log_utils::init_log_with_debug();
|
|
global::log_utils::init_log_with_debug();
|
|
|
|
|
|
|
|
- let params = Params::new("config.toml.gate").unwrap();
|
|
|
|
|
|
|
+ let params = Params::new_json("config.json.gate", 223).unwrap();
|
|
|
let mut strategy = Strategy::new(¶ms, true);
|
|
let mut strategy = Strategy::new(¶ms, true);
|
|
|
|
|
|
|
|
strategy.is_ready = true;
|
|
strategy.is_ready = true;
|
|
|
strategy.equity = dec!(1000);
|
|
strategy.equity = dec!(1000);
|
|
|
strategy.lever_rate = Decimal::ONE;
|
|
strategy.lever_rate = Decimal::ONE;
|
|
|
|
|
|
|
|
|
|
+ info!("{}", strategy.get_open_dist().await.to_string())
|
|
|
|
|
+
|
|
|
// debug!("{:?}", strategy.on_time(&trader_msg));
|
|
// debug!("{:?}", strategy.on_time(&trader_msg));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|