How to read Multi column and row CSV file and search each line
-
Hi Everyone.
Im struggling to get my code to read specific lines in my csv file. The CSV File has 6 Coloums and 28 Rows excluding the header.
I can only get the ea to pick up the last line in the file which is NZDUSD but i need it to search the current chart and pick that line from the CSV,( is this possible) if its available.
My mql programming skills are very poor and its taken me days just to get this right so any guidance is appreciated but please dumb it down for me.string line_read[6][29]; //assign array of string that will store 8 columns 28rows of csv data
int row=1,col=0; //column and row pointer for the array
handle=FileOpen(tempFileName,FILE_CSV|FILE_READ|FILE_ANSI,","); //comma delimiter
if(handle>0)
{
while(true) //loop through each cell
{
CurrencyPair=(string)FileReadString(handle);
BuyOrSell=(string)FileReadString(handle);
Bias=(int)FileReadString(handle);
TP=(double)FileReadNumber(handle);
Risk=(double)FileReadString(handle);
ATR=(double)FileReadString(handle);
string temp = FileReadString(handle); //read csv cell
if(FileIsEnding(handle)) break; //FileIsEnding = End of File
line_read[1][1]=temp; //save reading result to array
if(FileIsLineEnding(handle)); //FileIsLineEnding = End of Line
}
FileClose(handle);
}
else
{
Comment("File "+tempFileName+" not found, the last error is ", GetLastError());
}The CSV looks like this
Currecny Pairs,Long/Short,Bias,TP,Risk,ATR
AUDUSD,Long,53.49%,0.5,0.15,2.2
EURUSD,Long,66.28%,0.75,0.25,1.8
GBPUSD,Long,69.13%,0.75,0.25,1.8
USDCAD,Long,56.16%,0.5,0.15,2.2
USDCHF,Long,71.92%,2,0.5,1.5
USDJPY,Long,52.68%,0.5,0.15,2.2
AUDCAD,Buy,53.49%,0.5,0.15,2.2
AUDCHF,Buy,71.92%,2,0.5,1.5
AUDJPY,Buy,53.49%,0.5,0.15,2.2
AUDNZD,Neutral,0.00%,0.5,0.15,2.2
CADCHF,Neutral,0.00%,0.5,0.15,2.2
CADJPY,Neutral,0.00%,0.5,0.15,2.2
CHFJPY,Neutral,0.00%,0.5,0.15,2.2
EURAUD,Neutral,0.00%,0.5,0.15,2.2
EURCAD,Buy,66.28%,0.75,0.25,1.8
EURCHF,Buy,66.28%,0.75,0.25,1.8
EURGBP,Long,83.45%,3,1,1.2
EURJPY,Short,57.07%,0.5,0.15,2.2
EURNZD,Neutral,0.00%,0.5,0.15,2.2
GBPAUD,Neutral,0.00%,0.5,0.15,2.2
GBPCAD,Buy,69.13%,0.75,0.25,1.8
GBPCHF,Buy,69.13%,0.75,0.25,1.8
GBPJPY,Buy,69.13%,0.75,0.25,1.8
GBPNZD,Neutral,0.00%,0.5,0.15,2.2
NZDCAD,Buy,69.13%,0.75,0.25,1.8
NZDCHF,Buy,69.13%,0.75,0.25,1.8
NZDJPY,Buy,69.13%,0.75,0.25,1.8
NZDUSD,Long,67.76%,0.75,0.25,1.8 ( it only picks up this last line no matter what pair you place it on)Thanks for your time.
-
@jamesdaly its a bit difficult to "dumb down" such a long piece of code

The key thing you are missing is Symbol() function, that returns the current symbol you are using -> then you can scroll through that list and find a match
-
@roar Thanks Roar for the response.
Ok so would it look something like this then.
StringFind=Symbol();
or
FileSeek=Symbol();
If true read line.Apologies very basic understanding.
-
@roar Tried and failed. Iv used file seek and String Find but still cant get it to pick the correct line in the csv. Please somebody help.
im doing this in MQL5.string line_read[6][29]; //assign array of string that will store 6 columns 29rows of csv data
int row=1,col=0; //column and row pointer for the array
handle=FileOpen(tempFileName,FILE_CSV|FILE_READ|FILE_ANSI,","); //comma delimiter
if(handle>0)
{
while(true) //loop through each cell
{
FileSeek(_Symbol,Symbol(),0);
CurrencyPair=(string)FileReadString(handle);
BuyOrSell=(string)FileReadString(handle);
Bias=(int)FileReadString(handle);
TP=(double)FileReadNumber(handle);
Risk=(double)FileReadString(handle);
ATR=(double)FileReadString(handle);
string temp = FileReadString(handle); //read csv cell
if(FileIsEnding(handle)) break; //FileIsEnding = End of File
if(FileIsLineEnding(handle)); //FileIsLineEnding = End of Line
}
FileClose(handle);
}
else
{
Comment("File "+tempFileName+" not found, the last error is ", GetLastError());
} -
@jamesdaly ok, I'll try this myself once I got some free time..
Also, what do you need the data for? Is it okay if I load the data into 6 arrays, can you use those? -
just to let you know i came right
string line_read[6][31]; //assign array of string that will store 6 columns 29rows of csv data
int row=1,col=0,i=0; //column and row pointer for the array
int j=0;
while(i<5)
{
i++; //Increment variable
handle=FileOpen(tempFileName,FILE_CSV|FILE_READ|FILE_ANSI,","); //comma delimiter
if(handle>0)
{
while(true) //loop through each cell
{
CurrencyPair=(string)FileReadString(handle);
BuyOrSell=(string)FileReadString(handle);
Bias=(double)FileReadString(handle);
TP=(double)FileReadString(handle);
Risk=(double)FileReadString(handle);
ATR=(double)FileReadString(handle);
//if (i==3)break;
if (StringFind(Symbol(),CurrencyPair,0)>-1)break;
//string temp = FileReadString(handle); //read csv cell
if(FileIsEnding(handle)) break; //FileIsEnding = End of File
if(FileIsLineEnding(handle)); //FileIsLineEnding = End of Line
//i=i+1;
}FileClose(handle); break;}
else
{
if(j == 5)
{
Comment("File "+tempFileName+" not found, the last error is ", GetLastError());
}
Sleep(1000); //1 Second
}
} -
If the CSV file is created by something like python scalping, maybe you could consider splitting it into 28 files, one for each symbol with the same filename. It then just needs a few line to get what you want
-
my file is compiled from a exce/googlel document that has some calculations i run