First off, I would like to point out that using just a Simple Moving Average (SMA) is not the correct way to calculate an Least Square Moving Average (LSMA). Using moving averages to calculate Linear Regression is as follows:
You have to use both an SMA and a LWMA to calculate an LSMA end-point ("b = 3 * LWMA - 2 * SMA" in the above image). Someone else also described something similar in the following article (but did not provide all the equations):
So, I have also attached a ZIP file with both a PDF as well as the original MathCAD sheet of my equations showing the proof of how to calculate Linear Regression using only moving averages.
First off, I would like to point out that using just a Simple Moving Average (SMA) is not the correct way to calculate an Least Square Moving Average (LSMA). Using moving averages to calculate Linear Regression is as follows:
You have to use both an SMA and a LWMA to calculate an LSMA end-point ("b = 3 * LWMA - 2 * SMA" in the above image). Someone else also described something similar in the following article (but did not provide all the equations):
So, I have also attached a ZIP file with both a PDF as well as the original MathCAD sheet of my equations showing the proof of how to calculate Linear Regression using only moving averages.
i want to solve the problem that OnCalculate function is worked incorrectly too, are you have any suggestion for me?
Please don't use external URLs for adding screenshots. Please re-edit your post and use the Image icon on the toolbar to add images directly to your post.
It looks like new users or low ranking users don't get the "image" icon on their toolbar! I did not know that!
I will rewrite your code correctly and post it here later.
I will rewrite your code correctly and post it here later.
Here is the simplified code using LWMA and SMA to calculate a LSMA (please note that there is no need to calculate the Applied Price since MQL5 already does that with the second form of OnCalculate):
#property version "1.000" #property description "Least Square Moving Average" #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 3 #property indicator_label1 "LSMA" #property indicator_type1 DRAW_LINE #property indicator_color1 clrYellow #property indicator_style1 STYLE_SOLID #property indicator_width1 2 #property indicator_label2 "LWMA" #property indicator_type2 DRAW_LINE #property indicator_color2 clrViolet #property indicator_style2 STYLE_DOT #property indicator_width2 1 #property indicator_label3 "SMA" #property indicator_type3 DRAW_LINE #property indicator_color3 clrCyan #property indicator_style3 STYLE_DOT #property indicator_width3 1 #include <MovingAverages.mqh> // Include Moving Average Calculation Functions //--- Indicator Parameters input int input_period = 15; // Period (Bars) //--- Indicator Buffers double buffer_LSMA[], buffer_LWMA[], buffer_SMA[]; //--- Initialise Buffer Index and other Properties int OnInit( void ) { // Validate Input Parameters if( input_period < 1 ) { Print( "Error: Invalid Period!" ); return INIT_PARAMETERS_INCORRECT; }; // Initialise Buffer Objects SetIndexBuffer( 0, buffer_LSMA, INDICATOR_DATA ); SetIndexBuffer( 1, buffer_LWMA, INDICATOR_DATA ); SetIndexBuffer( 2, buffer_SMA, INDICATOR_DATA ); IndicatorSetInteger( INDICATOR_DIGITS, _Digits ); // Set Number of Significant Digits (Precision) return INIT_SUCCEEDED; // Successful Initialisation of Indicator }; //--- Calculate Indicator Values for Applied Price int OnCalculate( const int rates_total, const int prev_calculated, const int begin, const double &price[] ) { // Apply LWMA & SMA Calculations LinearWeightedMAOnBuffer( rates_total, prev_calculated, begin, input_period, price, buffer_LWMA ); SimpleMAOnBuffer( rates_total, prev_calculated, begin, input_period, price, buffer_SMA ); // Calculate LSMA using the LWMA & SMA Data int start_position = begin + input_period; if( prev_calculated < start_position ) ArrayInitialize( buffer_LSMA, EMPTY_VALUE ); else start_position = prev_calculated - 1; for( int i = start_position; i < rates_total; i++ ) buffer_LSMA[ i ] = 3.0 * buffer_LWMA[ i ] - 2.0 * buffer_SMA[ i ]; return rates_total; // Return value of prev_calculated for next call };
Here is the simplified code using LWMA and SMA to calculate a LSMA (please note that there is no need to calculate the Applied Price since MQL5 already does that with the second form of OnCalculate):
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guy, i'm writting a LSMA indicator but it display incorrectly, it work well except the at the first bar.
here is my plot display ***
I find out when i compile the OnCalculate() will be run 2 times, i try to console log the rates_total and prev_calculated and the result is: ***
It lead to this error but i dont know how to fix that, can anyone tell me a way to solve this problem? please, i need your help.
i have attached these images below.
My code below (sorry for my bad English)