Skip to content

Commit

Permalink
Update recently adding positions job brtr/coin_elite#716
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Sep 5, 2023
1 parent ef8badd commit 0af232b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 16 additions & 10 deletions app/jobs/get_recently_adding_positions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/page/position_detail.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<td class="<%= d.get_revenue.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= d.get_revenue.round(4) %></td>
<td><%= "#{d.roi}%" %></td>
<td><%= "#{d.amount_ratio}" %></td>
<td><%= "#{(d.trading_roi * 100).round(3)}%" %></td>
<td><%= "#{(d.trading_roi.to_f * 100).round(3)}%" %></td>
<td><%= d.source %></td>
</tr>
<% end %>
Expand Down Expand Up @@ -185,7 +185,7 @@
<td class="<%= d.get_revenue.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= d.get_revenue.round(4) %></td>
<td><%= "#{d.roi}%" %></td>
<td><%= "#{d.amount_ratio}" %></td>
<td><%= "#{(d.trading_roi * 100).round(3)}%" %></td>
<td><%= "#{(d.trading_roi.to_f * 100).round(3)}%" %></td>
<td><%= d.source %></td>
</tr>
<% end %>
Expand Down

0 comments on commit 0af232b

Please sign in to comment.