TicksFromMT5ToTicksFile

Author: Scriptong
Miscellaneous
It issuies visual alerts to the screenUses files from the file system
0 Views
0 Downloads
0 Favorites
TicksFromMT5ToTicksFile
ÿþ//+------------------------------------------------------------------+

//|                                      TicksFromMT5ToTicksFile.mq5 |

//|                                                        Scriptong |

//|                                          http://advancetools.net |

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

#property copyright "Scriptong"

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

#property version   "1.00"



#define LABEL_NAME          "TIFRTOTI_PROGRESS"



#property script_show_inputs



input datetime i_dtStartTime = D'2019.11.01 23:59';     // Copy ticks from



struct TickStruct                                                                                  // !B@C:BC@0 4;O 70?8A8 40==KE >1 >4=>< B8:5

{

   datetime time;

   double   bid;

   double   ask;   

};



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

//| Script program start function                                    |

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

void OnStart()

{

   MqlTick arrstWholeTicks[];

   if (!LoadingTicksData(i_dtStartTime, arrstWholeTicks))

      return;

   

   ObjectDelete(0, LABEL_NAME);

   

   // 5@570?8AL <0AA820 MqlTicks 2 <0AA82 B8:>2

   TickStruct arrstTickStruct[];

   int nTotal = ArraySize(arrstWholeTicks);

   if (ArrayResize(arrstTickStruct, nTotal) != nTotal)

   {

      Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to allocate the memory for TickStruct array. Error: ", GetLastError());

      return;

   }

   for (int i = 0; i < nTotal; ++i)

   {

      arrstTickStruct[i].time = arrstWholeTicks[i].time;

      arrstTickStruct[i].bid = arrstWholeTicks[i].bid;

      arrstTickStruct[i].ask = arrstWholeTicks[i].ask;

   }

   

   // 0?8AL A AB@C:BC@K 2 D09;

   SaveTicksToFile(arrstTickStruct);

}

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

//| 0:0G:0 8AB>@8G5A:8E 40==KE, =0G8=0O A C:070==>3> 2@5<5=8                                                                                                                                         |

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

bool LoadingTicksData(datetime dtLoadTicksFrom, MqlTick &arrstWholeTicks[])

{

   // >@@5:B5= ;8 8=45:A =0G0;L=>3> 10@0?

   ulong ulFromTime = dtLoadTicksFrom * 1000;

   ulong ulCurTime = TimeCurrent() * 1000;



   // >;CG5=85 2A5E B8:>2, =0G8=0O A >B:@KB8O 10@0 nBarIndex

   int nCnt = 0;

   do

   {

      // !G8BK20=85 B8:>2KE 40==KE

      ShowLoadingProgress(ulFromTime);

      MqlTick arrstTick[];

      int nTicksAmount = CopyTicksRange(Symbol(), arrstTick, COPY_TICKS_ALL, ulFromTime, ulCurTime);

//      Print("Cnt: ", nCnt, ", from time: ", datetime(ulFromTime / 1000), ", ticks: ", nTicksAmount);

      int nError = GetLastError();

      if (nTicksAmount < 0)

      {

         Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to receive ticks history. Error: ", nError);

         return false;

      }



      if (nTicksAmount < 2)

      {

         if (nError == 0)

            return true;

            

         int nArraySize = ArraySize(arrstWholeTicks);

         if (nArraySize > 0)

            Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to receive ticks history. Last received time: " + TimeToString(arrstWholeTicks[nArraySize - 1].time_msc / 1000) + ". Error: ", nError);

         else

            Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to receive ticks history. No ticks received. Error: ", nError);

         

         return false;         

      }

   

      // 5@570?8AL 40==KE 2 >1I89 <0AA82

      int nOldArraySize = ArraySize(arrstWholeTicks);

      int nNewArraySize = nOldArraySize + nTicksAmount;

      if (ArrayResize(arrstWholeTicks, nNewArraySize) != nNewArraySize)

      {

         Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to allocate the memory for ticks data. Error: ", GetLastError());

         return false;

      }

      

      for (int i = nOldArraySize; i < nNewArraySize; ++i)

         arrstWholeTicks[i] = arrstTick[i - nOldArraySize];

      

      ulFromTime = arrstTick[nTicksAmount - 1].time_msc;

      nCnt++;

   }

   while (!IsStopped());

   

   return true;

}

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

//| B>1@065=85 ?@>F5AA0 703@C7:8 B8:>2KE 40==KE                                                                                                                                                      |

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

void ShowLoadingProgress(ulong ulTime)

{

   string sText = "Loading ticks data. Processing time: " + TimeToString(ulTime / 1000);

   

   // B>1@065=85 =04?8A8

   ShowLabel(LABEL_NAME, 1, 1, sText, clrBlue);

   ChartRedraw();

}

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

//| B>1@065=85 3@0D8G5A:>3> >1J5:B0 "5B:0"                                                                                                                                                          |

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

void ShowLabel(string sName, int nX, int nY, string sText, color clr, ENUM_BASE_CORNER eCorner = CORNER_RIGHT_UPPER, ENUM_ANCHOR_POINT eAnchor = ANCHOR_RIGHT_UPPER, string sFontName = "Tahoma")

{

   if (ObjectFind(0, sName) < 0)

   {

      ObjectCreate(0, sName, OBJ_LABEL, 0, 0, 0);

      

      ObjectSetInteger(0, sName, OBJPROP_FONTSIZE, 11);

      ObjectSetString(0, sName, OBJPROP_FONT, sFontName);

      ObjectSetInteger(0, sName, OBJPROP_CORNER, eCorner);

      ObjectSetInteger(0, sName, OBJPROP_COLOR, clr);

      ObjectSetInteger(0, sName, OBJPROP_BACK, false);      

      ObjectSetInteger(0, sName, OBJPROP_ANCHOR, eAnchor);

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

      ObjectSetInteger(0, sName, OBJPROP_SELECTABLE, false);

      ObjectSetInteger(0, sName, OBJPROP_HIDDEN, true);

   }

   

   ObjectSetInteger(0, sName, OBJPROP_XDISTANCE, nX);

   ObjectSetInteger(0, sName, OBJPROP_YDISTANCE, nY);

   ObjectSetString(0, sName, OBJPROP_TEXT, sText);

}

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

//| 0?8AL <0AA820 B8:>2 2 D09; tks                                                                                                                                                                   |

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

void SaveTicksToFile(const TickStruct &arrstTicks[])

{

   // !>740=85 D09;0 B8:>2>9 8AB>@88

   int hTicksFile = FileOpen(Symbol() + ".tks", FILE_BIN | FILE_READ | FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE);

   if (hTicksFile < 1)

   {

      Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to create the ticks file. Error: ", GetLastError());

      return;

   }

   

   // 0?8AL D09;0

   int total = ArraySize(arrstTicks), i = 0;

   while (i < total)

   {

      if (FileWriteStruct(hTicksFile, arrstTicks[i]) == 0)

      {

         Alert(MQLInfoString(MQL_PROGRAM_NAME), ": unable to save data to the ticks file. Error: ", GetLastError());

         FileClose(hTicksFile);

         return;

      }

      

      i++;

   }



   FileClose(hTicksFile);

}





Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---