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 Dec 4, 2023
1 parent a9373d6 commit 9437823
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 4 additions & 2 deletions app/jobs/get_binance_open_orders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ def get_spot_orders
amount = price * qty
symbol = open_order[:symbol]
from_symbol = symbol.split('USDT')[0]
get_current_price(symbol, from_symbol)
current_price = 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,
current_price: current_price,
orig_qty: qty,
executed_qty: open_order[:executedQty],
amount: amount,
Expand All @@ -66,10 +67,11 @@ def get_spot_orders
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 = BinanceSpotsService.new.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
price
end

def get_coin_price(symbol)
Expand Down
6 changes: 1 addition & 5 deletions app/models/open_spot_order.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class OpenSpotOrder < ApplicationRecord
def current_price
$redis.get("binance_spot_price_#{symbol}").to_f
end

def margin_rate
current_price.zero? ? 0 : ((current_price - price) / current_price) * 100
current_price.to_f.zero? ? 0 : ((current_price - price) / current_price) * 100
end
end
2 changes: 1 addition & 1 deletion app/views/page/open_spot_orders.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<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.current_price&.round(4) %></td>
<td class="<%= order.margin_rate > 0 ? 'pos-num' : 'neg-num' %>"><%= order.margin_rate.round(2) %>%</td>
<td><%= order.orig_qty.round(4) %></td>
<td><%= order.amount.round(4) %></td>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCurrentPriceToOpenSpotOrders < ActiveRecord::Migration[6.1]
def change
add_column :open_spot_orders, :current_price, :decimal
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9437823

Please sign in to comment.