Skip to content

Commit

Permalink
Add open orders page
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 24, 2023
1 parent e660e1a commit 3670f47
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/controllers/page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ def holding_duration_chart
@average_roi_chart_data = GetHoldingDurationsByRoiChartService.execute(average: true)
end

def open_orders
@page_index = 36
@open_orders = OpenPositionOrder.order(amount: :desc).page(params[:page]).per(15)
end

private
def get_anchor(data_type)
case data_type
Expand Down
26 changes: 26 additions & 0 deletions app/jobs/get_binance_open_orders_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class GetBinanceOpenOrdersJob < ApplicationJob
queue_as :daily_job

def perform
open_orders = BinanceFuturesService.new.get_pending_orders

OpenPositionOrder.transaction do
open_orders.each do |open_order|
price = open_order['price'].to_f
qty = open_order['origQty'].to_f
amount = price * qty
order = OpenPositionOrder.where(order_id: open_order['orderId'], symbol: open_order['symbol']).first_or_initialize
order.update(
status: open_order['status'],
price: price,
orig_qty: qty,
amount: amount,
order_type: open_order['type'],
trade_type: open_order['side'].downcase,
position_side: open_order['positionSide'].downcase,
stop_price: open_order['stopPrice']
)
end
end
end
end
2 changes: 2 additions & 0 deletions app/models/open_position_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class OpenPositionOrder < ApplicationRecord
end
5 changes: 3 additions & 2 deletions app/services/binance_futures_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def get_my_trades(symbol, from_date)
do_request("get", url, payload)
end

def get_pending_orders(symbol)
def get_pending_orders(symbol=nil)
url = BASE_URL + "/fapi/v1/openOrders?"
payload = {timestamp: get_timestamp, symbol: symbol}
payload = {timestamp: get_timestamp}
payload.merge!({symbol: symbol}) if symbol
do_request("get", url, payload)
end

Expand Down
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<%= link_to "首页", public_user_positions_path, class: "nav-link #{@page_index == 1 ? 'active' : ''}" %>
</li>
<li class="nav-item dropdown">
<button class="nav-link btn dropdown-toggle <%= 'active' if @page_index.in?([2, 6, 11, 24, 25, 29, 30, 31]) %>" type="button" id="dropdownMenuTradingHistory" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
<button class="nav-link btn dropdown-toggle <%= 'active' if @page_index.in?([2, 6, 11, 24, 25, 29, 30, 31, 36]) %>" type="button" id="dropdownMenuTradingHistory" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
合约交易记录
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuTradingHistory">
Expand All @@ -44,6 +44,9 @@
<li class="nav-item">
<%= link_to "已平仓合约数据快照", closing_histories_snapshot_infos_path, class: "nav-link #{@page_index == 31 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<%= link_to "委托单列表", open_orders_path, class: "nav-link #{@page_index == 36 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<%= link_to "历史仓位变化曲线图", positions_graphs_snapshot_infos_path, class: "nav-link #{@page_index == 6 ? 'active' : ''}" %>
</li>
Expand Down
36 changes: 36 additions & 0 deletions app/views/page/open_orders.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<div class="m-3 row">
<div class="col-10 mx-auto">
<h3>币安合约委托单</h3>
<div class="mt-3">
<table class="table">
<thead>
<tr class="table-container-tr">
<th>币种</th>
<th>类别</th>
<th>委托价</th>
<th>触发价</th>
<th>委托数量</th>
<th>委托金额</th>
<th>订单类型</th>
</tr>
</thead>
<tbody>
<% if @open_orders.any? %>
<% @open_orders.each do |order| %>
<tr>
<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.stop_price.round(4) %></td>
<td><%= order.orig_qty.round(4) %></td>
<td><%= order.amount.round(4) %></td>
<td><%= order.order_type %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= paginate @open_orders %>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
get "/liquidations_ranking" => "page#liquidations_ranking", as: :liquidations_ranking
get "/refresh_liquidations_list" => "page#refresh_liquidations_list", as: :refresh_liquidations_list
get "/holding_duration_chart" => "page#holding_duration_chart", as: :holding_duration_chart
get "/open_orders" => "page#open_orders", as: :open_orders
post "/set_public_positions_filter" => "page#set_public_positions_filter", as: :set_public_positions_filter

get "/healthcheck", to: "page#health_check"
Expand Down
23 changes: 23 additions & 0 deletions db/migrate/20231124092319_create_open_position_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class CreateOpenPositionOrders < ActiveRecord::Migration[6.1]
def change
create_table :open_position_orders do |t|
t.string :symbol
t.string :order_id
t.string :trade_type
t.string :position_side
t.string :status
t.string :order_type
t.decimal :price
t.decimal :stop_price
t.decimal :orig_qty
t.decimal :amount

t.timestamps
end

add_index :open_position_orders, :order_id
add_index :open_position_orders, :symbol
add_index :open_position_orders, :trade_type
add_index :open_position_orders, :position_side
end
end
21 changes: 20 additions & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions spec/factories/open_position_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :open_position_order do
symbol { 'BTCUSDT' }
price { 1 }
orig_qty { 1 }
end
end
9 changes: 9 additions & 0 deletions spec/models/open_position_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

RSpec.describe OpenPositionOrder, type: :model do
let(:open_position_order) { create(:open_position_order) }

it "have a valid factory" do
expect(open_position_order).to be_valid
end
end

0 comments on commit 3670f47

Please sign in to comment.