Skip to content

Commit

Permalink
Update open orders page
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 25, 2023
1 parent 3670f47 commit 11fd0cb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
10 changes: 9 additions & 1 deletion app/controllers/page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,15 @@ def holding_duration_chart

def open_orders
@page_index = 36
@open_orders = OpenPositionOrder.order(amount: :desc).page(params[:page]).per(15)
@symbol = params[:search]
@trade_type = params[:trade_type]
@position_side = params[:position_side]
open_orders = OpenPositionOrder.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.where(position_side: @position_side) if @position_side.present?
@open_orders = open_orders.page(params[:page]).per(15)
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def trade_type_style(trade_type)
trade_type.to_s.downcase == 'sell' ? "text-danger" : "text-success"
end

def position_side_style(position_side)
position_side.to_s.downcase == 'long' ? "text-danger" : "text-success"
end

def display_symbol(h, s=nil)
str = "#{h.from_symbol} / #{h.fee_symbol}"
return str if s.present?
Expand Down
6 changes: 5 additions & 1 deletion app/jobs/get_binance_open_orders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ def perform
order_type: open_order['type'],
trade_type: open_order['side'].downcase,
position_side: open_order['positionSide'].downcase,
stop_price: open_order['stopPrice']
stop_price: open_order['stopPrice'],
order_time: Time.at(open_order['time']/1000)
)
end

symbols = open_orders.map{|order| order['symbol']}
OpenPositionOrder.where.not(symbol: symbols).delete_all
end
end
end
22 changes: 19 additions & 3 deletions app/views/page/open_orders.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
<div class="m-3 row">
<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 %>
<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>
<%= 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' %>
</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>
<th>委托金额</th>
<th>订单类型</th>
Expand All @@ -18,10 +33,11 @@
<% 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 class="<%= position_side_style(order.position_side) %>"><%= I18n.t("views.trading.#{order.position_side}") %></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>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ zh-CN:
trading:
sell:
buy:
long:
short:
revenue: "#买入交易为买入数量 * 当前价格 - 买入成本 / #卖出交易为卖出总价"
roi: 收益 / 成本
total_revenue: (买入数量-卖出数量)* 当前价格 + 卖出总价 - 买入成本
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddOrderTimeToOpenPositionOrders < ActiveRecord::Migration[6.1]
def change
add_column :open_position_orders, :order_time, :datetime
add_index :open_position_orders, :order_time
end
end
4 changes: 3 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 11fd0cb

Please sign in to comment.