Complete Test Linear Regression and Required to Apply Works on Real Project ...
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRegresser.mq5 |
|
||||
//| SaherElm IT Center |
|
||||
//| https://www.saherelm.ir |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "SaherElm IT Center"
|
||||
#property link "https://www.saherelm.ir"
|
||||
#property version "1.00"
|
||||
|
||||
//
|
||||
// Imports ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||
#include <ChartObjects/ChartObjectsLines.mqh>
|
||||
|
||||
//
|
||||
// Inputs ...
|
||||
input int mLoopbackLength = 200;
|
||||
input ENUM_X_PRICE mPriceType = X_PRICE_HIGH;
|
||||
|
||||
//
|
||||
// Variables ...
|
||||
double mSumX = 0;
|
||||
double mSumY = 0;
|
||||
double mSumXY = 0;
|
||||
double mSumX2 = 0;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
for (int i = 0; i < mLoopbackLength; i++)
|
||||
{
|
||||
//
|
||||
double iY = GetAppliedPrice(
|
||||
_Symbol,
|
||||
_Period,
|
||||
i,
|
||||
mPriceType //
|
||||
);
|
||||
double iX = i + 1;
|
||||
|
||||
//
|
||||
mSumX += iX;
|
||||
mSumY += iY;
|
||||
mSumXY += (iX * iY);
|
||||
mSumX2 += (iX * iX);
|
||||
}
|
||||
|
||||
//
|
||||
int n = mLoopbackLength;
|
||||
double slope = (n * mSumXY - mSumX * mSumY) / (n * mSumX2 - mSumX * mSumX);
|
||||
double intercept = (mSumY - slope * mSumX) / n;
|
||||
|
||||
//
|
||||
double xData[];
|
||||
double yData[];
|
||||
int xDataCount = CollectData(
|
||||
xData,
|
||||
yData //
|
||||
);
|
||||
double _cSlope;
|
||||
double _cIntercept;
|
||||
int calculatedCount = CalculateSlopeandIntercept(
|
||||
_cSlope,
|
||||
_cIntercept,
|
||||
xData,
|
||||
yData //
|
||||
);
|
||||
|
||||
//
|
||||
datetime time1 = iTime(_Symbol, _Period, mLoopbackLength);
|
||||
datetime time2 = iTime(_Symbol, _Period, 0);
|
||||
|
||||
//
|
||||
double price1 = (intercept + slope * mLoopbackLength);
|
||||
double price2 = (intercept + slope);
|
||||
|
||||
//
|
||||
double cPrice1 = PredictValue(
|
||||
_cSlope,
|
||||
_cIntercept,
|
||||
time1 //
|
||||
);
|
||||
double cPrice2 = PredictValue(
|
||||
_cSlope,
|
||||
_cIntercept,
|
||||
time2 //
|
||||
);
|
||||
|
||||
//
|
||||
string name = "TrendLine";
|
||||
string cName = "cTrendLine";
|
||||
|
||||
//
|
||||
CChartObjectTrend *iTrend = new CChartObjectTrend();
|
||||
bool isCreated = iTrend.Create(
|
||||
ChartID(),
|
||||
name,
|
||||
0,
|
||||
time1,
|
||||
price1,
|
||||
time2,
|
||||
price2 //
|
||||
);
|
||||
if (isCreated)
|
||||
{
|
||||
//
|
||||
// Styling Trend Line ...
|
||||
}
|
||||
|
||||
//
|
||||
CChartObjectTrend *iCTrend = new CChartObjectTrend();
|
||||
isCreated = iCTrend.Create(
|
||||
ChartID(),
|
||||
cName,
|
||||
0,
|
||||
time1,
|
||||
cPrice1,
|
||||
time2,
|
||||
cPrice2 //
|
||||
);
|
||||
if (isCreated)
|
||||
{
|
||||
//
|
||||
// Styling Trend Line ...
|
||||
}
|
||||
|
||||
//
|
||||
return (INIT_SUCCEEDED);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert deinitialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
//---
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert tick function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnTick()
|
||||
{
|
||||
//---
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
//
|
||||
int CollectData(
|
||||
double &xData[],
|
||||
double &yData[],
|
||||
int barIndex = 0 //
|
||||
)
|
||||
{
|
||||
//
|
||||
int result = false;
|
||||
|
||||
//
|
||||
SpecifiedClean(xData);
|
||||
SpecifiedClean(yData);
|
||||
|
||||
//
|
||||
barIndex = NormalizeInt(barIndex, 0);
|
||||
|
||||
//
|
||||
int start = barIndex;
|
||||
int end = start + mLoopbackLength;
|
||||
for (int i = start; i < end; i++)
|
||||
{
|
||||
//
|
||||
double iY = GetAppliedPrice(
|
||||
_Symbol,
|
||||
_Period,
|
||||
i //
|
||||
);
|
||||
|
||||
//
|
||||
datetime iTime = iTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
i //
|
||||
);
|
||||
double iX = i + 1; // (double)TimeToSeconds(iTime);
|
||||
|
||||
//
|
||||
Add(
|
||||
iX,
|
||||
xData //
|
||||
);
|
||||
|
||||
//
|
||||
Add(
|
||||
iY,
|
||||
yData //
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
result = ArraySize(xData);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
int CalculateSlopeandIntercept(
|
||||
double &_slope,
|
||||
double &_intercept,
|
||||
double &xData[],
|
||||
double &yData[] //
|
||||
)
|
||||
{
|
||||
//
|
||||
int result = 0;
|
||||
|
||||
//
|
||||
bool has = false;
|
||||
|
||||
//
|
||||
_slope = 0;
|
||||
_intercept = 0;
|
||||
|
||||
//
|
||||
if (!HasChild(xData) ||
|
||||
!HasChild(yData))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
double sumX = 0;
|
||||
double sumY = 0;
|
||||
double sumXY = 0;
|
||||
double sumX2 = 0;
|
||||
|
||||
//
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
int end = MathMin(ArraySize(xData), ArraySize(yData));
|
||||
for (int i = 0; i < end; i++)
|
||||
{
|
||||
//
|
||||
double x = xData[i];
|
||||
double y = yData[i];
|
||||
|
||||
//
|
||||
sumX += x;
|
||||
sumY += y;
|
||||
sumXY += x * y;
|
||||
sumX2 += x * x;
|
||||
}
|
||||
|
||||
//
|
||||
int n = end;
|
||||
_slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
|
||||
_intercept = (sumY - _slope * sumX) / n;
|
||||
|
||||
//
|
||||
result = n;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
double PredictValue(
|
||||
double &_slope,
|
||||
double &_intercept,
|
||||
datetime forTime //
|
||||
)
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsSpecifiedValid(forTime))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
double forX = iBarShift(
|
||||
_Symbol,
|
||||
_Period,
|
||||
forTime //
|
||||
); // (double)TimeToSeconds(forTime);
|
||||
result = (_intercept + _slope * forX);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1147,34 +1147,175 @@ void DetectPatterns()
|
||||
{
|
||||
//
|
||||
// ZIGZAG Pattern ...
|
||||
if (detectZIGZAGPatterns)
|
||||
// if (detectZIGZAGPatterns)
|
||||
// {
|
||||
// //
|
||||
// // Detect Market Analysis ...
|
||||
// XZigZagAnalysis zAnalysis;
|
||||
// has = fima2Signaller.fimaPOIDetector.AnalyseMarketUsingZigZag(
|
||||
// cBar,
|
||||
// cBar.period,
|
||||
// fima2Signaller.fimaHelper,
|
||||
// zAnalysis,
|
||||
// 15 //
|
||||
// );
|
||||
// if (has)
|
||||
// {
|
||||
// //
|
||||
// Print(ShortName + zAnalysis.GetMessage());
|
||||
|
||||
// //
|
||||
// XCZigZagAnalysisObject *iObj;
|
||||
// has = fima2Signaller.fimaPOIDrawer.DrawZigZagMarketAnalysisByPV(
|
||||
// iObj,
|
||||
// zAnalysis //
|
||||
// );
|
||||
// if (has)
|
||||
// {
|
||||
// draws.Add(iObj);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
//
|
||||
// Reading Pivots ...
|
||||
double iLows[];
|
||||
double iHighs[];
|
||||
double lowTo = 0;
|
||||
double highTo = 0;
|
||||
double lowFrom = 0;
|
||||
double highFrom = 0;
|
||||
datetime lowToTime = NULL;
|
||||
datetime highToTime = NULL;
|
||||
datetime lowFromTime = NULL;
|
||||
datetime highFromTime = NULL;
|
||||
double lowsSlope = EMPTY_VALUE;
|
||||
double highsSlope = EMPTY_VALUE;
|
||||
double lowsIntercept = EMPTY_VALUE;
|
||||
double highsIntercept = EMPTY_VALUE;
|
||||
|
||||
//
|
||||
int iLoopbackLength = 50;
|
||||
for (int i = 0; i < iLoopbackLength; i++)
|
||||
{
|
||||
//
|
||||
// Detect Market Analysis ...
|
||||
XZigZagAnalysis zAnalysis;
|
||||
has = fima2Signaller.fimaPOIDetector.AnalyseMarketUsingZigZag(
|
||||
cBar,
|
||||
cBar.period,
|
||||
fima2Signaller.fimaHelper,
|
||||
zAnalysis,
|
||||
15 //
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
//
|
||||
Print(ShortName + zAnalysis.GetMessage());
|
||||
double iLowPrice = iLow(_Symbol, _Period, i);
|
||||
double iHighPrice = iHigh(_Symbol, _Period, i);
|
||||
|
||||
//
|
||||
XCZigZagAnalysisObject *iObj;
|
||||
has = fima2Signaller.fimaPOIDrawer.DrawZigZagMarketAnalysis(
|
||||
iObj,
|
||||
zAnalysis //
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
draws.Add(iObj);
|
||||
}
|
||||
}
|
||||
//
|
||||
Add(
|
||||
iLowPrice,
|
||||
iLows //
|
||||
);
|
||||
Add(
|
||||
iHighPrice,
|
||||
iHighs //
|
||||
);
|
||||
}
|
||||
has = CalculateLinearRegression(
|
||||
lowsSlope,
|
||||
lowsIntercept,
|
||||
iLows,
|
||||
0,
|
||||
0 //
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
//
|
||||
lowToTime = GetBarTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
0 //
|
||||
);
|
||||
lowTo = CalculateValueBySlopeIntercept(
|
||||
lowsSlope,
|
||||
lowsIntercept,
|
||||
0 //
|
||||
);
|
||||
|
||||
//
|
||||
lowFromTime = GetBarTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
iLoopbackLength //
|
||||
);
|
||||
lowFrom = CalculateValueBySlopeIntercept(
|
||||
lowsSlope,
|
||||
lowsIntercept,
|
||||
iLoopbackLength //
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
has = CalculateLinearRegression(
|
||||
highsSlope,
|
||||
highsIntercept,
|
||||
iHighs,
|
||||
0,
|
||||
0 //
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
//
|
||||
highToTime = GetBarTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
0 //
|
||||
);
|
||||
highTo = CalculateValueBySlopeIntercept(
|
||||
highsSlope,
|
||||
highsIntercept,
|
||||
0 //
|
||||
);
|
||||
|
||||
//
|
||||
highFromTime = GetBarTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
iLoopbackLength //
|
||||
);
|
||||
highFrom = CalculateValueBySlopeIntercept(
|
||||
highsSlope,
|
||||
highsIntercept,
|
||||
iLoopbackLength //
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Draw ...
|
||||
|
||||
//
|
||||
// Low ...
|
||||
CChartObjectTrend *iLowTrend;
|
||||
has = fima2Signaller.fimaPOIDrawer.DrawTrendLine(
|
||||
lowFrom,
|
||||
lowFromTime,
|
||||
lowTo,
|
||||
lowToTime,
|
||||
iLowTrend,
|
||||
"LOW_TREMD",
|
||||
true // Ray Right ...
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
draws.Add(iLowTrend);
|
||||
}
|
||||
|
||||
//
|
||||
// High ...
|
||||
CChartObjectTrend *iHighTrend;
|
||||
has = fima2Signaller.fimaPOIDrawer.DrawTrendLine(
|
||||
highFrom,
|
||||
highFromTime,
|
||||
highTo,
|
||||
highToTime,
|
||||
iHighTrend,
|
||||
"HIGH_TREMD",
|
||||
true // Ray Right ...
|
||||
);
|
||||
if (has)
|
||||
{
|
||||
draws.Add(iHighTrend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user