294 lines
5.6 KiB
Plaintext
294 lines
5.6 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| 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;
|
|
}
|
|
|
|
// |