Skip to content

Commit

Permalink
Add open spot orders page
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 28, 2023
1 parent 11fd0cb commit 8fa33d5
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 9 deletions.
13 changes: 12 additions & 1 deletion app/controllers/page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def holding_duration_chart
@average_roi_chart_data = GetHoldingDurationsByRoiChartService.execute(average: true)
end

def open_orders
def open_position_orders
@page_index = 36
@symbol = params[:search]
@trade_type = params[:trade_type]
Expand All @@ -233,6 +233,17 @@ def open_orders
@open_orders = open_orders.page(params[:page]).per(15)
end

def open_spot_orders
@page_index = 37
@symbol = params[:search]
@trade_type = params[:trade_type]
open_orders = OpenSpotOrder.order(order_time: :desc)
@symbols = open_orders.pluck(:symbol).uniq
open_orders = open_orders.where(symbol: @symbol) if @symbol.present?
open_orders = open_orders.where(trade_type: @trade_type) if @trade_type.present?
@open_orders = open_orders.page(params[:page]).per(15)
end

private
def get_anchor(data_type)
case data_type
Expand Down
34 changes: 33 additions & 1 deletion app/jobs/get_binance_open_orders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class GetBinanceOpenOrdersJob < ApplicationJob
queue_as :daily_job

def perform
get_position_orders
get_spot_orders
end

def get_position_orders
open_orders = BinanceFuturesService.new.get_pending_orders

OpenPositionOrder.transaction do
Expand All @@ -23,8 +28,35 @@ def perform
)
end

symbols = open_orders.map{|order| order['symbol']}
symbols = open_orders.map{|order| order['symbol']}.uniq
OpenPositionOrder.where.not(symbol: symbols).delete_all
end
end

def get_spot_orders
open_orders = BinanceSpotsService.new.get_open_orders

OpenSpotOrder.transaction do
open_orders.each do |open_order|
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
order.update(
status: open_order[:status],
price: price,
orig_qty: qty,
executed_qty: open_order[:executedQty],
amount: amount,
order_type: open_order[:type],
trade_type: open_order[:side]&.downcase,
stop_price: open_order[:stopPrice],
order_time: Time.at(open_order[:time]/1000)
)
end

symbols = open_orders.map{|order| order[:symbol]}.uniq
OpenSpotOrder.where.not(symbol: symbols).delete_all
end
end
end
2 changes: 2 additions & 0 deletions app/models/open_spot_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class OpenSpotOrder < ApplicationRecord
end
2 changes: 1 addition & 1 deletion app/services/binance_spots_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_account
end

def get_open_orders
@client.open_order_list
@client.open_orders
end

def get_order(symbol, order_id)
Expand Down
7 changes: 5 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<%= 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' : ''}" %>
<%= link_to "委托单列表", open_position_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' : ''}" %>
Expand All @@ -62,7 +62,7 @@
</ul>
</li>
<li class="nav-item dropdown">
<button class="nav-link btn dropdown-toggle <%= 'active' if @page_index.in?([8, 9, 10, 22, 23, 34, 35]) %>" 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?([8, 9, 10, 22, 23, 34, 35, 37]) %>" 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 @@ -75,6 +75,9 @@
<li class="nav-item">
<%= link_to "现货收益曲线图", revenue_chart_origin_transactions_path, class: "nav-link #{@page_index == 34 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<%= link_to "委托单列表", open_spot_orders_path, class: "nav-link #{@page_index == 37 ? 'active' : ''}" %>
</li>
<li class="nav-item">
<%= link_to "合并交易记录列表", combine_transactions_path, class: "nav-link #{@page_index == 9 ? 'active' : ''}" %>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="col-10 mx-auto">
<h3>币安合约委托单</h3>
<div class="mb-3 mt-3">
<%= form_tag open_orders_path, id: "search_targets", class: "position-relative", method: "GET" do %>
<%= form_tag open_position_orders_path, id: "search_targets", class: "position-relative", method: "GET" do %>
<div class="input-group mb-3 position-filter">
<%= select_tag(:search, options_for_select(@symbols, @symbol), { prompt: '请选择币种...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
Expand All @@ -11,7 +11,7 @@
<%= select_tag(:position_side, options_for_select([['做多', 'long'], ['做空', 'short']], @position_side),{ prompt: '请选择持仓类别...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<button type="submit" class="btn btn-primary mx-3">确定</button>
<%= link_to "Reset", open_orders_path, class: 'btn btn-warning me-3' %>
<%= link_to "Reset", open_position_orders_path, class: 'btn btn-warning me-3' %>
</div>
<% end %>
</div>
Expand Down
50 changes: 50 additions & 0 deletions app/views/page/open_spot_orders.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="m-3 row">
<div class="col-10 mx-auto">
<h3>币安现货委托单</h3>
<div class="mb-3 mt-3">
<%= form_tag open_spot_orders_path, id: "search_targets", class: "position-relative", method: "GET" do %>
<div class="input-group mb-3 position-filter">
<%= select_tag(:search, options_for_select(@symbols, @symbol), { prompt: '请选择币种...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:trade_type, options_for_select([['买入', 'buy'], ['卖出', 'sell']], @trade_type),{ prompt: '请选择买卖类别...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<button type="submit" class="btn btn-primary mx-3">确定</button>
<%= link_to "Reset", open_spot_orders_path, class: 'btn btn-warning me-3' %>
</div>
<% end %>
</div>
<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>
<th>订单类型</th>
</tr>
</thead>
<tbody>
<% if @open_orders.any? %>
<% @open_orders.each do |order| %>
<tr>
<td><%= order.order_time %></td>
<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.orig_qty.round(4) %></td>
<td><%= order.executed_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>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
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
get "/open_position_orders" => "page#open_position_orders", as: :open_position_orders
get "/open_spot_orders" => "page#open_spot_orders", as: :open_spot_orders
post "/set_public_positions_filter" => "page#set_public_positions_filter", as: :set_public_positions_filter

get "/healthcheck", to: "page#health_check"
Expand Down
24 changes: 24 additions & 0 deletions db/migrate/20231128095531_create_open_spot_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class CreateOpenSpotOrders < ActiveRecord::Migration[6.1]
def change
create_table :open_spot_orders do |t|
t.string :symbol
t.string :order_id
t.string :trade_type
t.string :status
t.string :order_type
t.decimal :price
t.decimal :stop_price
t.decimal :orig_qty
t.decimal :executed_qty
t.decimal :amount
t.datetime :order_time

t.timestamps
end

add_index :open_spot_orders, :order_id
add_index :open_spot_orders, :symbol
add_index :open_spot_orders, :trade_type
add_index :open_spot_orders, :order_time
end
end
22 changes: 21 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_spot_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :open_spot_order do
symbol { 'BTCUSDT' }
price { 1 }
orig_qty { 1 }
end
end
9 changes: 9 additions & 0 deletions spec/models/open_spot_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

RSpec.describe OpenSpotOrder, type: :model do
let(:open_spot_order) { create(:open_spot_order) }

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

0 comments on commit 8fa33d5

Please sign in to comment.