PeriodAdaptiveMA

Author: Scriptong
PeriodAdaptiveMA
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
PeriodAdaptiveMA
//+------------------------------------------------------------------+
//|                                                      MAOnDay.mq4 |
//|                                                        Scriptong |
//|                                                scriptong@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link      "scriptong@mail.ru"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       MethodMA=0;
extern int       PriceMA=0;
extern int       LargeTF = 1440;

bool Activate;
datetime NowDay, LastDay;
double Buffer[];
int Count;

//---- buffers
double MA[];
//+-------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                            |
//+-------------------------------------------------------------------------------------+
int init()
  {
   Activate = False;
// - 1 - == Ïðîâåðêà ïðàâèëüíîñòè âûáðàííîãî ïîëüçîâàòåëåì òàéìôðåéìà ===================
   if (Period() >= LargeTF)
     {
      Comment("Èíäèêàòîð DayAdaptiveMA ðàáîòàåò íà òàéìôðåéìàõ, ìåíüøèõ, ÷åì D1.");
      return(0);
     }
// - 1 - == Îêîí÷àíèå áëîêà =============================================================
   
// - 2 - == Èíèöèàëèçàöèÿ èíäèêàòîðíîãî áóôåðà ==========================================
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, MA);
   SetIndexEmptyValue(0, 0);
// - 2 - == Îêîí÷àíèå áëîêà =============================================================
   
// - 3 - == Èíèöèàëèçàöèÿ áóôåðà äëÿ ïîäñ÷åòà ñðåäíåãî ==================================
   ArrayResize(Buffer, MathCeil(LargeTF/Period())+1);
   ArrayInitialize(Buffer, 0);
// - 3 - == Îêîí÷àíèå áëîêà =============================================================
   
   Activate = True;
//----
   return(0);
  }
//+-------------------------------------------------------------------------------------+
//| Custom indicator deinitialization function                                          |
//+-------------------------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
  
//+-------------------------------------------------------------------------------------+
//| Custom indicator iteration function                                                 |
//+-------------------------------------------------------------------------------------+
int start()
  {
// - 1 - == Ïðàâèëüíî ëè èíèöèàëèçèðîâàí èíäèêàòîð? =====================================
   if (!Activate) return(0);
// - 1 - == Îêîí÷àíèå áëîêà =============================================================
  
   int limit, counted_bars=IndicatorCounted();

// - 2 - == Ðàñ÷åò íîìåðà áàðà òåêóùåãî ÒÔ, ñîîòâåòñòâóþùåãî íà÷àëó äíÿ =================
   if(counted_bars>0) counted_bars--;
   limit = iBarShift(Symbol(), 0, iTime(Symbol(), LargeTF, 
                     iBarShift(Symbol(), LargeTF, Time[Bars-counted_bars])));
// - 2 - == Îêîí÷àíèå áëîêà =============================================================

   LastDay = 1;
   for (int i = limit; i >= 0; i--)
     {
// - 3 - == Åñëè íà÷àëñÿ íîâûé äåíü, ôîðìèðóåì âñå äàííûå çàíîâî ========================
      NowDay = iTime(Symbol(), LargeTF, iBarShift(Symbol(), LargeTF, Time[i]));
      if (LastDay != NowDay)
        {
         ArrayInitialize(Buffer, 0);
         Count = 0;
         LastDay = NowDay;
        }
// - 3 - == Îêîí÷àíèå áëîêà =============================================================
      
// - 4 - == Îïðåäåëåíèå öåíû, ïî êîòîðîé ïðîèçâîäèòñÿ ðàñ÷åò ÌÀ =========================
      switch (PriceMA)
        {  
         case 0: /*Close*/    Buffer[Count] = Close[i]; break;
         case 1: /*Open*/     Buffer[Count] = Open[i]; break;
         case 2: /*High*/     Buffer[Count] = High[i]; break;
         case 3: /*Low*/      Buffer[Count] = Low[i]; break;
         case 4: /*Median*/   Buffer[Count] = (High[i]+Low[i])/2; break;
         case 5: /*Typical*/  Buffer[Count] = (High[i]+Low[i]+Close[i])/3; break;
         case 6: /*Weighted*/ Buffer[Count] = (High[i]+Low[i]+2*Close[i])/4; break;  
        }
// - 4 - == Îêîí÷àíèå áëîêà =============================================================
      
// - 5 - == Ðàñ÷åò ñðåäíåé ñêîëüçÿùåé ïî äàííûì ñåãîäíÿøíåãî äíÿ ========================
      MA[i] = iMAOnArray(Buffer, Count+1, Count+1, 0, MethodMA, 0);
      Count++;      
// - 5 - == Îêîí÷àíèå áëîêà =============================================================
     } 

   return(0);
  }

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