Author: Integer
0 Views
0 Downloads
0 Favorites
LWMAFast
#property copyright "Integer"
#property link      "https://www.mql5.com/ru/users/integer"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      period=14;
//--- indicator buffers
double         lwma[];
double         sum[];

double k2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   k2=1.0/((1+period)*period/2);

//--- indicator buffers mapping
   SetIndexBuffer(0,lwma,INDICATOR_DATA);
   SetIndexBuffer(1,sum,INDICATOR_CALCULATIONS);   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{

   int start;
   
   if(prev_calculated==0){
      start=begin+period-1;      
      lwma[start]=0;
      sum[start]=0;
      for(int i=start-period+1;i<=start;i++){
         lwma[start]+=price[i]*k2*(i+1);
         sum[start]+=price[i];
      }
      start++;      
   }
   else{
      start=prev_calculated-1;
   }

   int j1,j2;
  
   for(int i=start;i<rates_total;i++){
      j1=i-period;
      j2=i-1;
      sum[i]=sum[j2]-price[j1]+price[i];
      lwma[i]=lwma[j2]+(price[i]*(i+1)+sum[j2]*j1-(price[j1]+sum[i])*(j1+1))*k2;
   }   

   return(rates_total);
}
//+------------------------------------------------------------------+

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 ---