Help me with this code please!

 

1. How can I plot consecutive number (text) with each bar? I want to start "1" at 09:00 and then 2, 3, 4,... I try to use for(int i=1;i>=1;i++) but my mt4 just shuts down.

2. How can I keep the numbers position staying at the bars? It keeps moving, updating to the present bar and the previous bar doesn't have number below it.

Here the code. I know it's a mess. I am a beginner so just teach me. Tell me what to do!

p/s: My goal is to create the indicator in the image.

#define HR2400 86400
#define SECONDS uint
SECONDS time(datetime when=0){return SECONDS(when==0?TimeCurrent():when)%HR2400;}
datetime date(datetime when=0){return datetime((when==0?TimeCurrent():when)-time(when));}
bool TradingTime(SECONDS start, SECONDS end, datetime when=0)
   {
    SECONDS now=time(when);
    return start<end?start<=now&&now<end:!TradingTime(end, start, when);
   }
//--------------------------------------------------------------------
bool PlotText(string name, bool del=false, int win=0, datetime dt1=0, double prc1=0, string text=" ", int clr=0, int size=12, string font="Arial", double angle=0, bool bg=false)
   {
    if (del)ObjectDelete(name);
    win = MathMax(win,0);
    if (clr<0)     clr  = White;
    size = MathMax(size,8);
    if (ObjectFind(name) < 0)
    ObjectCreate(name,OBJ_TEXT,win,dt1,prc1);
    ObjectSetText(name,text,size,font,clr);
    ObjectSet(name,OBJPROP_BACK,bg);
    ObjectSet(name,OBJPROP_ANGLE,angle);
    return(true);
   }
int deinit()
   {
    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[])
   {
    #define HR0900 32400
    #define HR1500 54000
    for(int i=1;i>=1;i++)
      {
       if(TradingTime(HR0900,HR1500))
         {
          PlotText("Number",true,0,Time[0],Low[0],IntegerToString(i),clrRed,8,"Arial",0,true);
         }
      }
    return(rates_total);
   }
//+------------------------------------------------------------------+
Files:
EURUSDM5.png  22 kb
 
2TJV: I try to use for(int i=1;i>=1;i++) but my mt4 just shuts down.
          PlotText("Number"
  1. Infinite loop, of course it dies.
  2. You can only create one object of a given name.
 
William Roeder:
  1. Infinite loop, of course it dies.
  2. You can only create one object of a given name.

Hello  William Roeder,

I'm happy when you comment on my thread. You can see this code is from you:

#define HR2400 86400
#define SECONDS uint
SECONDS time(datetime when=0){return SECONDS(when==0?TimeCurrent():when)%HR2400;}
datetime date(datetime when=0){return datetime((when==0?TimeCurrent():when)-time(when));}
bool TradingTime(SECONDS start, SECONDS end, datetime when=0)
   {
    SECONDS now=time(when);
    return start<end?start<=now&&now<end:!TradingTime(end, start, when);
   }

I searched a lot to know how to code limit time from "hh1:mm1" to "hh2:mm2" during the day. Thank you so much!

  1. So.... How can I do to limit the loop? I added if(TradingTime(HR0900,HR1500)) to try to stop when it was 15:00 but like you said: "Infinite loop, of course it dies". How can I start number 1 at 09:00 and go on? And yeah, it will stop at 15:00 then repeat next day. I'm still thinking right now.
  2. If I just can create only one object of a given name, I will have to create a sequence of different names of objects. That means I must add more like this right?

PlotText("Number" + IntegerToString(i);
 
2TJV: So.... How can I do to limit the loop?

Why do you need a loop?

 
William Roeder:

Why do you need a loop?

I don't know! I just want to create a sequence of numbers 1, 2, 3, 4,... at the low of each bar everyday. You can see the image I attached on #1. Truly, I think for() works but it's not. Now I got stuck into it. Do I need to use Array or something?
 

2TJV:
I don't know!

I just want to create a sequence of numbers 1, 2, 3, 4,... at the low of each bar everyday.

  1. Stop creating your own problem.
  2. Each new bar is a new number; no loop required.
 
William Roeder:
  1. Stop creating your own problem.
  2. Each new bar is a new number; no loop required.

But how do I start with number "1" then 2, 3, 4, 5,...?

PlotText("Number",true,0,Time[0],Low[0],????,clrRed,8,"Arial",0,true);
 
if(isNewBar){
   static int count = 0; ++count; 
   Plot
If that is so hard for you, that you couldn't even attempt it, step away from the computer and hire someone; you're wasting everyone's time including your own.
 
William Roeder:
If that is so hard for you, that you couldn't even attempt it, step away from the computer and hire someone; you're wasting everyone's time including your own.

Thanks for your advice! Have a nice day!

Reason: