How to code run commands only once?

 

Hi...

I have a custom indicator that shows me MACD and MA, but I want to shows me on H4 timeframe when I attach it to the chart. How I do that ?

I tried using OnTimer but somehow if I change to other timeframe, it will back to H4 again.


Thanks in advance


Here are my lines

//-------------------------------------------
// Indicator properties
//-------------------------------------------
#property strict
#property indicator_chart_window
#property indicator_buffers 4

//-------------------------------------------
// Declaring arrays for indicator buffers
//-------------------------------------------
// Envelopes
double bufEnvUpper[];
double bufEnvMA[];
double bufEnvLower[];

// Moving Average
double bufMA[];

//-------------------------------------------
// Declaring global variables
//-------------------------------------------
bool FirstTime = TRUE;

//-------------------------------------------
// Input parameters
//-------------------------------------------
// Envelopes
extern string Settings100             = "----- Envelope ------------------"; // Settings for ------------------------
extern int envPeriod                  = 120;                                 // Period (Default: 120)
extern int envShift                   = 0;                                   // Shift (Default: 0)
extern ENUM_MA_METHOD envMAMethod     = 1;                                   // MA Method (Default: 1-Exponential)
extern ENUM_APPLIED_PRICE envApply    = 0;                                   // Close Price (Default: 0-Close)
extern double envDeviation            = 0.15;                                // Deviation in decimal (Default: 0.15)
extern ENUM_LINE_STYLE envStyleUpper  = 0;                                   // Style of Upper Envelopes (Default: 0-Solid)
extern ENUM_LINE_STYLE envStyleMA     = 2;                                   // Style of MA Envelopes (Default: 2-Dot)
extern ENUM_LINE_STYLE envStyleLower  = 0;                                   // Style of Lower Envelopes (Default: 0-Solid)
extern int envWidthUpper              = 2;                                   // Width of Upper Envelopes (Default: 2)
extern int envWidthMA                 = 1;                                   // Width of MA Envelopes (Default: 1)
extern int envWidthLower              = 2;                                   // Width of Lower Envelopes (Default: 2)
extern color envColorUpper            = Lime;                                // Color of Upper Envelopes (Default: Lime)
extern color envColorMA               = C'0,231,105';                        // Color of MA Envelopes (Default: RGB(0,231,105))
extern color envColorLower            = DarkTurquoise;                       // Color of Lower Envelopes (Default: DarkTurquoise)

// Moving Average
extern string Settings200             = "----- Moving Average ------------"; // Settings for ------------------------
extern int maPeriod                   = 24;                                  // Period (Default: 24)
extern int maShift                    = 0;                                   // Shift (Default: 0)
extern ENUM_MA_METHOD maMAMethod      = 1;                                   // MA Method (Default: 1-Exponential)
extern ENUM_APPLIED_PRICE maApply     = 0;                                   // Close Price (Default: 0-Close)
extern ENUM_LINE_STYLE maStyle        = 0;                                   // Style of MA (Default: 0-Solid)
extern int maWidth                    = 2;                                   // Width of MA (Default: 2)
extern color maColor                  = Olive;                               // Color of MA (Default: Olive)

// Spread
extern string Settings300             = "----- Spread --------------------"; // Settings for ------------------------
extern int spreadFontSize             = 30;                                  // Font size
extern string spreadFontName          = "Impact";                            // Font name
extern ENUM_ANCHOR_POINT spreadAnchor = 4;                                   // Anchor (Default: 4-Right lower)
extern color spreadColor              = C'128,128,128';                      // Text color

// Others
extern string Settings999             = "----- Others --------------------"; // Settings for ------------------------
extern ENUM_CHART_MODE chartMode      = 1;                                   // Chart Mode (Default: 1-Candlebar)
extern ENUM_TIMEFRAMES chartTimeFrame = 240;                                 // Chart time frame (Default: 240-H4)
extern ENUM_BASE_CORNER labelCorner   = 3;                                   // Text corner (Default: 3-Right lower)
extern int labelXCoord                = 50;                                  // Text X-coordinate
extern int labelYCoord                = 50;                                  // Text Y-coordinate

//+======================================================================+
//| Initialization function                                              |
//+======================================================================+
int init()
  {
   // Envelopes
   SetIndexBuffer(0, bufEnvUpper);
   SetIndexStyle (0, DRAW_LINE, envStyleUpper, envWidthUpper, envColorUpper);
   SetIndexBuffer(1, bufEnvMA);
   SetIndexStyle (1, DRAW_LINE, envStyleMA, envWidthMA, envColorMA);
   SetIndexBuffer(2, bufEnvLower);
   SetIndexStyle (2, DRAW_LINE, envStyleLower, envWidthLower, envColorLower);
   
   // Moving Average
   SetIndexBuffer(3, bufMA);
   SetIndexStyle (3, DRAW_LINE, maStyle, maWidth, maColor);
   
   return(0);
  }

//+======================================================================+
//| Start function                                                       |
//+======================================================================+
int start()
  {
   //-------------------------------------------
   // Labels for indicators
   //-------------------------------------------
   // Envelopes
   string envLabelMAMethode;
   string envLabelApply;
   switch(envMAMethod)
     {
      case MODE_SMA  : envLabelMAMethode = "Sipl"; break;
      case MODE_EMA  : envLabelMAMethode = "Expn";  break;
      case MODE_SMMA : envLabelMAMethode = "Smtd"; break;
      case MODE_LWMA : envLabelMAMethode = "Liwe"; break;
     }
   switch(envApply)
     {
      case 0 : envLabelApply = "Clse"; break;
      case 1 : envLabelApply = "Open"; break;
      case 2 : envLabelApply = "High"; break;
      case 3 : envLabelApply = "Low"; break;
      case 4 : envLabelApply = "Mdin"; break;
      case 5 : envLabelApply = "Typc"; break;
      case 6 : envLabelApply = "Weig"; break;
     }
   SetIndexLabel(0, "Env(" + IntegerToString(envPeriod) + ", " + "Upper" + ")");
   SetIndexLabel(1, "Env(" +
                     IntegerToString(envPeriod) + ", " + 
                     IntegerToString(envShift) + ", " + 
                     DoubleToString(envDeviation, 2) + ", " + 
                     envLabelMAMethode +  ", " + 
                     envLabelApply + ")");
   SetIndexLabel(2, "Env(" + IntegerToString(envPeriod) + ", " + "Lower" + ")");
   
   // Moving Average
   string maLabelMAMethod;
   string maLabelApply;
   switch(maMAMethod)
     {
      case MODE_SMA  : maLabelMAMethod = "Sipl"; break;
      case MODE_EMA  : maLabelMAMethod = "Expn";  break;
      case MODE_SMMA : maLabelMAMethod = "Smtd"; break;
      case MODE_LWMA : maLabelMAMethod = "Liwe"; break;
     }
   switch(maApply)
     {
      case 0 : maLabelApply = "Clse"; break;
      case 1 : maLabelApply = "Open"; break;
      case 2 : maLabelApply = "High"; break;
      case 3 : maLabelApply = "Low"; break;
      case 4 : maLabelApply = "Mdin"; break;
      case 5 : maLabelApply = "Typc"; break;
      case 6 : maLabelApply = "Weig"; break;
     }
   SetIndexLabel(3, "MA(" + 
                     IntegerToString(maPeriod) + ", " +
                     IntegerToString(maShift) + ", " +
                     maLabelMAMethod + ", " +
                     maLabelApply + ")");
   
   //-------------------------------------------
   // Draw indicators
   //-------------------------------------------
   int i, CountedBars;
   CountedBars = IndicatorCounted();
   i = Bars - CountedBars- 1;
   while (i >= 0)
     {
      // Envelopes
      bufEnvUpper[i] = iEnvelopes(NULL, 0, envPeriod, envMAMethod, envShift, envApply, envDeviation, MODE_UPPER, i);
      bufEnvMA[i]    = iEnvelopes(NULL, 0, envPeriod, envMAMethod, envShift, envApply, envDeviation, MODE_MAIN, i);
      bufEnvLower[i] = iEnvelopes(NULL, 0, envPeriod, envMAMethod, envShift, envApply, envDeviation, MODE_LOWER, i);
      
      // Moving Average
      bufMA[i] = iMA(NULL, 0, maPeriod, maShift, maMAMethod, maApply, i);
      
      i--;
     }
   
   //-------------------------------------------
   // Draw text labels
   //-------------------------------------------
   // Spread
   int width, height;
   int totalh = 0;
   ObjectCreate(0, "SPREAD", OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, "SPREAD", OBJPROP_XDISTANCE, labelXCoord);
   ObjectSetInteger(0, "SPREAD", OBJPROP_YDISTANCE, labelYCoord);
   ObjectSetInteger(0, "SPREAD", OBJPROP_CORNER, labelCorner);
   ObjectSetString(0, "SPREAD", OBJPROP_TEXT, IntegerToString(MarketInfo(NULL, MODE_SPREAD)));
   ObjectSetString(0, "SPREAD", OBJPROP_FONT, spreadFontName);
   ObjectSetInteger(0, "SPREAD", OBJPROP_FONTSIZE, spreadFontSize);
   ObjectSetInteger(0, "SPREAD", OBJPROP_ANCHOR, spreadAnchor);
   ObjectSetInteger(0, "SPREAD", OBJPROP_COLOR, spreadColor);
   TextGetSize("SPREAD", width, height);
   totalh += height;
   
   ChartSetInteger(0, CHART_MODE, 1);
   if(FirstTime)
     {
      EventSetTimer(2);
     }
   
   return(0);
  }

//+======================================================================+
//| OnTimer() function                                                   |
//+======================================================================+

void OnTimer()
  {
   ChartSetSymbolPeriod(0, NULL, chartTimeFrame);
   FirstTime = FALSE;
  }
//+======================================================================+