Files
xMQL5/BKPS/14041018/Documents/BKP/TestRegresser.mq5
T
2026-01-08 11:39:42 +03:30

344 lines
6.7 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;
//
// Calculation ...
input group "Calculation";
input int zigzagDepth = 12; // Depth
input int zigzagDeviation = 5; // Deviation
input int zigzagBackStep = 3; // Back Step
//
// Variables ...
//
int mZigZagHandler = INVALID_HANDLE;
//
// double mSumX = 0;
// double mSumY = 0;
// double mSumXY = 0;
// double mSumX2 = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//
double xData[];
double yData[];
int xDataCount = CollectData(
xData,
yData //
);
//
// Initialize ZigZag Handler ...
mZigZagHandler = iCustom(
_Symbol,
_Period,
"ZigZagColor",
zigzagDepth,
zigzagDeviation,
zigzagBackStep //
);
if (mZigZagHandler == INVALID_HANDLE)
{
Print("Error creating ZigZag handle");
return (INIT_FAILED);
}
// //
// for (int i = 0; i < mLoopbackLength; i++)
// {
// //
// double iY = yData[i];
// double iX = xData[i];
// //
// mSumX += iX;
// mSumY += iY;
// mSumXY += (iX * iY);
// mSumX2 += (iX * iX);
// }
// //
// 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();
bool 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;
}
//