trend_arrows_sign

Author: Copyright � 2012, Vladimir Mametov
Indicators Used
Indicator of the average true range
0 Views
0 Downloads
0 Favorites
trend_arrows_sign
//+------------------------------------------------------------------+
//|                                            trend_arrows_sign.mq5 |
//|                               Copyright © 2012, Vladimir Mametov | 
//|                                                                  | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2012, Vladimir Mametov" 
#property link      "" 
//---- íîìåð âåðñèè èíäèêàòîðà
#property version   "1.00"
//---- îòðèñîâêà èíäèêàòîðà â îñíîâíîì îêíå
#property indicator_chart_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ 2
#property indicator_buffers 2
//---- èñïîëüçîâàíî âñåãî äâà ãðàôè÷åñêèõ ïîñòðîåíèÿ
#property indicator_plots   2
//+----------------------------------------------+
//|  Ïàðàìåòðû îòðèñîâêè áû÷üåãî èíäèêàòîðà      |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà â âèäå çíà÷êà
#property indicator_type1 DRAW_ARROW
//---- â êà÷åñòâå îêðàñêè èíäèêàòîðà èñïîëüçîâàí
#property indicator_color1 clrBlue
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 2
#property indicator_width1 2
//---- îòîáðàæåíèå ìåòêè ñèãíàëüíîé ëèíèè
#property indicator_label1  "Buy trend_arrows signal"
//+----------------------------------------------+
//|  Ïàðàìåòðû îòðèñîâêè ìåäâåæüåãî èíäèêàòîðà   |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà â âèäå çíà÷êà
#property indicator_type2 DRAW_ARROW
//---- â êà÷åñòâå îêðàñêè èíäèêàòîðà èñïîëüçîâàí
#property indicator_color2 clrRed
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style2 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 2
#property indicator_width2 2
//---- îòîáðàæåíèå ìåòêè ñèãíàëüíîé ëèíèè
#property indicator_label2  "Sell trend_arrows signal"
//+----------------------------------------------+
//|  îáúÿâëåíèå êîíñòàíò                         |
//+----------------------------------------------+
#define RESET  0 // Êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷¸ò èíäèêàòîðà
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà                 |
//+----------------------------------------------+
input uint iPeriod=15;  // Ïåðèîä èíäèêàòîðà
input uint iFullPeriods=1;
input int Shift=0;      // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ 
//+----------------------------------------------+
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå áóäóò â 
//---- äàëüíåéøåì èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double SignUp[],SignDown[];
int arr[];
bool boolp1;
int ATR_Handle,min_rates_total;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
int OnInit()
  {
//---- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
   min_rates_total=int(iPeriod+iFullPeriods);
   int ATR_Period=15;
   min_rates_total=MathMax(min_rates_total,ATR_Period);
   
//--- ïîëó÷åíèå õåíäëà èíäèêàòîðà ATR
   ATR_Handle=iATR(NULL,0,ATR_Period);
   if(ATR_Handle==INVALID_HANDLE)
     {
      Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà ATR");
      return(INIT_FAILED);
     }

//---- ðàñïðåäåëåíèå ïàìÿòè ïîä ìàññèâû ïåðåìåííûõ   
   ArrayResize(arr,min_rates_total);

//---- èíèöèàëèçàöèè ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
   string shortname;
   StringConcatenate(shortname,"trend_arrows(",string(iPeriod),", ",string(iFullPeriods),", ",string(Shift),")");
//---- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//---- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
   SetIndexBuffer(0,SignUp,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 1 ïî ãîðèçîíòàëè íà Shift
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 1
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,3*min_rates_total);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðàõ, êàê â òàéìñåðèÿõ   
   ArraySetAsSeries(SignUp,true);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---- ñèìâîë äëÿ èíäèêàòîðà
   PlotIndexSetInteger(0,PLOT_ARROW,233);

//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
   SetIndexBuffer(1,SignDown,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 2 ïî ãîðèçîíòàëè íà Shift
   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 2
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,3*min_rates_total);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðàõ, êàê â òàéìñåðèÿõ   
   ArraySetAsSeries(SignDown,true);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---- ñèìâîë äëÿ èíäèêàòîðà
   PlotIndexSetInteger(1,PLOT_ARROW,234);
//---   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷åòà
   if(BarsCalculated(ATR_Handle)<rates_total || rates_total<min_rates_total) return(RESET);

   int limit,bar,to_copy;
   double ATR[],TrendUp,TrendDown;
   static double TrendUp_prev,TrendDown_prev;

//---- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ, êàê â òàéìñåðèÿõ  
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(time,true);
   ArraySetAsSeries(ATR,true);

//---- ðàñ÷åò ñòàðòîâîãî íîìåðà first äëÿ öèêëà ïåðåñ÷åòà áàðîâ
   if(prev_calculated>rates_total || prev_calculated<=0) // ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
     {
      limit=rates_total-min_rates_total-1;               // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ

      int tms1=rates_total-2-min_rates_total;
      while(!isDelimeter(Period(),time,tms1)) tms1--;
      boolp1=tms1;
      tms1--;
      for(int rrr=0; rrr<int(iPeriod); rrr++)
        {
         while(!isDelimeter(Period(),time,tms1)) tms1--;
         tms1--;
        }
      tms1++;
      limit=tms1;
     }
   else
     {
      limit=rates_total-prev_calculated;                 // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
     }
   to_copy=limit+1;
     
   //--- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâ ATR[]
   if(CopyBuffer(ATR_Handle,0,0,to_copy,ATR)<=0) return(RESET);

//---- îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
   for(bar=limit; bar>=0 && !IsStopped(); bar--)
     {
      TrendUp=0.0;
      TrendDown=0.0;
      SignUp[bar]=0.0;
      SignDown[bar]=0.0;
      double HH=AverageHigh(high,time,bar);
      double LL=AverageLow(low,time,bar);

      if(close[bar]>HH) TrendUp=LL;
      else
        {
         if(close[bar]<LL) TrendDown=HH;
         else
           {
            if(TrendDown_prev) TrendDown=HH;
            if(TrendUp_prev) TrendUp=LL;
           }
        }

      if(!TrendUp_prev && TrendUp) SignUp[bar]=low[bar]-ATR[bar]*3/8;
      if(!TrendDown_prev && TrendDown) SignDown[bar]=high[bar]+ATR[bar]*3/8;
      
      if(bar)
        {
         TrendUp_prev=TrendUp;
         TrendDown_prev=TrendDown;
        }
     }
//----     
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| AverageHigh                                                      |
//+------------------------------------------------------------------+
double AverageHigh(const double &High[],const datetime &Time[],int index)
  {
//----
   double hhv;
   double ret= 0.0;
   int nbars = index;
   int max=int(iPeriod+iFullPeriods);
   boolp1=false;
   for(int iii=0; iii<max; iii++)
     {
      while(!isDelimeter(Period(),Time,nbars)) nbars++;
      if(!boolp1) boolp1=nbars;
      arr[iii]=nbars;
      nbars++;
     }

   for(int count=int(iPeriod-1); count>0; count--)
     {
      hhv=High[ArrayMaximum(High,arr[count-1]+1,arr[count]-arr[count-1])];
      ret+=hhv;
     }
   if(iFullPeriods==1)
     {
      hhv=High[ArrayMaximum(High,arr[iPeriod-1]+1,arr[iPeriod]-arr[iPeriod-1])];
      ret += hhv;
      ret /= NormalizeDouble(iPeriod, 0);
     }
   else
     {
      hhv=High[ArrayMaximum(High,index,arr[0]-index)];
      ret += hhv;
      ret /= NormalizeDouble(iPeriod, 0);
     }
//----
   return (ret);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double AverageLow(const double &Low[],const datetime &Time[],int index)
  {
//----
   double llv;
   double ret= 0.0;
   int nbars = index;
   int max=int(iPeriod+iFullPeriods);
   boolp1=false;
   for(int iii=0; iii<max; iii++)
     {
      while(!isDelimeter(Period(),Time,nbars)) nbars++;
      if(!boolp1) boolp1=nbars;
      arr[iii]=nbars;
      nbars++;
     }
   for(int count=int(iPeriod-1); count>0; count--)
     {
      llv=Low[ArrayMinimum(Low,arr[count-1]+1,arr[count]-arr[count-1])];
      ret+=llv;
     }
   if(iFullPeriods==1)
     {
      llv=Low[ArrayMinimum(Low,arr[iPeriod-1]+1,arr[iPeriod]-arr[iPeriod-1])];
      ret += llv;
      ret /= NormalizeDouble(iPeriod, 0);
     }
   else
     {
      llv=Low[ArrayMinimum(Low,index,arr[0]-index)];
      ret += llv;
      ret /= NormalizeDouble(iPeriod, 0);
     }
//----
   return (ret);
  }
//+------------------------------------------------------------------+
//| isDelimeter()                                                    |
//+------------------------------------------------------------------+
bool isDelimeter(ENUM_TIMEFRAMES TimFrame,const datetime &Time[],int index)
  {
//----
   MqlDateTime tm;
   TimeToStruct(Time[index],tm);
   bool blper=false;
   switch(TimFrame)
     {
      case PERIOD_M1: blper=tm.min==0; break;
      case PERIOD_M2: blper=tm.min==0; break;
      case PERIOD_M3: blper=tm.min==0; break;
      case PERIOD_M4: blper=tm.min==0; break;
      case PERIOD_M5: blper=tm.min==0; break;
      case PERIOD_M6: blper=tm.min==0; break;
      case PERIOD_M10: blper=tm.min==0; break;
      case PERIOD_M12: blper=tm.min==0; break;
      case PERIOD_M15: blper=tm.min==0; break;
      case PERIOD_M20: blper=tm.min==0; break;
      case PERIOD_M30: blper=tm.min==0 && MathMod(tm.hour,4.0)==0.0; break;
      case PERIOD_H1: blper=tm.min==0 && MathMod(tm.hour,4.0)==0.0; break;
      case PERIOD_H2: blper=tm.min==0 && MathMod(tm.hour,4.0)==0.0; break;
      case PERIOD_H3: blper=tm.min==0 && MathMod(tm.hour,4.0)==0.0; break;
      case PERIOD_H4: blper=tm.min==0 && tm.hour==0; break;
      case PERIOD_H6: blper=tm.min==0 && tm.hour==0; break;
      case PERIOD_H8: blper=tm.min==0 && tm.hour==0; break;
      case PERIOD_H12: blper=tm.min==0 && tm.hour==0; break;
      case PERIOD_D1: blper=tm.day_of_week==1 && tm.hour==0; break;
      case PERIOD_W1:
        {
         MqlDateTime tm2;
         TimeToStruct(Time[index+1],tm2);
         blper=tm.day==1 || (tm.day==2 && tm2.day!=1) || (tm.day==3 && tm2.day!=2);
         break;
        }
      case PERIOD_MN1:
        {
         MqlDateTime tm2;
         TimeToStruct(Time[index+1],tm2);
         blper=tm.day==1 || (tm.day==2 && tm2.day!=1) || (tm.day==3 && tm2.day!=2);
         break;
        }
     }
//----
   return (blper);
  }
//+------------------------------------------------------------------+

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