CandleDuration

Author: Scriptong
0 Views
0 Downloads
0 Favorites
CandleDuration
ÿþ#property copyright "Scriptong"

#property link      "http://advancetools.net"

#property description   "English: Displays the time from the opening of one candle to the opening of the next candle. Actually for charts which independents of time.\nRussian: B>1@0605B 2@5<O >B >B:@KB8O >4=>9 A25G8 4> >B:@KB8O A;54CNI59 A25G8. :BC0;L=> 4;O =5@02=>2@5<5==KE 3@0D8:>2."

#define VERSION "190.817"

#property version VERSION

#property strict



#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 clrDodgerBlue





enum ENUM_TIME_FORMAT

{

   TIME_FORMAT_NONE,                                                                               // Not required / 5 B@51C5BAO

   TIME_FORMAT_SECONDS,                                                                            // Seconds / !5:C=4K   

   TIME_FORMAT_MINUTES,                                                                            // Minutes / 8=CBK   

   TIME_FORMAT_HOURS,                                                                              // Hours / '0AK

   TIME_FORMAT_DAYS,                                                                               // Days / =8

   TIME_FORMAT_MM_SS,                                                                              // MM:SS / :!!

   TIME_FORMAT_HH_MM_SS,                                                                           // HH:MM:SS / ''::!!

   TIME_FORMAT_DD_HH                                                                               // DD HH:MM /  '':   

};





// 0AB@>5G=K5 ?0@0<5B@K 8=48:0B>@0

input ENUM_TIME_FORMAT  i_eTimeFormat                    = TIME_FORMAT_NONE;                       // Time format / $>@<0B 2@5<5=8

input color             i_clrTextColor                   = clrBlue;                                // Color of text / &25B B5:AB0

input string            i_strTimeOfFridayEnd             = "23:55";                                // End of trades in friday (HH:MM) / >=5F B>@3>2 2 ?OB=8FC ('':)

input int               i_nIndBarsCount                  = 1000;                                   // >;8G5AB2> 10@>2 >B>1@065=8O





#define  PREFIX               "CADU_"

#define  SECONDS_IN_DAY        uint(60 * 60 * 24)





bool  g_bIsActivate,

      g_bIsRussianLanguage;



int   g_nPrevCalculated;                                                                           // >;-2> ?>AG8B0==KE 10@>2 =0 ?@54K4CI5< B8:5. C1;8@C5B 8<5NICNAO ?5@5<5==CN B5@<8=0;0, B. :...

                                                                                                   // ..=0 >DD;09=-3@0D8:0E G5@57 >48= B8: ?@>8AE>48B A1@>A 25;8G8=K ?>AG8B0==KE 10@>2    



uint  g_uFridayEndTime;



double g_farrTimeBuffer[];

                                       

string g_strProgramName;

                                                                                                   

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Custom indicator initialization function                                                                                                                                                          |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

int OnInit()

{

   Print("Version: ", VERSION);

   g_bIsActivate = false;

   g_strProgramName = MQLInfoString(MQL_PROGRAM_NAME);

   g_bIsRussianLanguage = (TerminalInfoString(TERMINAL_LANGUAGE) == "Russian");

 

   if (!TunningParameters())

      return INIT_FAILED;

 

   if (!BuffersBind())                                                                        

      return INIT_FAILED; 

      

   g_bIsActivate = true;

   return INIT_SUCCEEDED;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Custom indicator deinitialization function                                                                                                                                                        |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0, PREFIX);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| 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[])

{

   if (!g_bIsActivate)

      return rates_total;

      

   int nTotal;   

   int nLimit = GetRecalcIndex(nTotal, rates_total, g_nPrevCalculated);                                



   if (!ShowIndicatorData(nLimit, nTotal))

      g_bIsActivate = false;

      

   g_nPrevCalculated = rates_total;



   return rates_total;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @>25@:0 :>@@5:B=>AB8 7=0G5=89 =0AB@>5G=KE ?0@0<5B@>2                                                                                                                                             |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool TunningParameters()

{

   //  07182:0 AB@>:8 =0 425 A>AB02;ONI85

   string strarrData[];

   int nStringsCnt = StringSplit(i_strTimeOfFridayEnd, StringGetCharacter(":", 0), strarrData);

   if (nStringsCnt < 2)

   {

      Alert(g_strProgramName, (g_bIsRussianLanguage)? ": =5?@028;L=K9 D>@<0B AB@>:8 ?0@0<5B@0 \">=5F B>@3>2 2 ?OB=8FC\". #:068B5 @0745;8B5;L G0A>2 8 <8=CB. =48:0B>@ >B:;NG5=." : 

                                                      ": invalid format of string parameter \"End of trades in friday\". Separator required. Indicator is turned off.");

      return false;

   }

   

   if (nStringsCnt > 2)

   {

      Alert(g_strProgramName, (g_bIsRussianLanguage)? ": ?0@0<5B@ \">=5F B>@3>2 2 ?OB=8FC\" 4>;65= A>45@60BL B>;L:> >48= @0745;8B5;L. =48:0B>@ >B:;NG5=." : 

                                                      ": parameter \"End of trades in friday\" must consists only one separator. Indicator is turned off.");

      return false;

   }

   

   // @5>1@07>20=85 ?>;CG5==KE 42CE AB@>: 2 G8A;0

   int nHours = int(StringToInteger(strarrData[0]));

   if (nHours < 0 || nHours > 24)

   {

      Alert(g_strProgramName, (g_bIsRussianLanguage)? ": 7=0G5=85 G0A>2 2 ?0@0<5B@5 \">=5F B>@3>2 2 ?OB=8FC\" ?@52KH05B 24. =48:0B>@ >B:;NG5=." : 

                                                      ": value of hours in parameter \"End of trades in friday\" exceeded the 24. Indicator is turned off.");

      return false;

   }

   

   int nMinutes = int(StringToInteger(strarrData[1]));

   if (nMinutes < 0 || nMinutes > 59 || (nHours == 24 && nMinutes > 0))

   {

      Alert(g_strProgramName, (g_bIsRussianLanguage)? ": 7=0G5=85 <8=CB 2 ?0@0<5B@5 \">=5F B>@3>2 2 ?OB=8FC\" ?@52KH05B 59. =48:0B>@ >B:;NG5=." : 

                                                      ": value of minutes in parameter \"End of trades in friday\" exceeded the 59. Indicator is turned off.");

      return false;

   }

   

   g_uFridayEndTime = nHours * 3600 + nMinutes * 60;

   

   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| !2O7K20=85 1CD5@>2 8=48:0B>@0 A <0AA820<8                                                                                                                                                         |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool BuffersBind()

{

   if (!SetIndexBuffer(0, g_farrTimeBuffer))       

   {

      Alert(g_strProgramName, (g_bIsRussianLanguage)? ": >H81:0 A2O7K20=8O <0AA82>2 A 1CD5@0<8 8=48:0B>@0. H81:0 !" + IntegerToString(GetLastError()) : 

                                                      ": error of binding the arrays with indicator buffers. Error !" + IntegerToString(GetLastError()));

      return false;

   }



   SetIndexStyle(0, DRAW_HISTOGRAM);

   SetIndexEmptyValue(0, 0.0);

      

   SetIndexLabel(0, "Duration of candle");

   

   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Determination of bar index which needed to recalculate                                                                                                                                            |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

int GetRecalcIndex(int &nTotal, const int nRatesTotal, const int nPrevCalculated)

{

   nTotal = nRatesTotal - 1;                                                                         

                                                   

   if (i_nIndBarsCount > 0 && i_nIndBarsCount < nTotal)

      nTotal = MathMin(i_nIndBarsCount, nTotal);                      

                                                   

   if (nPrevCalculated < nRatesTotal - 1)                     

   {       

      ObjectsDeleteAll(0, PREFIX);

      return nTotal;

   }

   

   return MathMin(nRatesTotal - nPrevCalculated, nTotal);                            

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Displaying of indicators values                                                                                                                                                                   |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool ShowIndicatorData(int nLimit, int nTotal)

{

   for (int i = nLimit; i >= 0; i--)

      if (!ProcessBar(i))

         return false;



   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Process the specified bar                                                                                                                                                                         |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool ProcessBar(int nBarIndex)

{

   MqlRates starrRates[];

   if (CopyRates(Symbol(), PERIOD_CURRENT, nBarIndex, 1, starrRates) < 0)

      return true;



   // @5<O D>@<8@>20=8O 10@0 2 A5:C=40E

   ulong ulBarDuration = GetBarDuration(nBarIndex, starrRates[0].time);

   if (ulBarDuration == ULONG_MAX)

      return true;



   // B>1@065=85 ?@>4>;68B5;L=>AB8

   if (i_eTimeFormat != TIME_FORMAT_NONE)

      ShowText(PREFIX + IntegerToString(starrRates[0].time), starrRates[0].time, starrRates[0].high, " " + FormatTime(ulBarDuration));

      

   g_farrTimeBuffer[nBarIndex] = double(ulBarDuration);

   

   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| ?@545;5=85 2@5<5=8 ACI5AB2>20=8O A25G8 2 A5:C=40E                                                                                                                                                |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

ulong GetBarDuration(int nBarIndex, datetime dtCurBarTime)

{

   // 0@ O2;O5BAO B5:CI8<

   if (nBarIndex == 0)

      return ulong(TimeCurrent() - dtCurBarTime);



   // !?@020 >B 10@0 5ABL 4@C385 10@K

   datetime dtNextBarTime[];

   if (CopyTime(Symbol(), PERIOD_CURRENT, nBarIndex - 1, 1, dtNextBarTime) < 0)

      return ULONG_MAX;

      

   if (TimeDayOfWeek(dtCurBarTime) == 5 && TimeDayOfWeek(dtNextBarTime[0]) != 5)

      return ulong(MathMax(g_uFridayEndTime - uint(dtCurBarTime % SECONDS_IN_DAY) + uint(dtNextBarTime[0] % SECONDS_IN_DAY), 0));

   

   return ulong(dtNextBarTime[0] - dtCurBarTime);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| B>1@065=85 B5:AB>2>3> 3@0D8G5A:>3> M;5<5=B0                                                                                                                                                      |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void ShowText(string strName, datetime dtTime, double fPrice, string strText)

{

   if (ObjectFind(0, strName) < 0)

   {

      ObjectCreate(0, strName, OBJ_TEXT, 0, dtTime, fPrice);

      ObjectSetString(0, strName, OBJPROP_FONT, "Tahoma"); 

      ObjectSetInteger(0, strName, OBJPROP_FONTSIZE, 10); 

      ObjectSetInteger(0, strName, OBJPROP_ANCHOR, ANCHOR_LEFT);

      ObjectSetInteger(0, strName, OBJPROP_COLOR, i_clrTextColor);

      ObjectSetDouble(0, strName, OBJPROP_ANGLE, 90.0);

      ObjectSetString(0, strName, OBJPROP_TEXT, strText); 

      ObjectSetString(0, strName, OBJPROP_TOOLTIP, "\n");

      return;

   }

   

   ObjectSetString(0, strName, OBJPROP_TEXT, strText); 

   ObjectMove(0, strName, 0, dtTime, fPrice);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @82545=85 2@5<5=8, 2K@065==>3> 2 A5:C=40E, : AB@>:>2><C ?@54AB02;5=8N 7040==>3> D>@<0B0                                                                                                          |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

string FormatTime(ulong ulSeconds)

{

   switch (i_eTimeFormat)

   {

      case TIME_FORMAT_MINUTES:     return IntegerToString(ulSeconds / 60) + " min";

      case TIME_FORMAT_HOURS:       return IntegerToString(ulSeconds / 3600) + " hours";

      case TIME_FORMAT_DAYS:        return IntegerToString(ulSeconds / SECONDS_IN_DAY) + " days";

      case TIME_FORMAT_MM_SS:       return LeadZero(ulSeconds / 60) + ":" + LeadZero(ulSeconds % 60);

      case TIME_FORMAT_HH_MM_SS:    {

                                       ulong ulHours = ulSeconds / 3600;

                                       return LeadZero(ulHours) + ":" + LeadZero((ulSeconds - ulHours * 3600) / 60) + ":" + LeadZero(ulSeconds % 60);

                                    }

      

      case TIME_FORMAT_DD_HH:       {

                                       ulong ulDays = ulSeconds / SECONDS_IN_DAY;

                                       ulong ulHours = (ulSeconds - ulDays * SECONDS_IN_DAY) / 3600;

                                       ulong ulMinutes = (ulSeconds - ulDays * SECONDS_IN_DAY - ulHours * 3600) / 60;

                                       return IntegerToString(ulDays) + "d " + LeadZero(ulHours) + ":" + LeadZero(ulMinutes) + ":" + LeadZero(ulSeconds % 60);

                                    }

   }

   

   return LeadZero(ulSeconds) + " sec";

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @5>1@07>20=85 F5;>3> G8A;0 2 AB@>:C A 4>102;5=85< 254CI53> =C;O 4;O G8A5;, <5=LH8E 10                                                                                                            |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

string LeadZero(ulong ulValue)

{

   if (ulValue > 9)

      return IntegerToString(ulValue);

      

   return "0" + IntegerToString(ulValue);

}

Comments