Skip to content

Commit

Permalink
Merge pull request #710 from Drakkar-Software/dev
Browse files Browse the repository at this point in the history
Master merge
  • Loading branch information
GuillaumeDSM authored Oct 21, 2022
2 parents 22c8b5d + 3f4fc89 commit 10f4dfe
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
19 changes: 19 additions & 0 deletions Services/Interfaces/web_interface/api/trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def orders():
return flask.jsonify(result)


@api.api.route("/positions", methods=['GET', 'POST'])
@login.login_required_when_activated
def positions():
if flask.request.method == 'GET':
real_positions, simulated_positions = interfaces_util.get_all_positions()

return json.dumps({"real_positions": real_positions, "simulated_positions": simulated_positions})
elif flask.request.method == "POST":
result = ""
request_data = flask.request.get_json()
action = flask.request.args.get("action")
if action == "close_position":
if interfaces_util.close_positions([request_data]):
result = "Position closed"
else:
return util.get_rest_reply('Impossible to close position: position already closed.', 500)
return flask.jsonify(result)


@api.api.route("/refresh_portfolio", methods=['POST'])
@login.login_required_when_activated
def refresh_portfolio():
Expand Down
38 changes: 38 additions & 0 deletions Services/Interfaces/web_interface/static/js/components/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ function handle_cancel_buttons() {
add_cancel_individual_orders_button();
}

function handle_close_buttons() {
$("button[data-action=close_position]").each(function () {
$(this).click(function () {
close_after_confirm($('#ClosePositionModal'), $(this).data("position_symbol"),
$(this).data("position_side"), $(this).data("update-url"));
});
});
}

function cancel_after_confirm(modalElement, data, update_url, disable_cancel_buttons=false){
modalElement.modal("toggle");
const confirmButton = modalElement.find(".btn-danger");
Expand All @@ -92,6 +101,30 @@ function cancel_after_confirm(modalElement, data, update_url, disable_cancel_but
});
}

function close_after_confirm(modalElement, symbol, side, update_url){
modalElement.modal("toggle");
const confirmButton = modalElement.find(".btn-danger");
confirmButton.off("click");
const data = {
symbol: symbol,
side: side,
}
modalElement.keypress(function(e) {
if(e.which === 13) {
handle_close_confirm(modalElement, confirmButton, data, update_url);
}
});
confirmButton.click(function () {
handle_close_confirm(modalElement, confirmButton, data, update_url);
});
}

function handle_close_confirm(modalElement, confirmButton, data, update_url){
send_and_interpret_bot_update(data, update_url, null, orders_request_success_callback, position_request_failure_callback);
modalElement.unbind("keypress");
modalElement.modal("hide");
}

function handle_confirm(modalElement, confirmButton, data, update_url, disable_cancel_buttons){
if (disable_cancel_buttons){
disable_cancel_all_buttons();
Expand Down Expand Up @@ -134,6 +167,10 @@ function orders_request_failure_callback(updated_data, update_url, dom_root_elem
reload_orders();
}

function position_request_failure_callback(updated_data, update_url, dom_root_element, msg, status) {
create_alert("error", msg.responseText, "");
}

function reload_orders(){
const previous_search = ordersDataTable.search();
const previous_order = ordersDataTable.order();
Expand Down Expand Up @@ -184,5 +221,6 @@ $(document).ready(function() {
"paging": false,
});
handle_cancel_buttons();
handle_close_buttons();
register_notification_callback(ordersNotificationCallback);
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
<td>{{ position.unrealized_pnl | round(5) }} {{ position.currency if position.symbol_contract.is_inverse_contract() else position.market }} ({{ position.get_unrealized_pnl_percent() | round(5) }}%)</td>
<td>{{ position.exchange_manager.exchange.name if position.exchange_manager else '' }}</td>
<td>{{ sim_or_real }}</td>
<td class="text-center py-1">
<button type="button" class="btn btn-sm btn-outline-danger waves-effect" data-action="close_position"
data-position_symbol="{{ position.symbol }}" data-position_side="{{ position.side.value }}"
data-update-url="{{ url_for('api.positions', action='close_position') }}">
<i class="fas fa-ban"></i>
</button>
</td>
</tr>
{% endif %}
{%- endmacro %}
Expand Down
24 changes: 24 additions & 0 deletions Services/Interfaces/web_interface/templates/trading.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ <h2>Positions</h2>
<th scope="col">Unrealized PNL</th>
<th scope="col">Exchange</th>
<th scope="col">#</th>
<th class="text-center" scope="col">Close</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -210,6 +211,29 @@ <h4>Cancel order ?</h4>
</div>
</div>
</div>

<div class="modal" id="ClosePositionModal" tabindex="-1" role="dialog" aria-labelledby="#CancelOrderModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content modal-text">
<div class="modal-header primary-text">
<h5 class="modal-title">Confirm action</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body text-center">
<h4>Close position ?</h4>
<p>
This will create orders to close this position.
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger"><i class="fas fa-ban"></i> Close position</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{% endif %}

{% endblock %}
Expand Down
12 changes: 6 additions & 6 deletions Trading/Mode/daily_trading_mode/daily_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
quantity=order_quantity,
price=order_price,
reduce_only=user_reduce_only)
if current_order := await self.trader.create_order(current_order):
if current_order := await self.trading_mode.create_order(current_order):
created_orders.append(current_order)

elif state == trading_enums.EvaluatorStates.SHORT.value and not self.DISABLE_SELL_ORDERS:
Expand All @@ -392,7 +392,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
quantity=order_quantity,
price=order_price,
reduce_only=user_reduce_only)
if updated_limit := await self.trader.create_order(current_order):
if updated_limit := await self.trading_mode.create_order(current_order):
created_orders.append(updated_limit)
# ensure stop orders are enabled and limit order was not instantly filled
if (self.USE_STOP_ORDERS or not user_stop_price.is_nan()) and updated_limit.is_open():
Expand All @@ -411,7 +411,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
side=trading_enums.TradeOrderSide.SELL,
reduce_only=True,
group=oco_group)
await self.trader.create_order(current_order)
await self.trading_mode.create_order(current_order)

elif state == trading_enums.EvaluatorStates.NEUTRAL.value:
return []
Expand All @@ -436,7 +436,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
quantity=order_quantity,
price=order_price,
reduce_only=user_reduce_only)
if updated_limit := await self.trader.create_order(current_order):
if updated_limit := await self.trading_mode.create_order(current_order):
created_orders.append(updated_limit)
# ensure future trading and stop orders are enabled and limit order was not instantly filled
if self.exchange_manager.is_future and (self.USE_STOP_ORDERS or not user_stop_price.is_nan()) \
Expand All @@ -456,7 +456,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
side=trading_enums.TradeOrderSide.BUY,
reduce_only=True,
group=oco_group)
await self.trader.create_order(current_order)
await self.trading_mode.create_order(current_order)

elif state == trading_enums.EvaluatorStates.VERY_LONG.value and not self.DISABLE_BUY_ORDERS:
quantity = self._get_market_quantity_from_risk(final_note, max_buy_size, base) \
Expand All @@ -473,7 +473,7 @@ async def create_new_orders(self, symbol, final_note, state, **kwargs):
quantity=order_quantity,
price=order_price,
reduce_only=user_reduce_only)
if current_order := await self.trader.create_order(current_order):
if current_order := await self.trading_mode.create_order(current_order):
created_orders.append(current_order)
if created_orders:
return created_orders
Expand Down

0 comments on commit 10f4dfe

Please sign in to comment.