diff --git a/app/jobs/get_recently_adding_positions_job.rb b/app/jobs/get_recently_adding_positions_job.rb index d4d8631..4463af3 100644 --- a/app/jobs/get_recently_adding_positions_job.rb +++ b/app/jobs/get_recently_adding_positions_job.rb @@ -3,27 +3,33 @@ class GetRecentlyAddingPositionsJob < ApplicationJob def perform(date: Date.today, symbol: nil) event_time = symbol.present? ? date.all_day : (date - 1.day).all_day - SyncedTransaction.where(user_id: nil, event_time: event_time).group_by{|tx| [tx.origin_symbol, tx.fee_symbol, tx.position_side, tx.source]}.each do |key, txs| + SyncedTransaction.where(user_id: nil, event_time: event_time).group_by{|tx| [tx.origin_symbol, tx.fee_symbol, tx.position_side, tx.trade_type, tx.source]}.each do |key, txs| origin_symbol = key[0] from_symbol = origin_symbol.split(key[1])[0] - trade_type = key[2] == 'short' ? 'buy' : 'sell' qty = txs.sum(&:qty) amount = txs.sum(&:amount) revenue = txs.sum(&:revenue) price = amount / qty trade_date = event_time.first.to_date - next if AddingPositionsHistory.where(event_date: trade_date - 1.day, origin_symbol: origin_symbol, qty: qty, amount: amount).any? + trade_type = key[2] == 'short' ? 'buy' : 'sell' + source = key[4] + next if AddingPositionsHistory.where(event_date: trade_date - 1.day, origin_symbol: origin_symbol, qty: qty, amount: amount, trade_type: trade_type).any? + + aph = AddingPositionsHistory.where(event_date: trade_date, origin_symbol: origin_symbol, from_symbol: from_symbol, fee_symbol: key[1], qty: qty, trade_type: trade_type, source: source).first_or_create - aph = AddingPositionsHistory.where(event_date: trade_date, origin_symbol: origin_symbol, from_symbol: from_symbol, fee_symbol: key[1], trade_type: trade_type, source: key[3]).first_or_create if trade_date == Date.today - up = UserPosition.where(user_id: nil, origin_symbol: origin_symbol, trade_type: trade_type, source: key[3]).take - current_price = up&.current_price || get_history_price(from_symbol.downcase, date) + up = UserPosition.where(user_id: nil, origin_symbol: origin_symbol, trade_type: trade_type, source: source).take + unit_cost = up&.price + trading_roi = up&.roi else - up = SnapshotPosition.joins(:snapshot_info).where(snapshot_info: {source_type: 'synced', user_id: nil, event_date: trade_date}, origin_symbol: origin_symbol, trade_type: trade_type, source: key[3]).take - current_price = up&.estimate_price || get_history_price(from_symbol.downcase, date) + up = SnapshotPosition.joins(:snapshot_info).where(snapshot_info: {source_type: 'synced', user_id: nil, event_date: trade_date}, origin_symbol: origin_symbol, trade_type: trade_type, source: source).take + cost_price = txs.sum(&:cost_price) / txs.count + unit_cost = up&.price || cost_price + trading_roi = up&.roi end - unit_cost = up&.price - aph.update(price: price, current_price: current_price, qty: qty, amount: amount, revenue: revenue, unit_cost: unit_cost, trading_roi: up&.roi) + + current_price = get_history_price(from_symbol.downcase, date) + aph.update(price: price, current_price: current_price, amount: amount, revenue: revenue, unit_cost: unit_cost, trading_roi: trading_roi) SlackService.send_notification(nil, enqueued_block(from_symbol)) if AddingPositionsHistory.where("id != ? and event_date = ? and origin_symbol = ? and amount BETWEEN ? AND ?", aph.id, date, from_symbol, amount * 0.95, amount * 1.05).any? end end diff --git a/app/views/page/position_detail.html.erb b/app/views/page/position_detail.html.erb index 48ad0f8..3981069 100644 --- a/app/views/page/position_detail.html.erb +++ b/app/views/page/position_detail.html.erb @@ -126,7 +126,7 @@ <%= d.get_revenue.round(4) %> <%= "#{d.roi}%" %> <%= "#{d.amount_ratio}" %> - <%= "#{(d.trading_roi * 100).round(3)}%" %> + <%= "#{(d.trading_roi.to_f * 100).round(3)}%" %> <%= d.source %> <% end %> @@ -185,7 +185,7 @@ <%= d.get_revenue.round(4) %> <%= "#{d.roi}%" %> <%= "#{d.amount_ratio}" %> - <%= "#{(d.trading_roi * 100).round(3)}%" %> + <%= "#{(d.trading_roi.to_f * 100).round(3)}%" %> <%= d.source %> <% end %>