Complete Test Linear Regression and Required to Apply Works on Real Project ...
This commit is contained in:
@@ -6641,22 +6641,22 @@ int GetPriceBoundary(
|
||||
|
||||
/**
|
||||
* Get Applied Price Buffer ...
|
||||
*
|
||||
*
|
||||
* @param mType: ENUM_X_PRICE, Specified Price Type ...
|
||||
* @param dest: double, Holds Destination Prices ...
|
||||
* @param mOpen: double collection, Open Prices ...
|
||||
* @param mHigh: double collection, High Prices ...
|
||||
* @param mLow: double collection, Low Prices ...
|
||||
* @param mClose: double collection, Close Prices ...
|
||||
*
|
||||
*
|
||||
* @return ( int )
|
||||
*/
|
||||
int GetAppliedPrice(
|
||||
ENUM_X_PRICE mType,
|
||||
double &dest[],
|
||||
const double &mOpen[], // Open Prices
|
||||
const double &mHigh[], // High Preices
|
||||
const double &mLow[], // Low Prices
|
||||
const double &mOpen[], // Open Prices
|
||||
const double &mHigh[], // High Preices
|
||||
const double &mLow[], // Low Prices
|
||||
const double &mClose[] // Close Prices
|
||||
)
|
||||
{
|
||||
@@ -7061,3 +7061,135 @@ bool SetChartVolumesColor(
|
||||
//
|
||||
|
||||
//
|
||||
// START Linear Regression ...
|
||||
//
|
||||
|
||||
/**
|
||||
* Linear Regression Based Calculate Data ...
|
||||
* int this Senario X Axis is Buffer Index ...
|
||||
*
|
||||
* @param _slope: double, refrence for Calculated Slope ...
|
||||
* @param _intercept: double, reference for Calculated Intercept ...
|
||||
* @param _source: double, Y Axis Valus Collection ...
|
||||
* @param _start: int, start Index ...
|
||||
* @param _count: int, number of Calculations ...
|
||||
*
|
||||
* @return ( bool )
|
||||
*/
|
||||
bool CalculateLinearRegression(
|
||||
double &_slope,
|
||||
double &_intercept,
|
||||
double &_source[],
|
||||
int _start = 0,
|
||||
int _count = 0 //
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Prepare ...
|
||||
_slope = EMPTY_VALUE;
|
||||
_intercept = EMPTY_VALUE;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
int count = ArraySize(_source);
|
||||
result = IsValidSize(count);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
_start = NormalizeInt(_start, 0, count - 1);
|
||||
if (_count == 0 || _count + _start >= count)
|
||||
{
|
||||
//
|
||||
// Get Whole Array ...
|
||||
_count = count - 1 - _start;
|
||||
}
|
||||
int _end = _start + _count;
|
||||
|
||||
//
|
||||
// Define Requirements ...
|
||||
double sumX = 0;
|
||||
double sumY = 0;
|
||||
double sumXY = 0;
|
||||
double sumX2 = 0;
|
||||
|
||||
//
|
||||
// Looping Through Source Buffer to Cellect Data ...
|
||||
for (int i = _start; i < _end; i++)
|
||||
{
|
||||
//
|
||||
double x = i + 1;
|
||||
double y = _source[i];
|
||||
|
||||
//
|
||||
sumX += x;
|
||||
sumY += y;
|
||||
sumXY += x * y;
|
||||
sumX2 += x * x;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Slope and Intercept ...
|
||||
int n = _count;
|
||||
_slope = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX * sumX));
|
||||
_intercept = ((sumY - _slope) * sumX) / n;
|
||||
|
||||
//
|
||||
result = sumX > 0 &&
|
||||
sumY > 0 &&
|
||||
sumXY > 0 &&
|
||||
sumX2 > 0 &&
|
||||
NotEmpty(_slope) &&
|
||||
NotEmpty(_intercept);
|
||||
if (!result)
|
||||
{
|
||||
//
|
||||
_slope = EMPTY_VALUE;
|
||||
_intercept = EMPTY_VALUE;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
double CalculateValueBySlopeIntercept(
|
||||
double _slope,
|
||||
double _intercept,
|
||||
int _index //
|
||||
)
|
||||
{
|
||||
//
|
||||
double result = EMPTY_VALUE;
|
||||
|
||||
//
|
||||
// Normalize Index ...
|
||||
_index = NormalizeInt(_index, 1);
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
!NotEmpty(_slope) ||
|
||||
!NotEmpty(_intercept) ||
|
||||
!IsValidIndex(_index))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = (_intercept + _slope * _index);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// END Linear Regression ...
|
||||
//
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user