Getting All Available Symbols

 

Hello,

I wrote the custom indicator below that writes the chart data to a file for my other charting software to use. I have tested it and it seems to work. Now, I'm thinking of making it create files for all symbols instead of just the current chart.


Any ideas on how to accomplish that?


If I could get an array of all the symbols then I should be able adjust my code to do the job.

//+------------------------------------------------------------------+
//|                                             WriteChartToFile.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "me"
#property link      ""

#property indicator_chart_window


extern int BarHistory=2000;


int    Handle;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
        string FileName = StringSubstr(Symbol(),0,6) + Period()+ ".csv" ;
        FileDelete(FileName);
        Handle  = FileOpen(FileName , FILE_CSV|FILE_READ|FILE_WRITE, "\t");  //not quite sure what the delimiter does 
                                                                             //here.
        if (Handle<1){
                Print("cannot open file error_",GetLastError());
                return(0);
        }

        string Strline;
        for (int cnt=BarHistory; cnt>0; cnt--){  //last bar not handled here
                Strline = MakeInfoString(cnt);
                FileWrite(Handle, Strline);
        }


        FileFlush(Handle);      
}
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
        FileClose(Handle);
}
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{

        static string LastTickStr;
        static double ClosePrice;
        static datetime CurrentTimeStamp=0;

        if(CurrentTimeStamp != Time[0]) 
        {
                CurrentTimeStamp = Time[0];

                //print a new line
                LastTickStr = MakeInfoString(0);
                FileWrite(Handle, LastTickStr);
                ClosePrice = Close[0];
        }


        if(ClosePrice != Close[0]){//delete the last line and rewrite           

                int ByteCount = StringLen(LastTickStr) + 2 ; //count number of bytes to go back

                LastTickStr = MakeInfoString(0);
                FileSeek(Handle,-1*ByteCount, SEEK_END);
                FileWrite(Handle, LastTickStr);

        }


        FileFlush(Handle);

        return(0);

}

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



string MakeInfoString(int Bar){

        string Strline = TimeToStr(Time[Bar],TIME_DATE|TIME_SECONDS);

        Strline = StringSubstr(Strline,0,4) + StringSubstr(Strline,5,2) + StringSubstr(Strline,8,2) + "," + StringSubstr(Strline,11,5);

        Strline = Strline + "," + DoubleToStr(Open[Bar],4) + "," + DoubleToStr(High[Bar],4) + "," 
                + DoubleToStr(Low[Bar],4) + "," + DoubleToStr(Close[Bar],4) + "," + DoubleToStr(Volume[Bar],0);

        return(Strline);
}


Btw, it would have been more convenient to just rewrite the file in start(), but my other charting software that is going to read the file will get confused sometimes. Not rewriting the file seems to work much better.



Please, any advice or comments on how to adjust the code and make it get the data of all symbols are welcome.

 
Search for AllMarketDate in the Code Base. Use parts of its code to obtain a list of symbols.
 
Thirteen:
Search for AllMarketDate in the Code Base. Use parts of its code to obtain a list of symbols.

Did you mean AllMarketData? I am aware of it, actually. It has a function that gets the symbols:


//+------------------------------------------------------------------+
//|                                                AllMarketData.mq4 |
//|                                 Copyright © 2011, MGAlgorithmics |
//|                                          http://www.mgaforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MGAlgorithmics"
#property link      "http://www.mgaforex.com"


int FindSymbols() 
{
  int    handle, i, r, TotalRecords;
  string fname, Sy, descr;
  //----->
  fname = "symbols.raw";
  handle=FileOpenHistory(fname, FILE_BIN | FILE_READ);
  if(handle<1)
    {
     Print("HTML Report generator - Unable to open file"+fname+", the last error is: ", GetLastError());
     return(false);
    }  
  TotalRecords=FileSize(handle) / 1936;
  ArrayResize(Symbols, TotalRecords);
    
  for(i=0; i<TotalRecords; i++) 
  {
    Sy=FileReadString(handle, 12);
    descr=FileReadString(handle, 75);
    FileSeek(handle, 1849, SEEK_CUR); // goto the next record
     
    Symbols[r]=Sy;
    Descr[r]=descr;
    SyDigits[r]=MarketInfo(Sy,MODE_DIGITS);
    
    Spread[r]=MarketInfo(Sy,MODE_SPREAD);
    
    swap[r][LONG]=MarketInfo(Sy,MODE_SWAPLONG);
    swap[r][SHORT]=MarketInfo(Sy,MODE_SWAPSHORT); 
    swap[r][TYPE]=MarketInfo(Sy,MODE_SWAPTYPE);
  
    Stop[r]=MarketInfo(Sy,MODE_STOPLEVEL);
  
    Lot[r][SIZE]=MarketInfo(Sy,MODE_LOTSIZE);
    Lot[r][MIN]= MarketInfo(Sy,MODE_MINLOT);
    Lot[r][LOTSTEP]=MarketInfo(Sy,MODE_LOTSTEP);
    Lot[r][MAXLOT]=MarketInfo(Sy,MODE_MAXLOT);
  
    Tick[r][VALUE]= MarketInfo(Sy,MODE_TICKVALUE);
    Tick[r][SIZE]=MarketInfo(Sy,MODE_TICKSIZE);
  
    Timing[r][STARTING]=MarketInfo(Sy,MODE_STARTING);
    Timing[r][EXPIRATION]=MarketInfo(Sy,MODE_EXPIRATION);
  
    Trade[r][ALLOWED]=MarketInfo(Sy,MODE_TRADEALLOWED);
    Trade[r][MODE]=MarketInfo(Sy,MODE_PROFITCALCMODE); 
    if (Trade[r][MODE]>MAX) MAX=Trade[r][MODE];
    
    
    // the script won't print the following items ... if you need them, amend yourself the PrintResult function below :)
    
    /*Margin[r][CALC]=MarketInfo(sSymbol,MODE_MARGINCALCMODE);
    Margin[r][INIT]=MarketInfo(sSymbol,MODE_MARGININIT);
    Margin[r][HEDGED]=MarketInfo(sSymbol,MODE_MARGINHEDGED);
    Margin[r][REQUIRED]=MarketInfo(sSymbol,MODE_MARGINREQUIRED);
    Freeze[r]=MarketInfo(sSymbol,MODE_FREEZELEVEL);*/
         
    // a different index for the count can be useful if you want to filter some elements from the whole list
    r++;
  }
 
  FileClose(handle);
  return(TotalRecords);
}
  



I don't understand the code completely, but can use it if there is no better way.

 

I attempted to modify the AllMarketData.mq4 code:

int start() 
{
  string SymbolArray[];
  StoreSymbols(SymbolArray);
  Alert(SymbolArray[0]);
  
  
}

void StoreSymbols(string& SymbolArray[]) //no pointers in mql4 :(
{
  
  int handle=FileOpenHistory("symbols.raw", FILE_BIN | FILE_READ);
  if(handle<1)
    {
     Print("Error Opening File: ", GetLastError());
     return(false);
    }  
    
  int TotalRecords=FileSize(handle) / 1936;
  
  string SymbolName;
  
  for(int i=0; i<TotalRecords; i++) 
  {
    SymbolName =FileReadString(handle, 12);
    FileSeek(handle, 1849, SEEK_CUR); // goto the next record
     
    SymbolArray[i]=SymbolName;
    
  }
 
  FileClose(handle);
  return(TotalRecords);
}


The alert is just blank, and I'm not sure what is wrong.

Does any one see mistakes in the code?

 
your array has zero size
 
BenLinus:

The alert is just blank, and I'm not sure what is wrong.

Does any one see mistakes in the code?

#1: Resize SymbolArray to be equal to number of TotalRecords.

#2: Adjust your FileSeek offset from 1849 to 1924 (because you aren't reading the symbol description like AllMarketData does).

See code below (modifications highlighted):

int StoreSymbols(string& SymbolArray[]) {
  
   int handle=FileOpenHistory("symbols.raw", FILE_BIN | FILE_READ);
   if(handle<1) {
     Print("Error Opening File: ", GetLastError());
     return(false);
   }  
    
   int TotalRecords=FileSize(handle) / 1936;
   ArrayResize(SymbolArray, TotalRecords);
   string SymbolName;
  
   for(int i=0; i<TotalRecords; i++) {
      SymbolName =FileReadString(handle, 12);
      FileSeek(handle, 1924, SEEK_CUR); // goto the next record
      SymbolArray[i]=SymbolName;
   }
 
   FileClose(handle);
   return(TotalRecords);
}
 

What data are you trying to export? Price data? I tried to do this but eventually gave up battling with loading bars of symbols that are not loaded. Watch out for error 4066. I think even after getting the "all symbols" part working, it might stop you in your tracks

I just manually load up each chart, scroll back as far as I need and run my exporter script :(

 
Thirteen:

#1: Resize SymbolArray to be equal to number of TotalRecords.

#2: Adjust your FileSeek offset from 1849 to 1924 (because you aren't reading the symbol description like AllMarketData does).

See code below (modifications highlighted):


Thanks alot Thirteen. Now the output is correct. I'm curious, how did you find the offset? When I looked at symbols.raw in notepad I didn't see the symbols the way it comes out in the output. I did see for example "EURCHF" in symbols.raw but my output when I write all the symbols array to a file appears as "EURCHF." -- which is how my broker writes it.

 
alladir:

What data are you trying to export? Price data? I tried to do this but eventually gave up battling with loading bars of symbols that are not loaded. Watch out for error 4066. I think even after getting the "all symbols" part working, it might stop you in your tracks

I just manually load up each chart, scroll back as far as I need and run my exporter script :(


Thanks for the heads-up. I'll show you the code if I succeed.
 
alladir:

What data are you trying to export? Price data?


This is a sample for one symbol:

20130906,16:00,0.9198,0.9212,0.9183,0.9198,743
20130906,17:00,0.9197,0.9214,0.9191,0.9194,464
20130906,18:00,0.9193,0.9205,0.9192,0.9203,220
20130906,19:00,0.9202,0.9202,0.9193,0.9194,141
20130906,20:00,0.9195,0.9196,0.9192,0.9194,74
20130906,21:00,0.9193,0.9197,0.9192,0.9194,69
Reason: