Skip to content

Commit

Permalink
add funding rate
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Mar 27, 2024
1 parent 44cfe60 commit 364f31b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/models/snapshot_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def self.last_summary(user_id: nil, data: nil)
loss_count: display_number(data[:loss_count] - records[:loss_count]),
loss_amount: display_number(data[:loss_amount] - records[:loss_amount]),
roi: display_number(new_roi * 100 - old_roi * 100),
total_funding_fee: display_number(data[:total_funding_fee] - records[:total_funding_fee])
total_funding_fee: display_number(data[:total_funding_fee] - records[:total_funding_fee]),
new_total_funding_fee: display_number(data[:new_total_funding_fee].to_f - records[:new_total_funding_fee].to_f)
}
end

Expand Down
8 changes: 6 additions & 2 deletions app/models/user_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ def self.total_summary(user_id=nil)
records = user_id ? UserPosition.where(user_id: user_id) : UserPosition.where(user_id: nil)
profit_records = records.select{|r| r.revenue > 0}
loss_records = records.select{|r| r.revenue < 0}
total_cost = records.sum(&:amount)
new_total_funding_fee = FundingFeeHistory.where('user_id is null and event_date > ?', '2024-02-14').sum(&:amount) #统计2月14号清仓之后新的资金费用
funding_rate = total_cost.zero? ? 0 : new_total_funding_fee.to_f / total_cost
date = Date.yesterday
infos = SnapshotInfo.includes(:snapshot_positions).where("event_date <= ?", date)
{
profit_count: profit_records.count,
profit_amount: profit_records.sum(&:revenue),
loss_count: loss_records.count,
loss_amount: loss_records.sum(&:revenue),
total_cost: records.sum(&:amount),
total_cost: total_cost,
total_revenue: records.sum(&:revenue),
total_funding_fee: FundingFeeHistory.where(user_id: nil).sum(&:amount),
new_total_funding_fee: FundingFeeHistory.where('user_id is null and event_date > ?', '2024-02-14').sum(&:amount), #统计2月14号清仓之后新的资金费用
new_total_funding_fee: new_total_funding_fee,
funding_rate: funding_rate,
max_profit: infos.max_profit(user_id: user_id),
max_profit_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_max_profit_date"),
max_loss: infos.max_loss(user_id: user_id),
Expand Down
2 changes: 1 addition & 1 deletion app/views/page/user_positions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<p>总投入: <%= @total_summary[:total_cost].round(4) %> <%= last_summary_display(@last_summary[:total_cost]) %> / 绝对收益: <%= @total_summary[:total_revenue].round(4) %> <%= last_summary_display(@last_summary[:total_revenue]) %> / 绝对收益占总投入的比例: <%= get_roi(@total_summary) %><%= last_summary_display(@last_summary[:roi], true) %></p>
<p>盈利总数量: <%= @total_summary[:profit_count] %> <%= last_summary_display(@last_summary[:profit_count]) %> / 盈利总金额: <%= @total_summary[:profit_amount].round(4) %><%= last_summary_display(@last_summary[:profit_amount]) %> </p>
<p>亏损总数量: <%= @total_summary[:loss_count] %> <%= last_summary_display(@last_summary[:loss_count]) %> / 亏损总金额: <%= @total_summary[:loss_amount].round(4) %> <%= last_summary_display(@last_summary[:loss_amount]) %> </p>
<p>总资金费用: <%= price_change_style @total_summary[:total_funding_fee].round(4) %><%= last_summary_display(@last_summary[:total_funding_fee].to_f) %> / 2月14号之后总的资金费用: <%= price_change_style @total_summary[:new_total_funding_fee].round(4) %><%= last_summary_display(@last_summary[:new_total_funding_fee].to_f) %><%= link_to '曲线图', funding_fee_chart_path, target: '_blank' %></p>
<p>总资金费用: <%= price_change_style @total_summary[:total_funding_fee].round(4) %><%= last_summary_display(@last_summary[:total_funding_fee].to_f) %> / 2月14号之后总的资金费用: <%= price_change_style @total_summary[:new_total_funding_fee].round(4) %><%= last_summary_display(@last_summary[:new_total_funding_fee].to_f) %> / 资金费用和总投入的比例 <%= price_change_style @total_summary[:funding_rate] %>%<%= link_to '曲线图', funding_fee_chart_path, target: '_blank' %></p>
<p>历史最高盈利: <%= price_change_style @total_summary[:max_profit].round(4) %> ( <%= @total_summary[:max_profit_date] %> ) / 历史最高亏损: <%= price_change_style @total_summary[:max_loss].round(4) %> ( <%= @total_summary[:max_loss_date] %> )</p>
<p>历史最高盈利占总投入的比例: <%= price_change_style @total_summary[:max_profit_roi].round(4) %>% ( <%= @total_summary[:max_profit_roi_date] %> ) / 历史最高亏损占总投入的比例: <%= price_change_style @total_summary[:max_loss_roi].round(4) %>% ( <%= @total_summary[:max_loss_roi_date] %> )</p>
<p>历史最高绝对收益: <%= price_change_style @total_summary[:max_revenue].round(4) %> ( <%= @total_summary[:max_revenue_date] %> ) / 历史最低绝对收益: <%= price_change_style @total_summary[:min_revenue].round(4) %> ( <%= @total_summary[:min_revenue_date] %> )</p>
Expand Down

0 comments on commit 364f31b

Please sign in to comment.