Fix wrong calculation of PNL

This commit is contained in:
Sik Yoon 2024-05-21 11:44:35 +09:00
parent 6fad7b0ec4
commit 2d19927048

View File

@ -202,24 +202,23 @@ async fn update_repeat_task(
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, element.current_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 profit = Decimal::new(0, 8);
if element.position.contains("Long") { if element.position.contains("Long") {
profit = decimal_sub(decimal_sub(element.current_price, element.entry_price),fee_total); unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(element.current_price, element.entry_price), position_size), fee_total);
} else { } else {
profit = decimal_sub(decimal_sub(element.entry_price, element.current_price),fee_total); unrealized_pnl = decimal_sub(decimal_mul(decimal_sub(element.entry_price, element.current_price), position_size), fee_total);
} }
let mut pure_profit_percent = (profit.to_f64().unwrap() let mut pure_profit_percent = (unrealized_pnl.to_f64().unwrap()
/ initial_margin.to_f64().unwrap()) / decimal_add(initial_margin, unrealized_pnl).to_f64().unwrap())
* 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_record_build.push(element.id.to_string()); // id
update_record_build.push(price.to_string()); // current_price update_record_build.push(price.to_string()); // current_price
update_record_build.push(decimal_add(profit, element.used_usdt).to_string()); //expected_get_usdt update_record_build.push(decimal_add(unrealized_pnl, element.used_usdt).to_string()); //expected_get_usdt
update_record_build.push(profit.to_string()); // expected_usdt_profit update_record_build.push(unrealized_pnl.to_string()); // expected_usdt_profit
update_record_build.push(pure_profit_percent.to_string()); // pure_profit_percent 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 {