Skip to content

Commit

Permalink
Trade: Adds action for closing profitable or loss side
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Oct 31, 2021
1 parent 0cb3dfc commit 716c138
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Trade.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@

// Trade actions.
enum ENUM_TRADE_ACTION {
TRADE_ACTION_CALC_LOT_SIZE = 1, // Recalculate lot size
TRADE_ACTION_ORDER_CLOSE_LEAST_LOSS, // Close order with least loss
TRADE_ACTION_ORDER_CLOSE_LEAST_PROFIT, // Close order with least profit
TRADE_ACTION_ORDER_CLOSE_MOST_LOSS, // Close order with most loss
TRADE_ACTION_ORDER_CLOSE_MOST_PROFIT, // Close order with most profit
TRADE_ACTION_ORDER_OPEN, // Open order
TRADE_ACTION_ORDERS_CLOSE_ALL, // Close open sell orders
TRADE_ACTION_ORDERS_CLOSE_BY_TYPE, // Close open orders by type (args)
TRADE_ACTION_ORDERS_CLOSE_IN_PROFIT, // Close open orders in profit
TRADE_ACTION_ORDERS_CLOSE_IN_TREND, // Close open orders in trend
TRADE_ACTION_ORDERS_CLOSE_IN_TREND_NOT, // Close open orders NOT in trend
TRADE_ACTION_CALC_LOT_SIZE = 1, // Recalculate lot size
TRADE_ACTION_ORDER_CLOSE_LEAST_LOSS, // Close order with least loss
TRADE_ACTION_ORDER_CLOSE_LEAST_PROFIT, // Close order with least profit
TRADE_ACTION_ORDER_CLOSE_MOST_LOSS, // Close order with most loss
TRADE_ACTION_ORDER_CLOSE_MOST_PROFIT, // Close order with most profit
TRADE_ACTION_ORDER_OPEN, // Open order
TRADE_ACTION_ORDERS_CLOSE_ALL, // Close open sell orders
TRADE_ACTION_ORDERS_CLOSE_BY_TYPE, // Close open orders by type (args)
TRADE_ACTION_ORDERS_CLOSE_IN_PROFIT, // Close open orders in profit
TRADE_ACTION_ORDERS_CLOSE_IN_TREND, // Close open orders in trend
TRADE_ACTION_ORDERS_CLOSE_IN_TREND_NOT, // Close open orders NOT in trend
TRADE_ACTION_ORDERS_CLOSE_SIDE_IN_LOSS, // Close orders in loss side
TRADE_ACTION_ORDERS_CLOSE_SIDE_IN_PROFIT, // Close orders in profit side
// TRADE_ACTION_ORDERS_REMOVE_ALL_PENDING,
TRADE_ACTION_ORDERS_LIMIT_SET, // Set orders per period limit
TRADE_ACTION_STATE_ADD, // Add trade specific state (1 arg)
Expand Down
21 changes: 21 additions & 0 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,27 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
RefreshActiveOrders(true);
}
break;
case TRADE_ACTION_ORDERS_CLOSE_SIDE_IN_LOSS:
if (Get<bool>(TRADE_STATE_ORDERS_ACTIVE) && orders_active.Size() > 0) {
ENUM_ORDER_TYPE _order_types1[] = {ORDER_TYPE_BUY, ORDER_TYPE_SELL};
ENUM_ORDER_TYPE _order_type_profitable =
_oquery_ref.Ptr()
.FindPropBySum<ENUM_ORDER_TYPE, ENUM_ORDER_PROPERTY_CUSTOM, ENUM_ORDER_PROPERTY_INTEGER, float>(
_order_types1, ORDER_PROP_PROFIT, ORDER_TYPE);
_result &=
OrdersCloseViaCmd(Order::NegateOrderType(_order_type_profitable), ORDER_REASON_CLOSED_BY_ACTION) >= 0;
}
break;
case TRADE_ACTION_ORDERS_CLOSE_SIDE_IN_PROFIT:
if (Get<bool>(TRADE_STATE_ORDERS_ACTIVE) && orders_active.Size() > 0) {
ENUM_ORDER_TYPE _order_types2[] = {ORDER_TYPE_BUY, ORDER_TYPE_SELL};
ENUM_ORDER_TYPE _order_type_profitable2 =
_oquery_ref.Ptr()
.FindPropBySum<ENUM_ORDER_TYPE, ENUM_ORDER_PROPERTY_CUSTOM, ENUM_ORDER_PROPERTY_INTEGER, float>(
_order_types2, ORDER_PROP_PROFIT, ORDER_TYPE);
_result &= OrdersCloseViaCmd(_order_type_profitable2, ORDER_REASON_CLOSED_BY_ACTION) >= 0;
}
break;
case TRADE_ACTION_ORDERS_LIMIT_SET:
// Sets the new limits.
tparams.SetLimits((ENUM_TRADE_STAT_TYPE)_args[0].integer_value, (ENUM_TRADE_STAT_PERIOD)_args[1].integer_value,
Expand Down

0 comments on commit 716c138

Please sign in to comment.