From 4bce70f7b7bbc18edf3c78de33791bb3499a09de Mon Sep 17 00:00:00 2001 From: Sik Yoon Date: Tue, 21 May 2024 18:40:15 +0900 Subject: [PATCH] Fix wrong update --- src/future/order.rs | 1 + src/future/table_mgmt.rs | 55 +++++++++++++++------------------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/future/order.rs b/src/future/order.rs index 83d35b6..4ec83a6 100644 --- a/src/future/order.rs +++ b/src/future/order.rs @@ -402,6 +402,7 @@ pub async fn monitoring_unfilled_order( let server_epoch = get_server_epoch().await; for element in open_closing_orders { query_open_closing_order(&element, &client).await; + sleep(Duration::from_millis(200)).await; } // let orders_outdated = open_closing_orders diff --git a/src/future/table_mgmt.rs b/src/future/table_mgmt.rs index 487ae58..e19c213 100644 --- a/src/future/table_mgmt.rs +++ b/src/future/table_mgmt.rs @@ -158,26 +158,12 @@ async fn update_repeat_task( futures_trade_fee: &FuturesTradeFee, ) -> Result<(), Box> { let update_table_name = String::from("future_ordered_coin_list"); - let mut update_values: Vec<(String, String)> = Vec::new(); - let mut update_condition: Vec<(String, String)> = Vec::new(); let mut price = Decimal::new(0, 8); - let mut profit_percent = 0.0; - - let update_colums = vec![ - "current_price", - "expected_get_usdt", - "pnl", - "pure_profit_percent", - "minimum_profit_percent", - "maximum_profit_percent", - ]; - let mut update_record_build: Vec = Vec::new(); - let mut update_record: Vec> = Vec::new(); - + let mut profit_percent = 0.0; + // update current_price, expected__get_usdt, pnl, pure_profit_percent, minimum_profit_percent, maximum_profit_percent for element in buy_ordered_coin_vec { // build update values - update_record_build.clear(); if coin_price_map.contains_key(&element.symbol) && exchange_info_map.contains_key(&element.symbol) && futures_trade_fee.user_level.is_some() @@ -187,6 +173,7 @@ async fn update_repeat_task( ) .unwrap(); if !price.is_zero() { + let mut update_values: Vec<(String, String)> = Vec::new(); // to get quote_commission_precision let trade_fee = decimal_div(futures_trade_fee.maker_fee_percent, dec!(100)); let lot_step_size = exchange_info_map.get(&element.symbol).unwrap().stepsize; @@ -198,16 +185,17 @@ async fn update_repeat_task( lot_step_size.normalize().scale(), RoundingStrategy::ToZero, ); + let entry_trade_fee = decimal_mul(decimal_mul(position_size, element.entry_price), trade_fee); - let exit_trade_fee = decimal_mul(decimal_mul(position_size, element.current_price), trade_fee); + let exit_trade_fee = decimal_mul(decimal_mul(position_size, price), trade_fee); let fee_total = decimal_add(entry_trade_fee, exit_trade_fee); let initial_margin = decimal_add(decimal_mul(position_size, element.entry_price), entry_trade_fee); let mut unrealized_pnl = Decimal::new(0, 8); if element.position.contains("Long") { - unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(element.current_price, element.entry_price), position_size), fee_total); + unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(price, element.entry_price), position_size), fee_total); } else { - unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(element.entry_price, element.current_price), position_size), fee_total); + unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(element.entry_price, price), position_size), fee_total); } let mut pure_profit_percent = (unrealized_pnl.to_f64().unwrap() @@ -215,39 +203,38 @@ async fn update_repeat_task( * 100.0; pure_profit_percent = (pure_profit_percent * 100.0).round() / 100.0; // Rounding - update_record_build.push(element.id.to_string()); // id - update_record_build.push(price.to_string()); // current_price - update_record_build.push(decimal_add(unrealized_pnl, element.used_usdt).to_string()); //expected_get_usdt - update_record_build.push(unrealized_pnl.to_string()); // pnl - update_record_build.push(pure_profit_percent.to_string()); // pure_profit_percent + update_values.push((String::from("current_price"), price.to_string())); + update_values.push((String::from("expected_get_usdt"), decimal_add(unrealized_pnl, element.used_usdt).to_string())); + update_values.push((String::from("pnl"), unrealized_pnl.to_string())); + update_values.push((String::from("pure_profit_percent"), pure_profit_percent.to_string())); if element.minimum_profit_percent > pure_profit_percent { - update_record_build.push(pure_profit_percent.to_string()); + update_values.push((String::from("minimum_profit_percent"), pure_profit_percent.to_string())); // minimum_profit_percent } else if pure_profit_percent >= 0.0 { - update_record_build.push(0.0.to_string()); // minimum_profit_percent + update_values.push((String::from("minimum_profit_percent"), 0.0.to_string())); } else { - update_record_build.push(element.minimum_profit_percent.to_string()); + update_values.push((String::from("minimum_profit_percent"), element.minimum_profit_percent.to_string())); // minimum_profit_percent } if element.maximum_profit_percent < pure_profit_percent { - update_record_build.push(pure_profit_percent.to_string()); + update_values.push((String::from("maximum_profit_percent"), pure_profit_percent.to_string())); // maximum_profit_percent } else if pure_profit_percent <= 0.0 { - update_record_build.push(0.0.to_string()); // maximum_profit_percent + update_values.push((String::from("maximum_profit_percent"), 0.0.to_string())); } else { - update_record_build.push(element.maximum_profit_percent.to_string()); + update_values.push((String::from("maximum_profit_percent"), element.maximum_profit_percent.to_string())); // maximum_profit_percent } - - update_record.push(update_record_build.clone()); + let update_condition = vec![(String::from("id"), element.id.to_string())]; + update_record2(&update_table_name, &update_values, &update_condition) + .await + .unwrap(); } } } - update_records(&update_table_name, &update_record, &update_colums).await; - Ok(()) }