Price period SMA_v2

Author: Copyright 2021, Lilita Bogackova
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Price period SMA_v2
ÿþ//+------------------------------------------------------------------+

//|                                             Price period SMA.mq5 |

//|                                 Copyright 2021, Lilita Bogackova |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, Lilita Bogackova"

#property link      "https://www.mql5.com"

#property strict

#property version   "1.00"

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   2

//--- plot symbolPrice

#property indicator_label1  "symbolPrice"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSpringGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot symbolSMA

#property indicator_label2  "symbolSMA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- input parameters

enum ENUM_USE_SMA

  {

   HOUR_SMA,                                                // Hourly moving average

   DAY_SMA                                                  // Daily moving average

  };

input string               _symbolName    ="0";             // Symbol, 0 = take a symbol from the chart

input ENUM_USE_SMA         _useSMA        =DAY_SMA;         // Use moving average

input ENUM_APPLIED_PRICE   _appliedPrice  =PRICE_WEIGHTED;  // Price

input bool                 _createVLine   =true;           // Create a period dividing line

//---

int      barsCalculated=0;

int      symbolHandle=-1;

int      currDateTime=0;

int      seperator=0;

int      dateTime=0;

int      settPeriod=1;

long     timeStatic=0;

string   shortName="";

string   symbolName="";

bool     chartSetSymbolPeriod;

ENUM_TIMEFRAMES period;

//--- indicator buffers

double   symbolPriceBuffer[];

double   symbolSMABuffer[];



double   symbolPriceHandleBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,symbolPriceBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,symbolSMABuffer,INDICATOR_DATA);

   SetIndexBuffer(2,symbolPriceHandleBuffer,INDICATOR_CALCULATIONS);

//---

   period=PERIOD_CURRENT;

//---

   symbolName=_symbolName;

   if(StringLen(_symbolName)<=1)

      //--- 15@5B A8<2>; A 3@0D8:0, =0 :>B>@>< @01>B05B 8=48:0B>@

      symbolName=_Symbol;

//--- 2K18@05< ?5@8>4

   switch(_useSMA)

     {

      case HOUR_SMA:

         seperator=1;

         shortName=StringFormat("PP SMA (%s, %s)",symbolName,"HOUR SMA");

         if(Period()>=PERIOD_H1)

            period=PERIOD_M1;

         break;

      case DAY_SMA:

         seperator=10;

         shortName=StringFormat("PP SMA (%s, %s)",symbolName,"DAY SMA");

         if(Period()>=PERIOD_D1)

            period=PERIOD_M15;

     }

//---

   IndicatorSetString(INDICATOR_SHORTNAME,shortName);

//--- A>740BL EM=4; 8=48:0B>@0

   symbolHandle=iMA(symbolName,PERIOD_CURRENT,settPeriod,0,MODE_SMA,_appliedPrice);

//--- 5A;8 =5 1K;> 2>7<>6=>AB8 A>740BL symbolHandle

   if(symbolHandle==INVALID_HANDLE)

     {

      //--- 8=48:0B>@ 70:0=G8205BAO @0=LH5 A@>:0

      return(INIT_FAILED);

     }

//---

   chartSetSymbolPeriod=false;

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| $C=:F8O 8B5@0F88 ?>;L7>20B5;LA:>3> 8=48:0B>@0                    |

//+------------------------------------------------------------------+

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[])

  {

   int i,limit;

//---

   static int calculated_min=INT_MAX;

   if(calculated_min==INT_MAX)

     {

      //--- >?@545;8BL :>;8G5AB2> 7=0G5=89, 2KG8A;O5<KE 2 8=48:0B>@5

      int calculated=BarsCalculated(symbolHandle);

      if(calculated<=settPeriod)

        {

         calculated_min=INT_MAX;

         return(0);

        }

      if(calculated_min>calculated-settPeriod)

         calculated_min=calculated-settPeriod;

      //--- >?@545;8BL :>;8G5AB2> 7=0G5=89, 2KG8A;O5<KE =0 3@0D8:5

      calculated=Bars(Symbol(),PERIOD_CURRENT);

      if(calculated<=settPeriod)

        {

         calculated_min=INT_MAX;

         return(0);

        }

      if(calculated_min>calculated-settPeriod)

         calculated_min=calculated-settPeriod;

     }

//--- :>;8G5AB2> 7=0G5=89, A:>?8@>20==KE A 8=48:0B>@0 iMA

   int values_to_copy=0;

//--- 5A;8 MB> ?5@2K9 AB0@B @0AG5B0 8=48:0B>@0 8;8 5A;8 87<5=8;>AL :>;8G5AB2> 7=0G5=89 2 8=48:0B>@5 iMA

//--- 8;8 5A;8 =5>1E>48<> @0AAG8B0BL 8=48:0B>@ 70 420 8 1>;55 10@0 (7=0G8B, GB>-B> 87<5=8;>AL 2 8AB>@88 F5=K)

   if(prev_calculated==0 || calculated_min!=barsCalculated || rates_total>prev_calculated+1)

     {

      //--- 5A;8 <0AA82 iMABuffer 1>;LH5 :>;8G5AB20 7=0G5=89 2 8=48:0B>@5 iMA 4;O A8<2>;0/?5@8>40, B> <K =5 :>?8@C5< 2A5

      //--- ?@>B82=>< A;CG05 <K :>?8@C5< <5=LH5 @07<5@0 8=48:0B>@=KE 1CD5@>2

      if(calculated_min>=rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated_min;

     }

   else

     {

      //--- MB> >7=0G05B, GB> MB> =5 ?5@2K9 @07 @0AG5B0 8=48:0B>@0, 0 A <><5=B0 ?>A;54=53> 2K7>20 OnCalculate()

      //--- 4;O @0AG5B0 4>102;O5BAO =5 1>;55 42CE 10@>2

      values_to_copy=(rates_total-prev_calculated)+2;

     }

//--- 70?><=8BL :>;8G5AB2> 7=0G5=89 2 8=48:0B>@5

   barsCalculated=calculated_min;

//---

   limit=rates_total-values_to_copy;

//--- 70?>;=8BL M;5<5=BK 2 1CD5@=>< <0AA825

   FillArrayFromBuffer(symbolPriceHandleBuffer,symbolHandle,values_to_copy);

//---

   static int SMAperiod=0;

//--- >A=>2=>9 F8:;

   for(i=limit+1; i<rates_total && !IsStopped(); i++)

     {

      //--- 70?>;=O5< F5=>2>9 1CD5@

      symbolPriceBuffer[i]=symbolPriceHandleBuffer[i];

      //--- ?@>25@O5<, 87<5=8;AO ;8 C:070==K9 ?5@8>4

      dateTime=DateTime(time[i],seperator);

      if(dateTime!=currDateTime)

        {

         SMAperiod=-1;

         currDateTime=dateTime;

         //--- @8AC5< 25@B8:0;L=K5 ;8=88

         if(_createVLine&&i>=rates_total-(1440/(PeriodSeconds(PERIOD_CURRENT)/60)*seperator))

           {

            if(!VLineCreate(time[i]))

               Print("H81:0 A>740=8O >1J5:B0: ",GetLastError());

           }

        }

      //--- 5A;8 ?@8E>48B =>2K9 10@, C25;8G8205< ?5@8>4 SMA

      if((long)time[i]!=timeStatic)

        {

         SMAperiod++;

         timeStatic=(long)time[i];

        }

      //--- @0AAG8B0BL SMA

      symbolSMABuffer[i]=symbolPriceBuffer[i]*2.0/(1.0+SMAperiod)+symbolSMABuffer[i-1]*(1.0-2.0/(1.0+SMAperiod));

     }

//--- update the chart

   if(!chartSetSymbolPeriod)

     {

      chartSetSymbolPeriod=ChartSetSymbolPeriod(ChartID(),Symbol(),period);

      ChartRedraw();

     }

//--- 2>72@0I05<>5 7=0G5=85 prev_calculated 4;O A;54CNI53> 2K7>20

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| >72@0I05B G0A 4=O, 8;8 45=L                                     |

//+------------------------------------------------------------------+

int DateTime(const datetime time, int sep)

  {

   MqlDateTime dt;

   TimeToStruct(time,dt);

//---

   if(sep==1)

      return dt.hour;

   else

      return dt.day_of_week;

  }

//+------------------------------------------------------------------+

//| !>7409B5 25@B8:0;L=CN ;8=8N                                      |

//+------------------------------------------------------------------+

bool VLineCreate(datetime time=0)

  {

   static int count=0;

   ResetLastError();

   if(ObjectFind(ChartID(),"PP SMA"+IntegerToString(++count))<0)

     {

      if(!ObjectCreate(ChartID(),"PP SMA"+IntegerToString(count),OBJ_VLINE,0,time,0))

        {

         return(false);

        }

      else

        {

         ObjectSetInteger(ChartID(),"PP SMA"+IntegerToString(count),OBJPROP_COLOR,clrGreen);

         ChartRedraw(ChartID());

        }

     }

   return(true);

  }

//+------------------------------------------------------------------+

//| 0?>;=5=85 8=48:0B>@=>3> 1CD5@0 87 8=48:0B>@0                    |

//+------------------------------------------------------------------+

bool FillArrayFromBuffer(double &ind_buffer[],  // 8=48:0B>@=K9 1CD5@ 7=0G5=89

                         int ind_handle,        // EM=4; 8=48:0B>@0

                         int amount             // :>;8G5AB2> :>?8@C5<KE 7=0G5=89

                        )

  {

//--- A1@>A8< :>4 >H81:8

   ResetLastError();

//--- 70?>;=O5< G0ABL <0AA820 ind_buffer 7=0G5=8O<8 87 8=48:0B>@=>3> 1CD5@0 ?>4 8=45:A>< 0

   if(CopyBuffer(ind_handle,0,0,amount,ind_buffer)<0)

     {

      //--- 5A;8 :>?8@>20=85 =5 C40;>AL, A>>1I8< :>4 >H81:8

      PrintFormat("5 C40;>AL A:>?8@>20BL 40==K5 87 8=48:0B>@0, :>4 >H81:8 %d",GetLastError());

      //--- 7025@H8< A =C;52K< @57C;LB0B>< - MB> >7=0G05B, GB> 8=48:0B>@ 1C45B AG8B0BLAO =5@0AAG8B0==K<

      return(false);

     }

//--- 2A5 ?>;CG8;>AL

   return(true);

  }

//+------------------------------------------------------------------+

//| $C=:F8O 458=8F80;870F88 8=48:0B>@0                               |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,"PP SMA");

  }

//+------------------------------------------------------------------+

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