Download MetaTrader 5

Counting Candles

To add comments, please log in or register
Hamidreza Taheri
1932
Hamidreza Taheri 2013.12.02 16:29 

Hi friends,

I want to calculate the candle number after and related to specific time period on current chart. For example on H1 cahrt i want to numerize the candle after 00:00 Oclock (After every day opening).

I have already make an indicator Using time formula and Ibarshift - Both of them are already done-

You can see it on below picture the already done indicator.

But if i want to do this on some syntetic charts like renko, those are price related charts and they are independed from time, I do not know how to do this. I have already make the below code for this porpuse but it does not work. Can you help me?

//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//----
static bool CountedOnThisBar;
extern int TimeFrame=1440;

//----
double period[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- 3 additional buffers are used for counting.
//---- indicator lines
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(0, period);
//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
     {
      case 1 : string TimeFrameStr="Period_M1"; break;
      case 5 : TimeFrameStr="Period_M5"; break;
      case 15 : TimeFrameStr="Period_M15"; break;
      case 30 : TimeFrameStr="Period_M30"; break;
      case 60 : TimeFrameStr="Period_H1"; break;
      case 240 : TimeFrameStr="Period_H4"; break;
      case 1440 : TimeFrameStr="Period_D1"; break;
      case 10080 : TimeFrameStr="Period_W1"; break;
      case 43200 : TimeFrameStr="Period_MN1"; break;
      default : TimeFrameStr="Current Timeframe";
     }
  }
//----
   return(0);
//+------------------------------------------------------------------+
//| MTF RSI                                                          |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
   int sum=0;
   // Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
     {
      if (Time[i]<TimeArray[y]) y++;
//----

               for(int j=1;j<=limit;j++)
               {
                if(Time[j]>=iTime(Symbol(),TimeFrame,y) && CountedOnThisBar!=Bars)
                 {
                 CountedOnThisBar=Bars;
                 sum++;
                }
              }
              
              period[i]=sum;

     }
   //----
   return(0);
  }
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.02 16:36  
hmrt135:


But if i want to do this on some syntetic charts like renko, those are price related charts and they are independed from time, I do not know how to do this. I have already make the below code for this porpuse but it does not work. Can you help me?

If you want the time of a bar on a chart where the bars do not have meaningful times then you are in trouble . . .
whroeder1
14753
whroeder1 2013.12.02 16:45  
hmrt135: i want to do this on some syntetic charts like renko,

Does your Renko generator create charts with valid timestamps? Then there's no difference.

As for counting how many bars for the higher time frame, try

  int    counted_bars=IndicatorCounted();
  for(int i = Bars -1 -counted_bars; i>=0;i--){
     datetime now = Time[i],
              nowTF = now - now % (60 * TimeFrame);
     period[i] = iBarShift(NULL,0, nowTF);
  }
Hamidreza Taheri
1932
Hamidreza Taheri 2013.12.02 16:49  
RaptorUK:
If you want the time of a bar on a chart where the bars do not have meaningful times then you are in trouble . . .


No no, i do not want the time of a bar on renko chart. I told that i have already made this indicator for regular time related chart using the time of a bar on a chart. So yet i want to counting the number of bar on renko chart on each day. So the above code was my try to achieve this goal but no success.
Hamidreza Taheri
1932
Hamidreza Taheri 2013.12.02 16:50  
WHRoeder:
Does your Renko generator create charts with valid timestamps?

No no, it does not do that at all. You know that renko is not time related chart. I told that i have already made this indicator for regular time related chart using the time of a bar on a chart. So yet i want to counting the number of bar on renko chart on each day. So the above code was my try to achieve this goal but no success.
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.02 16:54  
hmrt135:

No no, i do not want the time of a bar on renko chart. I told that i have already made this indicator for regular time related chart using the time of a bar on a chart. So yet i want to counting the number of bar on renko chart on each day. So the above code was my try to achieve this goal but no success.
"on each day" . . . day is a time period, if your chart does not have meaningful times where will you get the time from ?
Hamidreza Taheri
1932
Hamidreza Taheri 2013.12.02 17:04  

You have right, but the first candle on each daily period is the starting candle . Yes this is difficult so i share it here, if some one understand what i am looking for.

the below picture is wrong because it use time formula for counting the candle number. I want a mathematic solution on coding in mql4.

whroeder1
14753
whroeder1 2013.12.02 21:21  
hmrt135: No no, it does not do that at all. You know that renko is not time related chart.
Irrelevant. A Renko creates a new bar when price exceeds its limit. The time of that tick is the time of the new bar. Every bar (renko or not) has a TOHLCV. If your generator doesn't properly set the Time of each bar, then you can't possibly find which bar starts or crosses midnight.
Hamidreza Taheri
1932
Hamidreza Taheri 2013.12.02 21:34  
WHRoeder:
Irrelevant. A Renko creates a new bar when price exceeds its limit. The time of that tick is the time of the new bar. Every bar (renko or not) has a TOHLCV. If your generator doesn't properly set the Time of each bar, then you can't possibly find which bar starts or crosses midnight.


Yes, i am using your the same code that you send me. but if you compare both above charts, you see that this code does not generate linear graph on subwindow. That mean we need to use another code for counting bars in renko charts.
whroeder1
14753
whroeder1 2013.12.03 00:15  
What part of "if your generator doesn't properly set the Time" was unclear? You can NOT write code to overcome a bad generator. A bad chart is a bad chart. No amount of code will fix it. You need to fix the generator.
john
213
john 2013.12.03 02:11  
WHRoeder:
Irrelevant. A Renko creates a new bar when price exceeds its limit. The time of that tick is the time of the new bar. Every bar (renko or not) has a TOHLCV. If your generator doesn't properly set the Time of each bar, then you can't possibly find which bar starts or crosses midnight.


I don't think he needs the time for each bar. He just needs the bar count (renko bar's) to restart at a pre-given time, each day. The bars after the starting bar would not be time related, only 1 bar each day would be time related.


(note* Though renko can also be based on closing prices and a closing price is time related)

12
To add comments, please log in or register