Download MetaTrader 5

need a little help to read data from CSV file in MT4

To add comments, please log in or register
Mahdi E
195
Mahdi E  
Hi dear friends
Is any one knows how to make the below code that it work??

It should read from "Callouts1.csv" (File is located in ...\experts\files\)

-------------------------------------------------------------------------------------------------------------------------
int Handle;
string FileName = "Callouts1.csv";
string OP_Type = "";
string Symb = "";
double Entry;
double StopL;
double TakeP;

Handle=FileOpen(FileName,FILE_CSV|FILE_READ,","); // File opening
if(Handle<0) // File opening fails
{
if(GetLastError()==4103) // If the file does not exist,..
{
Alert("No file named ",FileName); //.. inform trader
}
else
{
// If any other error occurs..
Alert("Error while opening file ",FileName); //..this message
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exit start()
}
}

while(FileIsEnding(Handle)==false) // While the file pointer..
{ // ..is not at the end of the file
OP_Type = FileReadString(Handle); // Is this a buy or sell
Symb = FileReadString(Handle); // What symbol?
Entry = FileReadDouble(Handle); // Entry price
StopL = FileReadDouble(Handle); // Stop price
TakeP= FileReadDouble(Handle); // Take profit price
if (StringSubstr(Symbol(),0,6) == Symb)
{
if (OP_Type == "Buy")
{
BuyEntry = Entry;
BuyFirst = TakeP;
BuyStopLoss = StopL;
}
else
{
SellEntry = Entry;
SellFirst = TakeP;
SellStopLoss = StopL;
}
}
}

FileClose( Handle ); // Close file

-------------------------------------------------------------------------------------------------------------------------

File data looks like this:

Buy,Eur/Usd,1.423,1.419,1.426
Buy,Gbp/Usd,1.647,1.642,1.6495
Buy,Aud/Usd,0.809,0.811,0.814
Buy,Usd/Cad,1.119,1.115,1.1235
Buy,Usd/Chf,1.089,1.085,1.092
Buy,Usd/Jpy,94.5,94.1,94.8
Sell,Eur/Usd,1.404,1.408,1.402
Sell,Gbp/Usd,1.635,1.639,1.631
Sell,Aud/Usd,0.793,0.796,0.791
Sell,Usd/Cad,1.108,1.1125,1.105
Sell,Usd/Chf,1.068,1.071,1.065
Sell,Usd/Jpy,92.7,93.1,92.5
-------------------------------------------------------------------------------------------------------------------------
Keith Watford
Moderator
10611
Keith Watford  
What errors do you get?

Try putting the file in MQL4\Files
Carl Schreiber
7373
Carl Schreiber  

I generally read the file totally into a string and then split the string into an array of string-lines and each line into an array of elements.

This is a lot easier to read in terms of where I am and what do I read:

   int nL, nI, hdl  = FileOpen("myFile.csv",FILE_READ|FILE_SHARE_READ|FILE_BIN|FILE_COMMON); // for files in COMMON-folder!!
   if (hdl == INVALID_HANDLE) { ... }
   string allLines[], singLine[],
          f = FileReadString(hdl,(int)FileSize(hdl));
   FileClose(hdl);
   nL = StringSplit(f,StringGetCharacter("\n",0),allLines);
   while(nL-->0) {

      nI  = StringSplit(allLines[nL],';',singLine);
      while(nI-->0) {
         //singLine[nI]
         ...
      }
   }

// NOT Tested and you should replace the ';' by ','!

Mahdi E
195
Mahdi E  
Keith Watford:
What errors do you get?

Try putting the file in MQL4\Files
HI DEAR



SOME FRIENDS HELPED ME.

BUT ABOUT THIS CODE THE ERROR IS ATTACHED.
Files:
Untitled.jpg 253 kb
Keith Watford
Moderator
10611
Keith Watford  
Mahdi E:
HI DEAR



SOME FRIENDS HELPED ME.

BUT ABOUT THIS CODE THE ERROR IS ATTACHED.
Make sure that your {  and  } are paired correctly.
Fernando Carreiro
2979
Fernando Carreiro  
Mahdi E: BUT ABOUT THIS CODE THE ERROR IS ATTACHED.
You must put your code in a function or in a Event Handler function (depending on if it is a Script, EA or Indicator). It cannot be placed at the "Global Scope" Level.

Also, writing in ALL CAPS is considered "shouting". So please use normal text (like in this post).

EDIT: How is it, that you advertise on your blog being able to code EA's for people and yet, you don't even know the basics?
Mahdi E | 2017.01.20
Hi

I ll program your strategy to expert for free? (if it not be very Complex)
Event Handling Functions - Functions - Language Basics - MQL4 Reference
Event Handling Functions - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
Event Handling Functions - Functions - Language Basics - MQL4 Reference
To add comments, please log in or register