This commit is contained in:
2025-06-03 16:22:01 +03:30
parent 48123642b7
commit 078dc35ae0
9 changed files with 3005 additions and 45 deletions
@@ -78,6 +78,860 @@ string ToString(ENUM_XCATBEA_TRADE_FINALIZATION value)
return result;
}
//
// Position Selecting ...
enum ENUM_XCATBEA_POSITION_SELECT_TYPES
{
XCATBEA_POSITION_SELECT_NONE = 0, // None
XCATBEA_POSITION_SELECT_ALL = 1, // All
XCATBEA_POSITION_SELECT_IN_PROFITS = 2, // In Profit Positions
XCATBEA_POSITION_SELECT_IN_DRAWDOWNS = 3, // In Drawdown Positions
XCATBEA_POSITION_SELECT_OLDEST = 4, // Oldest Position
XCATBEA_POSITION_SELECT_YOUNGEST = 5, // Youngest Position
XCATBEA_POSITION_SELECT_IN_PROFITS_OLDEST = 6, // In Profit Positions Oldest one
XCATBEA_POSITION_SELECT_IN_PROFITS_YOUNGEST = 7, // In Profit Positions Youngest one
XCATBEA_POSITION_SELECT_IN_PROFITS_MIN = 8, // Minimum Profitable Position
XCATBEA_POSITION_SELECT_IN_PROFITS_MAX = 9, // Maximum Profitable Position
XCATBEA_POSITION_SELECT_IN_DRAWDOWN_OLDEST = 10, // In Drawdown Positions Oldest one
XCATBEA_POSITION_SELECT_IN_DRAWDOWN_YOUNGEST = 11, // In Drawdown Positions Youngest one
XCATBEA_POSITION_SELECT_IN_DRAWDOWN_MIN = 12, // Minimum in Drawdown Position
XCATBEA_POSITION_SELECT_IN_DRAWDOWN_MAX = 13, // Maximum in Drawdown Position
XCATBEA_POSITION_SELECT_SAMES = 14, // Same Type Positions
XCATBEA_POSITION_SELECT_SAMES_OLDEST = 15, // Same Type Positions Oldest one
XCATBEA_POSITION_SELECT_SAMES_YOUNGEST = 16, // Same Type Positions Youngest one
XCATBEA_POSITION_SELECT_OPPOSITS = 17, // Opposit Type Positions
XCATBEA_POSITION_SELECT_OPPOSIT_OLDEST = 18, // Opposit Type Positions Oldest one
XCATBEA_POSITION_SELECT_OPPOSIT_YOUNGEST = 19, // Opposit Type Positions Youngest one
XCATBEA_POSITION_SELECT_SAME_IN_PROFITS = 20, // Same Type In Profit Positions
XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_OLDEST = 21, // Same Type In Profit Positions Oldest one
XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_YOUNGEST = 22, // Same Type In Profit Positions Youngest one
XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_MIN = 23, // Minimum Profitable of Same Type Positions
XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_MAX = 24, // Maximum Profitable of Same Type Positions
XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS = 25, // Opposit Type In Profit Positions
XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_OLDEST = 26, // Opposit Type In Profit Positions Oldest one
XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_YOUNGEST = 27, // Opposit Type In Profit Positions Youngest one
XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_MIN = 28, // Minimum Profitable of Opposit Type Positions
XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_MAX = 29, // Maximum Profitable of Opposit Type Positions
XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWNS = 30, // Same Type In Deawdown Positions
XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_OLDEST = 31, // Same Type In Deawdown Positions Oldest one
XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_YOUNGEST = 32, // Same Type In Deawdown Positions Youngest one
XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_MIN = 33, // Minimum In Drawdown of Same Type Positions
XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_MAX = 34, // Maximum In Drawdown of Same Type Positions
XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWNS = 35, // Opposit Type In Deawdown Positions
XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_OLDEST = 36, // Opposit Type In Deawdown Positions Oldest one
XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_YOUNGEST = 37, // Opposit Type In Deawdown Positions Youngest one
XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_MIN = 38, // Minimum In Drawdown of Opposit Type Positions
XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_MAX = 39, // Maximum In Drawdown of Opposit Type Positions
};
//
bool IsValid(ENUM_XCATBEA_POSITION_SELECT_TYPES value)
{
return value != XCATBEA_POSITION_SELECT_NONE;
}
//
string ToString(ENUM_XCATBEA_POSITION_SELECT_TYPES value)
{
return EnumToString(value);
}
/**
* Select Specified Position's Related Position(s)
* for Protecting ...
*
* @param selecteds: XPosition instance reference Collection, for Holding Selected Positions ...
* @param positions: XPosition instance reference Collection, All available source Positions ...
* @param forType: ENUM_X_POSITION_TYPES member, Spcified Position Type to Select for it ...
* @param selectType: ENUM_XCATBEA_POSITION_SELECT_TYPES member, Specified Positions Selecting Senario ...
*
* @return ( bool )
*/
bool SelectPosition(
XPosition &selecteds[],
XPosition &positions[],
ENUM_X_POSITION_TYPES forType,
ENUM_XCATBEA_POSITION_SELECT_TYPES selectType //
)
{
//
bool result = false;
//
// Normalize Args ...
Clean(selecteds);
//
// Validate Args ...
result =
IsValid(forType) &&
HasChild(positions) &&
IsValid(selectType);
if (!result)
{
return result;
}
//
// Extract Separate Type of Positions ...
//
int idx = -1;
bool has = false;
XPosition iPosition;
//
bool isLong = IsLong(forType);
bool isShort = IsShort(forType);
int count = ArraySize(positions);
//
XPosition samePositions[];
XPosition oppositPositions[];
XPosition inProfitPositions[];
XPosition inDrawdownPositions[];
XPosition sameInProfitPositions[];
XPosition sameInDrawdownPositions[];
XPosition oppositInProfitPositions[];
XPosition oppositInDrawdownPositions[];
//
for (int i = 0; i < count; i++)
{
//
// Select Indexed Position ...
iPosition = positions[i];
//
bool isIPosLong = IsLong(iPosition.type);
bool isIPosShort = IsShort(iPosition.type);
//
// Select if Same Type ...
bool isSame =
(isLong && isIPosLong) ||
(isShort && isIPosShort);
if (isSame)
{
//
AddRef(
iPosition,
samePositions //
);
}
//
// Select if Opposit Type ...
bool isOpposit =
(isLong && isIPosShort) ||
(isShort && isIPosLong);
if (isOpposit)
{
//
AddRef(
iPosition,
oppositPositions //
);
}
//
iPosition.Clean();
}
//
// Filling Other Requirement Collections ...
//
ExtractInProfitPositions(
positions,
inProfitPositions //
);
//
ExtractInProfitPositions(
samePositions,
sameInProfitPositions //
);
//
ExtractInProfitPositions(
oppositPositions,
oppositInProfitPositions //
);
//
ExtractInDrawdownPositions(
positions,
inDrawdownPositions //
);
//
ExtractInDrawdownPositions(
samePositions,
sameInDrawdownPositions //
);
//
ExtractInDrawdownPositions(
oppositPositions,
oppositInDrawdownPositions //
);
//
// Implement Selection Senario ...
switch (selectType)
{
//
case XCATBEA_POSITION_SELECT_ALL:
//
Copy(
positions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_IN_PROFITS:
//
Copy(
inProfitPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_IN_DRAWDOWNS:
//
Copy(
inDrawdownPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_OLDEST:
//
GetOldest(
iPosition,
positions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_YOUNGEST:
//
GetYoungest(
iPosition,
positions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_PROFITS_OLDEST:
//
GetOldest(
iPosition,
inProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_PROFITS_YOUNGEST:
//
GetYoungest(
iPosition,
inProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_PROFITS_MIN:
//
idx = FindMinProfitIndex(inProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = inProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_PROFITS_MAX:
//
idx = FindMaxProfitIndex(inProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = inProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_DRAWDOWN_OLDEST:
//
GetOldest(
iPosition,
inDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_DRAWDOWN_YOUNGEST:
//
GetYoungest(
iPosition,
inDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_DRAWDOWN_MIN:
//
idx = FindMinDrawdownIndex(inDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = inDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_IN_DRAWDOWN_MAX:
//
idx = FindMaxDrawdownIndex(inDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = inDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAMES:
//
Copy(
samePositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_SAMES_OLDEST:
//
GetOldest(
iPosition,
samePositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAMES_YOUNGEST:
//
GetYoungest(
iPosition,
samePositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS:
//
Copy(
oppositPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_OLDEST:
//
GetOldest(
iPosition,
oppositPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_YOUNGEST:
//
GetYoungest(
iPosition,
oppositPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_PROFITS:
//
Copy(
sameInProfitPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_OLDEST:
//
GetOldest(
iPosition,
sameInProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_YOUNGEST:
//
GetYoungest(
iPosition,
sameInProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_MIN:
//
idx = FindMinProfitIndex(sameInProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = sameInProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_PROFITS_MAX:
//
idx = FindMaxProfitIndex(sameInProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = sameInProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS:
//
Copy(
oppositInProfitPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_OLDEST:
//
GetOldest(
iPosition,
oppositInProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_YOUNGEST:
//
GetYoungest(
iPosition,
oppositInProfitPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_MIN:
//
idx = FindMinProfitIndex(oppositInProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = oppositInProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSIT_IN_PROFITS_MAX:
//
idx = FindMaxProfitIndex(oppositInProfitPositions);
if (IsValidIndex(idx))
{
//
iPosition = oppositInProfitPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWNS:
//
Copy(
sameInDrawdownPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_OLDEST:
//
GetOldest(
iPosition,
sameInDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_YOUNGEST:
//
GetYoungest(
iPosition,
sameInDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_MIN:
//
idx = FindMinDrawdownIndex(sameInDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = sameInDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_SAME_IN_DRAWDOWN_MAX:
//
idx = FindMaxDrawdownIndex(sameInDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = sameInDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWNS:
//
Copy(
oppositInDrawdownPositions,
selecteds //
);
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_OLDEST:
//
GetOldest(
iPosition,
oppositInDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_YOUNGEST:
//
GetYoungest(
iPosition,
oppositInDrawdownPositions //
);
if (iPosition.IsValid())
{
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_MIN:
//
idx = FindMinDrawdownIndex(oppositInDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = oppositInDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
case XCATBEA_POSITION_SELECT_OPPOSITS_IN_DRAWDOWN_MAX:
//
idx = FindMaxDrawdownIndex(oppositInDrawdownPositions);
if (IsValidIndex(idx))
{
//
iPosition = oppositInDrawdownPositions[idx];
//
AddRef(
iPosition,
selecteds //
);
}
iPosition.Clean();
break;
//
}
//
// Validate Result ...
result = HasChild(selecteds);
//
// Cleanup Resources ...
//
if (!result)
{
Clean(selecteds);
}
//
Clean(samePositions);
Clean(oppositPositions);
Clean(inProfitPositions);
Clean(inDrawdownPositions);
Clean(sameInProfitPositions);
Clean(sameInDrawdownPositions);
Clean(oppositInProfitPositions);
Clean(oppositInDrawdownPositions);
//
return result;
}
//
struct XCATBEASymbolInfo
{