3054 lines
70 KiB
Plaintext
3054 lines
70 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Library
|
|
// ---------------------------------------
|
|
// Name: XCATBLib
|
|
// Description: required Parsers for XCATB ...
|
|
//
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
//
|
|
// Global Properties ...
|
|
#property library
|
|
#property copyright "Copyright `023, SaherElm IT Center"
|
|
#property link "https://www.saherelm.ir"
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Classes/x-saherelm.x-bar.analyser.class.mq5"
|
|
#include "../Classes/x-saherelm.x-poi.drawer.class.mq5"
|
|
#include "../Helpers/x-saherelm.x121.xcatb.helper.mq5"
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
#include "../Libraries/x-saherelm.x-poi.lib.mq5"
|
|
|
|
//
|
|
// Definiions ...
|
|
|
|
//
|
|
#define XLiquidityToken "XLQ";
|
|
#define XTriggerBlockToken "XTRG";
|
|
|
|
//
|
|
struct XTriggerBlock
|
|
{
|
|
//
|
|
// Props ...
|
|
XBoxZone ob; // Order Block
|
|
XBoxZone fvg; // Fair Value Gap
|
|
XOHCL swingBar; // Swing of Trigger
|
|
XBoxZone trigger; // Trigger Block
|
|
XBoxZone rLiquidity; // Reversal Liquidity
|
|
XBoxZone fLiquidity; // Following Liquidity
|
|
|
|
//
|
|
XSignal signal; // Pepared Signal
|
|
|
|
//
|
|
// Constructor ...
|
|
XTriggerBlock()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Cleanup ...
|
|
void Clean()
|
|
{
|
|
//
|
|
ob.Clean();
|
|
fvg.Clean();
|
|
signal.Clean();
|
|
trigger.Clean();
|
|
swingBar.Clean();
|
|
rLiquidity.Clean();
|
|
fLiquidity.Clean();
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = ob.IsValid() &&
|
|
fvg.IsValid() &&
|
|
trigger.IsValid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool IsBullish()
|
|
{
|
|
return IsValid() &&
|
|
ob.IsBullish();
|
|
}
|
|
|
|
//
|
|
bool IsBearish()
|
|
{
|
|
return IsValid() &&
|
|
ob.IsBearish();
|
|
}
|
|
|
|
//
|
|
int ToIndex()
|
|
{
|
|
return trigger.ToIndex();
|
|
}
|
|
|
|
//
|
|
int FromIndex()
|
|
{
|
|
return trigger.FromIndex();
|
|
}
|
|
|
|
//
|
|
ENUM_X_DIRECTION GetDirection()
|
|
{
|
|
//
|
|
ENUM_X_DIRECTION result = X_DIRECTION_NONE;
|
|
|
|
//
|
|
if (IsValid())
|
|
{
|
|
result = ob.dir;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|
|
// Trigger Block Validation Rules ...
|
|
struct XTriggerBlockValidationRules
|
|
{
|
|
//
|
|
// Props ...
|
|
bool forceHasSwing; // Force Block to Has Propper Swing ...
|
|
bool forceOBBarType; // Force Order Block to Has InDirectional Bars ...
|
|
int minRequiredOBBar; // Min Required OB Bar ...
|
|
bool forceFVGBarType; // Force Fair Value Gaps Bars Type ...
|
|
double maxAllowedRange; // Max Allowed Order Block Range ...
|
|
double minAllowedRange; // Min Allowed Order Block Range ...
|
|
bool forceHasFLiquidity; // Force Order Block To Has Reversal Liquidity ...
|
|
bool forceHasRLiquidity; // Force Order Block to Has Following Liquidity ...
|
|
int liquidityLoopback; // Liquidity Detection Loopback ...
|
|
bool validateGapSequence; // Validate Fair Value Gaps Bar Sequences ...
|
|
bool validateBlockEdgeBreakout; // Validate Order Block Edge Breakeout ...
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
XTriggerBlockValidationRules()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Cleanup ...
|
|
void Clean()
|
|
{
|
|
//
|
|
maxAllowedRange = 0;
|
|
minAllowedRange = 0;
|
|
minRequiredOBBar = 0;
|
|
liquidityLoopback = 0;
|
|
forceHasSwing = false;
|
|
forceOBBarType = false;
|
|
forceFVGBarType = false;
|
|
forceHasFLiquidity = false;
|
|
forceHasRLiquidity = false;
|
|
validateGapSequence = false;
|
|
validateBlockEdgeBreakout = false;
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
};
|
|
|
|
//
|
|
// Trigger Block Filters ...
|
|
struct XTriggerBlockFilters
|
|
{
|
|
//
|
|
// Props ...
|
|
bool filterBasedOnPV; // Filter Based On PV ...
|
|
bool filterBasedOnSar; // Filter Based On SAR ...
|
|
bool filterBasedOnRSI; // Filter Based On RSI ...
|
|
bool filterBasedOnADX; // Filter Based On ADX ...
|
|
bool filterBasedOnATR; // Filter Based On ATR ...
|
|
bool filterBasedOnTrend; // Filter Based On Trend ...
|
|
bool filterBasedOnDelta; // Filter Based On Delta ...
|
|
bool filterBasedOnVolume; // Filter Based On Volume ...
|
|
bool filterBasedOnSignalBar; // Filter Based on Signal Bar ...
|
|
bool filterBasedOnHKSignalBar; // Filter Based On HK Signal Bar ...
|
|
|
|
//
|
|
int bosLoopback; // PV Based Filtering BOS Lookup ...
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
XTriggerBlockFilters()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Cleanup ...
|
|
void Clean()
|
|
{
|
|
//
|
|
filterBasedOnPV = false;
|
|
filterBasedOnSar = false;
|
|
filterBasedOnRSI = false;
|
|
filterBasedOnADX = false;
|
|
filterBasedOnATR = false;
|
|
filterBasedOnTrend = false;
|
|
filterBasedOnDelta = false;
|
|
filterBasedOnVolume = false;
|
|
filterBasedOnSignalBar = false;
|
|
filterBasedOnHKSignalBar = false;
|
|
|
|
//
|
|
bosLoopback = 0;
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
};
|
|
|
|
//
|
|
// Extensions ...
|
|
|
|
/**
|
|
* Detect Trigger Block ...
|
|
*
|
|
* @param helper: XCX121XCatbHelper instance pointer ...
|
|
* @param barAnalyser: XCBarAnalyser instance pointer ...
|
|
* @param triggerBlock: XTriggerBlock Refrence, holds Detected Trigger Block ...
|
|
* @param conditions: X121XCatbConditions Refrence, holds Trigger Block Conditions ...
|
|
* @param validations: XTriggerBlockValidationRules Refrence, Provides Trigger Block Validation Rules ...
|
|
* @param filters: XTriggerBlockFilters Refrence, Provides Trigger Block Filters ...
|
|
* @param barIndex: int, Bar Index ...
|
|
* @param loopback: int, Loopback for Conditions reading ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool DetectTriggerBlock1(
|
|
XCX121XCatbHelper *helper,
|
|
XCBarAnalyser *barAnalyser,
|
|
XTriggerBlock &triggerBlock,
|
|
X121XCatbConditions &conditions,
|
|
XTriggerBlockValidationRules &validations,
|
|
XTriggerBlockFilters &filters,
|
|
int barIndex = 0,
|
|
int loopback = 10 //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Normalize Args ...
|
|
conditions.Clean();
|
|
triggerBlock.Clean();
|
|
barIndex = NormalizeInt(barIndex, 0);
|
|
loopback = NormalizeInt(loopback, 0);
|
|
|
|
//
|
|
// Validate Args ...
|
|
result =
|
|
helper != NULL &&
|
|
barAnalyser != NULL;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int zIDX = 0;
|
|
int cIDX = zIDX + 1;
|
|
int pIDX = cIDX + 1;
|
|
int p2IDX = pIDX + 1;
|
|
int p3IDX = p2IDX + 1;
|
|
int p4IDX = p3IDX + 1;
|
|
|
|
//
|
|
bool has = false;
|
|
string symbol = helper.GetSymbol();
|
|
ENUM_TIMEFRAMES period = helper.GetPeriod();
|
|
|
|
//
|
|
// Initial Requirements ...
|
|
|
|
//
|
|
// Required Bars ...
|
|
XOHCL zBar;
|
|
XOHCL cBar;
|
|
result =
|
|
result &&
|
|
zBar.Init(
|
|
symbol,
|
|
period,
|
|
barIndex //
|
|
);
|
|
result =
|
|
result &&
|
|
zBar.GetPreviousBar(cBar);
|
|
|
|
//
|
|
// Here we Must Detect Order Block ...
|
|
// then Detect FVG ...
|
|
// then Prepare Trigger Box ...
|
|
|
|
//
|
|
// Select Application Bar ...
|
|
XOHCL iBar;
|
|
if (barIndex == 0)
|
|
{
|
|
iBar = cBar;
|
|
}
|
|
else if (barIndex > 0)
|
|
{
|
|
iBar = zBar;
|
|
}
|
|
iBar = cBar;
|
|
result =
|
|
result &&
|
|
iBar.IsValid();
|
|
|
|
//
|
|
// Detect Order Block ...
|
|
result =
|
|
result &&
|
|
barAnalyser
|
|
.IsOB(
|
|
iBar,
|
|
triggerBlock.ob,
|
|
false,
|
|
false //
|
|
);
|
|
|
|
//
|
|
// Detect Fair Value Gap ...
|
|
result =
|
|
result &&
|
|
barAnalyser
|
|
.IsFVG(
|
|
iBar,
|
|
triggerBlock.fvg,
|
|
false //
|
|
);
|
|
|
|
//
|
|
// Check Result and Create Trigger Box based on it ...
|
|
if (result)
|
|
{
|
|
//
|
|
triggerBlock.trigger = triggerBlock.ob;
|
|
triggerBlock.trigger.type = XTriggerBlockToken;
|
|
|
|
//
|
|
// Update Upper and Lower ...
|
|
int toIDX = triggerBlock.ob.ToIndex();
|
|
int fromIDX = triggerBlock.ob.FromIndex();
|
|
|
|
//
|
|
iBar.Clean();
|
|
result = iBar.Init(
|
|
symbol,
|
|
period,
|
|
toIDX //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
double iLL = iBar.FindLowest(fromIDX - toIDX, MODE_LOW);
|
|
double iHH = iBar.FindHighest(fromIDX - toIDX, MODE_HIGH);
|
|
|
|
//
|
|
triggerBlock.trigger.upper = MathMax(iHH, triggerBlock.trigger.upper);
|
|
triggerBlock.trigger.lower = MathMin(iLL, triggerBlock.trigger.lower);
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// TODO: Remove this ...
|
|
// Temparory Testing Configs ...
|
|
// validations.maxAllowedRange = 200;
|
|
// validations.forceHasSwing = true;
|
|
// validations.forceFVGBarType = true;
|
|
// validations.forceOBBarType = true;
|
|
// validations.validateGapSequence = true;
|
|
// validations.validateBlockEdgeBreakout = true;
|
|
// validations.liquidityLoopback = 144;
|
|
// validations.forceHasFLiquidity = true;
|
|
// validations.forceHasRLiquidity = true;
|
|
|
|
//
|
|
// Apply Validations ...
|
|
if (result)
|
|
{
|
|
//
|
|
result = ValidateTriggerBlock(
|
|
helper,
|
|
barAnalyser,
|
|
triggerBlock,
|
|
conditions,
|
|
validations,
|
|
barIndex,
|
|
loopback //
|
|
);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
triggerBlock.Clean();
|
|
}
|
|
}
|
|
|
|
//
|
|
// TODO: Remove this ...
|
|
filters.bosLoopback = 30;
|
|
filters.filterBasedOnPV = true;
|
|
|
|
//
|
|
// Apply Filtering ...
|
|
if (result)
|
|
{
|
|
//
|
|
result = FilterTriggerBlock(
|
|
helper,
|
|
barAnalyser,
|
|
triggerBlock,
|
|
conditions,
|
|
filters,
|
|
barIndex,
|
|
loopback //
|
|
);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
triggerBlock.Clean();
|
|
}
|
|
}
|
|
|
|
//
|
|
// Summarizing Result ...
|
|
result =
|
|
result &&
|
|
triggerBlock.IsValid();
|
|
|
|
//
|
|
// Preparing Signal ...
|
|
if (result)
|
|
{
|
|
//
|
|
}
|
|
|
|
//
|
|
// Cleanup Resources ...
|
|
|
|
//
|
|
iBar.Clean();
|
|
zBar.Clean();
|
|
cBar.Clean();
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
triggerBlock.Clean();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Validate a Trigger Block ...
|
|
*
|
|
* @param helper: XCX121XCatbHelper instance pointer ...
|
|
* @param barAnalyser: XCBarAnalyser instance pointer ...
|
|
* @param triggerBlock: XTriggerBlock Refrence, Trigger Block ...
|
|
* @param conditions: X121XCatbConditions Refrence, Trigger Block Conditions ...
|
|
* @param validations: XTriggerBlockValidationRules Refrence, Provides Trigger Block Validation Rules ...
|
|
* @param barIndex: int, Bar Index ...
|
|
* @param loopback: int, Loopback for Conditions reading ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool ValidateTriggerBlock(
|
|
XCX121XCatbHelper *helper,
|
|
XCBarAnalyser *barAnalyser,
|
|
XTriggerBlock &triggerBlock,
|
|
X121XCatbConditions &conditions,
|
|
XTriggerBlockValidationRules &validations,
|
|
int barIndex = 0,
|
|
int loopback = 10 //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Normalize Args ...
|
|
conditions.Clean();
|
|
barIndex = NormalizeInt(barIndex, 0);
|
|
loopback = NormalizeInt(loopback, 0);
|
|
|
|
//
|
|
// Validate Args ...
|
|
result =
|
|
helper != NULL &&
|
|
barAnalyser != NULL &&
|
|
triggerBlock.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Reading Market Conditions ...
|
|
result = helper.GetConditions(
|
|
conditions,
|
|
barIndex,
|
|
loopback //
|
|
);
|
|
if (!result)
|
|
{
|
|
//
|
|
conditions.Clean();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int zIDX = 0;
|
|
int cIDX = zIDX + 1;
|
|
int pIDX = cIDX + 1;
|
|
int p2IDX = pIDX + 1;
|
|
int p3IDX = p2IDX + 1;
|
|
int p4IDX = p3IDX + 1;
|
|
|
|
//
|
|
XOHCL iBar;
|
|
int count = 0;
|
|
int toIDX = -1;
|
|
int fromIDX = -1;
|
|
bool has = false;
|
|
bool isBullish = triggerBlock.IsBullish();
|
|
bool isBearish = triggerBlock.IsBearish();
|
|
double points = GetPoints(triggerBlock.ob.symbol);
|
|
ENUM_X_DIRECTION iDir = triggerBlock.GetDirection();
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Range Validation ...
|
|
|
|
//
|
|
// Max Allowed OB Range ...
|
|
if (result &&
|
|
validations.maxAllowedRange > 0)
|
|
{
|
|
//
|
|
double iRange = triggerBlock.ob.GetRange();
|
|
result = iRange <= (validations.maxAllowedRange * points);
|
|
}
|
|
|
|
//
|
|
// Min Allowed OB Range ...
|
|
if (result &&
|
|
validations.minAllowedRange > 0)
|
|
{
|
|
//
|
|
double iRange = triggerBlock.ob.GetRange();
|
|
result = iRange >= (validations.maxAllowedRange * points);
|
|
}
|
|
|
|
//
|
|
// Has Swing ...
|
|
if (result &&
|
|
validations.forceHasSwing)
|
|
{
|
|
//
|
|
bool isSwing = false;
|
|
toIDX = triggerBlock.ToIndex();
|
|
fromIDX = triggerBlock.FromIndex();
|
|
ENUM_X_DIRECTION swingDir = X_DIRECTION_NONE;
|
|
for (int i = toIDX; i <= fromIDX; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
has = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
i //
|
|
);
|
|
isSwing =
|
|
has &&
|
|
barAnalyser
|
|
.IsSimpleSwing(
|
|
iBar,
|
|
swingDir //
|
|
);
|
|
if (isSwing &&
|
|
swingDir == iDir)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
result =
|
|
isSwing &&
|
|
iBar.IsValid() &&
|
|
swingDir == iDir;
|
|
if (result)
|
|
{
|
|
//
|
|
// Initialize Swing Bar ...
|
|
result = triggerBlock
|
|
.swingBar
|
|
.Init(
|
|
iBar.symbol,
|
|
iBar.period,
|
|
iBar.Index() + 2 //
|
|
);
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Force FVG Bar Types ...
|
|
if (result &&
|
|
validations.forceFVGBarType)
|
|
{
|
|
//
|
|
toIDX = triggerBlock.fvg.ToIndex();
|
|
fromIDX = triggerBlock.fvg.FromIndex();
|
|
for (int i = toIDX; i < fromIDX; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
result = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
i //
|
|
);
|
|
result =
|
|
result &&
|
|
iDir == iBar.GetDirection();
|
|
|
|
//
|
|
iBar.Clean();
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Force OB Start By InDirectional Bars ...
|
|
if (result &&
|
|
validations.forceOBBarType)
|
|
{
|
|
//
|
|
count = 0;
|
|
fromIDX = triggerBlock.fvg.FromIndex() + 1;
|
|
int sIDX = fromIDX;
|
|
has = true;
|
|
while (has)
|
|
{
|
|
//
|
|
// Initialize iBar ..
|
|
has = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
fromIDX //
|
|
);
|
|
has = has &&
|
|
iDir == Opposit(iBar.GetDirection());
|
|
if (has)
|
|
{
|
|
count++;
|
|
}
|
|
|
|
//
|
|
fromIDX++;
|
|
}
|
|
|
|
//
|
|
int required =
|
|
validations.minRequiredOBBar > 0
|
|
? validations.minRequiredOBBar
|
|
: 1;
|
|
|
|
//
|
|
result = count >= required;
|
|
|
|
//
|
|
// Update Trigger Block Box based on OB Bar Types ...
|
|
if (result)
|
|
{
|
|
//
|
|
result = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
sIDX //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
datetime iFrom = GetBarTime(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
sIDX + count //
|
|
);
|
|
double iLL = iBar.FindLowest(count, MODE_LOW);
|
|
double iHH = iBar.FindHighest(count, MODE_HIGH);
|
|
|
|
//
|
|
triggerBlock.trigger.from = iFrom;
|
|
triggerBlock.trigger.upper = MathMax(iHH, triggerBlock.trigger.upper);
|
|
triggerBlock.trigger.lower = MathMin(iLL, triggerBlock.trigger.lower);
|
|
}
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Force Fair Value Gap to Following Sequence ...
|
|
if (result &&
|
|
validations.validateGapSequence)
|
|
{
|
|
//
|
|
XOHCL iPBar;
|
|
toIDX = triggerBlock.fvg.ToIndex();
|
|
fromIDX = triggerBlock.fvg.FromIndex() - 1;
|
|
for (int i = toIDX; i < fromIDX; i++)
|
|
{
|
|
//
|
|
result = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
i //
|
|
);
|
|
result =
|
|
result &&
|
|
iBar.GetPreviousBar(iPBar);
|
|
result =
|
|
result &&
|
|
(isBullish
|
|
? iBar.low > iPBar.low
|
|
: iBar.high < iPBar.high);
|
|
if (!result)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
iPBar.Clean();
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
iPBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Validate OB Edge Breakeout Using Gaps End Bar ...
|
|
if (result &&
|
|
validations.validateBlockEdgeBreakout)
|
|
{
|
|
//
|
|
// Initialize Gap End Bar ...
|
|
toIDX = triggerBlock.fvg.ToIndex();
|
|
result = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
toIDX //
|
|
);
|
|
|
|
//
|
|
result =
|
|
result &&
|
|
(isBullish
|
|
? iBar.open < triggerBlock.ob.upper &&
|
|
iBar.close > triggerBlock.ob.upper
|
|
: iBar.open > triggerBlock.ob.lower &&
|
|
iBar.close < triggerBlock.ob.lower);
|
|
|
|
//
|
|
// Here we Can Apply Edge Breakout Using Fibo ...
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Force Block to Has Liquidity ...
|
|
if (result &&
|
|
validations.liquidityLoopback > 0 &&
|
|
(validations.forceHasFLiquidity ||
|
|
validations.forceHasRLiquidity))
|
|
{
|
|
//
|
|
toIDX = triggerBlock.ToIndex();
|
|
fromIDX = triggerBlock.FromIndex();
|
|
ENUM_X_DIRECTION iLiqDir = X_DIRECTION_NONE;
|
|
for (int i = toIDX; i < fromIDX + validations.liquidityLoopback; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
has = iBar.Init(
|
|
conditions.symbol,
|
|
conditions.period,
|
|
i //
|
|
);
|
|
has =
|
|
has &&
|
|
barAnalyser
|
|
.IsRejected(
|
|
iBar,
|
|
iLiqDir,
|
|
false, // Force Bar Type ...
|
|
true // Force Fibo Pressure ...
|
|
);
|
|
|
|
//
|
|
bool isFLiq =
|
|
has &&
|
|
iLiqDir == iDir;
|
|
if (isFLiq &&
|
|
validations.forceHasFLiquidity &&
|
|
!triggerBlock.fLiquidity.IsValid())
|
|
{
|
|
//
|
|
FillLiquidity(
|
|
iBar,
|
|
iLiqDir,
|
|
triggerBlock.fLiquidity,
|
|
triggerBlock.trigger.to //
|
|
);
|
|
}
|
|
|
|
//
|
|
bool isRLiq =
|
|
has &&
|
|
iLiqDir == Opposit(iDir);
|
|
if (isRLiq &&
|
|
validations.forceHasRLiquidity &&
|
|
!triggerBlock.rLiquidity.IsValid())
|
|
{
|
|
//
|
|
FillLiquidity(
|
|
iBar,
|
|
iLiqDir,
|
|
triggerBlock.rLiquidity,
|
|
triggerBlock.trigger.to //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Here We Can Apply Liquidity Validations ...
|
|
ValidateTriggerBlockLiquidities(
|
|
helper,
|
|
barAnalyser,
|
|
triggerBlock,
|
|
conditions,
|
|
validations //
|
|
);
|
|
|
|
//
|
|
// Check Liquidities ...
|
|
has = (!validations.forceHasFLiquidity
|
|
? true
|
|
: triggerBlock.fLiquidity.IsValid()) &&
|
|
(!validations.forceHasRLiquidity
|
|
? true
|
|
: triggerBlock.rLiquidity.IsValid());
|
|
|
|
//
|
|
iBar.Clean();
|
|
|
|
//
|
|
if (has)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Check Result ...
|
|
result = (!validations.forceHasFLiquidity
|
|
? true
|
|
: triggerBlock.fLiquidity.IsValid()) &&
|
|
(!validations.forceHasRLiquidity
|
|
? true
|
|
: triggerBlock.rLiquidity.IsValid());
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
triggerBlock.Clean();
|
|
}
|
|
|
|
//
|
|
// Summarize result ...
|
|
result = triggerBlock.IsValid();
|
|
|
|
//
|
|
// Cleanup Resources ...
|
|
|
|
//
|
|
iBar.Clean();
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
//
|
|
conditions.Clean();
|
|
triggerBlock.Clean();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Filter a Trigger Block ...
|
|
*
|
|
* @param helper: XCX121XCatbHelper instance pointer ...
|
|
* @param barAnalyser: XCBarAnalyser instance pointer ...
|
|
* @param triggerBlock: XTriggerBlock Refrence, Trigger Block ...
|
|
* @param conditions: X121XCatbConditions Refrence, Trigger Block Conditions ...
|
|
* @param filters: XTriggerBlockFilters Refrence, Provides Trigger Block Filters ...
|
|
* @param barIndex: int, Bar Index ...
|
|
* @param loopback: int, Loopback for Conditions reading ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool FilterTriggerBlock(
|
|
XCX121XCatbHelper *helper,
|
|
XCBarAnalyser *barAnalyser,
|
|
XTriggerBlock &triggerBlock,
|
|
X121XCatbConditions &conditions,
|
|
XTriggerBlockFilters &filters,
|
|
int barIndex = 0,
|
|
int loopback = 10 //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Normalize Args ...
|
|
conditions.Clean();
|
|
barIndex = NormalizeInt(barIndex, 0);
|
|
loopback = NormalizeInt(loopback, 0);
|
|
|
|
//
|
|
// Validate Args ...
|
|
result =
|
|
helper != NULL &&
|
|
barAnalyser != NULL &&
|
|
triggerBlock.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Reading Market Conditions ...
|
|
result = helper.GetConditions(
|
|
conditions,
|
|
barIndex,
|
|
loopback //
|
|
);
|
|
if (!result)
|
|
{
|
|
//
|
|
conditions.Clean();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int zIDX = 0;
|
|
int cIDX = zIDX + 1;
|
|
int pIDX = cIDX + 1;
|
|
int p2IDX = pIDX + 1;
|
|
int p3IDX = p2IDX + 1;
|
|
int p4IDX = p3IDX + 1;
|
|
|
|
//
|
|
XOHCL iBar;
|
|
int toIDX = -1;
|
|
int fromIDX = -1;
|
|
bool has = false;
|
|
bool isBullish = triggerBlock.IsBullish();
|
|
bool isBearish = triggerBlock.IsBearish();
|
|
double points = GetPoints(triggerBlock.ob.symbol);
|
|
ENUM_X_DIRECTION iDir = triggerBlock.GetDirection();
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Filter Based On PV ...
|
|
if (result &&
|
|
filters.filterBasedOnPV)
|
|
{
|
|
//
|
|
// For PV Filtering ...
|
|
|
|
//
|
|
// Detect Bos ...
|
|
if (filters.bosLoopback > 0)
|
|
{
|
|
//
|
|
has = true;
|
|
bool isBOS = false;
|
|
bool isPeakOverLast = false;
|
|
bool isValeUnderLast = false;
|
|
toIDX = triggerBlock.ob.ToIndex();
|
|
fromIDX = triggerBlock.ob.FromIndex();
|
|
int sIDX = fromIDX;
|
|
while (has)
|
|
{
|
|
//
|
|
double iPeak = helper.GetPeak(sIDX);
|
|
double iPPeak = helper.GetPeak(sIDX + 1);
|
|
|
|
//
|
|
double iVale = helper.GetVale(sIDX);
|
|
double iPVale = helper.GetVale(sIDX + 1);
|
|
|
|
//
|
|
isPeakOverLast =
|
|
iPeak > 0 &&
|
|
iPeak > iPPeak;
|
|
|
|
//
|
|
isValeUnderLast =
|
|
iVale > 0 &&
|
|
iVale < iPVale;
|
|
|
|
//
|
|
isBOS =
|
|
(isBullish &&
|
|
isPeakOverLast) ||
|
|
(isBearish &&
|
|
isValeUnderLast);
|
|
if (isBOS)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
sIDX++;
|
|
has = !isBOS &&
|
|
sIDX < fromIDX + filters.bosLoopback;
|
|
}
|
|
|
|
//
|
|
result = isBOS;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
triggerBlock.Clean();
|
|
}
|
|
|
|
//
|
|
// Summarize result ...
|
|
result = triggerBlock.IsValid();
|
|
|
|
//
|
|
// Cleanup Resources ...
|
|
|
|
//
|
|
iBar.Clean();
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
//
|
|
conditions.Clean();
|
|
triggerBlock.Clean();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
void ValidateTriggerBlockLiquidities(
|
|
XCX121XCatbHelper *helper,
|
|
XCBarAnalyser *barAnalyser,
|
|
XTriggerBlock &triggerBlock,
|
|
X121XCatbConditions &conditions,
|
|
XTriggerBlockValidationRules &validations //
|
|
)
|
|
{
|
|
//
|
|
bool has = false;
|
|
|
|
//
|
|
has =
|
|
helper != NULL &&
|
|
barAnalyser != NULL &&
|
|
triggerBlock.IsValid() &&
|
|
(triggerBlock.fLiquidity.IsValid() ||
|
|
triggerBlock.rLiquidity.IsValid());
|
|
if (!has)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
bool hasFLiq = triggerBlock.fLiquidity.IsValid();
|
|
bool hasRLiq = triggerBlock.rLiquidity.IsValid();
|
|
|
|
//
|
|
// TODO: Implement Liquidity Validation ...
|
|
}
|
|
|
|
//
|
|
bool DetectSignalZone(
|
|
XCPOIDrawer *&drawer,
|
|
XCX121XCatbHelper *helper,
|
|
XCBarAnalyser *barAnalyser,
|
|
XBoxZone &signalZone,
|
|
X121XCatbConditions &conditions,
|
|
int barIndex = 0,
|
|
int loopback = 10 //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Normalize Args ...
|
|
signalZone.Clean();
|
|
conditions.Clean();
|
|
barIndex = NormalizeInt(barIndex, 0);
|
|
loopback = NormalizeInt(loopback, 0);
|
|
|
|
//
|
|
// Validate Args ...
|
|
result = helper != NULL &&
|
|
barAnalyser != NULL;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Reading Market Conditions ...
|
|
result = helper.GetConditions(
|
|
conditions,
|
|
barIndex,
|
|
loopback //
|
|
);
|
|
if (!result)
|
|
{
|
|
//
|
|
signalZone.Clean();
|
|
conditions.Clean();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int zIDX = 0;
|
|
int cIDX = zIDX + 1;
|
|
int pIDX = cIDX + 1;
|
|
int p2IDX = pIDX + 1;
|
|
int p3IDX = p2IDX + 1;
|
|
int p4IDX = p3IDX + 1;
|
|
|
|
//
|
|
double sl = 0;
|
|
double tp = 0;
|
|
double upper = 0;
|
|
double lower = 0;
|
|
datetime to = NULL;
|
|
datetime from = NULL;
|
|
string signalProvider = NULL;
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
|
|
//
|
|
// Detect Bar Verifications ...
|
|
|
|
//
|
|
ENUM_X_DIRECTION iDir;
|
|
|
|
//
|
|
// Rejected ...
|
|
bool isRejected =
|
|
barAnalyser
|
|
.IsRejected(
|
|
conditions.bars[cIDX],
|
|
iDir //
|
|
);
|
|
bool isBullishRejected =
|
|
isRejected &&
|
|
IsBullish(iDir);
|
|
bool isBearishRejected =
|
|
isRejected &&
|
|
IsBearish(iDir);
|
|
|
|
//
|
|
// Engulfed ...
|
|
bool isEngulfed =
|
|
barAnalyser
|
|
.IsEngulfed(
|
|
conditions.bars[cIDX],
|
|
iDir //
|
|
);
|
|
bool isBullishEngulfed =
|
|
isEngulfed &&
|
|
IsBullish(iDir);
|
|
bool isBearishEngulfed =
|
|
isEngulfed &&
|
|
IsBearish(iDir);
|
|
|
|
//
|
|
// Momentum ...
|
|
bool isMomentum =
|
|
barAnalyser
|
|
.IsMomentum(
|
|
conditions.bars[cIDX],
|
|
iDir //
|
|
);
|
|
bool isBullishMomentum =
|
|
isMomentum &&
|
|
IsBullish(iDir);
|
|
bool isBearishMomentum =
|
|
isMomentum &&
|
|
IsBearish(iDir);
|
|
|
|
//
|
|
bool isBarBullishPressured =
|
|
isBullishRejected ||
|
|
isBullishEngulfed ||
|
|
isBullishMomentum;
|
|
|
|
//
|
|
bool isBarBearishPressured =
|
|
isBearishRejected ||
|
|
isBearishEngulfed ||
|
|
isBearishMomentum;
|
|
|
|
//
|
|
// Detect Indicator Base Start Directional Movement ...
|
|
|
|
//
|
|
// SAR ...
|
|
|
|
//
|
|
bool isSarBullishStart =
|
|
//
|
|
(
|
|
//
|
|
// Current SAR is Directional ...
|
|
conditions.isSarBullish &&
|
|
//
|
|
// Previous InDirectional Sar Exists ...
|
|
helper.lastSarBearish.IsValid() &&
|
|
//
|
|
// Current Close is Over previous Sar ...
|
|
conditions.bars[cIDX].close > helper.lastSarBearish.after &&
|
|
//
|
|
// Before Close is not Over previous Sar ...
|
|
conditions.bars[pIDX].close <= helper.lastSarBearish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isSarBearishStart =
|
|
//
|
|
(
|
|
//
|
|
// Current SAR is Directional ...
|
|
conditions.isSarBearish &&
|
|
//
|
|
// Previous InDirectional Sar Exists ...
|
|
helper.lastSarBullish.IsValid() &&
|
|
//
|
|
// Current Close is Under previous ...
|
|
conditions.bars[cIDX].close < helper.lastSarBullish.after &&
|
|
//
|
|
// Before Close is not Under previous ...
|
|
conditions.bars[pIDX].close >= helper.lastSarBullish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
// TREND ...
|
|
|
|
//
|
|
bool isTrendBullishStart =
|
|
//
|
|
(
|
|
//
|
|
// Current Trend is Directional ...
|
|
conditions.isTrendBullish &&
|
|
//
|
|
// Previous InDirectiona; Trend Exists ...
|
|
helper.lastTrendBearish.IsValid() &&
|
|
//
|
|
// Current Close is Over previous ...
|
|
conditions.bars[cIDX].close > helper.lastTrendBearish.after &&
|
|
//
|
|
// Before close is not Over previous ...
|
|
conditions.bars[pIDX].close <= helper.lastTrendBearish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isTrendBearishStart =
|
|
//
|
|
(
|
|
//
|
|
// Current Trend is Directional ...
|
|
conditions.isTrendBearish &&
|
|
//
|
|
// Previous InDirectiona; Trend Exists ...
|
|
helper.lastTrendBullish.IsValid() &&
|
|
//
|
|
// Current Close is Under previous ...
|
|
conditions.bars[cIDX].close < helper.lastTrendBullish.after &&
|
|
//
|
|
// Before close is not Under previous ...
|
|
conditions.bars[pIDX].close >= helper.lastTrendBullish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
// ADX ...
|
|
|
|
//
|
|
bool isADXBullishStart =
|
|
//
|
|
(
|
|
//
|
|
// ADX Value is Switched to Strong ...
|
|
conditions.adxBuffer[cIDX] > conditions.adxThreshold &&
|
|
conditions.adxBuffer[pIDX] <= conditions.adxThreshold &&
|
|
//
|
|
// ADX must Up ...
|
|
conditions.adxBuffer[cIDX] > conditions.adxBuffer[pIDX] &&
|
|
//
|
|
// ADX +DI is Bigger than -DI ...
|
|
conditions.adxpBuffer[cIDX] > conditions.adxnBuffer[cIDX] &&
|
|
//
|
|
// ADX +DI is Up ...
|
|
conditions.adxpBuffer[cIDX] > conditions.adxpBuffer[pIDX]
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isADXBearishStart =
|
|
//
|
|
(
|
|
//
|
|
// ADX Value is Switched to Strong ...
|
|
conditions.adxBuffer[cIDX] > conditions.adxThreshold &&
|
|
conditions.adxBuffer[pIDX] <= conditions.adxThreshold &&
|
|
//
|
|
// ADX must Up ...
|
|
conditions.adxBuffer[cIDX] > conditions.adxBuffer[pIDX] &&
|
|
//
|
|
// ADX -DI is Bigger than +DI ...
|
|
conditions.adxnBuffer[cIDX] > conditions.adxpBuffer[cIDX] &&
|
|
//
|
|
// ADX -DI is Up ...
|
|
conditions.adxnBuffer[cIDX] > conditions.adxnBuffer[pIDX]
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
// DELTA ...
|
|
|
|
//
|
|
bool isDeltaBullishStart =
|
|
//
|
|
// Check State ...
|
|
(
|
|
//
|
|
// Delta is Up ...
|
|
conditions.deltaBuffer[cIDX] > conditions.deltaBuffer[pIDX] &&
|
|
//
|
|
// Delta Signal is Up ...
|
|
conditions.deltaSignalBuffer[cIDX] > conditions.deltaSignalBuffer[pIDX] &&
|
|
//
|
|
// Delta Crossed Over Delta Signal ...
|
|
conditions.deltaBuffer[cIDX] > conditions.deltaSignalBuffer[cIDX]
|
|
//
|
|
)
|
|
//
|
|
&&
|
|
//
|
|
// Check Last InDirectional Exists ...
|
|
helper.lastDeltaBearish.IsValid() &&
|
|
//
|
|
// Current Close is Over last ...
|
|
conditions.bars[cIDX].close > helper.lastDeltaBearish.after &&
|
|
//
|
|
// Before Close is not Over last ...
|
|
conditions.bars[pIDX].close <= helper.lastDeltaBearish.after
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isDeltaBearishStart =
|
|
//
|
|
// Check State ...
|
|
(
|
|
//
|
|
// Delta is Down ...
|
|
conditions.deltaBuffer[cIDX] < conditions.deltaBuffer[pIDX] &&
|
|
//
|
|
// Delta Signal is Down ...
|
|
conditions.deltaSignalBuffer[cIDX] < conditions.deltaSignalBuffer[pIDX] &&
|
|
//
|
|
// Delta Crossed Under Delta Signal ...
|
|
conditions.deltaBuffer[cIDX] < conditions.deltaSignalBuffer[cIDX]
|
|
//
|
|
)
|
|
//
|
|
&&
|
|
//
|
|
// Check Last InDirectional Exists ...
|
|
helper.lastDeltaBullish.IsValid() &&
|
|
//
|
|
// Current Close is Under last ...
|
|
conditions.bars[cIDX].close < helper.lastDeltaBullish.after &&
|
|
//
|
|
// Before Close is not Under last ...
|
|
conditions.bars[pIDX].close >= helper.lastDeltaBullish.after
|
|
//
|
|
;
|
|
|
|
//
|
|
// VOLUME ...
|
|
|
|
//
|
|
bool isVolumeBullishStart =
|
|
//
|
|
(
|
|
//
|
|
//
|
|
// Volume is Up ...
|
|
conditions.bullishVolumeBuffer[1] > conditions.bullishVolumeBuffer[2] &&
|
|
//
|
|
// Volume Signal is Up ...
|
|
conditions.bullishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[2] &&
|
|
//
|
|
// Volume Signals Crossed Over ...
|
|
conditions.bullishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[1]
|
|
//
|
|
)
|
|
//
|
|
&&
|
|
//
|
|
// Check Last InDirectional Exists ...
|
|
helper.lastVolumeBearish.IsValid() &&
|
|
//
|
|
// Current Close is Over last ...
|
|
conditions.bars[cIDX].close > helper.lastVolumeBearish.after &&
|
|
//
|
|
// Before Close is not Over last ...
|
|
conditions.bars[pIDX].close <= helper.lastVolumeBearish.after
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isVolumeBearishStart =
|
|
//
|
|
(
|
|
//
|
|
//
|
|
// Volume is Up ...
|
|
conditions.bearishVolumeBuffer[1] > conditions.bearishVolumeBuffer[2] &&
|
|
//
|
|
// Volume Signal is Up ...
|
|
conditions.bearishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[2] &&
|
|
//
|
|
// Volume Signals Crossed Over ...
|
|
conditions.bearishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[1]
|
|
//
|
|
)
|
|
//
|
|
&&
|
|
//
|
|
// Check Last InDirectional Exists ...
|
|
helper.lastVolumeBullish.IsValid() &&
|
|
//
|
|
// Current Close is Over last ...
|
|
conditions.bars[cIDX].close < helper.lastVolumeBullish.after &&
|
|
//
|
|
// Before Close is not Over last ...
|
|
conditions.bars[pIDX].close >= helper.lastVolumeBullish.after
|
|
//
|
|
;
|
|
|
|
//
|
|
// SIGNAL Bar ...
|
|
|
|
//
|
|
bool isSBarBullishStart =
|
|
//
|
|
(
|
|
//
|
|
// Check Direction ...
|
|
conditions.isSBarBullish &&
|
|
//
|
|
// Previous InDirectional Exists ...
|
|
helper.lastSignalBarBearish.IsValid() &&
|
|
//
|
|
// Current Signal Bar close over prev ...
|
|
conditions.sBarCloseBuffer[cIDX] > helper.lastSignalBarBearish.after &&
|
|
//
|
|
// Prev Signal Bar not close over prev ...
|
|
conditions.sBarCloseBuffer[pIDX] <= helper.lastSignalBarBearish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isSBarBearishStart =
|
|
//
|
|
(
|
|
//
|
|
// Check Direction ...
|
|
conditions.isSBarBearish &&
|
|
//
|
|
// Previous InDirectional Exists ...
|
|
helper.lastSignalBarBullish.IsValid() &&
|
|
//
|
|
// Current Signal Bar close over prev ...
|
|
conditions.sBarCloseBuffer[cIDX] < helper.lastSignalBarBullish.after &&
|
|
//
|
|
// Prev Signal Bar not close over prev ...
|
|
conditions.sBarCloseBuffer[pIDX] >= helper.lastSignalBarBullish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
// HKSIGNAL Bar ...
|
|
|
|
//
|
|
bool isHKSBarBullishStart =
|
|
//
|
|
(
|
|
//
|
|
// Check Direction ...
|
|
conditions.isHKSBarBullish &&
|
|
//
|
|
// Previous InDirectional Exists ...
|
|
helper.lastHKSignalBarBearish.IsValid() &&
|
|
//
|
|
// Current Signal Bar close over prev ...
|
|
conditions.hkSBarCloseBuffer[cIDX] > helper.lastHKSignalBarBearish.after &&
|
|
//
|
|
// Prev Signal Bar not close over prev ...
|
|
conditions.hkSBarCloseBuffer[pIDX] <= helper.lastHKSignalBarBearish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isHKSBarBearishStart =
|
|
//
|
|
(
|
|
//
|
|
// Check Direction ...
|
|
conditions.isHKSBarBearish &&
|
|
//
|
|
// Previous InDirectional Exists ...
|
|
helper.lastHKSignalBarBullish.IsValid() &&
|
|
//
|
|
// Current Signal Bar close over prev ...
|
|
conditions.hkSBarCloseBuffer[cIDX] < helper.lastHKSignalBarBullish.after &&
|
|
//
|
|
// Prev Signal Bar not close over prev ...
|
|
conditions.hkSBarCloseBuffer[pIDX] >= helper.lastHKSignalBarBullish.after
|
|
//
|
|
)
|
|
//
|
|
;
|
|
|
|
//
|
|
// Check for Vale and Peak ...
|
|
|
|
//
|
|
bool isPeakOverLast =
|
|
//
|
|
(conditions.peakBuffer[cIDX] > conditions.peakBuffer[pIDX] &&
|
|
conditions.peakBuffer[pIDX] <= conditions.peakBuffer[p2IDX] &&
|
|
conditions.bars[cIDX].close > conditions.peakBuffer[pIDX])
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isValeUnderLast =
|
|
//
|
|
(conditions.valeBuffer[cIDX] < conditions.valeBuffer[pIDX] &&
|
|
conditions.valeBuffer[pIDX] >= conditions.valeBuffer[p2IDX] &&
|
|
conditions.bars[cIDX].close < conditions.valeBuffer[pIDX])
|
|
//
|
|
;
|
|
|
|
//
|
|
// Start Different Conditions Implemntation ...
|
|
|
|
//
|
|
// Condition 1:
|
|
// ---------------------------
|
|
// Descriptions:
|
|
//
|
|
//
|
|
// ---------------------------
|
|
|
|
//
|
|
// Bullish ...
|
|
bool isCond1Bullish = false;
|
|
|
|
//
|
|
// Bearish ...
|
|
bool isCond1Bearish = false;
|
|
|
|
//
|
|
bool isCond1 =
|
|
isCond1Bullish ||
|
|
isCond1Bearish;
|
|
if (isCond1)
|
|
{
|
|
//
|
|
// Prepare Cond1 Based Requirements ...
|
|
|
|
//
|
|
to = NULL;
|
|
upper = 0;
|
|
lower = 0;
|
|
from = NULL;
|
|
|
|
//
|
|
// Provider Definition ...
|
|
signalProvider = "Cond1";
|
|
}
|
|
|
|
//
|
|
// Try to Summarize Conditions ...
|
|
|
|
//
|
|
bool isBullish =
|
|
//
|
|
isCond1Bullish
|
|
//
|
|
;
|
|
|
|
//
|
|
bool isBearish =
|
|
//
|
|
isCond1Bearish
|
|
//
|
|
;
|
|
|
|
//
|
|
// Summarize Result ...
|
|
result =
|
|
isBullish ||
|
|
isBearish;
|
|
if (result)
|
|
{
|
|
//
|
|
// Prepare Signal Zone ...
|
|
|
|
//
|
|
signalZone.to = to;
|
|
signalZone.from = from;
|
|
signalZone.upper = upper;
|
|
signalZone.lower = lower;
|
|
signalZone.type = signalProvider;
|
|
signalZone.symbol = conditions.symbol;
|
|
signalZone.period = conditions.period;
|
|
|
|
//
|
|
signalZone.dir =
|
|
isBullish
|
|
? X_DIRECTION_BULLISH
|
|
: X_DIRECTION_BEARISH;
|
|
|
|
//
|
|
// Validate Signal Zone ...
|
|
result = signalZone.IsValid();
|
|
}
|
|
|
|
//
|
|
// Cleanup Resources ...
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
//
|
|
signalZone.Clean();
|
|
conditions.Clean();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Detect Trigger Block ...
|
|
bool DetectTriggerBlock(
|
|
//
|
|
string symbol,
|
|
ENUM_TIMEFRAMES period,
|
|
//
|
|
XTriggerBlock &tb,
|
|
//
|
|
XCBarAnalyser *barAnalyser,
|
|
//
|
|
// Required Buffers ...
|
|
const double &_atrBuffer[],
|
|
const double &_rsiBuffer[],
|
|
const double &_adxBuffer[],
|
|
const double &_adxpBuffer[],
|
|
const double &_adxnBuffer[],
|
|
const double &_peakBuffer[],
|
|
const double &_valeBuffer[],
|
|
const double &_deltaBuffer[],
|
|
const double &_sarBuffer[],
|
|
const double &_sarStateBuffer[],
|
|
const double &_trendBuffer[],
|
|
const double &_trendStateBuffer[],
|
|
const double &_atrUpperBuffer[],
|
|
const double &_atrLowerBuffer[],
|
|
const double &_sBarOpenSBuffer[],
|
|
const double &_sBarCloseBuffer[],
|
|
const double &_hkSBarOpenBuffer[],
|
|
const double &_hkSBarCloseBuffer[],
|
|
const double &_peakGoldenBuffer[],
|
|
const double &_valeGoldenBuffer[],
|
|
const double &_deltaSignalBuffer[],
|
|
const double &_bullishVolumeSignalBuffer[],
|
|
const double &_bearishVolumeSignalBuffer[],
|
|
//
|
|
int barIndex,
|
|
//
|
|
int _r2r,
|
|
double _slAtrMultiplier,
|
|
//
|
|
// Configs ...
|
|
double _rsiOBLevel,
|
|
double _rsiOSLevel,
|
|
double _adxThreshold,
|
|
//
|
|
// Validators ...
|
|
//
|
|
bool _forceOBBarType = false,
|
|
bool _forceFVGBarType = false,
|
|
bool _forceHasSwing = false,
|
|
bool _forceHasFLiquidity = false,
|
|
bool _forceHasRLiquidity = false,
|
|
bool _validateGapSequence = false,
|
|
bool _validateBlockEdgeBreakout = false,
|
|
//
|
|
// Filters ...
|
|
bool _filterBasedOnPV = false,
|
|
bool _filterBasedOnSar = false,
|
|
bool _filterBasedOnRSI = false,
|
|
bool _filterBasedOnADX = false,
|
|
bool _filterBasedOnATR = false,
|
|
bool _filterBasedOnTrend = false,
|
|
bool _filterBasedOnDelta = false,
|
|
bool _filterBasedOnVolume = false,
|
|
bool _filterBasedOnSignalBar = false,
|
|
bool _filterBasedOnHKSignalBar = false //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Custom Configs ...
|
|
|
|
// //
|
|
// _r2r = 2;
|
|
// _slAtrMultiplier = 1;
|
|
|
|
// //
|
|
// // Validators ...
|
|
// _forceHasSwing = true;
|
|
// _forceOBBarType = true;
|
|
// _forceFVGBarType = true;
|
|
// _validateGapSequence = true;
|
|
|
|
// //
|
|
// _forceHasFLiquidity = false;
|
|
// _forceHasRLiquidity = false;
|
|
// _validateBlockEdgeBreakout = true;
|
|
|
|
// //
|
|
// // Filters ...
|
|
// _filterBasedOnPV = true;
|
|
// _filterBasedOnATR = true;
|
|
// _filterBasedOnDelta = true;
|
|
// _filterBasedOnVolume = true;
|
|
|
|
// //
|
|
// _filterBasedOnSar = true;
|
|
// _filterBasedOnRSI = false;
|
|
// _filterBasedOnADX = false;
|
|
// _filterBasedOnTrend = false;
|
|
// _filterBasedOnSignalBar = true;
|
|
// _filterBasedOnHKSignalBar = true;
|
|
|
|
//
|
|
tb.Clean();
|
|
|
|
//
|
|
result = barAnalyser != NULL;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int zIDX = 0;
|
|
bool has = false;
|
|
bool isDone = false;
|
|
ENUM_X_DIRECTION iDir = X_DIRECTION_NONE;
|
|
|
|
//
|
|
// Retrieve Required Bars ...
|
|
|
|
//
|
|
XOHCL iBar;
|
|
XOHCL zBar; // Current Start Checking Bar (Usually Used for Triggering) ...
|
|
has = zBar.Init(
|
|
symbol,
|
|
period,
|
|
barIndex //
|
|
);
|
|
|
|
//
|
|
// Detect Based Order Block ...
|
|
has =
|
|
has &&
|
|
barAnalyser.IsOB(
|
|
zBar,
|
|
tb.ob,
|
|
_forceFVGBarType, // Force FVG Bar Type ...
|
|
_forceOBBarType // Force Block Two Bar Checking ...
|
|
);
|
|
|
|
//
|
|
// Detect Based Fair Value Gap ...
|
|
has =
|
|
has &&
|
|
barAnalyser.IsFVG(
|
|
zBar,
|
|
tb.fvg,
|
|
_forceFVGBarType // Force FVG Bar Type ...
|
|
);
|
|
|
|
//
|
|
// Clone Trigger Block ...
|
|
if (has)
|
|
{
|
|
//
|
|
tb.trigger = tb.ob;
|
|
tb.trigger.type = "XTRGB";
|
|
}
|
|
|
|
//
|
|
bool isBullish =
|
|
has &&
|
|
tb.IsBullish();
|
|
|
|
//
|
|
bool isBearish =
|
|
has &&
|
|
tb.IsBearish();
|
|
|
|
//
|
|
int toIDX = tb.ToIndex();
|
|
int fromIDX = tb.FromIndex();
|
|
|
|
//
|
|
// Verifications and Filters ...
|
|
|
|
//
|
|
int loopback = 144;
|
|
|
|
//
|
|
// Forces ...
|
|
|
|
//
|
|
// Force Has Proper Swing ...
|
|
if (has &&
|
|
_forceHasSwing)
|
|
{
|
|
//
|
|
XOHCL iPBar;
|
|
XOHCL iP2Bar;
|
|
toIDX = tb.ToIndex();
|
|
fromIDX = tb.FromIndex();
|
|
for (int i = toIDX; i <= fromIDX; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
isDone = iBar.Init(
|
|
zBar.symbol,
|
|
zBar.period,
|
|
i //
|
|
);
|
|
isDone =
|
|
isDone &&
|
|
iBar.GetPreviousBar(iPBar);
|
|
isDone =
|
|
isDone &&
|
|
iPBar.GetPreviousBar(iP2Bar);
|
|
isDone =
|
|
isDone &&
|
|
barAnalyser.IsSimpleSwing(
|
|
iBar,
|
|
iDir //
|
|
) &&
|
|
iDir == tb.GetDirection();
|
|
if (isDone)
|
|
{
|
|
//
|
|
bool canSet =
|
|
!tb.swingBar.IsValid()
|
|
? true
|
|
: (isBullish
|
|
? tb.swingBar.low > iP2Bar.low
|
|
: tb.swingBar.high < iP2Bar.high);
|
|
if (canSet)
|
|
{
|
|
tb.swingBar = iP2Bar;
|
|
}
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
iPBar.Clean();
|
|
iP2Bar.Clean();
|
|
}
|
|
|
|
//
|
|
has = tb.swingBar.IsValid();
|
|
}
|
|
|
|
//
|
|
// Force Has Liquidity ...
|
|
// TODO: Add Validation based on Liquidities
|
|
// if necessary ...
|
|
if (has &&
|
|
(_forceHasFLiquidity ||
|
|
_forceHasRLiquidity))
|
|
{
|
|
//
|
|
toIDX = tb.ToIndex();
|
|
fromIDX = tb.FromIndex();
|
|
for (int i = toIDX; i < fromIDX + loopback; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
isDone = iBar.Init(
|
|
zBar.symbol,
|
|
zBar.period,
|
|
i //
|
|
);
|
|
|
|
//
|
|
bool isRejected =
|
|
isDone &&
|
|
barAnalyser.IsRejected(
|
|
iBar,
|
|
iDir,
|
|
false, // Force Bar Type ...
|
|
true // Force Fibo Pressure ...
|
|
);
|
|
|
|
//
|
|
// Following Liquidity ...
|
|
bool isFLiq =
|
|
isDone &&
|
|
isRejected &&
|
|
iDir == tb.GetDirection() &&
|
|
(isBullish
|
|
? iBar.low < tb.trigger.lower
|
|
: iBar.high > tb.trigger.upper);
|
|
if (isFLiq &&
|
|
_forceHasFLiquidity &&
|
|
!tb.fLiquidity.IsValid())
|
|
{
|
|
//
|
|
FillLiquidity(
|
|
iBar,
|
|
iDir,
|
|
tb.fLiquidity,
|
|
tb.trigger.to //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Reversal Liquidity ...
|
|
bool isRLiq =
|
|
isDone &&
|
|
isRejected &&
|
|
Opposit(iDir) == tb.GetDirection() &&
|
|
(isBullish
|
|
? iBar.low > tb.trigger.upper
|
|
: iBar.high < tb.trigger.lower);
|
|
if (isRLiq &&
|
|
_forceHasRLiquidity &&
|
|
!tb.rLiquidity.IsValid())
|
|
{
|
|
//
|
|
FillLiquidity(
|
|
iBar,
|
|
iDir,
|
|
tb.rLiquidity,
|
|
tb.trigger.to //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
isDone =
|
|
(!_forceHasFLiquidity
|
|
? true
|
|
: tb.fLiquidity.IsValid()) &&
|
|
(!_forceHasRLiquidity
|
|
? true
|
|
: tb.rLiquidity.IsValid());
|
|
if (isDone)
|
|
{
|
|
//
|
|
iBar.Clean();
|
|
break;
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Checking Conditions ...
|
|
has =
|
|
(!_forceHasFLiquidity
|
|
? true
|
|
: tb.fLiquidity.IsValid()) &&
|
|
(!_forceHasRLiquidity
|
|
? true
|
|
: tb.rLiquidity.IsValid());
|
|
}
|
|
|
|
//
|
|
// Validations ...
|
|
|
|
//
|
|
// Validating Gap Sequence means
|
|
// Gap Bars must Follow each Other ...
|
|
if (has &&
|
|
_validateGapSequence)
|
|
{
|
|
//
|
|
XOHCL iPBar;
|
|
toIDX = tb.fvg.ToIndex();
|
|
fromIDX = tb.fvg.FromIndex();
|
|
|
|
//
|
|
for (int i = toIDX; i < fromIDX - 1; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
has = iBar.Init(
|
|
zBar.symbol,
|
|
zBar.period,
|
|
i //
|
|
);
|
|
has =
|
|
has &&
|
|
iBar.GetPreviousBar(iPBar);
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? iBar.low > iPBar.low
|
|
: iBar.high < iPBar.high);
|
|
if (!has)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
iPBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Validate Block Edge Breakout ...
|
|
if (has &&
|
|
_validateBlockEdgeBreakout)
|
|
{
|
|
//
|
|
has = tb.fvg.ToBar(iBar);
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? (iBar.GetDown() < tb.ob.upper &&
|
|
iBar.GetUp() > tb.ob.upper)
|
|
: (iBar.GetDown() < tb.ob.lower &&
|
|
iBar.GetUp() > tb.ob.lower));
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Filters ...
|
|
|
|
//
|
|
// PV ...
|
|
if (has &&
|
|
_filterBasedOnPV)
|
|
{
|
|
//
|
|
double iPeak = _peakBuffer[barIndex];
|
|
double iPPeak = _peakBuffer[barIndex + 1];
|
|
double iP2Peak = _peakBuffer[barIndex + 2];
|
|
|
|
//
|
|
double iPeakGolden = _peakGoldenBuffer[barIndex];
|
|
double iPPeakGolden = _peakGoldenBuffer[barIndex + 1];
|
|
double iP2PeakGolden = _peakGoldenBuffer[barIndex + 2];
|
|
|
|
//
|
|
double iVale = _valeBuffer[barIndex];
|
|
double iPVale = _valeBuffer[barIndex + 1];
|
|
double iP2Vale = _valeBuffer[barIndex + 2];
|
|
|
|
//
|
|
double iValeGolden = _valeGoldenBuffer[barIndex];
|
|
double iPValeGolden = _valeGoldenBuffer[barIndex + 1];
|
|
double iP2ValeGolden = _valeGoldenBuffer[barIndex + 2];
|
|
|
|
//
|
|
// TODO: Fix this ...
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? (tb.trigger.lower <= iVale &&
|
|
tb.trigger.upper < iPeak
|
|
// || tb.trigger.lower <= iValeGolden
|
|
)
|
|
: (tb.trigger.upper >= iPeak &&
|
|
tb.trigger.lower > iVale
|
|
// || tb.trigger.upper >= iPeakGolden
|
|
));
|
|
}
|
|
|
|
//
|
|
// SAR ...
|
|
if (has &&
|
|
_filterBasedOnSar)
|
|
{
|
|
//
|
|
double iSarState = _sarStateBuffer[barIndex];
|
|
double iPSarState = _sarStateBuffer[barIndex + 1];
|
|
double iP2SarState = _sarStateBuffer[barIndex + 2];
|
|
|
|
//
|
|
bool isSarBullish = iSarState > 0;
|
|
bool isPSarBullish = iPSarState > 0;
|
|
bool isP2SarBullish = iP2SarState > 0;
|
|
|
|
//
|
|
bool isSarBearish = iSarState < 0;
|
|
bool isPSarBearish = iPSarState < 0;
|
|
bool isP2SarBearish = iP2SarState < 0;
|
|
|
|
//
|
|
bool isSarSwitchedToBullish =
|
|
isSarBullish &&
|
|
!isPSarBullish;
|
|
|
|
//
|
|
bool isSarSwitchedToBearish =
|
|
isSarBearish &&
|
|
!isPSarBearish;
|
|
|
|
//
|
|
bool isSarPSwitchedToBullish =
|
|
isPSarBullish &&
|
|
!isP2SarBullish;
|
|
|
|
//
|
|
bool isSarPSwitchedToBearish =
|
|
isPSarBearish &&
|
|
!isP2SarBearish;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? (isSarSwitchedToBullish ||
|
|
isSarPSwitchedToBullish)
|
|
: (isSarSwitchedToBearish ||
|
|
isSarPSwitchedToBearish));
|
|
}
|
|
|
|
//
|
|
// RSI ...
|
|
if (has &&
|
|
_filterBasedOnRSI)
|
|
{
|
|
//
|
|
double rsiTrend = (_rsiOBLevel + _rsiOSLevel) / 2;
|
|
|
|
//
|
|
double iRSI = _rsiBuffer[barIndex];
|
|
double iPRSI = _rsiBuffer[barIndex + 1];
|
|
double iP2RSI = _rsiBuffer[barIndex + 2];
|
|
|
|
//
|
|
bool isRSICrossedOverOS =
|
|
iRSI > _rsiOSLevel &&
|
|
iPRSI <= _rsiOSLevel;
|
|
|
|
//
|
|
bool isRSICrossedUnderOB =
|
|
iRSI < _rsiOBLevel &&
|
|
iPRSI >= _rsiOBLevel;
|
|
|
|
//
|
|
bool isPRSICrossedOverOS =
|
|
iPRSI > _rsiOSLevel &&
|
|
iP2RSI <= _rsiOSLevel;
|
|
|
|
//
|
|
bool isPRSICrossedUnderOB =
|
|
iPRSI < _rsiOBLevel &&
|
|
iP2RSI >= _rsiOBLevel;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? (isRSICrossedOverOS ||
|
|
isPRSICrossedOverOS)
|
|
: (isRSICrossedUnderOB ||
|
|
isPRSICrossedUnderOB));
|
|
}
|
|
|
|
//
|
|
// ADX ...
|
|
if (has &&
|
|
_filterBasedOnADX)
|
|
{
|
|
//
|
|
// ADX ...
|
|
double iADX = _adxBuffer[barIndex];
|
|
double iPADX = _adxBuffer[barIndex + 1];
|
|
double iP2ADX = _adxBuffer[barIndex + 2];
|
|
|
|
//
|
|
// ADX +DI ...
|
|
double iADXP = _adxpBuffer[barIndex];
|
|
double iPADXP = _adxpBuffer[barIndex + 1];
|
|
double iP2ADXP = _adxpBuffer[barIndex + 2];
|
|
|
|
//
|
|
// ADX -DI ...
|
|
double iADXN = _adxnBuffer[barIndex];
|
|
double iPADXN = _adxnBuffer[barIndex + 1];
|
|
double iP2ADXN = _adxnBuffer[barIndex + 2];
|
|
|
|
//
|
|
// Preparing Conditions ...
|
|
|
|
//
|
|
bool isADXBullish = iADX > _adxThreshold;
|
|
bool isADXBearish = iADX < _adxThreshold;
|
|
|
|
//
|
|
bool isPADXBullish = iPADX > _adxThreshold;
|
|
bool isPADXBearish = iPADX < _adxThreshold;
|
|
|
|
//
|
|
bool isP2ADXBullish = iP2ADX > _adxThreshold;
|
|
bool isP2ADXBearish = iP2ADX < _adxThreshold;
|
|
|
|
//
|
|
bool isADXSwitchedToBullish =
|
|
isADXBullish &&
|
|
!isPADXBullish;
|
|
|
|
//
|
|
bool isADXSwitchedToBearish =
|
|
isADXBearish &&
|
|
!isPADXBearish;
|
|
|
|
//
|
|
bool isPADXSwitchedToBullish =
|
|
isPADXBullish &&
|
|
!isP2ADXBullish;
|
|
|
|
//
|
|
bool isPADXSwitchedToBearish =
|
|
isPADXBearish &&
|
|
!isP2ADXBearish;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? (isADXSwitchedToBullish ||
|
|
isPADXSwitchedToBullish)
|
|
: (isADXSwitchedToBearish ||
|
|
isPADXSwitchedToBearish));
|
|
}
|
|
|
|
//
|
|
// ATR ...
|
|
if (has &&
|
|
_filterBasedOnATR)
|
|
{
|
|
//
|
|
fromIDX = tb.FromIndex();
|
|
double iATRUpper = _atrUpperBuffer[fromIDX];
|
|
double iATRLower = _atrLowerBuffer[fromIDX];
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? tb.trigger.lower < iATRLower
|
|
: tb.trigger.upper > iATRUpper);
|
|
}
|
|
|
|
//
|
|
// TREND ...
|
|
if (has &&
|
|
_filterBasedOnTrend)
|
|
{
|
|
//
|
|
double iTrendState = _trendStateBuffer[barIndex];
|
|
double iPTrendState = _trendStateBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isTrendBullish = iTrendState > 0;
|
|
bool isPTrendBullish = iPTrendState > 0;
|
|
|
|
//
|
|
bool isTrendBearish = iTrendState < 0;
|
|
bool isPTrendBearish = iPTrendState < 0;
|
|
|
|
//
|
|
double trends[];
|
|
int toIDX = tb.ToIndex();
|
|
int fromIDX = tb.FromIndex();
|
|
double iTrend = _trendBuffer[toIDX];
|
|
for (int i = toIDX; i <= fromIDX; i++)
|
|
{
|
|
//
|
|
Add(
|
|
_trendBuffer[i],
|
|
trends //
|
|
);
|
|
}
|
|
double trendsMin = GetMin(trends);
|
|
double trendsMax = GetMax(trends);
|
|
double trendsAVG = GetAverage(trends);
|
|
|
|
//
|
|
bool isTrendUp =
|
|
iTrend > trendsMin &&
|
|
iTrend >= trendsAVG;
|
|
|
|
//
|
|
bool isTrendDown =
|
|
iTrend < trendsMax &&
|
|
iTrend <= trendsAVG;
|
|
|
|
//
|
|
bool isTrendSwitchedToBullish =
|
|
isTrendUp &&
|
|
isTrendBullish &&
|
|
!isPTrendBullish;
|
|
|
|
//
|
|
bool isTrendSwitchedToBearish =
|
|
isTrendDown &&
|
|
isTrendBearish &&
|
|
!isPTrendBearish;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? isTrendSwitchedToBullish
|
|
: isTrendSwitchedToBearish);
|
|
}
|
|
|
|
//
|
|
// DELTA ...
|
|
if (has &&
|
|
_filterBasedOnDelta)
|
|
{
|
|
//
|
|
double iDelta = _deltaBuffer[barIndex];
|
|
double iPDelta = _deltaBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isDeltaUp = iDelta > iPDelta;
|
|
bool isDeltaDown = iDelta < iPDelta;
|
|
|
|
//
|
|
double iDeltaSignal = _deltaSignalBuffer[barIndex];
|
|
double iPDeltaSignal = _deltaSignalBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isDeltaSignalUp = iDeltaSignal > iPDeltaSignal;
|
|
bool isDeltaSignalDown = iDeltaSignal < iPDeltaSignal;
|
|
|
|
//
|
|
bool isDeltaSwitchedToBullish =
|
|
isDeltaUp &&
|
|
isDeltaSignalUp &&
|
|
iDelta > iDeltaSignal &&
|
|
iPDelta < iPDeltaSignal;
|
|
|
|
//
|
|
bool isDeltaSwitchedToBearish =
|
|
isDeltaDown &&
|
|
isDeltaSignalDown &&
|
|
iDelta < iDeltaSignal &&
|
|
iPDelta > iPDeltaSignal;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? isDeltaSwitchedToBullish
|
|
: isDeltaSwitchedToBearish);
|
|
}
|
|
|
|
//
|
|
// VOLUME ...
|
|
if (has &&
|
|
_filterBasedOnVolume)
|
|
{
|
|
//
|
|
double iBullishVolumeSignal = _bullishVolumeSignalBuffer[barIndex];
|
|
double iPBullishVolumeSignal = _bullishVolumeSignalBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isBullishVolumeSignalUp = iBullishVolumeSignal > iPBullishVolumeSignal;
|
|
bool isBullishVolumeSignalDown = iBullishVolumeSignal < iPBullishVolumeSignal;
|
|
|
|
//
|
|
double iBearishVolumeSignal = _bearishVolumeSignalBuffer[barIndex];
|
|
double iPBearishVolumeSignal = _bearishVolumeSignalBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isBearishVolumeSignalUp = iBearishVolumeSignal > iPBearishVolumeSignal;
|
|
bool isBearishVolumeSignalDown = iBearishVolumeSignal < iPBearishVolumeSignal;
|
|
|
|
//
|
|
bool isVolumeSwitchdToBullish =
|
|
isBullishVolumeSignalUp &&
|
|
!isBearishVolumeSignalUp &&
|
|
iBullishVolumeSignal > iBearishVolumeSignal &&
|
|
iPBullishVolumeSignal < iPBearishVolumeSignal;
|
|
|
|
//
|
|
bool isVolumeSwitchdToBearish =
|
|
isBearishVolumeSignalUp &&
|
|
!isBullishVolumeSignalUp &&
|
|
iBearishVolumeSignal > iBullishVolumeSignal &&
|
|
iPBearishVolumeSignal < iPBullishVolumeSignal;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? isVolumeSwitchdToBullish
|
|
: isVolumeSwitchdToBearish);
|
|
}
|
|
|
|
//
|
|
// SIGNAL Bar ...
|
|
if (has &&
|
|
_filterBasedOnSignalBar)
|
|
{
|
|
//
|
|
double iSignalOpen = _sBarOpenSBuffer[barIndex];
|
|
double iPSignalOpen = _sBarOpenSBuffer[barIndex + 1];
|
|
|
|
//
|
|
double iSignalClose = _sBarCloseBuffer[barIndex];
|
|
double iPSignalClose = _sBarCloseBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isSignalBarBullish = iSignalOpen < iSignalClose;
|
|
bool isPSignalBarBullish = iPSignalOpen < iPSignalClose;
|
|
|
|
//
|
|
bool isSignalBarBearish = iSignalOpen > iSignalClose;
|
|
bool isPSignalBarBearish = iPSignalOpen > iPSignalClose;
|
|
|
|
//
|
|
bool isSignalBarSwitchedToBullish =
|
|
isSignalBarBullish &&
|
|
!isPSignalBarBullish;
|
|
|
|
//
|
|
bool isSignalBarSwitchedToBearish =
|
|
isSignalBarBearish &&
|
|
!isPSignalBarBearish;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? isSignalBarSwitchedToBullish
|
|
: isSignalBarSwitchedToBearish);
|
|
}
|
|
|
|
//
|
|
// HK SIGNAL Bar ...
|
|
if (has &&
|
|
_filterBasedOnHKSignalBar)
|
|
{
|
|
//
|
|
double iHKSignalOpen = _hkSBarOpenBuffer[barIndex];
|
|
double iPHKSignalOpen = _hkSBarOpenBuffer[barIndex + 1];
|
|
|
|
//
|
|
double iHKSignalClose = _hkSBarCloseBuffer[barIndex];
|
|
double iPHKSignalClose = _hkSBarCloseBuffer[barIndex + 1];
|
|
|
|
//
|
|
bool isHKSignalBarBullish = iHKSignalOpen < iHKSignalClose;
|
|
bool isPHKSignalBarBullish = iPHKSignalOpen < iPHKSignalClose;
|
|
|
|
//
|
|
bool isHKSignalBarBearish = iHKSignalOpen > iHKSignalClose;
|
|
bool isPHKSignalBarBearish = iPHKSignalOpen > iPHKSignalClose;
|
|
|
|
//
|
|
bool isHKSignalBarSwitchedToBullish =
|
|
isHKSignalBarBullish &&
|
|
!isPHKSignalBarBullish;
|
|
|
|
//
|
|
bool isHKSignalBarSwitchedToBearish =
|
|
isHKSignalBarBearish &&
|
|
!isPHKSignalBarBearish;
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? isHKSignalBarSwitchedToBullish
|
|
: isHKSignalBarSwitchedToBearish);
|
|
}
|
|
|
|
//
|
|
// Valdating Directional Trending ...
|
|
bool _filterBasedOnDirection = false;
|
|
if (has &&
|
|
_filterBasedOnDirection)
|
|
{
|
|
//
|
|
iBar.Clean();
|
|
fromIDX = tb.FromIndex();
|
|
int loopbackForValidating = 30;
|
|
for (int i = fromIDX; i < fromIDX + loopbackForValidating; i++)
|
|
{
|
|
//
|
|
// Initialize iBar ...
|
|
iBar.Clean();
|
|
has = iBar.Init(
|
|
symbol,
|
|
period,
|
|
i //
|
|
);
|
|
|
|
//
|
|
has =
|
|
has &&
|
|
(isBullish
|
|
? iBar.high < tb.trigger.upper
|
|
: iBar.low > tb.trigger.lower);
|
|
if (!has)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Update Trigger Block ...
|
|
if (has)
|
|
{
|
|
tb.trigger.from = iBar.time;
|
|
}
|
|
|
|
//
|
|
iBar.Clean();
|
|
}
|
|
|
|
//
|
|
// Preparing Signal ...
|
|
if (has)
|
|
{
|
|
//
|
|
// CArrayObj *tbObjects = new CArrayObj();
|
|
// DrawTriggerBlock(
|
|
// tb,
|
|
// drawer,
|
|
// tbObjects,
|
|
// NULL //
|
|
// );
|
|
|
|
//
|
|
double targets[];
|
|
for (int i = 1; i <= _r2r; i++)
|
|
{
|
|
//
|
|
Add(
|
|
(double)i,
|
|
targets //
|
|
);
|
|
}
|
|
|
|
//
|
|
double iATR = 0;
|
|
if (barIndex > 0 &&
|
|
barIndex < ArraySize(_atrBuffer))
|
|
{
|
|
iATR = _atrBuffer[barIndex];
|
|
}
|
|
double slAdditional = _slAtrMultiplier * iATR;
|
|
|
|
//
|
|
has = ToSignal(
|
|
tb.trigger,
|
|
tb.signal,
|
|
targets,
|
|
slAdditional //
|
|
);
|
|
}
|
|
|
|
//
|
|
result = has;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Draw Specific Trigger Block ...
|
|
bool DrawTriggerBlock(
|
|
XTriggerBlock &trigger,
|
|
XCPOIDrawer *drawer,
|
|
CArrayObj &objects,
|
|
datetime to = NULL,
|
|
//
|
|
bool _drawSignals = false,
|
|
int rrZoneLength = 10,
|
|
// Styles ...
|
|
//
|
|
// Trigger Block ...
|
|
int tbWidth = 1, // Trigger Block Width
|
|
color tbBullishColor = clrAqua, // Trigger Block Bullish Color
|
|
color tbBearishColor = clrMagenta, // Trigger Block Bearish Color
|
|
ENUM_LINE_STYLE tbStyle = STYLE_DASHDOTDOT, // Trigger Block Style
|
|
//
|
|
// OB ...
|
|
int obWidth = 1, // OB Width
|
|
color obBullishColor = clrLime, // OB Bullish Color
|
|
color obBearishColor = clrRed, // OB Bearish Color
|
|
ENUM_LINE_STYLE obStyle = STYLE_DOT, // OB Style
|
|
//
|
|
// FVG ...
|
|
int fvgWidth = 2, // FVG Width
|
|
color fvgBullishColor = clrLime, // FVG Bullish Color
|
|
color fvgBearishColor = clrRed, // FVG Bearish Color
|
|
ENUM_LINE_STYLE fvgStyle = STYLE_SOLID // FVG Style
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Validate Args ...
|
|
result = trigger.IsValid() &&
|
|
drawer != NULL;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Update To ...
|
|
bool canUpdateTo = IsValid(to);
|
|
if (canUpdateTo)
|
|
{
|
|
//
|
|
trigger.trigger.to = to;
|
|
trigger.rLiquidity.to = to;
|
|
trigger.fLiquidity.to = to;
|
|
}
|
|
|
|
//
|
|
bool isBullish = trigger.IsBullish();
|
|
|
|
//
|
|
// Draw OB ...
|
|
if (trigger.ob.IsValid())
|
|
{
|
|
//
|
|
XCBoxObject *iOBObj;
|
|
result = drawer.DrawBox(
|
|
trigger.ob,
|
|
iOBObj //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
// Style Object ...
|
|
|
|
//
|
|
color iClr =
|
|
isBullish
|
|
? obBullishColor
|
|
: obBearishColor;
|
|
|
|
//
|
|
iOBObj.BoxColor(iClr);
|
|
iOBObj.BoxWidth(obWidth);
|
|
iOBObj.BoxStyle(obStyle);
|
|
|
|
//
|
|
// Store Object ...
|
|
objects.Add(iOBObj);
|
|
}
|
|
ZeroMemory(iOBObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw FVG ...
|
|
if (trigger.fvg.IsValid())
|
|
{
|
|
//
|
|
XCBoxObject *iFVGObj;
|
|
result = drawer.DrawBox(
|
|
trigger.fvg,
|
|
iFVGObj //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
// Style Object ...
|
|
|
|
//
|
|
color iClr =
|
|
isBullish
|
|
? fvgBullishColor
|
|
: fvgBearishColor;
|
|
|
|
//
|
|
iFVGObj.BoxColor(iClr);
|
|
iFVGObj.BoxWidth(fvgWidth);
|
|
iFVGObj.BoxStyle(fvgStyle);
|
|
|
|
//
|
|
// Store Object ...
|
|
objects.Add(iFVGObj);
|
|
}
|
|
ZeroMemory(iFVGObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw Trigger ...
|
|
if (trigger.trigger.IsValid())
|
|
{
|
|
//
|
|
XCBoxObject *iTriggerObj;
|
|
result = drawer.DrawBox(
|
|
trigger.trigger,
|
|
iTriggerObj //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
// Style Object ...
|
|
|
|
//
|
|
color iClr =
|
|
isBullish
|
|
? tbBullishColor
|
|
: tbBearishColor;
|
|
|
|
//
|
|
iTriggerObj.BoxColor(iClr);
|
|
iTriggerObj.BoxWidth(tbWidth);
|
|
iTriggerObj.BoxStyle(tbStyle);
|
|
|
|
//
|
|
// Store Object ...
|
|
objects.Add(iTriggerObj);
|
|
}
|
|
ZeroMemory(iTriggerObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw Liquidities ...
|
|
|
|
//
|
|
// Reversla Liquidity ...
|
|
if (trigger.rLiquidity.IsValid())
|
|
{
|
|
//
|
|
XCBoxObject *iLiqObj;
|
|
result = drawer.DrawBox(
|
|
trigger.rLiquidity,
|
|
iLiqObj //
|
|
);
|
|
if (result)
|
|
{
|
|
objects.Add(iLiqObj);
|
|
}
|
|
ZeroMemory(iLiqObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Following Liquidity ...
|
|
if (trigger.fLiquidity.IsValid())
|
|
{
|
|
//
|
|
XCBoxObject *iLiqObj;
|
|
result = drawer.DrawBox(
|
|
trigger.fLiquidity,
|
|
iLiqObj //
|
|
);
|
|
if (result)
|
|
{
|
|
objects.Add(iLiqObj);
|
|
}
|
|
ZeroMemory(iLiqObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw Swing Bar ...
|
|
if (trigger.swingBar.IsValid())
|
|
{
|
|
//
|
|
color swingColor =
|
|
trigger.fvg.IsBullish()
|
|
? clrAqua
|
|
: clrMagenta;
|
|
ENUM_X_PRICE swingPType =
|
|
trigger.fvg.IsBullish()
|
|
? X_PRICE_LOW
|
|
: X_PRICE_HIGH;
|
|
|
|
//
|
|
XCBarArrowObject *swingObj;
|
|
result = drawer.CreateBarArrow(
|
|
trigger.swingBar,
|
|
swingObj,
|
|
swingPType,
|
|
159,
|
|
swingColor //
|
|
);
|
|
if (result)
|
|
{
|
|
objects.Add(swingObj);
|
|
}
|
|
ZeroMemory(swingObj);
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw RR of Signal ...
|
|
if (_drawSignals &&
|
|
trigger.signal.IsValid() &&
|
|
rrZoneLength > 0)
|
|
{
|
|
//
|
|
XCRRObject *iRRObj;
|
|
iRRObj = new XCRRObject();
|
|
result = iRRObj.Create(
|
|
drawer.ChartIdentification(),
|
|
drawer.SubWindowIdentification(),
|
|
trigger.signal,
|
|
rrZoneLength //
|
|
);
|
|
if (result)
|
|
{
|
|
//
|
|
int iWidth = 2;
|
|
ENUM_LINE_STYLE iStyle = STYLE_SOLID;
|
|
|
|
//
|
|
iRRObj.TPWidth(iWidth);
|
|
iRRObj.SLWidth(iWidth);
|
|
iRRObj.EntryWidth(iWidth);
|
|
iRRObj.TargetWidth(iWidth);
|
|
|
|
//
|
|
iRRObj.TPStyle(iStyle);
|
|
iRRObj.SLStyle(iStyle);
|
|
iRRObj.EntryStyle(iStyle);
|
|
iRRObj.TargetStyle(iStyle);
|
|
|
|
//
|
|
iRRObj.TPColor(clrLime);
|
|
iRRObj.SLColor(clrRed);
|
|
iRRObj.EntryColor(clrYellow);
|
|
iRRObj.TargetColor(clrLightBlue);
|
|
|
|
//
|
|
objects.Add(iRRObj);
|
|
}
|
|
ZeroMemory(iRRObj);
|
|
|
|
//
|
|
if (result)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|