last works ...

This commit is contained in:
2024-05-24 11:46:15 +03:30
parent dc4ee66b83
commit 7a50508816
10 changed files with 1515 additions and 567 deletions
+336 -4
View File
@@ -185,8 +185,44 @@ public:
bool CheckForGuard(XGuard &guards[]) override
{
//
// TODO: Implement this ...
return false;
bool result = false;
//
Clean(guards);
//
int descriptorsCount = CountDescriptors();
result = descriptorsCount > 0;
if (!result)
{
return result;
}
//
for (int i = 0; i < descriptorsCount; i++)
{
//
X121ProviderDescriptor iDescriptor = mDescriptors[i];
//
XGuard iGuards[];
bool iHasGuard = iDescriptor.provider.HasGuard(iGuards);
if (iHasGuard)
{
//
Copy(
iGuards,
guards,
false //
);
}
}
//
result = ArraySize(guards) > 0;
//
return result;
}
//
@@ -250,8 +286,193 @@ public:
bool HandleStateManagement() override
{
//
// TODO: Implement this ...
return false;
bool result = false;
//
int requiredAgeForSLTrail = 10;
int requiredDistanceForSLTrail = 10;
//
XPosition positions[];
int positionsCount = mTrader.GetPositions(positions);
if (positionsCount <= 0)
{
return result;
}
//
// Trail Positions Stop ...
for (int i = 0; i < positionsCount; i++)
{
//
XPosition iPosition = positions[i];
//
// Position Management for X786 ...
if (iPosition.provider == ToString(X786) ||
iPosition.provider == ToString(X121))
{
//
// Check Position in Profit or not ...
if (iPosition.profit > 0)
{
//
XTrail trail;
bool isTrailedBefore = GetTrail(
iPosition.ticket,
trail //
);
//
if (!isTrailedBefore)
{
//
// Add First Time ...
//
// Check Position Age ...
int iPositionAge = iPosition.GetAge(iPosition.period);
if (iPositionAge < requiredAgeForSLTrail)
{
continue;
}
//
// Check Position Profit Distance ...
double priceDistanceInPoint = PriceToPoint(iPosition.profit / iPosition.volume, iPosition.symbol);
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
if (priceDistance < requiredDistanceForSLTrail)
{
continue;
}
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double sl =
IsLong(iPosition.type)
? iPosition.sl + slDiff
: iPosition.sl - slDiff;
//
bool isInit = trail.Init(
iPosition.ticket,
iPosition.type,
iPosition.profit,
iPosition.sl,
sl);
if (!isInit)
{
continue;
}
}
else
{
//
// Update Exists ...
//
// Check Profit ...
bool isProfitPassed = iPosition.profit > trail.profit;
if (!isProfitPassed)
{
continue;
}
//
// Check Age Distance from Last ...
int age = iBarShift(
iPosition.symbol,
iPosition.period,
trail.time,
false //
);
if (age < requiredAgeForSLTrail)
{
continue;
}
//
double priceDistanceInPoint = PriceToPoint((iPosition.profit - trail.profit) / iPosition.volume, iPosition.symbol);
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
if (priceDistance < requiredDistanceForSLTrail)
{
//
// int positionAge = iPosition.GetAge(iPosition.period);
if (trail.level >= 3)
{
//
string comment = "EQM Close In Profit ...";
bool isClosed = mTrader.Close(
iPosition.ticket,
comment //
);
if (isClosed)
{
//
RemoveTrail(iPosition.ticket);
//
string message = "EQM Close (" + ToString(iPosition.ticket) + ") In Profit ...";
mAlert.Alert(message);
}
}
continue;
}
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double sl =
IsLong(iPosition.type)
? iPosition.sl + slDiff
: iPosition.sl - slDiff;
//
// Update Trail Values ...
bool isUpdated = trail.Update(
sl,
iPosition.profit //
);
if (!isUpdated)
{
continue;
}
}
//
if (trail.IsValid())
{
//
string comment = trail.GenerateTag();
bool isModified = mTrader.Modify(
trail.ticket,
trail.after,
iPosition.tp,
comment //
);
if (isModified)
{
//
RemoveTrail(trail.ticket);
//
AddTrail(trail);
//
string message = "Position (" + ToString(trail.ticket) + ") Trailed SL on Level: " + ToString(trail.level);
mAlert.Alert(message);
}
}
}
}
}
//
// Must false ...
return result;
}
//
@@ -319,6 +540,117 @@ private:
return ArraySize(mDescriptors);
}
//
// Trails Holding ...
//
XTrail mTrails[];
//
int CountTrails()
{
return ArraySize(mTrails);
}
//
bool RemoveTrail(ulong ticket)
{
//
bool result = false;
//
int idx = FindTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
return result;
}
//
result = ArrayRemove(
mTrails,
idx,
1 //
);
//
return result;
}
//
int FindTrailIndex(ulong ticket)
{
//
int result = -1;
//
int trailsCount = CountTrails();
if (ticket <= 0 || trailsCount <= 0)
{
return result;
}
//
for (int i = 0; i < trailsCount; i++)
{
//
XTrail iTrail = mTrails[i];
//
if (iTrail.ticket == ticket)
{
//
result = i;
break;
}
}
//
return result;
}
//
bool GetTrail(
ulong ticket,
XTrail &trail //
)
{
//
bool result = false;
//
int idx = FindTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
return result;
}
//
trail = mTrails[idx];
//
return result;
}
//
void AddTrail(
XTrail &trail //
)
{
//
if (!trail.IsValid())
{
return;
}
//
AddRef(
trail,
mTrails //
);
}
//
// Tools ...
};