mql5/Experts/testRefactor2/Positions.mqh
2026-02-01 21:39:33 +01:00

96 lines
3.1 KiB
MQL5

//+------------------------------------------------------------------+
//| Positions.mqh – kolekce otevřených pozic symbolu |
//+------------------------------------------------------------------+
#property copyright "TestRefactor"
#property strict
#ifndef __POSITIONS_MQH__
#define __POSITIONS_MQH__
#include "Position.mqh"
//+------------------------------------------------------------------+
//| CPositions – kolekce CPosition; veškerá logika počtů uvnitř |
//+------------------------------------------------------------------+
class CPositions {
private:
string m_symbol;
CPosition m_positions[];
int m_count;
void Build() {
m_count = 0;
ArrayResize(m_positions, 0);
for(int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) != m_symbol) continue;
ArrayResize(m_positions, m_count + 1);
m_positions[m_count].Set(ticket, m_symbol);
m_count++;
}
}
public:
CPositions(string symbol) : m_symbol(symbol), m_count(0) {}
void Refresh() { Build(); }
string GetSymbol() const { return m_symbol; }
int Count() const { return m_count; }
int CountByType(long posType) const {
int n = 0;
for(int i = 0; i < m_count; i++) {
if(m_positions[i].GetType() == posType) n++;
}
return n;
}
int CountBuy() const { return CountByType(POSITION_TYPE_BUY); }
int CountSell() const { return CountByType(POSITION_TYPE_SELL); }
double GetMinOpenPrice(long posType) const {
double minPrice = DBL_MAX;
for(int i = 0; i < m_count; i++) {
if(m_positions[i].GetType() != posType) continue;
double op = m_positions[i].GetOpenPrice();
if(op < minPrice) minPrice = op;
}
return (minPrice == DBL_MAX) ? 0 : minPrice;
}
double GetMaxOpenPrice(long posType) const {
double maxPrice = 0;
for(int i = 0; i < m_count; i++) {
if(m_positions[i].GetType() != posType) continue;
double op = m_positions[i].GetOpenPrice();
if(op > maxPrice) maxPrice = op;
}
return maxPrice;
}
double GetMinOpenPriceBuy() const { return GetMinOpenPrice(POSITION_TYPE_BUY); }
double GetMaxOpenPriceBuy() const { return GetMaxOpenPrice(POSITION_TYPE_BUY); }
double GetMinOpenPriceSell() const { return GetMinOpenPrice(POSITION_TYPE_SELL); }
double GetMaxOpenPriceSell() const { return GetMaxOpenPrice(POSITION_TYPE_SELL); }
double GetTotalProfit() const {
double sum = 0;
for(int i = 0; i < m_count; i++)
sum += m_positions[i].GetProfit() + m_positions[i].GetSwap();
return sum;
}
void GetTickets(ulong &tickets[]) const {
ArrayResize(tickets, m_count);
for(int i = 0; i < m_count; i++)
tickets[i] = m_positions[i].GetTicket();
}
bool HasAny() const { return m_count > 0; }
bool HasBuy() const { return CountBuy() > 0; }
bool HasSell() const { return CountSell() > 0; }
bool HasNoPositions() const { return m_count == 0; }
};
#endif