Download MetaTrader 5

File problems

To add comments, please log in or register
BigAl
489
BigAl  
bool restart = true;
int  starthandle = 0;
double test = 0;
void OnTick()
 { 
  MqlTick myprice;
  MqlTradeRequest myrequest;
  MqlTradeResult myresult;
  ZeroMemory(myrequest);
//--- If we do not have an open position then open one.
  if(!SymbolInfoTick(_Symbol, myprice))
  {
   Print("No price error#",GetLastError());
   return;
   }
  if(restart == true && PositionSelect(_Symbol) == false)
  {
    ZeroMemory(myrequest);
    myrequest.action = TRADE_ACTION_DEAL;
    myrequest.price = NormalizeDouble(myprice.ask,_Digits);
    myrequest.sl = 0;
    myrequest.tp = 0;
    myrequest.symbol = _Symbol;
    myrequest.volume = 5;
    myrequest.magic = 0;
    myrequest.type = ORDER_TYPE_BUY;
    myrequest.type_filling = ORDER_FILLING_FOK;
    myrequest.deviation = 10;
    OrderSend(myrequest, myresult);
    if(myresult.retcode == 10009 || myresult.retcode == 10008)
    {
      Print("Buy ordered");
      Sleep(10000);
      test = myrequest.price * myrequest.volume * 100000;
      Print("TestSet = ", test);
      starthandle = FileOpen("test.dat", FILE_READ|FILE_BIN);
      if (starthandle < 0)
       {
        FileOpen("test.dat", FILE_WRITE|FILE_BIN);
        FileWriteDouble(starthandle, test);
        FileClose(starthandle);
       }
      else
       {
        Print("file not written");
        return;
       }
     }
    else
     {
      Print("Buy failed error#",GetLastError());
      return;
     }
   }
  if (restart == 1 && PositionSelect(_Symbol)==true)
   {
    starthandle = FileOpen("test.dat", FILE_READ|FILE_BIN);
    if (starthandle > -1)
     {
      test = FileReadDouble(starthandle);
      FileClose(starthandle);
      Print("test = ",test);
      Print("file has been read");
      restart = 0;
     }
    else
     {
      Print("file not read");
     }
   }
  return;
 }
//+------------------------------------------------------------------+

This code is supposed to write a file when the EA is first run. Then, if the EA is removed from the chart and at a later time attached again it should recover the data ("test") and print it.

the restart flag is set each time the EA is attached to the chart. The first time this is done there is no active position so a position is opened, the test file is opened and the data test stored in the file which is then closed.

if the EA is removed from the chart and then attached again instead of the file being written the data "test" should be read from the file test. This is not happening.

What stupid thing am i doing???

Andrey Barinov
39426
Andrey Barinov  

restart==1 means the same as restart==true, so there is no part of the code wich works when restart==false

BigAl
489
BigAl  
Wahoo:

restart==1 means the same as restart==true, so there is no part of the code wich works when restart==false

yes I know that thanks. this is just a snippet of my code that i cant seem to get to work because I am not doing something correctly in my file handling and need someone to point me in the right direction. I have used this in MT4 but with that the file read takes the format of FileReadDouble(Handle,Test) but MT5 wont accept that so tried just FileReadDouble(Handle) but that doesnt work so not sure what to do. can u help?
phi nuts
2181
phi nuts  

Actually your mistake is a silly one.

      starthandle = FileOpen("test.dat", FILE_READ|FILE_BIN);  //--- the returned handle is for reading NOT writing !
      if (starthandle < 0) //--- wrong way to check the file handle
       {
        FileOpen("test.dat", FILE_WRITE|FILE_BIN);  //--- where the handle for writing ?
        FileWriteDouble(starthandle, test);
        FileClose(starthandle);
       }
      else
       {
        Print("file not written");
        return;
       }

And you can solve this problem yourself by simply checking the error.

Here's the correct one to write it. Since this is weekend, you can try it as a Script

    if(myresult.retcode == 10009 || myresult.retcode == 10008)
      {
      Print("Buy ordered");
      Sleep(10000);
      test = myrequest.price * myrequest.volume * 100000;
      Print("TestSet = ", test);
      ResetLastError();
      starthandle = FileOpen("test.dat", FILE_WRITE|FILE_BIN);
      if (starthandle != INVALID_HANDLE)
        {
        //--- FileOpen("test.dat", FILE_WRITE|FILE_BIN); //--- forget this
        FileWriteDouble(starthandle, test);
        Print("err 1 ",GetLastError());
        FileClose(starthandle);
        }
        else
        {
        Print("file not written",GetLastError());
        return;
        }
      }
      else
      {
      Print("Buy failed error#",GetLastError());
      return;
      }
   }
   
  if (restart == true && PositionSelect(_Symbol)== true)
   {
    ResetLastError();
    starthandle = FileOpen("test.dat", FILE_READ|FILE_BIN);
    if (starthandle != INVALID_HANDLE)
      {
      test = FileReadDouble(starthandle);
      Print("err 2 ",GetLastError());
      FileClose(starthandle);
      Print("test = ",test);
      Print("file has been read");
      restart = false;
      }
      else
      {
      Print("file not read",GetLastError());
      }
   }


BigAl
489
BigAl  

Thanks for that. Now I really feel silly!
To add comments, please log in or register