Download MetaTrader 5

Counting Bull and Bear candles in a range

To add comments, please log in or register
kei41202
29
kei41202  

Hi guys, got a question that you guys may help me with.

I am trying to make an EA to have a for loop to count the number of bull and bear candles in a range.

But it always count a zero as result. The following is my code.

Any idea?

//+------------------------------------------------------------------+
//|                                             BullBear Counter.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int CandlesToRetrace =50;
int Bull;
int Bear;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   for(int i=1; i<CandlesToRetrace;i++)
{
if(Close[i]>Open[i])
Bull++;
else(Bear++);
}


  }

whroeder1
16304
whroeder1  
kei41202: But it always count a zero as result. The following is my code.
int Bull;
int Bear;
   for(int i=1; i<CandlesToRetrace;i++){
      if(Close[i]>Open[i]) Bull++;
      else                (Bear++);
I doubt it's always zero.
  1. You never do anything with your results, so how can you know?
  2. Bull and Bear have random initial values.
  3. Then every tick you increase them more.
kei41202
29
kei41202  
whroeder1:
I doubt it's always zero.
  1. You never do anything with your results, so how can you know?
  2. Bull and Bear have random initial values.
  3. Then every tick you increase them more.
Right, I forgot to insert the comment statement. And I think it is adding up the correct values so it just keep getting larger. Is there anyway I can stop it from adding?

//+------------------------------------------------------------------+
//|                                             BullBear Counter.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int CandlesToRetrace =50;
int Bull;
int Bear;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   for(int i=1; i<CandlesToRetrace;i++)
{
if(Close[i]>Open[i])
Bull++;
else(Bear++);
Comment("The Bull and Bear are respectively "+(Bull)+" and"+ (Bear));
}


  }
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
whroeder1
16304
whroeder1  
kei41202 Is there anyway I can stop it from adding?
Why would you want to stop? When you start counting what number do you begin with?
kei41202
29
kei41202  
whroeder1:
Why would you want to stop? When you start counting what number do you begin with?


Because it adds up non-stop. Retracing 50 candles, 23 is bull and 27 is bear. However, it doesnt stop there. It just continue to roll to 46 and 54 and so on.

I want to have it static like. It will count again next candle comes. Before that it stays 23-27.

Keith Watford
Moderator
10569
Keith Watford  
Bull=0;
Bear=0;
   for(int i=1; i<CandlesToRetrace;i++)
{
if(Close[i]>Open[i])
Bull++;
else(Bear++);
Comment("The Bull and Bear are respectively "+(Bull)+" and"+ (Bear));
}
.
nicholishen
971
nicholishen  
Keith Watford:
.


You can't add an integer to a string. 

Comment("The Bull and Bear are respectively "+(Bull)+" and"+ (Bear)); // adding string to int.
// instead use
Comment("The Bull and Bear are respectively ",Bull," and ",Bear); 
// or
Comment("The Bull and Bear are respectively "+IntegerToString(Bull)+" and "+IntegerToString(Bear)); 
kei41202
29
kei41202  
Guys, thanks for the tips. Gotta try it out tomorrow @v@
Keith Watford
Moderator
10569
Keith Watford  
nicholishen:


You can't add an integer to a string. 

That was not my code, so no need to quote me. I simply added the resetting of global-scope variables to zero.

You can add integers to a string but you will get a warning that it will be implicitly converted to a string.

To add comments, please log in or register