Skip to content

Commit

Permalink
Add sold revenue to combine transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Oct 16, 2023
1 parent d62b744 commit eada408
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def transactions_table_headers
},
{
name: "成交价",
sort: "none"
sort: "price"
},
{
name: "成本价",
Expand Down Expand Up @@ -140,7 +140,7 @@ def transactions_table_headers
},
{
name: "收益/总收益",
sort: "none"
sort: "revenue_ratio"
},
{
name: "来源",
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/get_okx_spot_trades_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def combine_trades
total_cost = 0
total_qty = 0
total_fee = 0
total_sold_revenue = 0

origin_txs.each do |tx|
if tx.trade_type == 'buy'
Expand All @@ -86,6 +87,7 @@ def combine_trades
else
total_cost -= tx.amount
total_qty -= tx.qty
total_sold_revenue += tx.revenue
end

total_fee += tx.fee
Expand All @@ -100,7 +102,7 @@ def combine_trades
revenue = trade_type == 'buy' ? origin_tx.current_price * total_qty - total_cost : total_cost.abs - origin_tx.current_price * total_qty
roi = revenue / total_cost.abs

combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: origin_tx.current_price, revenue: revenue, roi: roi)
combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: origin_tx.current_price, revenue: revenue, roi: roi, sold_revenue: total_sold_revenue)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/get_spot_trades_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def combine_trades(symbol, current_price)
total_cost = 0
total_qty = 0
total_fee = 0
total_sold_revenue = 0

origin_txs = OriginTransaction.available.year_to_date.where(user_id: nil, source: SOURCE, original_symbol: symbol).order(event_time: :asc)
return if origin_txs.empty?
Expand All @@ -71,6 +72,7 @@ def combine_trades(symbol, current_price)
else
total_cost -= tx.amount
total_qty -= tx.qty
total_sold_revenue += tx.revenue
end

total_fee += tx.fee
Expand All @@ -84,7 +86,7 @@ def combine_trades(symbol, current_price)
revenue = trade_type == 'buy' ? current_price * total_qty - total_cost : total_cost.abs - current_price * total_qty
roi = revenue / total_cost.abs

combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: current_price, revenue: revenue, roi: roi)
combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: current_price, revenue: revenue, roi: roi, sold_revenue: total_sold_revenue)
end
end

Expand Down
12 changes: 9 additions & 3 deletions app/models/combine_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ def self.total_summary
}
end

def revenue_ratio(total_revenue)
ratio = (revenue / total_revenue).abs
revenue > 0 ? ratio : ratio * -1
def revenue_ratio(summary)
case
when revenue > 0
revenue / summary[:profit_amount]
when revenue < 0
revenue / summary[:loss_amount]
else
0
end
end

def cost_ratio(total_cost)
Expand Down
4 changes: 3 additions & 1 deletion app/services/get_combine_transactions_snapshots_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def execute
total_cost = record&.amount.to_f
total_qty = record&.qty.to_f
total_fee = record&.fee.to_f
total_sold_revenue = record&.sold_revenue.to_f

origin_txs.each do |tx|
if tx.trade_type == 'buy'
Expand All @@ -23,6 +24,7 @@ def execute
else
total_cost -= tx.amount
total_qty -= tx.qty
total_sold_revenue += tx.revenue
end

total_fee += tx.fee
Expand All @@ -37,7 +39,7 @@ def execute
revenue = trade_type == 'buy' ? origin_tx.current_price * total_qty - total_cost : total_cost.abs - origin_tx.current_price * total_qty
roi = revenue / total_cost.abs

combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: origin_tx.current_price, revenue: revenue, roi: roi)
combine_tx.update(price: price, qty: total_qty, amount: total_cost, fee: total_fee, current_price: origin_tx.current_price, revenue: revenue, roi: roi, sold_revenue: total_sold_revenue)
end

last_info.combine_tx_snapshot_records.where.not(id: ids).each do |record|
Expand Down
12 changes: 9 additions & 3 deletions app/views/combine_transactions/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
<% transactions_table_headers.each do |h| %>
<% next if h[:sort].in? ['event_time', 'cost'] %>
<th scope="col">
<% if h[:sort] == "none" %>
<% if h[:sort].in? ['revenue_ratio', 'none'] %>
<%= "#{h[:name]}" %>
<% else %>
<%= link_to "#{h[:name]}", combine_transactions_path(ch_remote_params(params, h[:sort])), remote: true, class: "#{params[:sort] == h[:sort] ? 'text-primary' : ''}" %>
<% end %>
<% if h[:sort] == 'price' %>
<span class="question-mark" data-bs-toggle="tooltip" title='<%= t("views.contract_trading.price") %>'>?</span>
<% end %>
<% if h[:sort] == 'revenue_ratio' %>
<span class="question-mark" data-bs-toggle="tooltip" title='<%= t("views.contract_trading.revenue_ratio") %>'>?</span>
<% end %>
</th>
<% end %>
</tr>
Expand All @@ -23,9 +29,9 @@
<td><%= h.qty.round(4) %></td>
<td><%= h.amount.round(4) %> <%= h.to_symbol %></td>
<td><%= "#{(h.cost_ratio(@total_summary[:total_cost].to_f) * 100).round(4)}%" %></td>
<td class="<%= h.revenue > 0 ? 'pos-num' : 'neg-num' %>"><%= "#{h.revenue.round(4)} #{h.to_symbol}" %></td>
<td class="<%= h.revenue > 0 ? 'pos-num' : 'neg-num' %>"><%= "#{h.revenue.round(4)} #{h.to_symbol} (#{h.sold_revenue.round(4)} #{h.to_symbol})" %></td>
<td class="<%= h.roi > 0 ? 'pos-num' : 'neg-num' %>" ><%= "#{(h.roi * 100).round(4)}%" %></td>
<td><%= "#{(h.revenue_ratio(@total_summary[:total_revenue].to_f) * 100).round(4)}%" %></td>
<td><%= "#{(h.revenue_ratio(@total_summary) * 100).round(4)}%" %></td>
<td><%= h.source %></td>
</tr>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@
<li class="nav-item">
<%= link_to "合并交易记录快照", combine_tx_snapshot_infos_path, class: "nav-link #{@page_index == 35 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<!-- <li class="nav-item">
<%= link_to "现货仓位列表", public_spot_balances_path, class: "nav-link #{@page_index == 23 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<%= link_to "现货仓位快照", spot_balance_snapshot_infos_path, class: "nav-link #{@page_index == 22 ? 'active' : ''}" %>
</li>
</li> -->
</ul>
</li>
<% if current_user %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ zh-CN:
trading_fee: 交易当天的仓位快照总的资金费用乘以该笔交易占当天仓位的比例
roi: 空单:已实现收益 / 总投入 / 多单: 已实现收益 / (总投入 + 已实现收益)
closing_revenue: 收益 + 资金费用
price: 交易金额 / 数量
revenue_ratio: 如果收益为正,就是和盈利总金额的比例 / 收益为负,就是和亏损总金额的比例
wallet_history:
deposit: 充值
withdraw: 提币
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSoldRevenueToCombineTransactions < ActiveRecord::Migration[6.1]
def change
add_column :combine_transactions, :sold_revenue, :decimal, default: 0
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSoldRevenueToCombineTxSnapshotRecords < ActiveRecord::Migration[6.1]
def change
add_column :combine_tx_snapshot_records, :sold_revenue, :decimal, default: 0
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eada408

Please sign in to comment.