How can I make my Expert Advisor read cells from a CSV and return them as an int value?

To add comments, please log in or register
Antonio Bartolucci
43
Antonio Bartolucci  

Hello everybody, I'm trying to make my EA capable of assigning to an opened order in loss its specific hedging order so to avoid randomly opened hedging orders. 

To do that I was trying to create a blacklist, blacklist which I thought i could do by simply using the FileWrite() function. So I came up with this, and it works: 

void WriteOrdersTickets()

{
int handle;
int ticket = OrderTicket();
handle = FileOpen("Blacklist"+Symbol()+".csv",FILE_CSV|FILE_WRITE|FILE_READ,';');
 for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!= MAGICMA || OrderSymbol()!=Symbol()) continue;
      ticket = OrderTicket();
      if(OrderProfit() < 0 )
         {
            FileWriteInteger(handle,ticket);
            Print("Prendo nota degli ordini in perdita");
            FileClose(handle);
         }
      else
      break;

     }
}

What I now can't do, is reading each cell of the CSV the expert created and return the OrderTicket() value in each cell to another function. I was trying something like this, but none seem to be happening. 

int AssignHedgingTarget()
{
string filename = "Blacklist"+Symbol()+".csv";
int vars[10];
int count = 0;
int file = FileOpen(filename,FILE_CSV|FILE_READ);


while(!FileIsEnding(file) && count < 5)
   {
      vars[count] = FileReadNumber(file);
      count++;
   }
FileClose(file);
return(vars[count]);
Print("Target Order is ",vars[count]);

}

Can anybody please help me? 

nicholi shen
2394
nicholi shen  

There are no "cells" and since you don't have any comma separated values you should use a bin file to read and write the entire array. 

#include <files/filebin.mqh>

void OnStart()
{
   string file_name = StringFormat("Blacklist_%s.bin", _Symbol);
   if (writeOrders(file_name)) {
      int tickets[];
      int total = readOrders(file_name, tickets);
      for (int i=0; i<total; i++)
         Print(tickets[i]);
   }
}

bool writeOrders(string file_name) {
   CFileBin f;
   f.Open(file_name, FILE_WRITE);
   if (f.Handle() == INVALID_HANDLE)
      return false;
   int tickets[];
   for (int i=OrdersTotal()-1; i>=0; --i) {
      if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == _Symbol) {
         uint index = ArrayResize(tickets, ArraySize(tickets) + 1) - 1;
         tickets[index] = OrderTicket();
      }
   }
   f.WriteArray(tickets);
   return true;
}

int readOrders(string file_name, int &tickets[]) {
   CFileBin f;
   f.Open(file_name, FILE_READ);
   if (f.Handle() == INVALID_HANDLE)
      return WRONG_VALUE;
   return (int)f.ReadIntegerArray(tickets);
}
Antonio Bartolucci
43
Antonio Bartolucci  
Ok thank you I will try it out! 
Antonio Bartolucci
43
Antonio Bartolucci  
nicholi shen:

There are no "cells" and since you don't have any comma separated values you should use a bin file to read and write the entire array. 

Thank you so much, it worked!! :D 

Antonio Bartolucci
43
Antonio Bartolucci  
nicholi shen:

There are no "cells" and since you don't have any comma separated values you should use a bin file to read and write the entire array. 

Although it correctly reads the ticket values from the bin files, I'd like to know how can I return the value of the tickets in order to set a condition like

if(OrderTicket() != TicketReturnedValue())
        
DoHedging();

else

break;
ForexOnlineTrading
184
ForexOnlineTrading  

I would write the infos into the file in another style, for example xml style:

<order1>188428</order1>

<direction1>buy</direction1>

then you can read them and sort them easyer

Christina Ledger
29
Christina Ledger  
what are the best EAs and are they free?
Christina Ledger
29
Christina Ledger  
also looking for indicators i can use for forex and crypto- anyone ?
To add comments, please log in or register