Download MetaTrader 5

Simple calculating candle stats

To add comments, please log in or register
Find out how to install MetaTrader on Linux
Pasi Hakamaki
4678
Pasi Hakamaki 2013.02.04 12:08 

Hi,

My first post, just joined the forums. My problem was how to calculate the amount of bull,bear and no body candles on the current chart and timeframe and alert the result: Being the newbie I am would you consider this to be a good solution? It seems to be working fine. Thanks for your insight. Br, Candles.

//+------------------------------------------------------------------+
//|                                            SingleCandleStats.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

bool     bear=false,
         bull=false,
         no_body=false;
double   o,h,l,c,high_wick,low_wick,length,body,
         no_low_wick,no_high_wick,no_wick,wicks_equal;
int      bears,bulls,no_bodys,
         bars=Bars(_Symbol,_Period);
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   MqlRates rates[];
   CopyRates(_Symbol,_Period,1,bars,rates);
   ArraySetAsSeries(rates,true);
   
   for(int i=0;i<bars-1;i++)
     {
      o=rates[i].open;
      h=rates[i].high;
      l=rates[i].low;
      c=rates[i].close;
         if(c<o)
           {
            bear=true;
            bears++;
           }
         else if(c>o)
           {
            bull=true;
            bulls++;
           }
         else
           {
            no_body=true;
            no_bodys++;      
           }
     }
     Alert("Bulls: ",bulls," ","Bears: ",bears," ", "No body: ",no_bodys);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
khai hoan nguyen
242
khai hoan nguyen 2013.02.06 04:40  
code is good but you can give us ea to install for mt4, mt5
phi nuts
2184
phi nuts 2013.02.06 16:08  
Candles:

Hi,

My first post, just joined the forums. My problem was how to calculate the amount of bull,bear and no body candles on the current chart and timeframe and alert the result: Being the newbie I am would you consider this to be a good solution? It seems to be working fine. Thanks for your insight. Br, Candles.

Yes, that's OK as newbie and that's OK, if what you want is to read the whole chart, and not just since open day. But, why indicator and why not just script, and why written in OnInit and not in OnCalculate ? If it is written for indicator, you could use data from OnCalculate.

In your code, I prefer to assign value of variable bars in OnInit(), or better in OnCalculate (if the code in there) and also I should check the return value of bars, ArraySetAsSeries, and CopyRates, just in case if it fails.

int      bears,bulls,no_bodys,
         bars=Bars(_Symbol,_Period);
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   MqlRates rates[];
   bars = Bars(_Symbol,_Period);

   if (bars > 0 && ArraySetAsSeries(rates,true) == true)
      {
      if (CopyRates(_Symbol, _Period, 1, bars, rates) >= bars - 1)
         {
          //--- your codes 
   

 

Pasi Hakamaki
4678
Pasi Hakamaki 2013.02.06 19:56  

Thank you for your reply, phi.nuts. I'm testing out different approaches thus I chose indicator and OnInit intentionally. Checking the return values of ArraySetAsSeries and CopyRates is a good idea.



khai hoan nguyen
242
khai hoan nguyen 2013.02.07 02:40  
i am testing your code. it very enjoyable
Pasi Hakamaki
4678
Pasi Hakamaki 2013.02.07 09:56  

Clad to hear it is working for you :)


Br,

Candles

khai hoan nguyen
242
khai hoan nguyen 2013.02.10 14:31  
thanks for sharing above code. but it not working
To add comments, please log in or register