|
|
@@ -1,8 +1,73 @@
|
|
|
+use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
+use rand::Rng;
|
|
|
+use rust_decimal::Decimal;
|
|
|
+use rust_decimal_macros::dec;
|
|
|
+
|
|
|
// TODO 市场数据汇总(market_info)的下标集合
|
|
|
pub const LEVEL: usize = 1;
|
|
|
-pub const TRADE_LENGTH: usize = 2; // TODO 推测应该是交易交易所的数据长度
|
|
|
+pub const TRADE_LENGTH: usize = 2; // 推测应该是交易交易所的数据长度
|
|
|
pub const LENGTH: usize = LEVEL * 4 + TRADE_LENGTH; // 市场数据汇总的总长度
|
|
|
pub const BID_PRICE_INDEX: usize = LEVEL * 0; // 买入价格下标
|
|
|
pub const BID_QUANTITY_INDEX: usize = LEVEL * 0 + 1; // 买入数量下标
|
|
|
pub const ASK_PRICE_INDEX: usize = LEVEL * 2; // 卖出价格下标
|
|
|
-pub const ASK_QUANTITY_INDEX: usize = LEVEL * 2 + 1; // 卖出数量下标
|
|
|
+pub const ASK_QUANTITY_INDEX: usize = LEVEL * 2 + 1; // 卖出数量下标
|
|
|
+// 上面是市场数据汇总的下标相关
|
|
|
+pub const MARKET_DELAY_LIMIT: i64 = 30*1000; // 市场信息延迟限制(单位:毫秒)
|
|
|
+pub const GRID: i64 = 1; // 策略资金分成多少份
|
|
|
+pub const STOP_LOSS: Decimal = dec!(0.02); // 风控止损比例,0.02代表2%,是原文的STOPLOSS
|
|
|
+pub const GAMMA: Decimal = dec!(0.999); // gamma默认值
|
|
|
+pub const EFF_RANGE: Decimal = dec!(0.001); // 每1权重需要多少价格距离,0.001代表0.1%,每0.1%代表1权重
|
|
|
+
|
|
|
+// 生成订单的id,可以根据交易所名字来
|
|
|
+pub fn generate_client_id(mut exchange_name_some: Option<&str>) -> String {
|
|
|
+ // 交易所名字获取
|
|
|
+ let exchange_name = exchange_name_some.unwrap_or("");
|
|
|
+ // 随机时间戳,作为订单id的前列
|
|
|
+ let start = SystemTime::now();
|
|
|
+ let since_the_epoch = start
|
|
|
+ .duration_since(UNIX_EPOCH)
|
|
|
+ .expect("Time went backwards");
|
|
|
+ let in_ms = since_the_epoch.as_millis(); // 或者你可以使用 as_secs 获取秒
|
|
|
+ let time_str = in_ms.to_string();
|
|
|
+ let time_slice = &time_str[4..10];
|
|
|
+ // 0-1000的随机数
|
|
|
+ let rand_num = rand::thread_rng().gen_range(1..1000);
|
|
|
+ let result = format!("{}{}{}", time_slice, rand_num, exchange_name);
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+// 大小限定:将num限定在lower_limit和upper_limit范围内
|
|
|
+pub fn clip(num: Decimal, lower_limit: Decimal, upper_limit: Decimal) -> Decimal {
|
|
|
+ if num.ge(&upper_limit) {
|
|
|
+ return upper_limit;
|
|
|
+ }
|
|
|
+ if num.le(&lower_limit) {
|
|
|
+ return lower_limit;
|
|
|
+ }
|
|
|
+ num
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod tests {
|
|
|
+ use rust_decimal_macros::dec;
|
|
|
+ use crate::utils::{clip, generate_client_id};
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn clip_test() {
|
|
|
+ let num = dec!(11);
|
|
|
+ let num2 = dec!(2);
|
|
|
+
|
|
|
+ let lower_limit = dec!(3);
|
|
|
+ let upper_limit = dec!(6);
|
|
|
+
|
|
|
+ println!("mum: {}", clip(num, lower_limit, upper_limit));
|
|
|
+ println!("mum2: {}", clip(num, lower_limit, upper_limit));
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn generate_client_id_test() {
|
|
|
+ println!("{}", generate_client_id(None));
|
|
|
+ println!("{}", generate_client_id(Some("binance")));
|
|
|
+ }
|
|
|
+}
|