Complete Test Linear Regression and Required to Apply Works on Real Project ...

This commit is contained in:
2025-09-21 17:49:39 +03:30
parent 84dc311c72
commit 7a4f1aa48b
6 changed files with 1688 additions and 258 deletions
+396 -59
View File
@@ -570,7 +570,20 @@ struct XZigZagAnalysis
XZigZagPivot pivots[]; // ZigZag Pivots ...
//
ENUM_X_DIRECTION dir; // Direction ...
// Calculated Values ...
double peakTo; // Recent Peak ..
double valeTo; // Recent Vale ...
double peakFrom; // Previous Peak ...
double valeFrom; // Previous Vale ...
double peakSlope; // Calculated Slope for Peak ...
double valeSlope; // Calculated Slope for Vale ...
datetime peakToTime; // Recent Peak Time ...
datetime valeToTime; // Recent Vale Time ...
double peakIntercept; // Calculated Intercept for Peak ...
double valeIntercept; // Calculated Intercept for Vale ...
ENUM_X_DIRECTION dir; // Direction ...
datetime peakFromTime; // Previous Peak Time ...
datetime valeFromTime; // Previous Vale Time ...
//
// Constructor ...
@@ -611,78 +624,191 @@ struct XZigZagAnalysis
}
//
// AssignSymbol and Period ...
// Assign Requirements ...
//
time = bar.time;
symbol = bar.symbol;
period = bar.period;
Copy(_pivots, pivots);
//
// Extracting Pivots ...
int numberOfPivots = 2;
int oldestPeakIdx = GetOldest(pivots, X_ZIGZAG_PIVOT_TYPE_PEAK);
int oldestValeIdx = GetOldest(pivots, X_ZIGZAG_PIVOT_TYPE_VALE);
int youngestPeakIdx = GetYoungest(pivots, X_ZIGZAG_PIVOT_TYPE_PEAK);
int youngestValeIdx = GetYoungest(pivots, X_ZIGZAG_PIVOT_TYPE_VALE);
//
// Reading Recent Peaks ...
XZigZagPivot recentPeaks[];
int recentPeaksCount = GetRecent(
_pivots,
recentPeaks,
numberOfPivots,
X_ZIGZAG_PIVOT_TYPE_PEAK //
);
// Calculate Peaks and Vales Regressions ...
//
// Reading Recent Vales ...
XZigZagPivot recentVales[];
int recentValesCount = GetRecent(
_pivots,
recentVales,
numberOfPivots,
X_ZIGZAG_PIVOT_TYPE_VALE //
);
// Peak Time ...
peakToTime = pivots[youngestPeakIdx].time;
peakFromTime = pivots[oldestPeakIdx].time;
//
// Validate Pivots ...
// Vale Time ...
valeToTime = pivots[youngestValeIdx].time;
valeFromTime = pivots[oldestValeIdx].time;
//
// Validating Calculated Peaks and Vales Times ...
result =
recentPeaksCount >= numberOfPivots &&
recentValesCount >= numberOfPivots;
(IsSpecifiedValid(valeToTime) &&
IsSpecifiedValid(valeFromTime)) ||
(IsSpecifiedValid(peakToTime) &&
IsSpecifiedValid(peakFromTime));
if (!result)
{
//
Clean();
SpecifiedClean(recentPeaks);
SpecifiedClean(recentVales);
//
return result;
}
//
// Detect Direction ...
// Calculate Slope and Intercepts ...
//
int zIdx = 0;
int pIdx = numberOfPivots - 1;
//
bool isBullish =
recentPeaks[zIdx].value > recentPeaks[pIdx].value &&
recentVales[zIdx].value > recentVales[pIdx].value;
//
bool isBearish =
recentPeaks[zIdx].value < recentPeaks[pIdx].value &&
recentVales[zIdx].value < recentVales[pIdx].value;
//
result = isBullish || isBearish;
// Peaks ...
result = CalculateZigZagLinearRegression(
peakSlope,
peakIntercept,
pivots,
X_ZIGZAG_PIVOT_TYPE_PEAK //
);
if (!result)
{
//
Clean();
SpecifiedClean(recentPeaks);
SpecifiedClean(recentVales);
return result;
}
//
// Vales ...
result = CalculateZigZagLinearRegression(
valeSlope,
valeIntercept,
pivots,
X_ZIGZAG_PIVOT_TYPE_VALE //
);
if (!result)
{
//
Clean();
return result;
}
//
// Calculate Peaks and Vales ...
//
// Peaks ...
//
int iPeakToIDX = GetBarIndex(
symbol,
period,
peakToTime //
);
if (IsValidIndex(iPeakToIDX))
{
//
peakTo = CalculateValueBySlopeIntercept(
peakSlope,
peakIntercept,
iPeakToIDX //
);
}
//
int iPeakFromIDX = GetBarIndex(
symbol,
period,
peakFromTime //
);
if (IsValidIndex(iPeakFromIDX))
{
//
peakFrom = CalculateValueBySlopeIntercept(
peakSlope,
peakIntercept,
iPeakFromIDX //
);
}
//
// Vales ...
//
int iValeToIDX = GetBarIndex(
symbol,
period,
valeToTime //
);
if (IsValidIndex(iValeToIDX))
{
//
valeTo = CalculateValueBySlopeIntercept(
valeSlope,
valeIntercept,
iValeToIDX //
);
}
//
int iValeFromIDX = GetBarIndex(
symbol,
period,
valeFromTime //
);
if (IsValidIndex(iValeFromIDX))
{
//
valeFrom = CalculateValueBySlopeIntercept(
valeSlope,
valeIntercept,
iValeFromIDX //
);
}
//
// Validating Calculated Peaks and Vales ...
result =
(NotEmpty(valeTo) &&
NotEmpty(valeFrom) > 0) ||
(NotEmpty(peakTo) > 0 &&
NotEmpty(peakFrom) > 0);
if (!result)
{
//
Clean();
return result;
}
//
// Detecting Direction ...
//
bool isBullish =
valeTo > valeFrom ||
(valeTo > valeFrom &&
peakTo > peakFrom);
//
bool isBearish =
peakTo < peakFrom ||
(peakTo < peakFrom &&
valeTo < valeFrom);
//
// Validate When Only One Direction Exists ...
result =
(isBullish && !isBearish) ||
(isBearish && !isBullish);
if (!result)
{
//
Clean();
return result;
}
@@ -692,9 +818,6 @@ struct XZigZagAnalysis
? X_DIRECTION_BULLISH
: X_DIRECTION_BEARISH;
//
Copy(_pivots, pivots);
//
// Calculate Support and Resistance ...
@@ -706,10 +829,6 @@ struct XZigZagAnalysis
Clean();
}
//
SpecifiedClean(recentPeaks);
SpecifiedClean(recentVales);
//
return result;
}
@@ -729,6 +848,16 @@ struct XZigZagAnalysis
dir = X_DIRECTION_NONE;
SpecifiedClean(pivots);
//
peakTo = 0;
valeTo = 0;
peakFrom = 0;
valeFrom = 0;
peakToTime = NULL;
valeToTime = NULL;
peakFromTime = NULL;
valeFromTime = NULL;
//
ZeroMemory(this);
}
@@ -752,6 +881,30 @@ struct XZigZagAnalysis
IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period)
//
&&
//
(
//
// Validate Peak ...
(peakTo > 0 &&
peakFrom > 0 &&
peakSlope != EMPTY_VALUE &&
peakIntercept != EMPTY_VALUE &&
IsSpecifiedValid(peakToTime) &&
IsSpecifiedValid(peakFromTime))
//
||
//
// Validate Vale ...
(valeTo > 0 &&
valeFrom > 0 &&
valeSlope != EMPTY_VALUE &&
valeIntercept != EMPTY_VALUE &&
IsSpecifiedValid(valeToTime) &&
IsSpecifiedValid(valeFromTime))
//
)
//
;
//
@@ -1337,7 +1490,7 @@ class XCZigZagAnalysisObject : public XCBaseObject
*
* @return ( bool )
*/
bool Create(
bool CreateByRecent(
long chart_id,
int window,
XZigZagAnalysis &model,
@@ -1392,10 +1545,10 @@ class XCZigZagAnalysisObject : public XCBaseObject
chart_id,
upperName,
window,
rPeaks[0].time,
rPeaks[0].value,
rPeaks[numberOfPivots - 1].time,
rPeaks[numberOfPivots - 1].value //
rPeaks[numberOfPivots - 1].value,
rPeaks[0].time,
rPeaks[0].value //
);
if (!result)
{
@@ -1408,10 +1561,93 @@ class XCZigZagAnalysisObject : public XCBaseObject
chart_id,
lowerName,
window,
rVales[0].time,
rVales[0].value,
rVales[numberOfPivots - 1].time,
rVales[numberOfPivots - 1].value //
rVales[numberOfPivots - 1].value,
rVales[0].time,
rVales[0].value //
);
if (!result)
{
//
mUpper.Detach();
mUpper.Delete();
//
return result;
}
//
// Setting Object Name ...
if (result)
{
ObjName(objID);
}
//
return result;
}
/**
* Create Object ...
*
* @param chart_id: Long, Specified Chart ID ...
* @param window: Integer, Specified Chart Window ID ...
* @param model: XZigZagAnalysis, reference model ...
*
* @return ( bool )
*/
bool CreateByPV(
long chart_id,
int window,
XZigZagAnalysis &model //
)
{
//
bool result = false;
//
// Validate ...
result = model.IsValid();
if (!result)
{
return result;
}
//
// Generate Object ID ...
string objID = model.GetObjectID();
string upperName = "Upper_" + objID;
string lowerName = "Lower_" + objID;
//
// Extract Recent Pivots ...
//
// Creating Upper ...
result = mUpper.Create(
chart_id,
upperName,
window,
model.peakFromTime,
model.peakFrom,
model.peakToTime,
model.peakTo //
);
if (!result)
{
return result;
}
//
// Create Lower ...
result = mLower.Create(
chart_id,
lowerName,
window,
model.valeFromTime,
model.valeFrom,
model.valeToTime,
model.valeTo //
);
if (!result)
{
@@ -1974,4 +2210,105 @@ int GetRecent(
return result;
}
bool CalculateZigZagLinearRegression(
double &_slope,
double &_intercept,
XZigZagPivot &_pivots[],
ENUM_X_ZIGZAG_POINT_TYPE _type //
)
{
//
bool result = false;
//
// Prepare ...
_slope = EMPTY_VALUE;
_intercept = EMPTY_VALUE;
//
// Validate ...
result =
IsXValid(_type) &&
HasChild(_pivots);
if (!result)
{
return result;
}
//
// Extracted Type Specific Valus ...
double values[];
XZigZagPivot tmp[];
XZigZagPivot iPivot;
Copy(
_pivots,
tmp //
);
int count = ArraySize(tmp);
while (HasChild(tmp))
{
//
// Get Model ...
iPivot = tmp[0];
//
// Remove Model ...
ArrayRemove(
tmp,
0,
1 //
);
//
// Validate Type ...
result = _type == iPivot.type;
if (result)
{
//
// Add Value if Type Passed ...
Add(
iPivot.value,
values //
);
}
//
iPivot.Clean();
}
//
// Validation Valus ...
result = HasChild(values);
if (result)
{
//
// Calculate Slope / Intercept ...
result = CalculateLinearRegression(
_slope,
_intercept,
values //
);
}
//
// Validate Result ...
result =
_slope != EMPTY_VALUE &&
_intercept != EMPTY_VALUE;
if (!result)
{
//
_slope = EMPTY_VALUE;
_intercept = EMPTY_VALUE;
}
//
// Cleanup Resources ...
iPivot.Clean();
SpecifiedClean(tmp);
SpecifiedClean(values);
//
return result;
}
//