Draw lines only in current timeframe

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
6
Rogh 2015.05.28 16:29 #
 
paulepanke:

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:

edit: removed the check for REASON_CHARTCHANGE

/**
 * 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);
}

Great indicator, it's really useful.  I'm not a programmer and have been trying to modify it so it only works on HLines. Unfortunately at the moment if a rectangle is drawn then it only shows on the current TF. I'd be greatful if someone who knows what theyre doing could modifly the above to apply only to HLines so that Tlines and Rectangles etc are unaffected and show on all time frames. Many thanks.

 

avatar
6
Rogh 2015.05.28 17:44 #
 
Rogh:

Great indicator, it's really useful.  I'm not a programmer and have been trying to modify it so it only works on HLines. Unfortunately at the moment if a rectangle is drawn then it only shows on the current TF. I'd be greatful if someone who knows what theyre doing could modifly the above to apply only to HLines so that Tlines and Rectangles etc are unaffected and show on all time frames. Many thanks.

 

//+-----------------------------SCRIPT CODE--------------------------+
int start()
  { 
ObjectCreate("HLine", OBJ_HLINE , 0,Time[0], Close[0]);
ObjectSet("HLine", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("HLine", OBJPROP_COLOR, MediumOrchid);
ObjectSet("HLine", OBJPROP_WIDTH, 0);
ObjectSet("HLine", OBJPROP_TIMEFRAMES, OBJ_PERIOD_M15 | OBJ_PERIOD_M5 | OBJ_PERIOD_M1);
  }
//+------------------------------------------------------------------+
Hi again, I've actually come up with a work around myself. The above script draws a HLine with the required parameters and time-frames. I've modified it for 3 different line types and applied hot key to each. Hope someone finds it useful.
Back to topics list   | 1 2  

To add comments, please log in or register