apply signaller's Policies in EA signal manager before Execute Signals ...

This commit is contained in:
2024-06-05 06:25:20 +03:30
parent 00631650dc
commit 6b05f48cde
8 changed files with 447 additions and 15 deletions
+230
View File
@@ -272,6 +272,236 @@ public:
return false;
}
//
// Here we Manage Signals for Executing ...
// - Check Policies based on their Signaller ...
// - Check Same Time Open Positions ...
// - Check Signal Age for new Trade ...
// and etc ...
int HandleSignalManagement(XSignal &signals[])
{
//
int result = 0;
//
int signalsCount = ArraySize(signals);
if (!IsValidSize(signalsCount))
{
return result;
}
//
XSignal tmpSignals[];
Copy(
signals,
tmpSignals //
);
//
Clean(signals);
//
for (int i = 0; i < signalsCount; i++)
{
//
XSignal iSignal = tmpSignals[i];
//
// Find Provider Descriptor Which Issued this Signal ...
int iProviderIDX = FindDescriptorIndex(
iSignal.symbol,
iSignal.period);
if (!IsValidIndex(iProviderIDX))
{
//
string message = "Couldn't find Signal Descriptor ...";
Log(message);
//
continue;
}
//
// Now we Have to Find Signaller Which Issued this Signal ...
int iSignallerIDX = mDescriptors[iProviderIDX]
.FindSignallerIndex(iSignal.provider);
if (!IsValidIndex(iSignallerIDX))
{
//
string message = "Couldn't find Signal Provider ...";
Log(message);
//
continue;
}
//
// Check Position Type is Enabled or not ...
bool isLong = IsLong(iSignal.type);
bool isAllowedType = isLong
? mDescriptors[iProviderIDX].signallers[iSignallerIDX].allowLong
: mDescriptors[iProviderIDX].signallers[iSignallerIDX].allowShort;
if (!isAllowedType)
{
//
string message = "ignore Signal due Type Policy ...";
Log(message);
//
continue;
}
//
// Retrieve Long and Short Signals of same Provider ...
XPosition longs[];
XPosition shorts[];
mTrader.GetPositions(
longs,
shorts,
iSignal.symbol,
iSignal.provider,
iSignal.period //
);
int longsCount = ArraySize(longs);
int shortsCount = ArraySize(shorts);
//
// Check Max Number of Positions ...
bool isMaxNumberOfPositionsPassed = isLong
? mDescriptors[iProviderIDX].signallers[iSignallerIDX].maxAllowedLong <= 0
? true
: longsCount < mDescriptors[iProviderIDX].signallers[iSignallerIDX].maxAllowedLong
: mDescriptors[iProviderIDX].signallers[iSignallerIDX].maxAllowedShort <= 0
? true
: shortsCount < mDescriptors[iProviderIDX].signallers[iSignallerIDX].maxAllowedShort;
if (!isMaxNumberOfPositionsPassed)
{
//
string message = "ignore Signal due Max Allowed Positions Reached ...";
Log(message);
//
continue;
}
//
// Check Delay Between Two Same Type Signals ...
// Check Open Next Behaviour ...
if (longsCount > 0 || shortsCount > 0)
{
//
// Check if Signaller Configured for Check Delay between Same type Signals ...
if (mDescriptors[iProviderIDX].signallers[iSignallerIDX].delayBetweenTwoSameTypeSignals > 0)
{
//
bool isDelayPassed = true;
//
if (longsCount > 0)
{
//
XPosition youngestLong;
int youngestLongAge = GetYoungest(
youngestLong,
longs //
);
//
if (isLong)
{
isDelayPassed = youngestLongAge >= mDescriptors[iProviderIDX].signallers[iSignallerIDX].delayBetweenTwoSameTypeSignals;
}
}
//
if (shortsCount > 0)
{
//
XPosition youngestShort;
int youngetsShortAge = GetYoungest(
youngestShort,
shorts //
);
//
if (!isLong)
{
isDelayPassed = youngetsShortAge >= mDescriptors[iProviderIDX].signallers[iSignallerIDX].delayBetweenTwoSameTypeSignals;
}
}
//
if (!isDelayPassed)
{
//
string message = "ignore Signal due Delay Between Same Type Policy ...";
Log(message);
//
continue;
}
}
//
// Check Next Position ...
if (mDescriptors[iProviderIDX].signallers[iSignallerIDX].openNextPositionOnProfit)
{
//
bool isNextPassed = true;
//
if (isLong && longsCount > 0)
{
//
double profit = SpecifiedCalculatePositionsProfit(longs);
isNextPassed = profit > 0;
}
//
if (!isLong && shortsCount > 0)
{
//
double profit = SpecifiedCalculatePositionsProfit(shorts);
isNextPassed = profit > 0;
}
//
// Check Ignore for Opposit Signals ...
if (!isNextPassed &&
((isLong && shortsCount > longsCount) ||
(!isLong && longsCount > shortsCount)))
{
isNextPassed = mDescriptors[iProviderIDX].signallers[iSignallerIDX].ignoreProfitForOppositeSignals;
}
//
if (!isNextPassed)
{
//
string message = "ignore Signal due Next Must be In Profit Policy ...";
Log(message);
//
continue;
}
}
}
//
// Finally Add Filtered Signals into Result ...
AddRef(
iSignal,
signals //
);
}
//
result = ArraySize(signals);
//
return result;
}
//
// Tools ...