ソースを参照

下单数量计算公式搞定。

skyfffire 2 年 前
コミット
991b802df9
1 ファイル変更68 行追加3 行削除
  1. 68 3
      src/as_libs.rs

+ 68 - 3
src/as_libs.rs

@@ -1,3 +1,4 @@
+use std::num::FpCategory::Nan;
 use crate::exchange_middle_ware::{Depth, Record};
 
 // 获取买一卖一价差(spread)、中间价
@@ -75,9 +76,10 @@ pub fn calc_theta(gamma: f64, std: f64, kappa: f64, ira: f64) -> f64 {
 }
 
 // 给定ira、当前库存、最大库存、买入价格、下单数量小数位数,计算合理下单数量
-pub fn calc_order_amount(ira: f64, quantity: f64, quantity_max: f64, bid_price: f64, amount_decimal_places: usize) -> f64 {
-    let eta = -ira / quantity;
-    let order_value = (quantity_max * (-eta * quantity_max).ln()).abs();
+// 下单数量要与ira(风险系数)、quantity(库存)成反比
+pub fn calc_order_amount(ira_max: f64, ira: f64, quantity: f64, quantity_max: f64, bid_price: f64, amount_decimal_places: usize) -> f64 {
+    let eta = (ira_max / (ira + 1.0)) / (quantity * 2);
+    let order_value = (-quantity_max * eta).abs();
     let order_amount = order_value / bid_price;
 
     return truncate_decimal_places(order_amount, amount_decimal_places);
@@ -138,4 +140,67 @@ mod tests {
     fn test_calc_gamma() {
         println!("{}", calc_gamma(0.1, 49.0, 23.0))
     }
+
+    #[test]
+    fn test_calc_deviation_range() {
+        let max_ira = 10.0;
+        let ira = 0.1;
+        let spread_list = vec![
+            0.1,
+            0.2,
+            0.15,
+            0.1
+        ];
+
+        println!("{}", calc_deviation_range(max_ira, &spread_list, ira));
+    }
+
+    #[test]
+    fn test_calc_rp() {
+        let mid_price = 1992.01;
+        let quantity = 49.0;
+        let ira = 0.1;
+        let gamma = 1.9289379267775165e-06;
+        let std = 23.0;
+
+        println!("{}", calc_rp(mid_price, quantity, ira, gamma, std));
+    }
+
+    #[test]
+    fn test_calc_dk() {
+        let deviation_range = 1.9900000000000002;
+        let ira = 0.1;
+        let gamma = 1.9289379267775165e-06;
+        let std = 23.0;
+
+        println!("{}", calc_dk(deviation_range, gamma, std, ira));
+    }
+
+    #[test]
+    fn test_calc_kappa() {
+        let deviation_range = 1.9900000000000002;
+        let ira = 0.1;
+        let gamma = 1.9289379267775165e-06;
+        let dk = 0.00000019191948219432835;
+
+        println!("{}", calc_kappa(gamma, dk, deviation_range, ira));
+    }
+
+    #[test]
+    fn test_calc_order_amount() {
+        let quantity = 400.0;
+        let quantity_max = 1000.0;
+        let bid_price = 30000.0;
+        let amount_decimal_places = 4;
+
+        let start = 0.1;
+        let end = 10.0;
+        let step = 0.1;
+        let mut ira = start;
+
+        while ira <= end {
+            println!("ira={}, order_amount={}", ira, calc_order_amount(end, ira, quantity, quantity_max, bid_price, amount_decimal_places));
+            ira += step;
+        }
+    }
 }