Download MetaTrader 5

Draw lines only in current timeframe

To add comments, please log in or register
fxrmp
23
fxrmp  

Hi there,

in MQL5 it is very easy to fetch a new drawn line and set it's period to the current one. I wonder if it works in mql4 too and like to ask you how?

Maybe someone has done it before. The target is to keep every drawn object in the current timeframe and the drawn lines in the current and lower timeframes.

Simon Gniadkowski
Moderator
18042
Simon Gniadkowski  
fxrmp
23
fxrmp  
Yee. The crux is how to get the last created / modified object.
whroeder1
16336
whroeder1  
fxrmp:
Yee. The crux is how to get the last created / modified object.
Remember the name you used to create it.
fxrmp
23
fxrmp  

Err. They get some unique name as they were created but no name by the user.

I just like to get the last created object (which will get some unique name).

Or some possibility to iterate over every object in the chart.

dabbler
1102
dabbler  
fxrmp:

Err. They get some unique name as they were created but no name by the user.

I just like to get the last created object (which will get some unique name).

Or some possibility to iterate over every object in the chart.

AFAIK there is no way to iterate over objects on a chart (other than trying every possible permutation of characters as a name) and even if you did there is no "time/date of creation" attribute. If you are not creating them, and you have no algorithm for their naming convention, then you will have to think again.
Paule Panke
123
Paule Panke  
dabbler:
AFAIK there is no way to iterate over objects on a chart (other than trying every possible permutation of characters as a name) ...
string myName = "remember the name you gave at creation";

for (int i=ObjectsTotal()-1; i >= 0; i--) {
   if (ObjectName(i) == myName) {
      ObjectSet(ObjectName(i), OBJPROP_TIMEFRAMES, OBJ_PERIOD_M1 | OBJ_PERIOD_M5 | OBJ_PERIOD_M15);
   }
}
there is. just an example. it's recommended but not necessary to iterate in reverse direction. why? an object might be deleted in another thread while you're iterating here.
fxrmp
23
fxrmp  

Thank you paulepanke.

I got some code now ( http://pastebin.com/nYRYCWMi ), but as I see, it only updates per tick and has some other issues that can be solved by using a timeout. But as I want to do it as described ( https://book.mql4.com/special/index ) the MT4 hung up in an endless while without accepting anything.

How can I get such a timeout function without having an endless while that crashes the mt4 ?

Simon Gniadkowski
Moderator
18042
Simon Gniadkowski  
fxrmp:

Thank you paulepanke.

I got some code now ( http://pastebin.com/nYRYCWMi ), but as I see, it only updates per tick and has some other issues that can be solved by using a timeout. But as I want to do it as described ( https://book.mql4.com/special/index ) the MT4 hung up in an endless while without accepting anything.

How can I get such a timeout function without having an endless while that crashes the mt4 ?

If you are adding an endless while into a script you can add a sleep() in the loop so that it doesn't run hell for leather all the time, instead it occasionally takes a rest (sleep)
Paule Panke
123
Paule Panke  

don't call it onTick(), you only want it to do something when you change timeframes so deinit() is the right place for you.

you would use something like this:

/**
 * MyIndicator
 */
#property indicator_chart_window

#define ERR_NO_ERROR 0


/**
 * Initialization
 *
 * @return int - error status
 */
int init() {
   return(0);
}


/**
 * Deinitialization
 *
 * @return int - error status
 */
int deinit() {
   string label;
   int type, flags, current=PeriodToFlag(), objects=ObjectsTotal();
   GetLastError();                                          // reset last error

   for (int i=objects-1; i>=0; i--) {
      label = ObjectName(i);
      if (GetLastError() != ERR_NO_ERROR)                   // skip index on threading issues
         continue;

      // skip already processed objects
      flags = ObjectGet(label, OBJPROP_TIMEFRAMES) +0.1;    // (int)(double) int
      if (flags == 0) {

         // all objects: visible in creation timeframe
         ObjectSet(label, OBJPROP_TIMEFRAMES, current);

         type = ObjectType(label);
         if (type==OBJ_TREND || type==OBJ_HLINE || type==OBJ_VLINE) {
            // lines: visible in lower timeframes too
            ObjectSet(label, OBJPROP_TIMEFRAMES, current|(current-1));
         }
      }
   }
   return(0);
}


/**
 * Main function
 *
 * @return int - error status
 */
int start() {
   return(0);
}


/**
 * Convert a chart period identifier into an object visibility flag.
 *
 * @param  int period - period id (default: current chart period id)
 *
 * @return int - visibility flag
 */
int PeriodToFlag(int period=NULL) {
   if (period == NULL)
      period = Period();

   switch (period) {
      case PERIOD_M1:  return(OBJ_PERIOD_M1 );
      case PERIOD_M5:  return(OBJ_PERIOD_M5 );
      case PERIOD_M15: return(OBJ_PERIOD_M15);
      case PERIOD_M30: return(OBJ_PERIOD_M30);
      case PERIOD_H1:  return(OBJ_PERIOD_H1 );
      case PERIOD_H4:  return(OBJ_PERIOD_H4 );
      case PERIOD_D1:  return(OBJ_PERIOD_D1 );
      case PERIOD_W1:  return(OBJ_PERIOD_W1 );
      case PERIOD_MN1: return(OBJ_PERIOD_MN1);
   }
   return(0);
}

edit: removed the check for REASON_CHARTCHANGE

fxrmp
23
fxrmp  

Of course! That's so easy :D Deinit is such the right one. Thanks for that!

I like your design of code! :) What other languages do you know?

12
To add comments, please log in or register