Skip to content

Commit

Permalink
Update adding positions job
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Sep 11, 2023
1 parent b90900c commit d938dab
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
14 changes: 7 additions & 7 deletions app/jobs/get_recently_adding_positions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ def perform(date: Date.today, symbol: nil)
trading_roi = up&.roi
end

current_price = get_history_price(from_symbol.downcase, date)
current_price = get_current_price(source, origin_symbol)
aph.update(price: price, current_price: current_price, amount: amount, revenue: revenue, unit_cost: unit_cost, trading_roi: trading_roi)
SlackService.send_notification(nil, enqueued_block(from_symbol)) if AddingPositionsHistory.where("id != ? and event_date = ? and origin_symbol = ? and amount BETWEEN ? AND ?", aph.id, date, from_symbol, amount * 0.95, amount * 1.05).any?
end
end

def get_history_price(symbol, event_date)
date = event_date == Date.today ? event_date - 1.day : event_date
url = ENV['COIN_ELITE_URL'] + "/api/coins/history_price?symbol=#{symbol}&from_date=#{date}&to_date=#{date}"
response = RestClient.get(url)
data = JSON.parse(response.body)
data['result'].values[0].to_f rescue nil
def get_current_price(source, symbol)
if source == 'binance'
BinanceFuturesService.new.get_ticker_price(symbol)["price"].to_f rescue 0
else
OkxSpotsService.new.get_price(symbol)["data"].first["last"] rescue 0
end
end

def enqueued_block(origin_symbol)
Expand Down
4 changes: 3 additions & 1 deletion app/models/snapshot_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.total_summary(user_id: nil, is_synced: false, date: Date.yesterday)
profit_amount: profit_records.sum(&:revenue),
loss_count: loss_records.count,
loss_amount: loss_records.sum(&:revenue),
total_funding_fee: FundingFeeHistory.where('user_id is null and event_date <= ?', date - 1.day).sum(&:amount),
max_profit: infos.max_profit(user_id: user_id, is_synced: is_synced, date: date),
max_profit_date: $redis.get("#{redis_key}_max_profit_date"),
max_loss: infos.max_loss(user_id: user_id, is_synced: is_synced, date: date),
Expand Down Expand Up @@ -46,7 +47,8 @@ def self.last_summary(user_id: nil, data: nil)
profit_amount: display_number(data[:profit_amount] - records[:profit_amount]),
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)
roi: display_number(new_roi * 100 - old_roi * 100),
total_funding_fee: display_number(data[:total_funding_fee] - records[:total_funding_fee])
}
end

Expand Down
6 changes: 6 additions & 0 deletions app/services/binance_futures_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def get_24hr_tickers
end.map{|k,v| v[0]}
end

def get_ticker_price(symbol)
url = BASE_URL + "/fapi/v1/ticker/price?symbol=#{symbol}"
response = RestClient.get(url)
JSON.parse(response)
end

def get_funding_rate(symbol, start_time=nil)
url = BASE_URL + "/fapi/v1/fundingRate?"
payload = {symbol: symbol, limit: 1000, startTime: start_time}
Expand Down
6 changes: 3 additions & 3 deletions app/views/page/position_detail.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@
<td>总计</td>
<td class="<%= trade_type_style(d.trade_type) %>"><%= I18n.t("views.contract_trading.#{d.trade_type}") %></td>
<td></td>
<td><%= @open_data.average(:price).round(4) %></td>
<td><%= (@open_data.total_amount / @open_data.total_qty).round(4) %></td>
<td><%= @open_data.average(:unit_cost).round(4) %></td>
<td><%= d.current_price.round(4) %></td>
<td class="<%= @open_data.total_qty.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= @open_data.total_qty %></td>
<td class="<%= @open_data.total_qty.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= @open_data.total_qty.round(4) %></td>
<td class="<%= @open_data.total_amount.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= @open_data.total_amount.round(4) %></td>
<td class="<%= total_revenue.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= total_revenue.round(4) %></td>
<td><%= ((total_revenue / @open_data.total_amount.abs) * 100).round(4) %>%</td>
Expand Down Expand Up @@ -194,7 +194,7 @@
<tr>
<td>总计</td>
<td class="<%= trade_type_style(d.trade_type) %>"><%= I18n.t("views.contract_trading.#{d.trade_type}") %></td>
<td><%= @close_data.average(:price).round(4) %></td>
<td><%= (@close_data.total_amount / @close_data.total_qty).round(4) %></td>
<td><%= @close_data.average(:unit_cost).round(4) %></td>
<td><%= d.current_price.round(4) %></td>
<td class="<%= @close_data.total_qty.to_f > 0 ? 'pos-num' : 'neg-num' %>"><%= @close_data.total_qty.round(4) %></td>
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) %></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) %> ( <%= actual_event_date @total_summary[:max_profit_date] %> ) / 历史最高亏损: <%= @total_summary[:max_loss].round(4) %> ( <%= actual_event_date @total_summary[:max_loss_date] %> )</p>
<p>历史最高绝对收益: <%= @total_summary[:max_revenue].round(4) %> ( <%= actual_event_date @total_summary[:max_revenue_date] %> ) / 历史最低绝对收益: <%= @total_summary[:min_revenue].round(4) %> ( <%= actual_event_date @total_summary[:min_revenue_date] %> )</p>
<p>历史最高绝对收益占总投入的比例: <%= @total_summary[:max_roi].round(4) %>% ( <%= actual_event_date @total_summary[:max_roi_date] %> ) / 历史最低绝对收益占总投入的比例: <%= @total_summary[:min_roi].round(4) %>% ( <%= actual_event_date @total_summary[:min_roi_date] %> )</p>
Expand Down
1 change: 0 additions & 1 deletion spec/jobs/get_recently_adding_positions_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
describe '#GetRecentlyAddingPositionsJob perform' do
before do
ActiveJob::Base.queue_adapter = :test
allow(subject).to receive(:get_history_price).and_return(nil)
end

it 'should enqeue job' do
Expand Down

0 comments on commit d938dab

Please sign in to comment.