Fast & Slow Linear Regression

Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Fast & Slow Linear Regression
//+------------------------------------------------------------------+
//|                                Fast & Slow Linear Regression.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property strict
// Input parameters
extern int FastPeriod = 20;
extern int SlowPeriod = 50;

// Buffers
double FastBuffer[];
double SlowBuffer[];

// Functions
int OnInit()
  {
// Set the indicator buffer to hold a single value
   SetIndexBuffer(0, FastBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, SlowBuffer, INDICATOR_DATA);

// Set the indicator line color
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);

// Set the indicator to be displayed in a separate chart window
   IndicatorSetString(INDICATOR_SHORTNAME, "Fast & Slow Linear Regression");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(ChartID(),"Angle");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
// Check if the number of rates is sufficient for the indicator
   if(rates_total < FastPeriod || rates_total < SlowPeriod)
      return(0);

// Calculate the fast linear regression line
   double sum_x = 0.0, sum_y = 0.0, sum_xy = 0.0, sum_xx = 0.0;
   for(int i = 0; i < FastPeriod; i++)
     {
      sum_x += i;
      sum_y += close[i];
      sum_xy += i * close[i];
      sum_xx += i * i;
     }
   double slope = (FastPeriod * sum_xy - sum_x * sum_y) / (FastPeriod * sum_xx - sum_x * sum_x);
   double intercept = (sum_y - slope * sum_x) / FastPeriod;

// Store the fast linear regression line values in the indicator buffer
   for(int i = 0; i < rates_total; i++)
      FastBuffer[i] = intercept + slope * i;

// Calculate the slow linear regression line
   sum_x = 0.0;
   sum_y = 0.0;
   sum_xy = 0.0;
   sum_xx = 0.0;
   for(int i = 0; i < SlowPeriod; i++)
     {
      sum_x += i;
      sum_y += close[i];
      sum_xy += i * close[i];
      sum_xx += i * i;
     }
   slope = (SlowPeriod * sum_xy - sum_x * sum_y) / (SlowPeriod * sum_xx - sum_x * sum_x);
   intercept = (sum_y - slope * sum_x) / SlowPeriod;

// Store the slow linear regression line values in the indicator buffer
   for(int i = 0; i < rates_total; i++)
      SlowBuffer[i] = intercept + slope * i;

// Calculate the angle between the lines
// double angle = atan((SlowBuffer[rates_total - 1] - FastBuffer[rates_total - 1]) / (SlowPeriod - FastPeriod)) * 180.0 / M_PI;
   double angle = atan2(SlowBuffer[rates_total - 1] - FastBuffer[rates_total - 1], SlowPeriod - FastPeriod) * 180.0 / M_PI;

// Plot the angle as text on the chart
   string angle_text = "Angle: " + DoubleToString(angle, 2) + " degrees";
   ObjectCreate("Angle", OBJ_LABEL, 0, 0, 0, 0);
   ObjectSetText("Angle", angle_text, 9, "Arial", clrYellow);

// Set the number of rates to be displayed in the indicator
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double atan2(double y, double x)
  {
   if(x == 0.0 && y == 0.0)
      return(0.0);
   if(x == 0.0)
      return(y > 0.0 ? M_PI_2 : -M_PI_2);
   return(atan(y / x));
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---