EquityDataExporter

Author: Equity Data Exporter - by transcendreamer - origins from KIM, Xupypr
Price Data Components
Series array that contains close prices for each bar
Miscellaneous
Uses files from the file systemIt opens Message Boxes to the userIt writes information to file
0 Views
0 Downloads
0 Favorites
EquityDataExporter
#property copyright "Equity Data Exporter - by transcendreamer - origins from KIM, Xupypr"
// The script exports equity data for group of currencies or CDFs into CSV file
// Spaces are used as separators between symbol names, number of symbols is not limited
// Spreads, commissions and swaps are not counted
/////////////////////////////////////////////////////////////////////////////
#property show_inputs
extern datetime start_time=D'2012.01.01 00:00';
extern datetime finish_time=D'2021.01.01 00:00';
extern double lots=0.01;
extern string symbol_list="";
extern bool remove_empty=true;
extern string separator=";";
string symbols[1000];
bool error=false;
int total=0;
string currency;
/////////////////////////////////////////////////////////////////////////////
void init()
{
currency=AccountCurrency();
int file_handle=FileOpen("equity_data_export.csv",FILE_WRITE," ");
if(file_handle==-1) {MessageBox("Error opening file!");error=true;}
FileClose(file_handle);
ParseSymbols();
string heading="NUM"+separator+"DATE"+separator+"TIME";
for(int i=0;i<total;i++) heading=StringConcatenate(heading,separator,symbols[i]);
WritingLineInFile("equity_data_export.csv",heading);
}
/////////////////////////////////////////////////////////////////////////////
int start()
{
if(error) return(0);
datetime current_time=start_time;
int NUM=1; int i;
bool miss_step=false;
int shift[1000]; ArrayResize(shift,total);
double start[1000]; ArrayResize(start,total);
double finish[1000]; ArrayResize(finish,total);
double result[1000]; ArrayResize(result,total);
for(i=0;i<total;i++) shift[i]=iBarShift(symbols[i],0,start_time,false);
for(i=0;i<total;i++) start[i]=iClose(symbols[i],0,shift[i]);
while(current_time<Time[0]&&current_time<finish_time)
   {
   current_time+=Period()*60;
   miss_step=false;
   for(i=0;i<total;i++)
      {
      if(remove_empty) shift[i]=iBarShift(symbols[i],0,current_time,true);
      else shift[i]=iBarShift(symbols[i],0,current_time,false);
      if(shift[i]==-1) miss_step=true;
      finish[i]=iClose(symbols[i],0,shift[i]);
      result[i]=(finish[i]-start[i])*LotSize(symbols[i],Time[shift[i]])*lots;
      }
   if(!miss_step){
   string line=NUM+separator+TimeToStr(current_time,TIME_DATE)+separator+TimeToStr(current_time,TIME_MINUTES);
   for(i=0;i<total;i++) line=StringConcatenate(line,separator,result[i]);
   WritingLineInFile("equity_data_export.csv",line);
   NUM++;}
   }
MessageBox("Equity data export finished.");
return(0);
}
/////////////////////////////////////////////////////////////////////////////
void WritingLineInFile(string FileName, string text) {
  int file_handle=FileOpen(FileName, FILE_READ|FILE_WRITE, " ");
  if (file_handle>0) {
    FileSeek (file_handle, 0, SEEK_END);
    FileWrite(file_handle, text);
    FileClose(file_handle);
  }
}
/////////////////////////////////////////////////////////////////////////////
double LotSize(string symbol, datetime tbar)
{
double size,close1,close2;
string BQ;
if(currency=="") currency="USD";
int type=MarketInfo(symbol,MODE_PROFITCALCMODE);
switch (type)
{
case 0:
   {
   int sbar=iBarShift(symbol,0,tbar);
   size=MarketInfo(symbol,MODE_LOTSIZE);
   if(StringSubstr(symbol,3,3)=="USD") break;
   if(StringSubstr(symbol,0,3)=="USD")
      {
      close1=iClose(symbol,0,sbar);
      if(close1>0) size=size/close1;
      }
   else
      {
      BQ=StringSubstr(symbol,0,3)+"USD";
      if(iClose(BQ,0,0)==0) BQ="USD"+StringSubstr(symbol,0,3);
      if(iClose(BQ,0,0)==0) break;
      int BQbar=iBarShift(BQ,0,tbar);
      close1=iClose(symbol,0,sbar);
      close2=iClose(BQ,0,BQbar);
      if(close1>0 && close2>0)
         {
         if(StringSubstr(BQ,0,3)=="USD") size=size/close2/close1;
         else size=size*close2/close1;
         }
      }
   } break;
case 1: size=MarketInfo(symbol,MODE_LOTSIZE); break;
case 2: size=MarketInfo(symbol,MODE_TICKVALUE)/MarketInfo(symbol,MODE_TICKSIZE);
}
if(currency!="USD")
   {
   BQ=currency+"USD";
   if(iClose(BQ,0,0)==0)
      {
      BQ="USD"+currency;
      close1=iClose(BQ,0,iBarShift(BQ,0,tbar));
      if(close1>0) size*=close1;
      }
   else
      {
      close1=iClose(BQ,0,iBarShift(BQ,0,tbar));
      if(close1>0) size/=close1;
      }
   }
return(size);
}
/////////////////////////////////////////////////////////////////////////////
void ParseSymbols()
{
int position=0; 
string name=""; 
int length=StringLen(symbol_list);
for(position=0;position<length;position++)
{
int sym=StringGetChar(symbol_list,position);
if(sym!=32&&sym!=9&&sym!=10&&sym!=13) name=name+StringSubstr(symbol_list,position,1);
if(sym==32||sym==9||sym==10||sym==13||position==length-1) 
if(StringLen(name)>0) 
{total++;ArrayResize(symbols,total);symbols[total-1]=name;name="";}
}
for(int i=0;i<total;i++)
if(!CheckSymbol(symbols[i])) 
{error=true; MessageBox("Error parsing symbol list!");}
}
/////////////////////////////////////////////////////////////////////////////
bool CheckSymbol(string symbol)
{
double bid=MarketInfo(symbol,MODE_BID);
if(GetLastError()==4106) return(false);
else return(true);
}
/////////////////////////////////////////////////////////////////////////////

Comments