Files
mt5-quant-lib/Orders/OrderTracker.mqh
T

64 lines
1.8 KiB
Plaintext
Raw Normal View History

2025-07-04 16:56:12 +02:00
#include <Trade/OrderInfo.mqh>
#include <Trade/PositionInfo.mqh>
class OrderTracker {
protected:
2025-07-07 15:43:17 +02:00
COrderInfo m_order;
CPositionInfo m_position;
2025-07-04 16:56:12 +02:00
public:
2025-07-07 15:43:17 +02:00
int count_open_positions(string symbol, int order_side, long magic_number);
int count_all_positions(string symbol, long magic_number);
int count_pending_orders(string symbol, ENUM_ORDER_TYPE order_type, long magic);
2025-07-04 16:56:12 +02:00
};
int OrderTracker::count_open_positions(string symbol, int order_side, long magic_number) {
2025-07-07 15:43:17 +02:00
int count = 0;
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
if (PositionGetString(POSITION_SYMBOL) == symbol && PositionGetInteger(POSITION_MAGIC) == magic_number) {
if (order_side == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
count++;
}
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
if (order_side == 2 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) {
count++;
}
}
}
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
return count;
2025-07-04 16:56:12 +02:00
}
int OrderTracker::count_all_positions(string symbol, long magic_number) {
2025-07-07 15:43:17 +02:00
int count = 0;
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
if (PositionGetString(POSITION_SYMBOL) == symbol && PositionGetInteger(POSITION_MAGIC) == magic_number) {
count++;
}
}
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
return count;
2025-07-04 16:56:12 +02:00
}
int OrderTracker::count_pending_orders(string symbol, ENUM_ORDER_TYPE order_type, long magic) {
2025-07-07 15:43:17 +02:00
int count = 0;
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (m_order.SelectByIndex(i)) {
if (OrderGetInteger(ORDER_MAGIC) == magic && OrderGetString(ORDER_SYMBOL) == symbol) {
if (m_order.OrderType() == order_type) {
count++;
}
2025-07-04 16:56:12 +02:00
}
2025-07-07 15:43:17 +02:00
}
}
2025-07-04 16:56:12 +02:00
2025-07-07 15:43:17 +02:00
return count;
2025-07-04 16:56:12 +02:00
}