Is it possbile to set a different color for all (trend)lines in the chart at once (maybe a script)? - page 3

 
  1.       datetime Midnight = TimeCurrent() - i*(TimeCurrent()%(PERIOD_D1 * 60));                               // last midnight
          datetime Midnight2 = Midnight - (24 * PERIOD_H1 * 60);                                                // midnight before last midnight
    
    For the first you could also use iTime(NULL, Period_D1, 0). The second is wrong over week end, over holidays. You MUST use iTime()
  2.          ObjectCreate("YHi"+i, OBJ_RECTANGLE, 0, TimeHigh, YHi, TimeCurrent(),BodyHigh);
    
    The first time you create YHi0. The next day you again try to create YHi0 and that fails.
 
datetime Midnight = TimeCurrent() - i*(TimeCurrent()%(PERIOD_D1 * 60));

This will only be correct when i == 1

 

Wow, it seems to be much harder then I thought. But I understand that it is wrong to create the Objects every tick. But how can I do it instead? I read that the start() code is executed on every tick. Where can I create the Objects? Or do I have to check if the Objects exist before creating them? I also have to idea what init() is for. Maybe I have to check for objects or clear all objects in the init() part?


@WHRoeder: I don't really understand how the calculation to find the midnight-bar really works. I googled how to find this particular bar and found that solution. But I have no idea how to correct my error with iTime(). It would be great if you explained me how exactly I find the midnight-bar (or some other specified bar). I don't mean the code, I mean the logic. Unfortunately I really don't understand a little bit what these midnight-formulas are about. I understand the syntax but here again not the logic itself.. What does it mean when something is multiplied with PERIOD_H1 ? 24*PERIOD_H1*60 makes absolutely no sense to me..

What you wrote (iTime(NULL, Period_D1, 0)) to find the last midnight bar is clear to me. But then how many bars do I have to go back to find the midnight before when I don't know which timeframe is used. It is so complicated for a beginner...


Nevertheless I appreciate your help, guys!

 

If you look at the documentation for PERIOD_H1, you will see that it represents an integer that is equal to the chart time-frame in minutes.

So on the H1 chart, it is 60. Multiplying this by 60 will give the seconds in 1 hour, datetime is in seconds, so you need to calculate with seconds.

You could just use 3600, but using PERIOD_H1 makes the code easily readable.

 

Hello GumRai,

thank you, that helped me a lot! I didn't know that datetime is in seconds. That makes many things much more understandable for me.

I still have no idea how to do ti that the lines are not printed every tick but I understand how to find the correct datetime now..

 
mar:

Hello GumRai,

thank you, that helped me a lot! I didn't know that datetime is in seconds. That makes many things much more understandable for me.

I still have no idea how to do ti that the lines are not printed every tick but I understand how to find the correct datetime now..


You know the name of the object, so use ObjectFind() to see if the object already exists.
 

Ok, now I check it before. Is that ok? Now I only have to calculate the correct highs and lows to start the lines. Finding the correct midnights is still a problem for me. Especially when I consider weekends, as WHRoeder said.

if (DrawZones)
      {
        if (ObjectFind("YHi"+i) == -1)
         {
            ObjectCreate("YHi"+i, OBJ_RECTANGLE, 0, TimeHigh, YHi, TimeCurrent(),BodyHigh);
         }
         if (ObjectFind("YLo"+i) == -1)
         {
            ObjectCreate("YLo"+i, OBJ_RECTANGLE, 0, TimeLow, YLo, TimeCurrent(),BodyLow);
         } 
      }
      else
      {
         if (ObjectFind("YHi"+i) == -1)
         {
            ObjectCreate("YHi"+i, OBJ_TREND, 0, TimeHigh, YHi, TimeCurrent(), YHi);
            ObjectSet("YHi"+i, OBJPROP_RAY, false);
         }
         if (ObjectFind("YLo"+i) == -1)
         {
            ObjectCreate("YLo"+i, OBJ_TREND, 0, TimeLow, YLo, TimeCurrent(), YLo);
            ObjectSet("YLo"+i, OBJPROP_RAY, false);
         }
      }
Reason: