How code indicator to count number of crossings of Moving Average with Open - page 2

 
qjol:
read this code & u will get the idea



Pardon me qjol for my ignorance. But I cant figure out how to put it together. I attempted here but it doesn't display anything so I think I crumbled it up. Kindly help out how to assemble it together in the proper order. Thanks.
/--- input parameter
input int InpMACOPeriod=14; //  Period
input int MA_Period=1;

int Cnt;
int count;

//--- buffers
double ExtMACOBuffer[];//Tell the computer that this is an array.

// PROBLEM WITH THIS INDICATOR. 1. IT COUNTED THE TICKS NOT THE CROSSINGS.2. IT KEEP COUNTING CONTINUOUSLY. 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  string short_name;
//--- indicator buffers mapping
  IndicatorBuffers(1);
  SetIndexBuffer(0,ExtMACOBuffer);  
  SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 1,Red);
  SetIndexDrawBegin(0,InpMACOPeriod);
//--- name for DataWindow and indicator subwindow label
   short_name="OpenEqualBid("+IntegerToString(InpMACOPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);  
   
//---
   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[])
  {
//---
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;

   if(Bars<=InpMACOPeriod) return(0);
   int uncountedbars=Bars-counted_bars;
 
  
//---

for (int i=0;i<uncountedbars; i++)
{





ExtMACOBuffer[i] = Cnt/InpMACOPeriod;

}   
   
   
 if (rates_total != prev_calculated) Cnt = 0;  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

bool Up, Down;

int Crosses()
   {
   if(NewBar())
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         }
      else if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         }
      }

   if (Up)
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         Cnt++;
         }
      }
   if (Down)
      {
      if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         Cnt++;
         }
      }
   return(Cnt);
   }

bool NewBar ()
   {
   static datetime LastTime = 0;

   if (Time[0] != LastTime) 
      {
      LastTime = Time[0];     
      return (true);
      }
   else
      {
      return (false);
      }
   }
 
input double InpMACOPeriod=14; // Period

double Cnt;

ExtMACOBuffer[i] = Crosses()/InpMACOPeriod;


B.T.W i don't get it y divide by 14

or maybe u mean the average crosses in 14 bars then you have to use iMAOnArray() function

 
qjol:
input double InpMACOPeriod=14; // Period

double Cnt;

ExtMACOBuffer[i] = Crosses()/InpMACOPeriod;Indi


B.T.W i don't get it y divide by 14

or maybe u mean the average crosses in 14 bars then you have to use iMAOnArray() function


Hi, I tried to use iMAOnArray() per attached codes and noticed the following:

1. Every 6 bars the indicator resets back to zero: I like the indicator to continue the line without it touching the zero again unless there is no count. The count must reset every bar but the number of count per bar must be added into the array ensuring and be averaged by the period.

2. The count is counting the ticks: I would like the indicator to count the crossings of MA with Open[0].

I attached the graph and the latest codes here. Please let me know how I can code correctly.

//--- input parameter
input int InpMACOPeriod=14; //  Period
input int MA_Period=1;

int count=0;
//--- buffers
double ExtMACOBuffer[];//Tell the computer that this is an array.

// PROBLEM WITH THIS INDICATOR. 1. IT COUNTED THE TICKS NOT THE CROSSINGS.2. IT KEEP COUNTING CONTINUOUSLY. 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  string short_name;
//--- indicator buffers mapping
  IndicatorBuffers(1);
  SetIndexBuffer(0,ExtMACOBuffer);  
  SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 1,Red);
  SetIndexDrawBegin(0,InpMACOPeriod);
//--- name for DataWindow and indicator subwindow label
   short_name="MACO("+IntegerToString(InpMACOPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);  
   
//---
   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[])
  {
//---
  
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   if(Bars<=InpMACOPeriod) return(0);
   int uncountedbars=Bars-counted_bars;
 
  
//---

for (int i=0;i<uncountedbars; i++)
{

if(crossed()) count++;
ExtMACOBuffer[i] = count;

}   
   
   
 if (rates_total != prev_calculated) count = 0;  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
bool crossed()
{
int i=0;
double MA0= iMAOnArray(ExtMACOBuffer,0,InpMACOPeriod,0,MODE_SMA,0);

double MA1= iMAOnArray(ExtMACOBuffer,0,InpMACOPeriod,0,MODE_SMA, 1);

if ((Open[0]>MA1 && Open[0]<MA0) || (Open[0]<MA1 && Open[0]>MA0))
return(true);

else
return(EMPTY_VALUE);
}
 

i realy dont get it

do u need the average of 14 periods crosses ?

i.e. Bar[0] bid crossed the Open[0] 14 times

+ Bar[1] bid crossed the Open[1] 16 times

+ Bar[2] bid crossed the Open[2] 8 times

+ ...

+ ...

+ Bar[13] bid crossed the Open[13] 7 times

Equal let's say 154

then 154 / 14 ?

do i understand correctly ?

 
qjol:

i realy dont get it

do u need the average of 14 periods crosses ?

i.e. Bar[0] bid crossed the Open[0] 14 times

+ Bar[1] bid crossed the Open[1] 16 times

+ Bar[2] bid crossed the Open[2] 8 times

+ ...

+ ...

+ Bar[13] bid crossed the Open[13] 7 times

Equal let's say 154

then 154 / 14 ?

do i understand correctly ?


Yes you are correct. Remember that the 14 is a period input so I can change it to any value thus averaging the counts based on the period. The biggest head ache of this indicator code now is it is only counting the every ticks, that is not what this indicator for. I need it to count the number of cross over of price with Open[0] or how many times it pass the Open[] up or down. I noticed that if I use the Open[0]==Bid statement it still counts not this statement but the every ticks, the same thing with the MA crossing with Open[0] also counting the every ticks. Dont know why it is doing that. But I think we are very close to a solution from where I began.
 
#include <MovingAverages.mqh>

#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  clrMagenta
#property  indicator_color2  clrRed
#property  indicator_width1  1
#property  indicator_width2  2

//--- indicator buffers


input double InpMACOPeriod=14; //  Period
input int MA_Period=1;

bool Up, Down;
double Cnt;
int count;

//--- buffers
double ExtMACOBuffer[], ExtMACOBufferArr[];//Tell the computer that this is an array.

// PROBLEM WITH THIS INDICATOR. 1. IT COUNTED THE TICKS NOT THE CROSSINGS.2. IT KEEP COUNTING CONTINUOUSLY. 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  string short_name;
//--- indicator buffers mapping
  IndicatorBuffers(2);
  SetIndexBuffer(0,ExtMACOBuffer);  
  SetIndexStyle(0,DRAW_LINE, STYLE_SOLID);//, 1,clrBlue);
  SetIndexBuffer(1,ExtMACOBufferArr);  
  SetIndexStyle(1,DRAW_LINE, STYLE_SOLID);//, 1,clrRed);
  SetIndexDrawBegin(0,InpMACOPeriod);
//--- name for DataWindow and indicator subwindow label
   short_name="OpenEqualBid("+IntegerToString(InpMACOPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Crosses");  
   SetIndexLabel(1,"MACrosses");  
   
//---
   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[])
  {
//---

   int i, limit;

   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
   
 
//---

for (i=0;i<limit; i++)
   ExtMACOBuffer[i] = Crosses();
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpMACOPeriod,ExtMACOBuffer,ExtMACOBufferArr);

 if (rates_total != prev_calculated) Cnt = 0;  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

int Crosses()
   {
   if(NewBar())
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         }
      else if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         }
      }

   if (Up)
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         Cnt++;
         }
      }
   if (Down)
      {
      if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         Cnt++;
         }
      }
   return(Cnt);
   }

bool NewBar ()
   {
   static datetime LastTime = 0;

   if (Time[0] != LastTime) 
      {
      LastTime = Time[0];     
      return (true);
      }
   else
      {
      return (false);
      }
   }

now for the million dollar question, what is it worth without saving those data, because if you restart the indicator (the computer or the terminal) for any reason (even if you change the time frame) you gonna lose all those data
(it's not stored in some physically place it's stored in the memory(array))
once you have to restart, those data is gone

 
qjol:

now for the million dollar question, what is it worth without saving those data, because if you restart the indicator (the computer or the terminal) for any reason (even if you change the time frame) you gonna lose all those data
(it's not stored in some physically place it's stored in the memory(array))
once you have to restart, those data is gone

//+------------------------------------------------------------------+
//|                                               CountCrossingI.mq4 |
//|                                              Copyright 2014, xxx |
//|                                                              xxx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, xxx"
#property link      "xxx"
#property version   "1.00"
#property strict

#property indicator_separate_window // Drawing in a separate window
#property  indicator_buffers 2
#property  indicator_color1  clrMagenta
#property  indicator_color2  clrRed
#property  indicator_width1  1
#property  indicator_width2  2

//--- indicator buffers


input double InpMACOPeriod=14; //  Period
input int MA_Period=1;

bool Up, Down;
double Cnt;
int count;

//--- buffers
double ExtMACOBuffer[], ExtMACOBufferArr[];//Tell the computer that this is an array.

// PROBLEM WITH THIS INDICATOR. 1. IT COUNTED THE TICKS NOT THE CROSSINGS.2. IT KEEP COUNTING CONTINUOUSLY. 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  string short_name;
//--- indicator buffers mapping
  IndicatorBuffers(2);
  SetIndexBuffer(0,ExtMACOBuffer);  
  SetIndexStyle(0,DRAW_LINE, STYLE_SOLID);//, 1,clrBlue);
  SetIndexBuffer(1,ExtMACOBufferArr);  
  SetIndexStyle(1,DRAW_LINE, STYLE_SOLID);//, 1,clrRed);
  SetIndexDrawBegin(0,InpMACOPeriod);       //WARNING! POSSIBLE LOSS OF DATA DUE TO TYPE CONVERSION.
//--- name for DataWindow and indicator subwindow label
   short_name="OpenEqualBid("+IntegerToString(InpMACOPeriod)+")"; ////WARNING! POSSIBLE LOSS OF DATA DUE TO TYPE CONVERSION.
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Crosses");  
   SetIndexLabel(1,"MACrosses");  
   
//---
   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[])
  {
//---

   int i, limit;

   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
   
 
//---

for (i=0;i<limit; i++)
   ExtMACOBuffer[i] = Crosses();
  SimpleMAOnBuffer(rates_total,prev_calculated,0,InpMACOPeriod,ExtMACOBuffer,ExtMACOBufferArr);//ERROR "FUNCTION NOT DEFINED"

 if (rates_total != prev_calculated) Cnt = 0;  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

int Crosses()
   {
   if(NewBar())
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         }
      else if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         }
      }

   if (Up)
      {
      if (MarketInfo(Symbol(), MODE_BID) < Open[0])
         {
         Up = false;
         Down = true;
         Cnt++;
         }
      }
   if (Down)
      {
      if (MarketInfo(Symbol(), MODE_BID) > Open[0])
         {
         Up = true;
         Down = false;
         Cnt++;
         }
      }
   return(Cnt); //WARNING! POSSIBLE LOSS OF DATA DUE TO TYPE CONVERSION.
   }

bool NewBar ()
   {
   static datetime LastTime = 0;

   if (Time[0] != LastTime) 
      {
      LastTime = Time[0];     
      return (true);
      }
   else
      {
      return (false);
      }
   }

Big big thanks Qjol. I compiled your code but received 1 error and 3 warnings that I copy paste here. Kindly fine tune it so I can compile. So excited to see it running. Yes I understand about the data loss issue. I need to run it for a few hours to gather enough info on the chart useful for my EA.