Fix wrong update

This commit is contained in:
Sik Yoon 2024-05-21 18:40:15 +09:00
parent 982a679703
commit 4bce70f7b7
2 changed files with 22 additions and 34 deletions

View File

@ -402,6 +402,7 @@ pub async fn monitoring_unfilled_order(
let server_epoch = get_server_epoch().await; let server_epoch = get_server_epoch().await;
for element in open_closing_orders { for element in open_closing_orders {
query_open_closing_order(&element, &client).await; query_open_closing_order(&element, &client).await;
sleep(Duration::from_millis(200)).await;
} }
// let orders_outdated = open_closing_orders // let orders_outdated = open_closing_orders

View File

@ -158,26 +158,12 @@ async fn update_repeat_task(
futures_trade_fee: &FuturesTradeFee, futures_trade_fee: &FuturesTradeFee,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let update_table_name = String::from("future_ordered_coin_list"); 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 price = Decimal::new(0, 8);
let mut profit_percent = 0.0; 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<String> = Vec::new();
let mut update_record: Vec<Vec<String>> = Vec::new();
// update current_price, expected__get_usdt, pnl, pure_profit_percent, minimum_profit_percent, maximum_profit_percent // update current_price, expected__get_usdt, pnl, pure_profit_percent, minimum_profit_percent, maximum_profit_percent
for element in buy_ordered_coin_vec { for element in buy_ordered_coin_vec {
// build update values // build update values
update_record_build.clear();
if coin_price_map.contains_key(&element.symbol) if coin_price_map.contains_key(&element.symbol)
&& exchange_info_map.contains_key(&element.symbol) && exchange_info_map.contains_key(&element.symbol)
&& futures_trade_fee.user_level.is_some() && futures_trade_fee.user_level.is_some()
@ -187,6 +173,7 @@ async fn update_repeat_task(
) )
.unwrap(); .unwrap();
if !price.is_zero() { if !price.is_zero() {
let mut update_values: Vec<(String, String)> = Vec::new();
// to get quote_commission_precision // to get quote_commission_precision
let trade_fee = decimal_div(futures_trade_fee.maker_fee_percent, dec!(100)); 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; 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(), lot_step_size.normalize().scale(),
RoundingStrategy::ToZero, RoundingStrategy::ToZero,
); );
let entry_trade_fee = decimal_mul(decimal_mul(position_size, element.entry_price), trade_fee); 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 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 initial_margin = decimal_add(decimal_mul(position_size, element.entry_price), entry_trade_fee);
let mut unrealized_pnl = Decimal::new(0, 8); let mut unrealized_pnl = Decimal::new(0, 8);
if element.position.contains("Long") { 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 { } 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() let mut pure_profit_percent = (unrealized_pnl.to_f64().unwrap()
@ -215,39 +203,38 @@ async fn update_repeat_task(
* 100.0; * 100.0;
pure_profit_percent = (pure_profit_percent * 100.0).round() / 100.0; // Rounding pure_profit_percent = (pure_profit_percent * 100.0).round() / 100.0; // Rounding
update_record_build.push(element.id.to_string()); // id update_values.push((String::from("current_price"), price.to_string()));
update_record_build.push(price.to_string()); // current_price update_values.push((String::from("expected_get_usdt"), decimal_add(unrealized_pnl, element.used_usdt).to_string()));
update_record_build.push(decimal_add(unrealized_pnl, element.used_usdt).to_string()); //expected_get_usdt update_values.push((String::from("pnl"), unrealized_pnl.to_string()));
update_record_build.push(unrealized_pnl.to_string()); // pnl update_values.push((String::from("pure_profit_percent"), pure_profit_percent.to_string()));
update_record_build.push(pure_profit_percent.to_string()); // pure_profit_percent
if element.minimum_profit_percent > pure_profit_percent { 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 // minimum_profit_percent
} else if pure_profit_percent >= 0.0 { } 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 { } 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 // minimum_profit_percent
} }
if element.maximum_profit_percent < pure_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 // maximum_profit_percent
} else if pure_profit_percent <= 0.0 { } 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 { } 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 // maximum_profit_percent
} }
let update_condition = vec![(String::from("id"), element.id.to_string())];
update_record.push(update_record_build.clone()); update_record2(&update_table_name, &update_values, &update_condition)
.await
.unwrap();
} }
} }
} }
update_records(&update_table_name, &update_record, &update_colums).await;
Ok(()) Ok(())
} }