Skip to content

Commit

Permalink
Add current price to open spot orders
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 30, 2023
1 parent 4bbc1e9 commit e0a3846
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/jobs/get_binance_open_orders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def get_spot_orders
price = open_order[:price].to_f
qty = open_order[:origQty].to_f
amount = price * qty
order = OpenSpotOrder.where(order_id: open_order[:orderId], symbol: open_order[:symbol]).first_or_initialize
symbol = open_order[:symbol]
from_symbol = symbol.split('USDT')[0]
get_current_price(symbol, from_symbol)
order = OpenSpotOrder.where(order_id: open_order[:orderId], symbol: symbol).first_or_initialize
order.update(
status: open_order[:status],
price: price,
Expand All @@ -59,4 +62,21 @@ def get_spot_orders
OpenSpotOrder.where.not(symbol: symbols).delete_all
end
end

def get_current_price(symbol, from_symbol)
price = $redis.get("binance_spot_price_#{symbol}").to_f
if price == 0
price = BinanceSpotsService.new(user_id: user_id).get_price(symbol)[:price].to_f rescue 0
price = get_coin_price(from_symbol) if price.zero?
$redis.set("binance_spot_price_#{symbol}", price, ex: 2.hours)
end
end

def get_coin_price(symbol)
date = Date.yesterday
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
end
end
7 changes: 7 additions & 0 deletions app/models/open_spot_order.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class OpenSpotOrder < ApplicationRecord
def current_price
$redis.get("binance_spot_price_#{symbol}").to_f
end

def margin_rate
current_price.zero? ? 0 : ((price - current_price) / current_price) * 100
end
end
4 changes: 4 additions & 0 deletions app/views/page/open_spot_orders.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<th>币种</th>
<th>买卖类别</th>
<th>委托价</th>
<th>当前价</th>
<th>委托家和当前价的比例</th>
<th>委托数量</th>
<th>成交数量</th>
<th>委托金额</th>
Expand All @@ -35,6 +37,8 @@
<td><%= order.symbol %></td>
<td class="<%= trade_type_style(order.trade_type) %>"><%= I18n.t("views.trading.#{order.trade_type}") %></td>
<td><%= order.price.round(4) %></td>
<td><%= order.current_price.round(4) %></td>
<td><%= order.margin_rate.round(2) %>%</td>
<td><%= order.orig_qty.round(4) %></td>
<td><%= order.executed_qty.round(4) %></td>
<td><%= order.amount.round(4) %></td>
Expand Down

0 comments on commit e0a3846

Please sign in to comment.