last works on adding threshold to pivots ...

This commit is contained in:
2024-07-11 08:31:23 +03:30
parent 9ab980be84
commit 827697dae7
3 changed files with 1401 additions and 1077 deletions
+562
View File
@@ -0,0 +1,562 @@
//
// Detect Trends ...
struct XTrendDetector
{
//
string symbol;
ENUM_TIMEFRAMES period;
//
double higherHighs[];
datetime higherHighTimes[];
//
double higherLows[];
datetime higherLowTimes[];
//
double lowerHighs[];
datetime lowerHighTimes[];
//
double lowerLows[];
datetime lowerLowTimes[];
//
bool isFirstLow;
//
bool isUpTrend;
bool isDownTrend;
//
int _numberOfPivots;
int _pivotShoulders;
//
// Constructor ...
XTrendDetector()
{
Clean();
}
//
bool FindTrend(
string _symbol,
ENUM_TIMEFRAMES _period,
int barIndex = 0,
int _requiredPivots = 3,
int _requiredShoulders = 3 //
)
{
//
bool result = false;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
if (_requiredPivots < 3)
{
_requiredPivots = 3;
}
//
if (_requiredShoulders < 3)
{
_requiredShoulders = 3;
}
//
result =
//
IsValid(_symbol) &&
IsValid(_period)
//
;
if (!result)
{
return result;
}
//
// Find Swing Lows and Swing Highs ...
//
int hhIndexes[];
datetime hhTimes[];
double hhs[];
//
int llIndexes[];
datetime llTimes[];
double lls[];
//
bool isEnough = false;
//
double lastHH = 0;
int lastHHIndex = barIndex;
datetime lastHHTime = NULL;
//
double lastLL = 0;
int lastLLIndex = barIndex;
datetime lastLLTime = NULL;
//
// Calculate First ...
lastHHIndex = FindSwing(
_symbol,
_period,
X_SWING_HIGH,
_requiredShoulders,
lastHHIndex + 1 //
);
if (lastHHIndex >= 0)
{
//
XOHCL lastBar;
bool isInited = lastBar.Init(
_symbol,
_period,
lastHHIndex //
);
if (isInited)
{
//
lastHH = lastBar.high;
lastHHTime = lastBar.time;
}
}
//
lastLLIndex = FindSwing(
_symbol,
_period,
X_SWING_LOW,
_requiredShoulders,
lastLLIndex + 1 //
);
if (lastLLIndex >= 0)
{
//
XOHCL lastBar;
bool isInited = lastBar.Init(
_symbol,
_period,
lastLLIndex //
);
if (isInited)
{
//
lastHH = lastBar.high;
lastHHTime = lastBar.time;
}
}
//
result =
//
lastLLIndex >= 0 &&
lastHHIndex >= 0
//
;
if (!result)
{
return result;
}
//
bool isFirstLow =
//
lastLLIndex < lastHHIndex
//
;
//
//
while (!isEnough)
{
//
int hhSize = ArraySize(hhs);
if (hhSize < _requiredPivots)
{
//
}
//
int llSize = ArraySize(lls);
if (llSize < _requiredPivots * 2)
{
}
//
}
// while (!isEnough)
// {
// //
// int hhSize = ArraySize(hhs);
// if (hhSize < _requiredPivots * 2)
// {
// //
// lastHHIndex = FindSwing(
// _symbol,
// _period,
// X_SWING_HIGH,
// _requiredShoulders,
// lastHHIndex + 1 //
// );
// if (lastHHIndex >= 0)
// {
// //
// XOHCL hBar;
// bool isInited = hBar.Init(
// _symbol,
// _period,
// lastHHIndex //
// );
// if (isInited)
// {
// //
// Add(
// hBar.high,
// hhs //
// );
// //
// Add(
// hBar.time,
// hhTimes //
// );
// //
// Add(
// lastHHIndex,
// hhIndexes //
// );
// }
// }
// }
// //
// int llSize = ArraySize(lls);
// if (llSize < _requiredPivots * 2)
// {
// //
// lastLLIndex = FindSwing(
// _symbol,
// _period,
// X_SWING_LOW,
// _requiredShoulders,
// lastLLIndex + 1 //
// );
// if (lastLLIndex >= 0)
// {
// //
// XOHCL lBar;
// bool isInited = lBar.Init(
// _symbol,
// _period,
// lastLLIndex //
// );
// if (isInited)
// {
// //
// Add(
// lBar.low,
// lls //
// );
// //
// Add(
// lBar.time,
// llTimes //
// );
// //
// Add(
// lastHHIndex,
// llIndexes //
// );
// }
// }
// }
// //
// hhSize = ArraySize(hhs);
// llSize = ArraySize(lls);
// isEnough =
// //
// hhSize == _requiredPivots * 2 &&
// llSize == _requiredPivots * 2
// //
// ;
// }
// //
// ArrayReverse(hhs);
// ArrayReverse(hhTimes);
// ArrayReverse(hhIndexes);
// //
// ArrayReverse(lls);
// ArrayReverse(llTimes);
// ArrayReverse(llIndexes);
//
// Draws ...
for (int i = 0; i < ArraySize(hhs); i++)
{
//
DrawArrow(
ChartID(),
"XTRND_HH" + ToString(i),
0,
hhTimes[i],
hhs[i],
234,
ANCHOR_BOTTOM,
clrMagenta,
STYLE_SOLID,
3 //
);
//
DrawArrow(
ChartID(),
"XTRND_LL" + ToString(i),
0,
llTimes[i],
lls[i],
233,
ANCHOR_TOP,
clrAqua,
STYLE_SOLID,
3 //
);
}
// //
// // Here We Try to Detect Trands ...
// int idxHH = 0;
// int idxLL = 0;
// isEnough = false;
// isFirstLow = llTimes[0] > hhTimes[0];
// while(!isEnough) {
// //
// datetime firstTime = NULL;
// if (isFirstLow) {
// //
// firstTime = llTimes[0];
// //
// // Looking for a High ...
// //
// if (hhs[hhIdx] > lls[llIdx]) {
// }
// } else {
// //
// firstTime = hhTimes[0];
// //
// // Looking for a Low ...
// }
// }
//
isUpTrend =
//
isFirstLow
?
//
lls[0] > lls[1] &&
lls[1] > lls[2] &&
//
hhs[0] > hhs[1] &&
hhs[1] > hhs[2]
//
:
//
hhs[0] > hhs[1] &&
hhs[1] > hhs[2] &&
//
lls[0] > lls[1] &&
lls[1] > lls[2]
//
;
isDownTrend =
//
isFirstLow
?
//
lls[0] < lls[1] &&
lls[1] < lls[2] &&
//
hhs[0] < hhs[1] &&
hhs[1] < hhs[2]
//
:
//
hhs[0] < hhs[1] &&
hhs[1] < hhs[2] &&
//
lls[0] < lls[1] &&
lls[1] < lls[2]
//
;
for (int i = 1; i < _requiredPivots; i++)
{
//
// Lows ...
//
// Lower Lows ...
if (lls[i - 1] < lls[i])
{
//
Add(
lls[i - 1],
lowerLows //
);
//
Add(
llTimes[i - 1],
lowerLowTimes //
);
}
else
//
// Higher Lows ...
if (lls[i - 1] > lls[i])
{
//
Add(
lls[i - 1],
higherLows //
);
//
Add(
llTimes[i - 1],
higherLowTimes //
);
}
//
// Highs ...
//
// Higher Highs ...
if (hhs[i - 1] > hhs[i])
{
//
Add(
hhs[i - 1],
higherHighs //
);
//
Add(
hhTimes[i - 1],
higherHighTimes //
);
}
else
//
// Lower Highs ...
if (hhs[i - 1] < hhs[i])
{
//
Add(
hhs[i - 1],
lowerHighs //
);
//
Add(
hhTimes[i - 1],
lowerHighTimes //
);
}
}
//
// Remove Draws ...
RemoveDraws("XTRND");
//
symbol = _symbol;
period = _period;
_numberOfPivots = _requiredPivots;
_pivotShoulders = _requiredShoulders;
//
result = IsValid();
//
return result;
}
//
// Tools ...
//
// Cleanup ...
void Clean()
{
//
symbol = NULL;
period = NULL;
//
_numberOfPivots = 0;
_pivotShoulders = 0;
//
Clean(higherHighs);
Clean(higherHighTimes);
//
Clean(higherLows);
Clean(higherLowTimes);
//
Clean(lowerHighs);
Clean(lowerHighTimes);
//
Clean(lowerLows);
Clean(lowerLowTimes);
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
IsValid(symbol) &&
IsValid(period)
//
;
//
return result;
}
};
+88 -62
View File
@@ -60,8 +60,8 @@ XPivot lastResistancePivot;
//
// For Zone Checking ...
XZone lastSupportZone;
XZone lastResistanceZone;
// XZone lastSupportZone;
// XZone lastResistanceZone;
//
XTrendDetector trendDetector;
@@ -151,7 +151,7 @@ void OnTick()
//
// Check Pivots ...
// HandleCheckPivots();
HandleCheckPivots();
//
// Check Zones ...
@@ -163,7 +163,7 @@ void OnTick()
//
// Check Trend State ...
HandleCheckTrend();
// HandleCheckTrend();
}
//
@@ -413,8 +413,6 @@ void HandleCheckPivots()
);
if (!isInited)
{
//
lastResistancePivot.Clean();
return;
}
@@ -424,6 +422,26 @@ void HandleCheckPivots()
{
Print("Valid Resistance ...");
}
} else {
//
bool isBreaked = lastResistancePivot.IsBreaked();
bool isRejected = lastResistancePivot.IsRejected();
//
if (isBreaked)
{
//
// lastResistancePivot.Clean();
Print("Resistance Breaked ...");
}
//
if (isRejected)
{
//
// lastResistancePivot.Clean();
Print("Resistance Rejected ...");
}
}
//
@@ -437,8 +455,6 @@ void HandleCheckPivots()
);
if (!isInited)
{
//
lastSupportPivot.Clean();
return;
}
@@ -459,7 +475,7 @@ void HandleCheckPivots()
if (isBreaked)
{
//
lastSupportPivot.Clean();
// lastSupportPivot.Clean();
Print("Support Breaked ...");
}
@@ -467,7 +483,7 @@ void HandleCheckPivots()
if (isRejected)
{
//
lastSupportPivot.Clean();
// lastSupportPivot.Clean();
Print("Support Rejected ...");
}
}
@@ -476,58 +492,58 @@ void HandleCheckPivots()
//
void HandleCheckZones()
{
//
bool result = false;
// //
// bool result = false;
//
// Find Last Support Zone ...
if (!lastSupportZone.IsValid())
{
//
result = lastSupportZone.FindLastSupportZone(
_Symbol,
_Period //
);
if (!result)
{
return;
}
// //
// // Find Last Support Zone ...
// if (!lastSupportZone.IsValid())
// {
// //
// result = lastSupportZone.FindLastSupportZone(
// _Symbol,
// _Period //
// );
// if (!result)
// {
// return;
// }
//
result = lastSupportZone.Draw(ChartID());
if (result)
{
Print("Valid Support Zone ...");
}
}
else
{
}
// //
// result = lastSupportZone.Draw(ChartID());
// if (result)
// {
// Print("Valid Support Zone ...");
// }
// }
// else
// {
// }
//
// Find Last Resistance Zone ...
if (!lastResistanceZone.IsValid())
{
//
result = lastResistanceZone.FindLastResistanceZone(
_Symbol,
_Period //
);
if (!result)
{
return;
}
// //
// // Find Last Resistance Zone ...
// if (!lastResistanceZone.IsValid())
// {
// //
// result = lastResistanceZone.FindLastResistanceZone(
// _Symbol,
// _Period //
// );
// if (!result)
// {
// return;
// }
//
result = lastResistanceZone.Draw(ChartID());
if (result)
{
Print("Valid Resistance Zone ...");
}
}
else
{
}
// //
// result = lastResistanceZone.Draw(ChartID());
// if (result)
// {
// Print("Valid Resistance Zone ...");
// }
// }
// else
// {
// }
}
//
@@ -573,10 +589,20 @@ void HandleCheckTimeTracker()
void HandleCheckTrend()
{
//
bool hasTren = trendDetector.FindTrend(
_Symbol,
_Period //
);
bool hasTrend = trendDetector.HasTrend();
if (!hasTrend)
{
//
bool trendFound = trendDetector.FindTrend(
_Symbol,
_Period //
);
}
else
{
//
Print("Has Trend: " + (trendDetector.isUpTrend ? "Bullish" : "Bearish"));
}
}
//
File diff suppressed because it is too large Load Diff