This commit is contained in:
2024-06-16 11:11:33 +03:30
parent 8d26a582dd
commit 8eab3ef9b6
8 changed files with 1398 additions and 398 deletions
+211 -317
View File
@@ -2919,332 +2919,257 @@ public:
}
//
// XPosition Pack ...
// Support Functions ...
//
// Extract specific Positions Pack ...
// in this senario there is no need to Position exists itself ...
bool GetPositionPack(
ulong ticket, // Specified Positions Ticket
string symbol, // Specified Position Symbol
ENUM_TIMEFRAMES period, // Specified Position Period
XPositionPack &pack // Hold Result
// Check a Position has Support or not ...
bool HasSupport(
ulong ticket,
bool forceOpen = true //
)
{
//
bool result = false;
//
pack.Clean();
//
XPosition position;
bool hasPosition = GetPosition(
ticket,
position
//
);
if (hasPosition)
{
pack.position = position;
}
//
XOrder supportOrders[];
XPosition supportPositions[];
//
XOrder orders[];
GetOrders(
orders,
symbol, // Specified Symbol
NULL, // All Providers
period, // Specified Period
NULL, // All Types
ORDER_STATE_PLACED, // Just UnTriggered Orders
true // Filter by Magic
);
int ordersCount = ArraySize(orders);
//
// Select Orders ...
if (ordersCount > 0)
{
//
for (int i = 0; i < ordersCount; i++)
{
//
XOrder iOrder = orders[i];
//
// Retrieve Order Supported Ticket ...
ulong parentTicket = ExtractSupportedTicket(iOrder.comment);
if (NotEmpty(parentTicket) && parentTicket == ticket)
{
//
AddRef(
iOrder,
supportOrders
//
);
}
}
}
//
int supportOrdersCount = ArraySize(supportOrders);
if (supportOrdersCount > 0)
{
//
pack.supportOrdersCount = supportOrdersCount;
//
Copy(
supportOrders,
pack.supportOrders);
}
//
XPosition positions[];
GetPositions(
positions,
symbol, // Specified Symbol ...
NULL, // All Providers ...
period // Specified Period ...
);
int positionsCount = ArraySize(positions);
//
// Select Positions ...
if (positionsCount > 0)
{
//
for (int i = 0; i < positionsCount; i++)
{
//
XPosition iPosition = positions[i];
//
ulong parentTicket = ExtractSupportedTicket(iPosition.comment);
if (NotEmpty(parentTicket) && parentTicket == ticket)
{
//
AddRef(
iPosition,
supportPositions
//
);
}
}
}
//
int supportPositionsCount = ArraySize(supportPositions);
if (supportPositionsCount > 0)
{
//
pack.supportPositionsCount = supportPositionsCount;
//
Copy(
supportPositions,
pack.supportPositions);
}
//
result = pack.IsValid();
//
return result;
}
//
// Close Position Pack ...
bool ClosePositionPack(
XPositionPack &pack, // Specified Position Pack
string comment, // Specified Close Comment
bool inProfit = false, // if it's true only close Pack if in profit
double minProfitPerTrade = 0, // Min Profit based On Volume Factor ...
double minProfitPerVolumeFactor = 0 // Min Volume Factor ...
)
{
//
bool result = false;
//
ulong ticket = pack.ticket;
//
XPosition positions[];
//
XPosition pPosition;
result = GetPosition(
ticket,
pPosition //
);
if (result)
{
//
AddRef(
pPosition,
positions //
);
}
//
if (pack.supportPositionsCount > 0)
{
//
for (int i = 0; i < pack.supportPositionsCount; i++)
{
//
XPosition iPosition = pack.supportPositions[i];
//
AddRef(
iPosition,
positions //
);
}
}
//
// Force Close ...
if (!inProfit)
{
//
int closed = Close(
positions,
comment //
);
//
int cancelled = 0;
if (pack.supportOrdersCount > 0)
{
cancelled = CancelOrders(pack.supportOrders);
}
//
result = (closed + cancelled) == (pack.supportPositionsCount + pack.supportOrdersCount + 1);
}
else
{
//
bool canHedge = SpecifiedIsPositionsReadyForHedge(
positions,
minProfitPerTrade,
minProfitPerVolumeFactor //
);
if (canHedge)
{
//
int closed = Close(
positions,
comment //
);
//
int cancelled = 0;
if (pack.supportOrdersCount > 0)
{
cancelled = CancelOrders(pack.supportOrders);
}
//
result = (closed + cancelled) == (pack.supportPositionsCount + pack.supportOrdersCount + 1);
}
else
{
result = false;
}
}
//
return result;
}
bool ClosePositionPack(
ulong ticket, // Specified Position Pack
string symbol, // Specified Position Symbol
ENUM_TIMEFRAMES period, // Specified Position Period
string comment, // Specified Close Comment
bool inProfit = false, // if it's true only close Pack if in profit
double minProfitPerTrade = 0, // Min Profit based On Volume Factor ...
double minProfitPerVolumeFactor = 0 // Min Volume Factor ...
)
{
//
bool result = false;
//
XPositionPack pack;
result = GetPositionPack(
ticket,
symbol,
period,
pack //
);
result = ticket > 0;
if (!result)
{
return result;
}
//
result = ClosePositionPack(
pack,
comment,
inProfit,
minProfitPerTrade,
minProfitPerVolumeFactor //
if (forceOpen)
{
//
result = IsOpen(ticket);
if (!result)
{
return result;
}
}
//
XPosition positions[];
int positionsCount = GetPositions(positions);
result = IsValidSize(positionsCount);
if (!result)
{
return result;
}
//
// Extract Support Positions ...
XPosition supports[];
int supportsCount = ExtractSupports(
positions,
supports //
);
result = IsValidSize(supportsCount);
if (!result)
{
return result;
}
//
// Loop Through Supports ...
for (int i = 0; i < supportsCount; i++)
{
//
XPosition iSupport = supports[i];
//
ulong supportedTicket = ExtractSupportedTicket(iSupport.comment);
ulong eqmSupportedTicket = ExtractEQMSupportedTicket(iSupport.comment);
//
result =
//
(supportedTicket > 0 && ticket == supportedTicket)
//
||
//
(eqmSupportedTicket > 0 && ticket == eqmSupportedTicket)
//
;
if (result)
{
break;
}
}
//
return result;
}
//
// Close Positions Pack ...
int ClosePositionsPack(
XPosition &positions[], // Positions ...
string comment, // Specified Close Comment
bool inProfit = false, // if it's true only close Pack if in profit
double minProfitPerTrade = 0, // Min Profit based On Volume Factor ...
double minProfitPerVolumeFactor = 0 // Min Volume Factor ...
// Retrieve Specific Position's Supports ...
int GetSupports(
ulong ticket,
XPosition &dest[], // Result ...
bool forceOpen = true //
)
{
//
int result = 0;
//
int positionsCount = ArraySize(positions);
if (positionsCount <= 0)
Clean(dest);
//
bool hasSupport = HasSupport(
ticket,
forceOpen //
);
if (!hasSupport)
{
return result;
}
//
for (int i = 0; i < positionsCount; i++)
XPosition positions[];
int positionsCount = GetPositions(positions);
if (!IsValidSize(positionsCount))
{
return result;
}
//
// Extract Support Positions ...
XPosition supports[];
int supportsCount = ExtractSupports(
positions,
supports //
);
if (!IsValidSize(supportsCount))
{
return result;
}
//
// Loop Through Supports ...
for (int i = 0; i < supportsCount; i++)
{
//
XPosition iPosition = positions[i];
XPosition iSupport = supports[i];
//
bool isClosed = ClosePositionPack(
iPosition.ticket,
iPosition.symbol,
iPosition.period,
comment,
inProfit,
minProfitPerTrade,
minProfitPerVolumeFactor //
);
if (isClosed)
ulong supportedTicket = ExtractSupportedTicket(iSupport.comment);
ulong eqmSupportedTicket = ExtractEQMSupportedTicket(iSupport.comment);
//
bool isOwn =
//
(supportedTicket > 0 && ticket == supportedTicket)
//
||
//
(eqmSupportedTicket > 0 && ticket == eqmSupportedTicket)
//
;
if (isOwn)
{
result++;
//
AddRef(
iSupport,
dest //
);
}
}
//
result = ArraySize(dest);
//
return result;
}
//
// Retrieve Specific Position's Supports ...
int GetSupports(
ulong ticket,
XOrder &dest[], // Result ...
bool forceOpen = true //
)
{
//
int result = 0;
//
Clean(dest);
//
bool hasSupport = HasSupport(
ticket,
forceOpen //
);
if (!hasSupport)
{
return result;
}
//
XOrder orders[];
int ordersCount = GetOrders(
orders,
NULL, // All Symbols ...
NULL, // All Providers ...
NULL, // All Periods ...
X_POSITION_TYPE_ALL, // All Types ...
ORDER_STATE_PLACED, // Untriggere Orders ...
true //
);
if (!IsValidSize(ordersCount))
{
return result;
}
//
// Extract Support Positions ...
XOrder supports[];
int supportsCount = ExtractSupports(
orders,
supports //
);
if (!IsValidSize(supportsCount))
{
return result;
}
//
// Loop Through Supports ...
for (int i = 0; i < supportsCount; i++)
{
//
XOrder iSupport = supports[i];
//
ulong supportedTicket = ExtractSupportedTicket(iSupport.comment);
ulong eqmSupportedTicket = ExtractEQMSupportedTicket(iSupport.comment);
//
bool isOwn =
//
(supportedTicket > 0 && ticket == supportedTicket)
//
||
//
(eqmSupportedTicket > 0 && ticket == eqmSupportedTicket)
//
;
if (isOwn)
{
//
AddRef(
iSupport,
dest //
);
}
}
//
result = ArraySize(dest);
//
return result;
}
@@ -3536,37 +3461,6 @@ protected:
return result;
}
//
// Extract Specific Deals Position's Pack ...
// this used when a Position SL or TP triggered and we want to close
// all it's Supported Packs ...
bool GetDealPack(
XDeal &deal,
XPositionPack &pack //
)
{
//
bool result = false;
//
ulong ticket = deal.positionId;
ENUM_TIMEFRAMES dealPeriod = GetDealPeriod(deal);
string dealSymbol = deal.symbol;
//
// So We Have to Get Positions Pack ...
result = GetPositionPack(
ticket,
dealSymbol,
dealPeriod,
pack
//
);
//
return result;
}
//
ENUM_TIMEFRAMES GetDealPeriod(XDeal &deal)
{