-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement spot transactions brtr/coin_elite#730
- Loading branch information
Showing
10 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
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) | ||
end | ||
|
||
def total_loss | ||
snapshot_records.available.year_to_date.loss.sum(&:revenue) | ||
end | ||
|
||
def total_revenue | ||
snapshot_records.available.year_to_date.where(trade_type: 'buy').sum(&:revenue) | ||
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 | ||
if total_profit == 0 | ||
infos = TransactionsSnapshotInfo.joins(:snapshot_records) | ||
max_profit = infos.max {|a, b| a.total_profit <=> b.total_profit} | ||
if max_profit | ||
total_profit = max_profit.total_profit | ||
$redis.set(redis_key, total_profit, ex: 10.hours) | ||
$redis.set("#{redis_key}_date", max_profit.event_date.strftime("%Y-%m-%d"), ex: 10.hours) | ||
end | ||
end | ||
total_profit | ||
end | ||
|
||
def self.max_loss(user_id: nil, date: Date.yesterday) | ||
redis_key = "user_#{user_id}_#{date.to_s}_spots_max_loss" | ||
total_loss = $redis.get(redis_key).to_f | ||
if total_loss == 0 | ||
infos = TransactionsSnapshotInfo.joins(:snapshot_records) | ||
max_loss = infos.min {|a, b| a.total_loss <=> b.total_loss} | ||
if max_loss | ||
total_loss = max_loss.total_loss | ||
$redis.set(redis_key, total_loss, ex: 10.hours) | ||
$redis.set("#{redis_key}_date", max_loss.event_date.strftime("%Y-%m-%d"), ex: 10.hours) | ||
end | ||
end | ||
total_loss | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/views/origin_transactions/_total_revenue_chart.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<div class="mt-3"> | ||
<canvas id="revenueChart" style="height: 300px; width: 100%" data-data="<%= @records.to_json %>" ></canvas> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener('turbolinks:load', () => { | ||
var canvas = document.getElementById('revenueChart'); | ||
if(canvas){ | ||
var ctx = canvas.getContext('2d'); | ||
var chart_data = JSON.parse(ctx.canvas.dataset.data) | ||
const result = Object.values(chart_data) | ||
var myChart = new Chart(ctx, { | ||
type: 'line', | ||
data: { | ||
labels: Object.keys(chart_data), | ||
datasets: [ | ||
{ | ||
label: "绝对收益走势图(每天)", | ||
data: result, | ||
tension: 0.4, | ||
backgroundColor: value => { | ||
const rate = Object.values(value)[3]; | ||
return rate > 0 ? 'rgb(54, 162, 235)' : 'rgb(255, 99, 132)' | ||
}, | ||
yAxisID: 'y' | ||
}, | ||
], | ||
}, | ||
options: { | ||
scales: { | ||
x: { | ||
grid: { | ||
display: false, | ||
} | ||
}, | ||
y: { | ||
position: 'left', | ||
ticks: { | ||
maxTicksLimit: 5 | ||
}, | ||
}, | ||
}, | ||
responsive: false | ||
} | ||
}); | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<div class="m-3 row" id="analysis_page"> | ||
<h2> | ||
现货收益走势图 | ||
</h2> | ||
<div class="mt-5"> | ||
<%= form_tag revenue_chart_origin_transactions_path, id: "period_targets", method: "GET", class: "row" do %> | ||
<div class="btn-group" role="group" aria-label="Basic radio toggle button group" style="margin-left:3rem;width:33%;"> | ||
<%= radio_button_tag :period, "quarter", params[:period] == "quarter", class: "btn-check", id: "btnradio1", onclick: "this.form.submit();" %> | ||
<label class="btn btn-outline-primary quarter" for="btnradio1">三个月</label> | ||
<%= radio_button_tag :period, "month", params[:period] == "month", class: "btn-check", id: "btnradio2", onclick: "this.form.submit();" %> | ||
<label class="btn btn-outline-primary month" for="btnradio2">一个月</label> | ||
</div> | ||
<% end %> | ||
</div> | ||
<div class="chart mb-5"> | ||
<div> | ||
<%= render partial: "total_revenue_chart" %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
get :refresh | ||
get :users | ||
post :add_key | ||
get :revenue_chart | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters