Skip to content

Commit

Permalink
Add trading fee and total cost to adding positions list brtr/coin_eli…
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Sep 11, 2023
1 parent d938dab commit 39724c7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ def closing_histories_headers
name: '类别',
sort: 'trade_type',
},
{
name: '资金费用',
sort: 'trading_fee',
},
{
name: '币种',
sort: 'origin_symbol',
Expand All @@ -351,6 +355,10 @@ def closing_histories_headers
name: '平仓数量',
sort: 'qty',
},
{
name: '总投入',
sort: 'total_cost',
},
{
name: '平仓金额',
sort: 'amount',
Expand Down
12 changes: 12 additions & 0 deletions app/models/adding_positions_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ def cost
trade_type == 'buy' ? (amount.abs + revenue.to_f) / qty.abs : (amount.abs - revenue.to_f) / qty.abs
end

def total_cost
unit_cost.to_f * qty.abs
end

def holding_duration
return 0 if qty < 0
Time.current - event_date.to_time
end

def trading_fee
(AddingPositionsHistory.where(origin_symbol: origin_symbol, source: source).average_holding_duration / 86400) * last_funding_fee
end

def last_funding_fee
FundingFeeHistory.where(origin_symbol: origin_symbol, source: source).last.amount rescue 0
end

def self.total_qty
AddingPositionsHistory.sum(&:qty)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/closing_histories_snapshot_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def self.last_summary(data: nil)
}
end

def total_cost
unit_cost.to_f * qty.abs
end

private
def self.display_number(num)
num.to_f.round(2)
Expand Down
10 changes: 7 additions & 3 deletions app/services/import_okx_transactions_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def store_okx_model
source: @source,
user_id: @user_id,
origin_symbol: symbol,
qty: qty,
trade_type: trade_type
trade_type: trade_type,
qty: get_number(qty, revenue),
amount: get_number(amount,revenue)
).first_or_create

tx.update(
event_time: event_time,
price: price,
amount: amount,
fee: fee,
revenue: revenue,
fee_symbol: fee_symbol
Expand All @@ -100,4 +100,8 @@ def is_valid_file?
%w(csv xlsx).include?(file.original_filename.to_s.split(".").last)
end
end

def get_number(num, revenue)
revenue == 0 || num == 0 ? num : num * -1
end
end
2 changes: 2 additions & 0 deletions app/views/adding_positions_histories/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
<tr>
<td><%= h.event_date %></td>
<td class="<%= trade_type_style(h.trade_type) %>"><%= I18n.t("views.contract_trading.#{h.trade_type}") %></td>
<td><%= h.trading_fee.to_f.round(4) %></td>
<td>
<%= link_to display_symbol(h, 1), position_detail_path(origin_symbol: h.origin_symbol, source: h.source, trade_type: h.trade_type), target: '_blank' %>
</td>
<td><%= h.price.round(4) %></td>
<td><%= h.unit_cost&.round(4) %></td>
<td><%= h.current_price.round(4) %></td>
<td class="<%= h.qty.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.qty.round(4) %></td>
<td class="<%= h.total_cost.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.total_cost.round(4) %></td>
<td class="<%= h.amount.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.amount.round(4) %></td>
<td class="<%= h.get_revenue.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.get_revenue.round(4) %></td>
<td><%= "#{h.roi}%" %></td>
Expand Down
2 changes: 2 additions & 0 deletions app/views/closing_histories_snapshot_infos/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<thead>
<tr class="table-container-tr">
<% closing_histories_headers.each do |h| %>
<% next if h[:sort] == 'trading_fee' %>
<th scope="col">
<% if h[:sort] == "none" %>
<%= "#{h[:name]}" %>
Expand All @@ -27,6 +28,7 @@
<td><%= h.unit_cost&.round(4) %></td>
<td><%= h.current_price&.round(4) %></td>
<td class="<%= h.qty.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.qty.round(4) %></td>
<td class="<%= h.total_cost.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.total_cost.round(4) %></td>
<td class="<%= h.amount.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.amount.round(4) %></td>
<td class="<%= h.revenue.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= h.revenue.round(4) %></td>
<td><%= "#{h.roi}%" %></td>
Expand Down

0 comments on commit 39724c7

Please sign in to comment.