Files
xMQL5/BKPS/14040218 XCAEA/Documents/BKP/signallers1/xcaea.signaller.class.mq5
T
2025-05-08 14:17:59 +03:30

1070 lines
26 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: XCXCAEASignaller ...
// Description: Class for XCAEA Signaller ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm Class for XCAEA Signaller"
#property strict
//
// Imports ...
#include "../../Classes/x-saherelm.base.class.mq5"
#include "../../Classes/x-saherelm.x-cobject.class.mq5"
#include "../Classes/xcaea.x-poi.detector.class.mq5"
#include "../Classes/xcaea.x-poi.drawer.class.mq5"
#include "../Helpers/xcaea.helper.mq5"
#include "../Libraries/xcaea.signaller.lib.mq5"
//
// Definitions ...
class XCXCAEASignaller : XCBase
{
//
// Public ...
public:
//
// Props ...
bool useGuards;
bool useTargets;
int poiLoopback;
XCAEAInputs inputs;
XBarTracker barTraker;
XCXCAEAHelper *helper;
bool rfOnInvalidPivots;
XCXCAEAPOIDrawer *drawer;
bool continuesRFTillTarget;
XBoxZone unAvailableBoxes[];
XCXCAEAPOIDetector *detector;
XCAEAStrategyConditions mConditions;
XCAEAStrategyConditions mConditionsCollection[];
//
// Constructor ...
XCXCAEASignaller(
string _symbol,
ENUM_TIMEFRAMES _period //
)
{
//
symbol = _symbol;
period = _period;
//
Init();
}
//
// Deconstructor ...
~XCXCAEASignaller()
{
DeInit();
}
//
// Properties ...
/**
* Initialize ...
*/
void Init()
{
//
useGuards = false;
useTargets = true;
rfOnInvalidPivots = false;
continuesRFTillTarget = false;
//
barTraker.Init(
symbol,
period //
);
//
inputs.Default();
//
// Configure Indicators ...
//
// XCC ...
inputs.ccInputs.showCandles = true;
//
// XCT ...
inputs.ctInputs.showCandleTime = true;
//
// XCA ...
//
inputs.caInputs.supResSmoothingLength = 7;
inputs.caInputs.supResSmoothingMode = X_MA_MODE_NONE;
//
inputs.x3maInputs.applyColor = true;
//
inputs.x3maInputs.fastLength = 5;
//
inputs.x3maInputs.showFast = true;
inputs.x3maInputs.showSlow = false;
inputs.x3maInputs.showMedium = false;
//
inputs.caInputs.showMAH = true;
inputs.caInputs.showMAL = true;
inputs.caInputs.showMAC = true;
inputs.caInputs.showSar = true;
inputs.caInputs.showSwings = true;
inputs.caInputs.showFiboLevel1 = true;
inputs.caInputs.showFiboLevel2 = true;
inputs.caInputs.showFiboLevel3 = true;
inputs.caInputs.showFiboLevel4 = true;
inputs.caInputs.showFiboLevel5 = true;
inputs.caInputs.showPeaksAndVales = true;
//
inputs.caInputs.showKI = false;
inputs.caInputs.showTKI = false;
inputs.caInputs.showVidya = false;
inputs.caInputs.showTrend = false;
inputs.caInputs.showPeakAndValeGolden = false;
inputs.caInputs.showSupportAndResistance = false;
//
// XOSCS ...
// inputs.oscsInputs.rsiLength = 5;
// inputs.oscsInputs.rsiOBLevel = 90;
// inputs.oscsInputs.rsiOSLevel = 10;
// inputs.oscsInputs.cciLength = 7;
// inputs.oscsInputs.cciOBLevel = 130;
// inputs.oscsInputs.cciOSLevel = -130;
// inputs.oscsInputs.mfiLength = 5;
// inputs.oscsInputs.rviLength = 5;
//
// Oscillators Views ...
inputs.showRSI = false;
inputs.showCCI = false;
inputs.showMFI = false;
inputs.showRVI = false;
inputs.showADX = false;
inputs.showMACD = false;
//
// XCAEA Helper ...
helper = new XCXCAEAHelper();
helper.Init(
symbol,
period,
inputs //
);
//
// Initialize and Configure Detector ...
detector = new XCXCAEAPOIDetector(helper);
//
// Common Configurations ...
detector.maxAllowedPivots = 0;
detector.pivotVerifications = 5;
detector.ticksDividerInPoints = 15;
detector.pivotDetectionLoopbac = 1000;
//
// Configure Detector ...
//
// PV ...
//
detector.maxAllowedPVLifetime = 0;
detector.applySwingsOnPVPivots = true;
detector.selectRejectedPVPivots = true;
detector.selectEngulfedPVPivots = true;
detector.applySupportAndResistanceOnPVPivots = true;
//
// PEAK ...
detector.detectPeakPivots = true;
//
// VALE ...
detector.detectValePivots = true;
//
// FVG ...
//
detector.selectSameBarFVGs = false;
detector.maxAllowedFVGLifetime = 0;
detector.validateFVGUsingPV = false;
detector.applyPVToFVGBoundary = false;
detector.selectRejectedFVGPivots = false;
detector.selectEngulfedFVGPivots = false;
detector.validateFVGUsingManalyser = true;
detector.applyLastBarToFVGBoundary = false;
detector.selectOnlyFVGWhichHasIndirectionalLastBar = true;
//
// Bullish ...
detector.detectFVGBullishPivots = true;
//
// Bearish ...
detector.detectFVGBearishPivots = true;
//
// TREND ...
//
detector.maxAllowedTrendLifetime = 0;
//
// Bullish ...
detector.detectTrendBullishPivots = true;
//
// Bearish ...
detector.detectTrendBearishPivots = true;
//
// DRAWER ...
//
// Initialize and Configure Drawer ...
drawer = new XCXCAEAPOIDrawer(detector);
//
drawer.ignoreUpdateOSCTos = true;
//
// Configure Drawer ...
drawer.drawPeakPivots = true;
drawer.drawValePivots = true;
drawer.drawFVGBullishPivots = true;
drawer.drawFVGBearishPivots = true;
drawer.drawTrendBullishPivots = true;
drawer.drawTrendBearishPivots = true;
//
// Configure Styles ...
//
// PV ...
//
// PEAK ...
XCAPivotStyle peakStyle;
peakStyle.width = 2;
peakStyle.clr = clrMagenta;
peakStyle.style = STYLE_SOLID;
drawer.peakPivotStyle = peakStyle;
peakStyle.Clean();
//
// VALE ...
XCAPivotStyle valeStyle;
valeStyle.width = 2;
valeStyle.clr = clrAqua;
valeStyle.style = STYLE_SOLID;
drawer.valePivotStyle = valeStyle;
valeStyle.Clean();
//
// FVG ...
//
// Bullish ...
XCAPivotStyle fvgBullishStyle;
fvgBullishStyle.width = 2;
fvgBullishStyle.clr = clrLime;
fvgBullishStyle.style = STYLE_DASH;
drawer.fvgBullishPivotStyle = fvgBullishStyle;
fvgBullishStyle.Clean();
//
// Bearish ...
XCAPivotStyle fvgBearishStyle;
fvgBearishStyle.width = 2;
fvgBearishStyle.clr = clrRed;
fvgBearishStyle.style = STYLE_DASH;
drawer.fvgBearishPivotStyle = fvgBearishStyle;
fvgBearishStyle.Clean();
//
// TREND ...
//
// Bullish ...
XCAPivotStyle trendBullishStyle;
trendBullishStyle.width = 2;
trendBullishStyle.clr = clrLime;
trendBullishStyle.style = STYLE_DOT;
drawer.trendBullishPivotStyle = trendBullishStyle;
trendBullishStyle.Clean();
//
// Bearish ...
XCAPivotStyle trendBearishStyle;
trendBearishStyle.width = 2;
trendBearishStyle.clr = clrRed;
trendBearishStyle.style = STYLE_DOT;
drawer.trendBearishPivotStyle = trendBearishStyle;
trendBearishStyle.Clean();
//
}
/**
* DeInitialize ...
*/
void DeInit()
{
//
inputs.Clean();
//
// Cleaning Draws ...
drawer.Clear();
//
ZeroMemory(drawer);
ZeroMemory(helper);
ZeroMemory(detector);
//
mConditions.Clean();
Clean(mConditionsCollection);
//
mObjects.Clear();
mBarObjects.Clear();
//
barTraker.Clean();
}
//
string GetSymbol()
{
return symbol;
}
//
ENUM_TIMEFRAMES GetPeriod()
{
return period;
}
//
// Signaller Tools ...
/**
* Detect Setup Conditions based on Signalling ...
*
* @param maxAllowedSLDistanceInPoint: int, Max Allowed SL Distance in Points (if Bigger thn Zero Provided) ...
*
* @return ( bool )
*/
bool DetectSignalSetup(
double tpReward = 3,
double maxAllowedSLDistanceInPoint = 100 //
)
{
//
bool result = false;
//
// Check Bar Processing ...
if (!barTraker.CanProcessBar())
{
return result;
}
//
bool isBullish = false;
bool isBearish = false;
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
int loopback = 10;
//
int idx = -1;
int count = 0;
bool has = false;
//
double ask = GetAsk(symbol);
double bid = GetBid(symbol);
datetime cTime = TimeCurrent();
double points = GetPoints(symbol);
//
XOHCL zBar;
XOHCL cBar;
XOHCL pBar;
XOHCL pPBar;
result = zBar.Init(
symbol,
period,
zIndex //
);
result =
result &&
zBar.GetPreviousBar(cBar);
result =
result &&
cBar.GetPreviousBar(pBar);
result =
result &&
pBar.GetPreviousBar(pPBar);
if (!result)
{
//
zBar.Clean();
cBar.Clean();
pBar.Clean();
pPBar.Clean();
//
return result;
}
//
// Setup Conditions ...
double targets[];
XBoxZone pivots[];
XBoxZone pivotBox;
XBoxZone signalBox;
int pivotsCount = 0;
bool hasTrend = false;
bool hasPivots = false;
double additionalSL = 0;
bool hasSignalBox = false;
XCAEAConditions conditions;
ENUM_X_DIRECTION trend = X_DIRECTION_NONE;
ENUM_X_DIRECTION signalBoxDir = X_DIRECTION_NONE;
//
// Detecting Pivots ...
// detector.Detect(
// zIndex //
// );
//
// Validate Pivots ...
// detector.Validate(
// unAvailableBoxes,
// zIndex //
// );
//
// if (detector.pivotDetectionLoopbac > 100)
// {
// detector.pivotDetectionLoopbac = 100;
// }
//
// Draw Founded Pivots ...
// drawer.Draw(cTime);
//
// drawer.DrawPeakPivots(cTime);
// drawer.DrawValePivots(cTime);
// drawer.DrawFVGBullishPivots(cTime);
// drawer.DrawFVGBearishPivots(cTime);
// drawer.DrawTrendBullishPivots(cTime);
// drawer.DrawTrendBearishPivots(cTime);
//
// Retrieve Valid Pviots ...
// pivotsCount = detector.FillPivots(pivots);
// hasPivots = IsValidSize(pivotsCount);
//
// Filter UnAvailable Boxes ...
// if (hasPivots)
// {
// //
// // Remove UnAvailable Boxes ...
// has = HasChild(unAvailableBoxes);
// if (has)
// {
// //
// count = ArraySize(unAvailableBoxes);
// for (int i = 0; i < count; i++)
// {
// //
// XBoxZone iBox = unAvailableBoxes[i];
// //
// has = FindBoxIndex(
// idx,
// iBox,
// pivots //
// );
// if (has)
// {
// //
// ArrayRemove(
// pivots,
// idx,
// 1 //
// );
// }
// }
// }
// //
// hasPivots = HasChild(pivots);
// pivotsCount = ArraySize(pivots);
// }
//
//
//
//
TestDetectSignal(
helper,
drawer,
signalBox,
signalBoxDir,
conditions,
zIndex,
loopback //
);
// //
// // Detect Sinal Box ...
// hasSignalBox = DetectXCAEAConditions(
// drawer,
// trend,
// pivots,
// conditions,
// pivotBox,
// signalBox,
// targets,
// additionalSL,
// signalBoxDir,
// helper,
// zIndex,
// loopback //
// );
//
// Make Pivot Box UnAvailable ...
if (pivotBox.IsValid())
{
//
AddRef(
pivotBox,
unAvailableBoxes //
);
}
//
// Make Signal Box UnAvailable ...
if (signalBox.IsValid())
{
//
AddRef(
signalBox,
unAvailableBoxes //
);
}
//
isBullish = hasSignalBox &&
IsBullish(signalBoxDir);
//
isBearish = hasSignalBox &&
IsBearish(signalBoxDir);
//
result = isBullish ||
isBearish;
if (result)
{
//
// Do What we Want ...
barTraker.Waits();
//
// Draw Signal Box ...
XCBoxObject *iObj;
bool isCreated = helper.poiDrawer.DrawBox(
signalBox,
iObj //
);
if (isCreated)
{
//
mObjects.Add(iObj);
ZeroMemory(iObj);
//
// Print("Signal Box ...");
}
//
// Prepare Conditions ...
mConditions.symbol = symbol;
mConditions.period = period;
//
double spread = GetSpread(symbol);
//
double entry =
isBullish
? signalBox.upper
: signalBox.lower;
//
double sl =
isBullish
? signalBox.lower
: signalBox.upper;
if (additionalSL > 0)
{
//
sl =
isBullish
? sl - additionalSL
: sl + additionalSL;
}
//
double risk =
isBullish
? entry - sl
: sl - entry;
risk += spread;
if (risk >= maxAllowedSLDistanceInPoint * points)
{
//
// Fix Risk in Big Distances ...
double hh5 = zBar.FindHighest(5, MODE_HIGH);
double ll5 = zBar.FindLowest(5, MODE_LOW);
//
double hhll5Delta = hh5 - ll5;
double riskDelta = hhll5Delta / 2;
risk = riskDelta;
}
//
double rewardTP = risk * tpReward;
//
double tp =
isBullish
? entry + rewardTP
: entry - rewardTP;
//
// Filling ...
mConditions.sl = sl;
mConditions.tp = tp;
mConditions.pivot =
isBullish
? sl
: entry;
mConditions.point =
isBullish
? entry
: sl;
mConditions.dir = signalBoxDir;
mConditions.provider = "XCAEA";
mConditions.signalBox = signalBox;
mConditions.setupTime = zBar.time;
mConditions.conditions = conditions;
//
// Targets Preparing ...
if (useTargets)
{
//
Copy(
targets,
mConditions.targets //
);
//
// Select RF Rewards ...
if (continuesRFTillTarget)
{
//
double rfReward = 1;
while (rfReward < tpReward)
{
//
double rewardRF = risk * rfReward;
double rfTP =
isBullish
? entry + rewardRF
: entry - rewardRF;
//
Add(
rfTP,
mConditions.targets //
);
//
rfReward++;
}
}
else
{
//
double rfReward = 2;
//
double rewardRF = risk * rfReward;
double rfTP =
isBullish
? entry + rewardRF
: entry - rewardRF;
//
Add(
rfTP,
mConditions.targets //
);
}
//
// Detect Rf on Invalid Pivots ...
if (rfOnInvalidPivots)
{
//
has = detector.HasInvalidPivots();
if (has)
{
//
XBoxZone targetBox;
double targetBoxPrice = 0;
count = detector.CountInvalidPivots();
for (int i = 0; i < count; i++)
{
//
XBoxZone iPivot = detector.invalidPivots[i];
//
bool isValidPivot =
Contains(
"XCA_PEAKSWHRES",
iPivot.type //
) ||
Contains(
"XCA_VALESWLSUP",
iPivot.type //
) ||
Contains(
"XCA_FVG",
iPivot.type //
);
if (!isValidPivot)
{
//
iPivot.Clean();
continue;
}
//
double selectedPrice =
isBullish
? iPivot.lower - (5 * points)
: iPivot.upper + (5 * points);
//
double targetBoxSelectedPrice =
isBullish
? targetBox.lower - (5 * points)
: targetBox.upper + (5 * points);
//
bool isDirPassed = iPivot.dir = signalBoxDir;
bool isPricePassed =
isBullish
? entry < selectedPrice
: entry > selectedPrice;
bool canSetTarget =
!targetBox.IsValid()
? true
: isBullish
? targetBoxSelectedPrice > selectedPrice
: targetBoxSelectedPrice < selectedPrice;
if (canSetTarget)
{
//
targetBox = iPivot;
targetBoxPrice = targetBoxSelectedPrice;
}
//
iPivot.Clean();
}
//
if (targetBox.IsValid() &&
targetBoxPrice > 0)
{
//
Add(
targetBoxPrice,
mConditions.targets //
);
}
//
targetBox.Clean();
}
}
}
}
//
// Cleanup Resource ...
//
Clean(pivots);
//
zBar.Clean();
cBar.Clean();
pBar.Clean();
pPBar.Clean();
//
pivotBox.Clean();
signalBox.Clean();
conditions.Clean();
//
return result;
}
/**
* Detect Trigger Conditions for Specified Signal ...
*
* @param conditions: XCAEAStrategyConditions instance Reference, Specified Signal Conditions ...
* @param maxAllowedSetupAge: specified Setup Age for Expiration (if bigger than Zero) ...
*
* @return ( bool )
*/
bool DetectSignalTrigger(
XCAEAStrategyConditions &conditions,
int maxAllowedSetupAge = 60 //
)
{
//
bool result = false;
//
result = conditions.IsSetuped();
if (!result)
{
return result;
}
//
datetime setupTime = conditions.setupTime;
ENUM_X_DIRECTION setupDir = conditions.dir;
//
bool isBullish = IsBullish(setupDir);
bool isBearish = IsBearish(setupDir);
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
int loopback = 10;
//
double ask = GetAsk(symbol);
double bid = GetBid(symbol);
datetime cTime = TimeCurrent();
double points = GetPoints(symbol);
//
// Check Signal Validation ...
// IMPORTANT: since all Validations done in SetUp Process ...
// here we only Execute Signal ...
//
result = isBullish ||
isBearish;
//
if (result)
{
//
conditions.triggerTime = cTime;
conditions.type = isBullish
? X_POSITION_TYPE_LONG
: X_POSITION_TYPE_SHORT;
//
}
//
// Cleanup ...
//
return result;
}
/**
* Detect Guards bsed on Signalling Class ...
*
* @param guards: XCAEAGuard instance Collection, Holds Provided Guards ...
* @param positions: XPosition instance Collection, Provides Signalling Based Positions for Apply Guards on them ...
* @param provider: X121SMCStrategySignalProviderData instance Reference, Specified Data for Cycles ...
*
* @return ( bool )
*/
bool DetectGuards(
XCAEAGuard &guards[],
const XPosition &positions[] //
)
{
//
bool result = false;
//
return result;
}
/**
* Add Conditions to Conditions Collection if not Exists ...
*
* @param maxAllowed: int, Specified Max Allowed Holding Conditions ...
*
* @return ( int )
*/
int AddConditionsIfNotExists(
int maxAllowed = 10 //
)
{
//
int result = 0;
//
bool isSetuped = IsValid(mConditions.setupTime) &&
HasDirection(mConditions.dir);
if (!isSetuped)
{
return result;
}
//
int count = ArraySize(mConditionsCollection);
if (IsValidSize(count))
{
//
// Copy Original Items ...
XCAEAStrategyConditions tmpItems[];
Copy(
mConditionsCollection,
tmpItems //
);
//
// Prese Item Data ...
bool isBullish = IsBullish(mConditions.dir);
//
for (int i = 0; i < count; i++)
{
//
XCAEAStrategyConditions iItem = tmpItems[i];
//
bool canRemove = mConditions.dir == iItem.dir &&
mConditions.pivot == iItem.pivot;
if (canRemove)
{
//
ArrayRemove(
mConditionsCollection,
i,
1 //
);
}
}
//
Clean(tmpItems);
}
//
AddRef(
mConditions,
mConditionsCollection //
);
//
// Remove Olds ...
CleanupArray(
mConditionsCollection,
maxAllowed //
);
//
result = ArraySize(mConditionsCollection);
//
return result;
}
//
// Protected ...
protected:
//
//
// Private ...
private:
//
// Props ...
string symbol;
ENUM_TIMEFRAMES period;
CArrayObj mObjects;
CArrayObj mBarObjects;
//
};
//