Author: Copyright � 2011, Matus German
MAcrosses
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MAcrosses
//+-------------------------------------------------------------------------------------------------------+
//|                                                                                         MAcrosses.mq4 |
//|                                                                        Copyright © 2011, Matus German |                                                                                         
//|                                                                             http://www.MTexperts.net/ |
//| Counts crosses off MAs, it shows % that all MAs are above slower MAs                                  |
//| If an MA crosses above another, the indicator rises                                                   |
//| If an MA crosses under another, the indicator falls                                                   |
//| If the indicator rises to 100%, every MA is above MAs with higher period                              |
//| If the indicator falls to 0%, every MA is under MAs with higher period                                |                                    
//+-------------------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2011, Matus German"
#property link      "http://www.MTexperts.net/"

#property indicator_separate_window
#property indicator_buffers 2               // Number of buffers
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 Red
#property indicator_color2 Blue

double MAcrs[];
double MAcrsAvg[];
extern int MAmax = 50;                    // the maximal MA
extern int Step = 5;                   // the increse between MAs, optimal MA/10
extern int MAavg = 20;                 // parameter to calculate moving average from MAcrosses

extern int MAmethod = 1;               // iMA moving average method:	
                                       //          0	Simple moving average,
                                       //          1	Exponential moving average,
                                       //          2	Smoothed moving average,
                                       //          3	Linear weighted moving average.
                                       
extern int AppliedPrice = 0;           // Applied to price:
                                       //       	0	Close price.
                                       //          1	Open price.
                                       //          2	High price.
                                       //          3	Low price.
                                       //          4	Median price, (high+low)/2.
                                       //          5	Typical price, (high+low+close)/3.
                                       //          6	Weighted close price, (high+low+close+close)/4.
                                       
                                       
double maxCrosses;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicators
    IndicatorBuffers(2);
    SetIndexBuffer(0,MAcrs);
    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
    SetIndexBuffer(1,MAcrsAvg);
    SetIndexStyle (1,DRAW_LINE,STYLE_DASH,1);
//----
   SetLevelValue(0,10);
   SetLevelValue(1,90);
   maxCrosses=max(MAmax,Step);
   return;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

// Function counts how many crosses could be made by MAs
double max(int MA,int MAstep)
{  
   /*
   int max=0;
   int MAcount=MA/MAstep;
   while(MAcount>0)
   {  
      MAcount=MAcount-1;
      max=max+MAcount;
   }
   */
   int i=MA;
   int MAs=0;
   while(i>0)
   {
      i-=MAstep;
      MAs++;
   }
   double max=((MAs*MAs)-MAs)/2;
   return (max);
}

////////////////////////////////////////////////////////////////////
int start()
{
   int    counted_bars,i,j,k;
   double    crosses;

   if(Bars<=MAmax) return(0);
   
   counted_bars=IndicatorCounted();          // Number of counted bars
   i=Bars-counted_bars-1;                    // Index of the first uncounted
   
   while(i>=0)
   {  
      crosses=0;                   
      k=MAmax;
      while(k>0)
      {  
         j=MAmax;
         while(j>k)
         {  

            if(iMA(NULL,0,j,0,MAmethod,AppliedPrice,i)<iMA(NULL,0,k,0,MAmethod,AppliedPrice,i))
            {
               crosses=crosses+1;                          // count crosses
            }
            j=j-Step;
         }
         k=k-Step;
      }
      MAcrs[i]=(crosses/maxCrosses)*100;      // % that all MAs are abowe slower MAs
      
      double MAcrsSum=0;
      int l;
      for(l=i; l<i+MAavg;l++)
      {
         MAcrsSum = MAcrsSum + MAcrs[l];
      }
      
      MAcrsAvg[i]=MAcrsSum/MAavg;                 // MAcrosses average
      
      i--;
   }
   return;
}
//+------------------------------------------------------------------+

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