Export_Symbol_To_MT5

Author: avoitenko
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each barSeries array that contains tick volumes of each bar
Miscellaneous
Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
Export_Symbol_To_MT5
ÿþ//+------------------------------------------------------------------+

//|                                         Export_Symbol_To_MT5.mq4 |

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

#property copyright  "avoitenko"

#property link       "https://www.mql5.com/en/users/avoitenko"

#property version    "1.10"

#property strict



#include <Arrays\ArrayString.mqh>



#ifdef __MQL5__

#define Ask SymbolInfoDouble(_Symbol,SYMBOL_ASK)

#define Bid SymbolInfoDouble(_Symbol,SYMBOL_BID)

#endif



//---

uint spread=0;

int file_ticks;

int file_bars;

int file_config;

datetime saved_tick_time=0;

datetime saved_bar_time=0;

int saved_tick_msc=0;

int saved_bars=0;

CArrayString err_list;

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

int OnInit()

  {

   err_list.Clear();



//---

   if(!MQLInfoInteger(MQL_TESTER))

     {

      err_list.Add("Run only in strategy tester!");

     }



//---

   if(PeriodSeconds()!=PeriodSeconds(PERIOD_M1))

     {

      err_list.Add("Use M1 timeframe!");

     }



//---

   spread=(int)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);



//---

   if(err_list.Total()==0)

     {

      string filename_ticks=_Symbol+"_ticks.csv";

      file_ticks=FileOpen(filename_ticks,FILE_WRITE|FILE_TXT|FILE_ANSI|FILE_SHARE_READ);

      if(file_ticks==INVALID_HANDLE)

        {

         err_list.Add("Error creating '"+filename_ticks+"' file, error: "+(string)_LastError);

        }

      //---

      string filename_bars=_Symbol+"_bars.csv";

      file_bars=FileOpen(filename_bars,FILE_WRITE|FILE_TXT|FILE_ANSI|FILE_SHARE_READ);

      if(file_bars==INVALID_HANDLE)

        {

         err_list.Add("Error creating '"+filename_bars+"' file, error: "+(string)_LastError);

        }

      //---

      string filename_config=_Symbol+"_config.json";

      file_config=FileOpen(filename_config,FILE_WRITE|FILE_TXT|FILE_UNICODE|FILE_SHARE_READ);

      if(file_config==INVALID_HANDLE)

        {

         err_list.Add("Error creating '"+filename_config+"' file, error: "+(string)_LastError);

        }

     }



//--- print errors

   if(err_list.Total()>0)

     {

      for(int i=0;i<err_list.Total();i++)

         Print(err_list.At(i));

     }



//---

   return(INIT_SUCCEEDED);

  }

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

void OnDeinit(const int reason)

  {

//--- if error then exit

   if(err_list.Total()!=0)

      return;



   SaveBar(0);

   SaveConfig();

//---

   FileClose(file_ticks);

   FileClose(file_bars);

   FileClose(file_config);

  }

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

void OnTick()

  {



//--- if error then exit

   if(err_list.Total()!=0)

      return;



//--- TICKS

   bool first_line=(saved_tick_time==0);

   datetime tick_time=TimeCurrent();



   int tick_msc=0;

   if(tick_time==saved_tick_time)

     {

      tick_msc=++saved_tick_msc;

     }

   else

      saved_tick_msc=0;

   saved_tick_time=TimeCurrent();



//---

   string line=TimeToString(tick_time,TIME_DATE|TIME_SECONDS)+"."+StringFormat("%03d",tick_msc)+

               "\t"+

               DoubleToString(Bid,_Digits)+

               "\t"+

               DoubleToString(Ask,_Digits);



//---   

   if(!first_line)

      line="\n"+line;

   FileWriteString(file_ticks,line);



//---

   SaveBar();

  }

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

void SaveBar(const int bar=1)

  {

   if(saved_bar_time!=iTime(NULL,0,bar))

     {

      if(saved_bar_time>0)

        {

         string _line=TimeToString(iTime(NULL,0,bar),TIME_DATE|TIME_SECONDS)+"\t"+

                      DoubleToString(iOpen(NULL,0,bar),_Digits)+"\t"+

                      DoubleToString(iHigh(NULL,0,bar),_Digits)+"\t"+

                      DoubleToString(iLow(NULL,0,bar),_Digits)+"\t"+

                      DoubleToString(iClose(NULL,0,bar),_Digits)+"\t"+

                      IntegerToString(iVolume(NULL,0,bar))+"\t"+

                      (string)0+"\t"+

                      IntegerToString(spread);



         if(saved_bars!=0)

            _line="\n"+_line;



         FileWriteString(file_bars,_line);

         saved_bars++;

        }

      saved_bar_time=iTime(NULL,0,bar);

     }

  }

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

string GetSpace(const int row,const int characters=3)

  {

   int total=row*characters;

   string space=NULL;

   StringInit(space,total,' ');

   return space;

  }

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

void SaveConfig()

  {

   string S1=GetSpace(1);

   string S2=GetSpace(2);

   string S3=GetSpace(3);

//---   

   string _line="{"+

                "\n"+S1+"\"ConfigSymbols\":["+

                "\n"+S2+"{"+

                "\n"+S3+"\"Symbol\":\""+_Symbol+"\","+

                "\n"+S3+"\"Path\":\"Custom\\\\"+_Symbol+"\","+

                "\n"+S3+"\"CurrencyBase\":\""+SymbolInfoString(_Symbol,SYMBOL_CURRENCY_BASE)+"\","+

                "\n"+S3+"\"CurrencyProfit\":\""+SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT)+"\","+

                "\n"+S3+"\"CurrencyMargin\":\""+SymbolInfoString(_Symbol,SYMBOL_CURRENCY_MARGIN)+"\","+

                "\n"+S3+"\"ColorBackground\":\""+(string)(int)clrSalmon+"\","+

                "\n"+S3+"\"Digits\":\""+(string)_Digits+"\","+

                "\n"+S3+"\"Point\":\""+DoubleToString(_Point,_Digits)+"\","+

                "\n"+S3+"\"TickFlags\":\"3\","+

                "\n"+S3+"\"TickBookDepth\":\"10\","+

                "\n"+S3+"\"TickChartMode\":\"0\","+

                "\n"+S3+"\"TradeMode\":\"4\","+

                "\n"+S3+"\"TradeFlags\":\"2\","+

                "\n"+S3+"\"CalcMode\":\"0\","+

                "\n"+S3+"\"ExecMode\":\"1\","+

                "\n"+S3+"\"GTCMode\":\"0\","+

                "\n"+S3+"\"FillFlags\":\"1\","+

                "\n"+S3+"\"ExpirFlags\":\"15\","+

                "\n"+S3+"\"OrderFlags\":\"127\","+

                "\n"+S3+"\"Spread\":\""+(string)spread+"\","+

                "\n"+S3+"\"TickValue\":\""+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE),_Digits)+"\","+

                "\n"+S3+"\"TickSize\":\""+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE),_Digits)+"\","+

                "\n"+S3+"\"ContractSize\":\""+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_CONTRACT_SIZE),_Digits)+"\","+

                "\n"+S3+"\"StopsLevel\":\""+(string)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL)+"\","+

                "\n"+S3+"\"VolumeMin\":\""+(string)((int)(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN)*100))+"\","+

                "\n"+S3+"\"VolumeMinExt\":\"1000000\","+

                "\n"+S3+"\"VolumeMax\":\""+(string)((int)(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX)*100))+"\","+

                "\n"+S3+"\"VolumeMaxExt\":\"50000000000\","+

                "\n"+S3+"\"VolumeStep\":\""+(string)((int)(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP)*100))+"\","+

                "\n"+S3+"\"VolumeStepExt\":\"1000000\","+

                "\n"+S3+"\"VolumeLimit\":\""+(string)((int)(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP)*100))+"\","+

                "\n"+S3+"\"VolumeLimitExt\":\"1000000\","+

                "\n"+S3+"\"SwapMode\":\"1\","+

                "\n"+S3+"\"SwapLong\":\""+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_SWAP_LONG),2)+"\","+

                "\n"+S3+"\"SwapShort\":\""+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_SWAP_SHORT),2)+"\","+

                "\n"+S3+"\"Swap3Day\":\""+(string)SymbolInfoInteger(_Symbol,SYMBOL_SWAP_ROLLOVER3DAYS)+"\""+

                "\n"+S2+"}"+

                "\n"+S1+"]"+

                "\n}";



   FileWriteString(file_config,_line);



  }

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

Comments