Problem with CopyTime() function

 

Hello

I made an indicator that exports data from mt5 to txt.

The problem occurs when the Internet connection is interrupted.

Message appears -> "Error defining data start, please try again later"

So I think I did something wrong with theCopyTime()  function

I have one additional question. I am a beginner in mt5 and mql5.

To run the indicator I have to place it in the chart window. this can somehow be omitted. Run indicator or EA or something else entirely without a chart window.


Thank you for your answer.

Best regards.

 

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

//-----------------------------------------------------------------------------

#define TIMEFRAME(tf)      StringSubstr ( EnumToString (( ENUM_TIMEFRAMES )tf), 7 )

//-----------------------------------------------------------------------------
string SymbolArray[] = {"GBPUSD", "GBPJPY" , "USDJPY" };     // , "GBPJPY" , "USDJPY"
ENUM_TIMEFRAMES TFArray[] = {1,30,16385};
/*
1 -> M1; 2 -> M2; 3 -> M3; 4 -> M4; 5 -> M5; 6 -> M6; 10 -> M10;  12 -> M12;  15 -> M15;  30 -> M30;
16385 -> H1;   16386 -> H2;   16387 -> H3;   16388 -> H4;   16390 -> H6;   16392 -> H8;   16396 -> H12;  16408 -> D1;   32769 -> W1;   49153 -> MN1;
/**/
#define SymbSize ArraySize(SymbolArray)
#define TFSize ArraySize(TFArray)
static int OldBars[SymbSize][TFSize];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                         |
//+------------------------------------------------------------------+  
  void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| 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[])
  {


for(int Symb=0; Symb<ArraySize(SymbolArray); Symb++)
{
   for(int TF=0; TF<ArraySize(TFArray); TF++)
   {
      if(OldBars[Symb][TF] != Bars(SymbolArray[Symb],TFArray[TF]))
      {

   string FileName = SymbolArray[Symb] + "_" + TIMEFRAME(TFArray[TF]) + ".txt";
   MqlRates rates[];
   datetime DTArray[];

   int bars=Bars(SymbolArray[Symb],TFArray[TF]);


   if(CopyTime(SymbolArray[Symb],TFArray[TF],TimeCurrent(),bars - 1,DTArray)==-1)
     {
      Comment("Error defining data start, please try again later");
     }

   if(CopyRates(SymbolArray[Symb],TFArray[TF],DTArray[0],TimeCurrent(),rates)==-1)
     {
      Comment("Error copying quotes, please try again later");
     }

   int h=FileOpen(FileName,FILE_WRITE|FILE_ANSI|FILE_CSV,";");
   if(h==INVALID_HANDLE)
     {
      Comment("Error opening file");
     }

   FileWrite(h,"Time","High","Low","Close");

   for(int i=1; i<ArraySize(rates); i++)   
     {
      FileWrite(h,rates[i].time,rates[i].high,rates[i].low,rates[i].close);
     }
   FileClose(h);
      
   Comment("File "+FileName+" has been created. "+TimeToString(TimeCurrent(),TIME_SECONDS));



      }
      OldBars[Symb][TF] = Bars(SymbolArray[Symb],TFArray[TF]);
   }
}


   return(rates_total);
  }
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
mkset: The problem occurs when the Internet connection is interrupted.

Does it start working again once the connection is re-established?

 
R4tna C #: Does it start working again once the connection is re-established?

If it worked, it would be great :)

Auto-translation applied by moderator. On the English forum, please write in English.

 
mkset #:

If it worked, it would be great :)

Auto-translation applied by moderator. On the English forum, please write in English.

Do you get the same problem if you put the code inside OnTick() ?

 

I made a few changes and now it resumes operation after the Internet connection is interrupted

 

The first change is the argument of the function CopyTime()

I don't know why but if Bars() exceeds  99999 then CopyTime() returns 1970.01.01 00:00:00

When reconnecting, sometimes Bars() returned 0 or -1

that's why I did it:

   if(Bars(SymbolArray[Symb],TFArray[TF]) >= 100000)  {  BarsStart = 99999;   }
   else if(Bars(SymbolArray[Symb],TFArray[TF]) < 1)   {  BarsStart = 1500;   }
   else   { BarsStart = Bars(SymbolArray[Symb],TFArray[TF]) - 1 ; }

In int OnCalculate()I added:

   if(CopyTime(SymbolArray[Symb],TFArray[TF],BarsStart,1,DTArray)==-1)
     {
      Comment("Error defining data start, please try again later. bars = " + SymbolArray[Symb] + "_" + TIMEFRAME(TFArray[TF]) + " " + Bars(SymbolArray[Symb],TFArray[TF]) + " " + BarsStart);
      ArrayResize(DTArray,1);
      DTArray[0] = TimeCurrent() - 60*5;
      Alert(SymbolArray[Symb] +  TIMEFRAME(TFArray[TF]) + "DTArray[0] = " + DTArray[0]);
     }

This helped when the connection was disconnected and the indicator resumed operation

In void OnTick() this is not necessary, the "indicator" resumes operation itself.


The question remains:

To run the indicator I have to place it in the chart window. this can somehow be omitted. Run indicator or EA or something else entirely without a chart window.

 
Do not hard code constants
ENUM_TIMEFRAMES TFArray[] = {1,30,16385};
Use the proper symbols.
ENUM_TIMEFRAMES TFArray[] = {PERIOD_M1, PERIOD_M30, PERIOD_H1};
 
William Roeder #:
Do not hard code constants
Use the proper symbols.

THX :)