From 0fa0dc003ed00bc2d192c01801e1f817861507c0 Mon Sep 17 00:00:00 2001 From: Pan Xiong Date: Thu, 16 Nov 2023 15:39:19 +1100 Subject: [PATCH] fix spot current price --- app/jobs/get_okx_spot_trades_job.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/jobs/get_okx_spot_trades_job.rb b/app/jobs/get_okx_spot_trades_job.rb index 1ccc254..3c45e8b 100644 --- a/app/jobs/get_okx_spot_trades_job.rb +++ b/app/jobs/get_okx_spot_trades_job.rb @@ -43,12 +43,12 @@ def perform(user_id: nil) current_price: current_price, event_time: event_time ) - update_tx(tx, current_price) + update_tx(tx) ids.push(tx.id) end txs.where.not(id: ids).each do |tx| - update_tx(tx, current_price) + update_tx(tx) end combine_trades if user_id.nil? @@ -65,10 +65,12 @@ def get_current_price(symbol, user_id) price.to_f end - def update_tx(tx, current_price) - tx.current_price = current_price - tx.cost = get_spot_cost(tx.user_id, tx.original_symbol, tx.event_time.to_date) || tx.price - tx.revenue = get_revenue(tx.trade_type, tx.amount, tx.cost, tx.qty, current_price) + def update_tx(tx) + tx.current_price = get_current_price(tx.origin_symbol, tx.user_id) + if tx.cost.to_f.zero? + tx.cost = get_spot_cost(tx.user_id, tx.original_symbol, tx.event_time.to_date) || tx.price + end + tx.revenue = get_revenue(tx.trade_type, tx.amount, tx.cost, tx.qty, tx.current_price) tx.roi = tx.revenue / tx.amount tx.save end