This commit is contained in:
2024-05-25 04:25:11 +03:30
parent 7a50508816
commit da63c4cb71
9 changed files with 3522 additions and 165 deletions
+346 -23
View File
@@ -289,7 +289,11 @@ public:
bool result = false;
//
int requiredAgeForSLTrail = 10;
// TODO:
// Make them Configurable ...
int numberOfPositionsForHege = 4;
int levelsOnClose = 6;
int requiredAgeForSLTrail = 3;
int requiredDistanceForSLTrail = 10;
//
@@ -307,10 +311,18 @@ public:
//
XPosition iPosition = positions[i];
//
bool canManageState =
//
iPosition.provider == ToString(X786) ||
iPosition.provider == ToString(X121) ||
iPosition.provider == ToString(X92)
//
;
//
// Position Management for X786 ...
if (iPosition.provider == ToString(X786) ||
iPosition.provider == ToString(X121))
if (canManageState)
{
//
// Check Position in Profit or not ...
@@ -318,7 +330,7 @@ public:
{
//
XTrail trail;
bool isTrailedBefore = GetTrail(
bool isTrailedBefore = GetSLTrail(
iPosition.ticket,
trail //
);
@@ -355,6 +367,12 @@ public:
? iPosition.sl + slDiff
: iPosition.sl - slDiff;
// //
// double sl =
// IsLong(iPosition.type)
// ? iPosition.entry + (slDiff / 2)
// : iPosition.entry - (slDiff / 2);
//
bool isInit = trail.Init(
iPosition.ticket,
@@ -400,7 +418,7 @@ public:
{
//
// int positionAge = iPosition.GetAge(iPosition.period);
if (trail.level >= 3)
if (trail.level >= levelsOnClose)
{
//
string comment = "EQM Close In Profit ...";
@@ -411,7 +429,7 @@ public:
if (isClosed)
{
//
RemoveTrail(iPosition.ticket);
RemoveSLTrail(iPosition.ticket);
//
string message = "EQM Close (" + ToString(iPosition.ticket) + ") In Profit ...";
@@ -424,11 +442,20 @@ public:
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double selectedValue =
//
trail.level == 2
? iPosition.entry
: iPosition.sl
//
;
//
double sl =
IsLong(iPosition.type)
? iPosition.sl + slDiff
: iPosition.sl - slDiff;
? selectedValue + slDiff
: selectedValue - slDiff;
//
// Update Trail Values ...
@@ -456,10 +483,10 @@ public:
if (isModified)
{
//
RemoveTrail(trail.ticket);
RemoveSLTrail(trail.ticket);
//
AddTrail(trail);
AddSLTrail(trail);
//
string message = "Position (" + ToString(trail.ticket) + ") Trailed SL on Level: " + ToString(trail.level);
@@ -470,11 +497,193 @@ public:
}
}
//
// Hedge ...
if (positionsCount >= numberOfPositionsForHege)
{
//
double profit = CalculatePositionsProfit(positions);
double requiredProfit = CalculateRequiredProfitForHedge(positions);
//
bool isReadyForHedge = profit >= requiredProfit;
if (!isReadyForHedge)
{
return result;
}
//
string comment = "EQM Hedge ...";
int closed = mTrader.Close(
positions,
comment
//
);
if (closed > 0)
{
//
string message = "EQM Hedge Positions on Profit: " + ToString(profit);
//
mAlert.Alert(message);
//
for (int i = 0; i < positionsCount; i++)
{
RemoveSLTrail(positions[i].ticket);
}
}
}
//
// Must false ...
return result;
}
//
// Here we Can Filter Signals before Sending them to Execution ...
// based on Providers and etc ...
// for Volumes and Benefits ...
int HandleSignalManagement(XSignal &signals[]) override
{
//
int result = 0;
//
if (ArraySize(signals) <= 0)
{
return result;
}
//
// Create temp Array for Filtering ...
XSignal tmps[];
Copy(
signals,
tmps //
);
int tmpsCount = ArraySize(tmps);
//
// Cleaning Source ...
Clean(signals);
//
// Loop through Signals for Management ...
for (int i = 0; i < tmpsCount; i++)
{
//
XSignal iSignal = tmps[i];
//
string provider = iSignal.provider;
if (!IsValid(provider))
{
//
AddRef(
iSignal,
signals //
);
//
continue;
}
//
XPosition positions[];
int positionsCount = mTrader.GetPositions(
positions,
iSignal.symbol,
iSignal.provider,
NULL, // All Periods ...
iSignal.type //
);
if (positionsCount <= 5)
{
//
// First Signal in a While of Specific Provider ...
AddRef(
iSignal,
signals //
);
//
continue;
}
else
{
//
// not First Signal ...
//
// Calculate Provider Specified Open Positions Profit ...
double profits = CalculatePositionsProfit(positions);
if (profits > 0)
{
//
AddRef(
iSignal,
signals //
);
//
continue;
}
else
{
//
// Find Max In Profit of Same Position ...
Clean(positions);
positionsCount = mTrader.GetInProfitPositions(
positions,
iSignal.symbol,
iSignal.provider,
NULL, // All Periods ...
iSignal.type,
X_POSITION_SELECT_MAX // Max In Profit ...
);
if (positionsCount <= 0)
{
continue;
}
//
string comment = "EQM Close Max Profit ...";
bool isClosed = mTrader.Close(
positions[0].ticket,
comment //
);
if (isClosed)
{
//
RemoveTrail(positions[0].ticket);
//
string message = "EQM Close Max In Profit (" + ToString(positions[0].ticket) + ") for new Signal ...";
//
mAlert.Alert(message);
//
AddRef(
iSignal,
signals //
);
//
continue;
}
}
}
}
//
result = ArraySize(signals);
//
return result;
}
//
// Request for Support Signals using Guard ...
bool RequestForSupport(
@@ -487,6 +696,12 @@ public:
{
//
// TODO: Implement this ...
//
// Support Senario ...
// Check Support Position Exists or not ...
// Check Positions for Support based on Types ...
// Update Untriggered Positions ...
return false;
}
@@ -544,22 +759,39 @@ private:
// Trails Holding ...
//
XTrail mTrails[];
XTrail mSLTrails[];
XTrail mTPTrails[];
//
int CountTrails()
int CountSLTrails()
{
return ArraySize(mTrails);
return ArraySize(mSLTrails);
}
int CountTPTrails()
{
return ArraySize(mTPTrails);
}
//
bool RemoveTrail(ulong ticket)
{
//
bool isSLRemoved = RemoveSLTrail(ticket);
bool isTPRemoved = RemoveTPTrail(ticket);
//
bool result = isSLRemoved || isTPRemoved;
//
return result;
}
bool RemoveSLTrail(ulong ticket)
{
//
bool result = false;
//
int idx = FindTrailIndex(ticket);
int idx = FindSLTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
@@ -568,7 +800,30 @@ private:
//
result = ArrayRemove(
mTrails,
mSLTrails,
idx,
1 //
);
//
return result;
}
bool RemoveTPTrail(ulong ticket)
{
//
bool result = false;
//
int idx = FindTPTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
return result;
}
//
result = ArrayRemove(
mTPTrails,
idx,
1 //
);
@@ -578,13 +833,13 @@ private:
}
//
int FindTrailIndex(ulong ticket)
int FindSLTrailIndex(ulong ticket)
{
//
int result = -1;
//
int trailsCount = CountTrails();
int trailsCount = CountSLTrails();
if (ticket <= 0 || trailsCount <= 0)
{
return result;
@@ -594,7 +849,37 @@ private:
for (int i = 0; i < trailsCount; i++)
{
//
XTrail iTrail = mTrails[i];
XTrail iTrail = mSLTrails[i];
//
if (iTrail.ticket == ticket)
{
//
result = i;
break;
}
}
//
return result;
}
int FindTPTrailIndex(ulong ticket)
{
//
int result = -1;
//
int trailsCount = CountTPTrails();
if (ticket <= 0 || trailsCount <= 0)
{
return result;
}
//
for (int i = 0; i < trailsCount; i++)
{
//
XTrail iTrail = mTPTrails[i];
//
if (iTrail.ticket == ticket)
@@ -610,7 +895,7 @@ private:
}
//
bool GetTrail(
bool GetSLTrail(
ulong ticket,
XTrail &trail //
)
@@ -619,7 +904,7 @@ private:
bool result = false;
//
int idx = FindTrailIndex(ticket);
int idx = FindSLTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
@@ -627,14 +912,36 @@ private:
}
//
trail = mTrails[idx];
trail = mSLTrails[idx];
//
return result;
}
bool GetTPTrail(
ulong ticket,
XTrail &trail //
)
{
//
bool result = false;
//
int idx = FindTPTrailIndex(ticket);
result = idx >= 0;
if (!result)
{
return result;
}
//
trail = mTPTrails[idx];
//
return result;
}
//
void AddTrail(
void AddSLTrail(
XTrail &trail //
)
{
@@ -647,7 +954,23 @@ private:
//
AddRef(
trail,
mTrails //
mSLTrails //
);
}
void AddTPTrail(
XTrail &trail //
)
{
//
if (!trail.IsValid())
{
return;
}
//
AddRef(
trail,
mTPTrails //
);
}