last works on adding threshold to pivots ...
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user