Skip to content

Commit

Permalink
Update origin transactions total summary
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Oct 11, 2023
1 parent 5fd97e0 commit cc1f898
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/controllers/transactions_snapshot_infos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def show
@page_index = 10
sort = params[:sort].presence || "revenue"
sort_type = params[:sort_type].presence || "desc"
records = @info.snapshot_records
records = @info.snapshot_records.available.year_to_date
records = records.where(from_symbol: params[:search]) if params[:search].present?
@records = records.order("#{sort} #{sort_type}").page(params[:page]).per(20)
@total_summary = records.total_summary
Expand Down
12 changes: 12 additions & 0 deletions app/models/origin_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def self.total_summary(user_id=nil)
max_profit_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_profit_date"),
max_loss: infos.max_loss(user_id: user_id),
max_loss_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_loss_date"),
max_revenue: infos.max_revenue(user_id: user_id),
max_revenue_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_revenue_date"),
min_revenue: infos.min_revenue(user_id: user_id),
min_revenue_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_min_revenue_date"),
max_roi: infos.max_roi(user_id: user_id),
max_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_roi_date"),
min_roi: infos.min_roi(user_id: user_id),
min_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_min_roi_date"),
max_profit_roi: infos.max_profit_roi(user_id: user_id),
max_profit_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_profit_roi_date"),
max_loss_roi: infos.max_loss_roi(user_id: user_id),
max_loss_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_loss_roi_date")
}
end

Expand Down
40 changes: 40 additions & 0 deletions app/models/snapshot_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def margin_summary
}
end

def total_profit_roi
total_cost.zero? ? 0 : ((total_profit / total_cost) * 100).round(4)
end

def total_loss_roi
total_cost.zero? ? 0 : ((total_loss / total_cost) * 100).round(4)
end

def self.max_profit(user_id: nil, is_synced: false, date: Date.yesterday)
redis_key = is_synced ? "user_#{user_id}_#{date.to_s}_synced_positions_max_profit" : "user_#{user_id}_#{date.to_s}_positions_max_profit"
total_profit = $redis.get(redis_key).to_f
Expand Down Expand Up @@ -139,6 +147,38 @@ def self.min_roi(user_id: nil, is_synced: false, date: Date.yesterday)
total_roi.to_f
end

def self.max_profit_roi(user_id: nil, is_synced: false, date: Date.yesterday)
redis_key = is_synced ? "user_#{user_id}_#{date.to_s}_synced_positions_max_profit_roi" : "user_#{user_id}_#{date.to_s}_positions_max_profit_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = SnapshotInfo.where(user_id: user_id)
infos = user_id.present? && !is_synced ? infos.uploaded : infos.synced
max_roi = infos.max {|a, b| a.total_profit_roi <=> b.total_profit_roi}
if max_roi
total_roi = max_roi.total_profit_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", max_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

def self.max_loss_roi(user_id: nil, is_synced: false, date: Date.yesterday)
redis_key = is_synced ? "user_#{user_id}_#{date.to_s}_synced_positions_max_loss_roi" : "user_#{user_id}_#{date.to_s}_positions_max_loss_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = SnapshotInfo.where(user_id: user_id)
infos = user_id.present? && !is_synced ? infos.uploaded : infos.synced
max_roi = infos.min {|a, b| a.total_loss_roi <=> b.total_loss_roi}
if max_roi
total_roi = max_roi.total_loss_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", max_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

private
def display_number(num)
num.to_f.round(2)
Expand Down
117 changes: 115 additions & 2 deletions app/models/transactions_snapshot_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ class TransactionsSnapshotInfo < ApplicationRecord
has_many :snapshot_records, class_name: 'TransactionsSnapshotRecord', foreign_key: :transactions_snapshot_info_id

def total_profit
snapshot_records.available.year_to_date.profit.sum(&:revenue)
snapshot_records.available.year_to_date.profit.where(trade_type: 'buy').sum(&:revenue)
end

def total_loss
snapshot_records.available.year_to_date.loss.sum(&:revenue)
snapshot_records.available.year_to_date.loss.where(trade_type: 'buy').sum(&:revenue)
end

def total_revenue
snapshot_records.available.year_to_date.where(trade_type: 'buy').sum(&:revenue)
end

def total_cost
records = snapshot_records.available.year_to_date
calculate_field(records, :amount)
end

def total_roi
total_cost.zero? ? 0 : ((total_revenue / total_cost) * 100).round(4)
end

def total_profit_roi
total_cost.zero? ? 0 : ((total_profit / total_cost) * 100).round(4)
end

def total_loss_roi
total_cost.zero? ? 0 : ((total_loss / total_cost) * 100).round(4)
end

def self.max_profit(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_profit"
total_profit = $redis.get(redis_key).to_f
Expand Down Expand Up @@ -42,4 +59,100 @@ def self.max_loss(user_id: nil, date: Date.yesterday)
end
total_loss
end

def self.max_revenue(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_revenue"
total_revenue = $redis.get(redis_key).to_f
if total_revenue == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
max_revenue = infos.max {|a, b| a.total_revenue <=> b.total_revenue}
if max_revenue
total_revenue = max_revenue.total_revenue
$redis.set(redis_key, total_revenue, ex: 10.hours)
$redis.set("#{redis_key}_date", max_revenue.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_revenue
end

def self.min_revenue(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_min_revenue"
total_revenue = $redis.get(redis_key).to_f
if total_revenue == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
min_revenue = infos.min {|a, b| a.total_revenue <=> b.total_revenue}
if min_revenue
total_revenue = min_revenue.total_revenue
$redis.set(redis_key, total_revenue, ex: 10.hours)
$redis.set("#{redis_key}_date", min_revenue.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_revenue
end

def self.max_roi(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
max_roi = infos.max {|a, b| a.total_roi <=> b.total_roi}
if max_roi
total_roi = max_roi.total_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", max_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

def self.min_roi(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_min_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
min_roi = infos.min {|a, b| a.total_roi <=> b.total_roi}
if min_roi
total_roi = min_roi.total_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", min_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

def self.max_profit_roi(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_profit_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
max_roi = infos.max {|a, b| a.total_profit_roi <=> b.total_profit_roi}
if max_roi
total_roi = max_roi.total_profit_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", max_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

def self.max_loss_roi(user_id: nil, date: Date.yesterday)
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_loss_roi"
total_roi = $redis.get(redis_key).to_f
if total_roi == 0
infos = TransactionsSnapshotInfo.joins(:snapshot_records)
max_roi = infos.min {|a, b| a.total_loss_roi <=> b.total_loss_roi}
if max_roi
total_roi = max_roi.total_loss_roi
$redis.set(redis_key, total_roi, ex: 10.hours)
$redis.set("#{redis_key}_date", max_roi.event_date.strftime("%Y-%m-%d"), ex: 10.hours)
end
end
total_roi.to_f
end

private
def calculate_field(records, field_name = :revenue)
buys, sells = records.partition { |record| record.trade_type == "buy" }
buys.sum { |record| record.send(field_name) } - sells.sum { |record| record.send(field_name) }
end
end
25 changes: 10 additions & 15 deletions app/models/transactions_snapshot_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@ def self.year_to_date

def self.total_summary
records = TransactionsSnapshotRecord.available.year_to_date.where(trade_type: 'buy')
result = $redis.get("origin_transactions_snapshots_total_summary")
if result.nil?
profit_records = records.select{|r| r.revenue > 0}
loss_records = records.select{|r| r.revenue < 0}
result = {
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_revenue: records.sum(&:revenue)
}.to_json
$redis.set("origin_transactions_snapshots_total_summary", result, ex: 5.hours)
end
JSON.parse result
profit_records = records.select{|r| r.revenue > 0}
loss_records = records.select{|r| r.revenue < 0}
result = {
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_revenue: records.sum(&:revenue)
}
end

def revenue_ratio(total_cost)
Expand Down
6 changes: 5 additions & 1 deletion app/models/user_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def self.total_summary(user_id=nil)
max_roi: infos.max_roi(user_id: user_id),
max_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_max_roi_date"),
min_roi: infos.min_roi(user_id: user_id),
min_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_min_roi_date")
min_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_min_roi_date"),
max_profit_roi: infos.max_profit_roi(user_id: user_id),
max_profit_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_max_profit_roi_date"),
max_loss_roi: infos.max_loss_roi(user_id: user_id),
max_loss_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_positions_max_loss_roi_date"),
}
end

Expand Down
3 changes: 3 additions & 0 deletions app/views/origin_transactions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<p>盈利总数量: <%= @total_summary[:profit_count] %> / 盈利总金额: <%= @total_summary[:profit_amount].to_f.round(4) %> </p>
<p>亏损总数量: <%= @total_summary[:loss_count] %> / 亏损总金额: <%= @total_summary[:loss_amount].to_f.round(4) %> </p>
<p>历史最高盈利: <%= @total_summary[:max_profit].round(4) %> ( <%= @total_summary[:max_profit_date] %> ) / 历史最高亏损: <%= @total_summary[:max_loss].round(4) %> ( <%= @total_summary[:max_loss_date] %> )</p>
<p>历史最高盈利占总投入的比例: <%= @total_summary[:max_profit_roi].round(4) %>% ( <%= @total_summary[:max_profit_roi_date] %> ) / 历史最高亏损占总投入的比例: <%= @total_summary[:max_loss_roi].round(4) %>% ( <%= @total_summary[:max_loss_roi_date] %> )</p>
<p>历史最高绝对收益: <%= @total_summary[:max_revenue].round(4) %> ( <%= @total_summary[:max_revenue_date] %> ) / 历史最低绝对收益: <%= @total_summary[:min_revenue].round(4) %> ( <%= @total_summary[:min_revenue_date] %> )</p>
<p>历史最高绝对收益占总投入的比例: <%= @total_summary[:max_roi].round(4) %>% ( <%= @total_summary[:max_roi_date] %> ) / 历史最低绝对收益占总投入的比例: <%= @total_summary[:min_roi].round(4) %>% ( <%= @total_summary[:min_roi_date] %> )</p>
</div>
</div>
<div id="trading-histories-container">
Expand Down
1 change: 1 addition & 0 deletions app/views/page/user_positions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<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) %><%= link_to '曲线图', funding_fee_chart_path, target: '_blank' %></p>
<p>历史最高盈利: <%= @total_summary[:max_profit].round(4) %> ( <%= @total_summary[:max_profit_date] %> ) / 历史最高亏损: <%= @total_summary[:max_loss].round(4) %> ( <%= @total_summary[:max_loss_date] %> )</p>
<p>历史最高盈利占总投入的比例: <%= @total_summary[:max_profit_roi].round(4) %>% ( <%= @total_summary[:max_profit_roi_date] %> ) / 历史最高亏损占总投入的比例: <%= @total_summary[:max_loss_roi].round(4) %>% ( <%= @total_summary[:max_loss_roi_date] %> )</p>
<p>历史最高绝对收益: <%= @total_summary[:max_revenue].round(4) %> ( <%= @total_summary[:max_revenue_date] %> ) / 历史最低绝对收益: <%= @total_summary[:min_revenue].round(4) %> ( <%= @total_summary[:min_revenue_date] %> )</p>
<p>历史最高绝对收益占总投入的比例: <%= @total_summary[:max_roi].round(4) %>% ( <%= @total_summary[:max_roi_date] %> ) / 历史最低绝对收益占总投入的比例: <%= @total_summary[:min_roi].round(4) %>% ( <%= @total_summary[:min_roi_date] %> )</p>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/transactions_snapshot_infos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</div>
<div>
<div class="p-3">
<p>总投入: <%= @total_summary['total_cost'].to_f.round(4) %> / 总盈利: <%= @total_summary['total_revenue'].to_f.round(4) %> </p>
<p>盈利总数量: <%= @total_summary['profit_count'] %> / 盈利总金额: <%= @total_summary['profit_amount'].to_f.round(4) %> </p>
<p>亏损总数量: <%= @total_summary['loss_count'] %> / 亏损总金额: <%= @total_summary['loss_amount'].to_f.round(4) %> </p>
<p>总投入: <%= @total_summary[:total_cost].to_f.round(4) %> / 总盈利: <%= @total_summary[:total_revenue].to_f.round(4) %> </p>
<p>盈利总数量: <%= @total_summary[:profit_count] %> / 盈利总金额: <%= @total_summary[:profit_amount].to_f.round(4) %> </p>
<p>亏损总数量: <%= @total_summary[:loss_count] %> / 亏损总金额: <%= @total_summary[:loss_amount].to_f.round(4) %> </p>
</div>
</div>
<div id="position-histories-container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe TransactionsSnapshotInfosController, type: :controller do
let(:snapshot_info) { create(:transactions_snapshot_info) }
before do
10.times{ create(:transactions_snapshot_record, transactions_snapshot_info_id: snapshot_info.id) }
10.times{ create(:transactions_snapshot_record, transactions_snapshot_info_id: snapshot_info.id, event_time: Time.current) }
end

describe "GET index" do
Expand Down

0 comments on commit cc1f898

Please sign in to comment.