Help: Save CSV to local got Error 5004

 

Hi All!

I use Webrequest to load the CSV and save contents to local with the code below. I succeed many time before except this time, i'm keep trying 2 days so far in this struggle.

Anyone please help me to teach me how can i get the error 5004 all the time.

Code:

void CSVToLocal(string url,string filename)
  {
   char data[];
   char result[];
   string result_headers;
   int res = WebRequest("GET", url, "", 500, data, result, result_headers);
   if(res==-1)
     {
      Print("Error in WebRequest. Error code  =",GetLastError());
      MessageBox("Add the address '' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
     }
   else
     {
      //--- Load successfully
      int handle;
      string csv = CharArrayToString(result);

      string rows[];
      StringSplit(csv, ';', rows);
      if(ArraySize(rows)>0)
         counter = StrToInteger(rows[0]);          

      //--- Save the data to a file
      handle=FileOpen(filename,FILE_WRITE|FILE_CSV);
      //--- Checking errors
      if(handle!=INVALID_HANDLE)
        {
         //            Alert(csv);
         //--- Save the contents of the result[] array to a file
         FileWriteString(handle,csv+"\n");
         FileFlush(handle);
         FileClose(handle);
        }
      else
         Print("FileOpen error in ",filename,": ",GetLastError());
     }
  }

Thank You!

 
The problem is the filename which you don't show.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem
 
William Roeder #:
The problem is the filename which you don't show.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

My file's name is a random number combine with characters also, sure filename is ok because i've checked on local disk - the file is created and can open by Word. But in next step, EA opens that file to get orders, but nothing happens, and Error 5004 message found in Experts panel https://prnt.sc/1qmd4eh

Please help me bro!

    { 
      CSVToLocal("http://fbgfx.pro/fbgfx.pro/fbgfxcopier/8485443orders.csv","8485443orders.csv");

      //--- Checking for the new deals and SL/TP changes
      filehandle=FileOpen("8485443orders.csv",FILE_READ|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
        {
         int o=0;
         opened_list[o]=0;
         while(!FileIsEnding(filehandle))
           {
            counter=StrToInteger(FileReadString(filehandle));  // Get the number of orders from Master
            ticket=StrToInteger(FileReadString(filehandle));   // Set the key for orders managing
            symbol=FileReadString(filehandle);                 // Set the symbol
            type=StrToInteger(FileReadString(filehandle));     // Set the order type
            price=StrToDouble(FileReadString(filehandle));     // Set the price for order
            lot=StrToDouble(FileReadString(filehandle))*mult;  // Set the volume (Lot) for Order
            sl=StrToDouble(FileReadString(filehandle));        // Set the Stop Loss for Order
            tp=StrToDouble(FileReadString(filehandle));        // Set the Take Profit for Order

            for(int i=0; i<OrdersTotal(); i++)
              {
               if(!OrderSelect(i,SELECT_BY_POS))
                  continue;
               if(OrderMagicNumber()!=MagicNumber)             // Filter the orders from EA only
                  continue;
               opened_list[o]=ticket;
               opened_list[o+1]=0;
               o++;

               //Modify the limit orders.
               if(OrderType()>1 && OrderOpenPrice()!=price)
                 {
                  if(!OrderModify(OrderTicket(),price,0,0,0))
                     Print("Error: ",GetLastError()," during modification of the order.");
                 }

               if(tp!=OrderTakeProfit() || sl!=OrderStopLoss())
                 {
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0))
                     Print("Error: ",GetLastError()," during modification of the order.");
                 }
               //--- If deal was not opened yet on Slave-account, open it.
               if(InList(ticket)==-1 && ticket!=0)
                 {
                  //               FileClose(filehandle);
                  if(type<2)
                     OpenMarketOrder(ticket,symbol,type,price,lot);
                  if(type>1)
                     OpenPendingOrder(ticket,symbol,type,price,lot);
                  return;
                 }

               //--- If deal was closed on Master-account, close it on Slave-account
               for(i=0; i<OrdersTotal(); i++)
                 {
                  if(!OrderSelect(i,SELECT_BY_POS))
                     continue;
                  if(OrderMagicNumber()!=MagicNumber)    //Only select the orders from EA
                     continue;

                  if(InList(ticket)==-1)
                    {
                     if(OrderType()==0)
                       {
                        if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slip))
                           Alert("Error: ",GetLastError()," during closing the order.");
                       }
                     else
                        if(OrderType()==1)
                          {
                           if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slip))
                              Alert("Error: ",GetLastError()," during closing the order.");
                          }
                        else
                           if(OrderType()>1)
                             {
                              if(!OrderDelete(OrderTicket()))
                                 Alert("Error: ",GetLastError()," during deleting the pending order.");
                             }
                    }
                 }
               break;
              }
           }
         FileClose(filehandle);
        }
  
Screenshot
Screenshot
  • prnt.sc
Captured with Lightshot
 
  1. Tran Ngoc Quang #: the file is created and can open by Word. But in next step, EA opens that file to get orders, but nothing happens, and Error 5004 message 

    Your title is: "Save CSV to local got Error 5004" Your original post you are writing the file. Last post you are reading a file.

    Make up your mind. Where is your error, what are you doing.

  2. counter=StrToInteger(FileReadString(filehandle));  

    Why are you reading a string and converting it? Why don't you just read the correct data type?

 
William Roeder #:
  1. Your title is: "Save CSV to local got Error 5004" Your original post you are writing the file. Last post you are reading a file.

    Make up your mind. Where is your error, what are you doing.

  2. Why are you reading a string and converting it? Why don't you just read the correct data type?

1. Yes, the error message came from Writing function but the file is created, that made me confusing, last post just show what i did in next step... and for your debugging code if any mistake.

2. You're right, i did an unnecessary thing.

Thank you!

Reason: