Download MetaTrader 5
To add comments, please log in or register
Add the thread to Favorites. Track the changes even faster!
ak20 ak20
4440
ak20 ak20 2011.03.26 18:15 

Hi, I'm trying to save an arrow object to a file using binary read/write mode. I've got two problems:

  • The .bin file that is created seems very small: 5 bytes for an arrow object. I don't think it's storing all the arrow properties.
  • I'm unable to load the object from the .bin file. The Load() method always results in Error 0.

Below is the code I use to test saving and loading an arrow object. Could someone please take a look at this code and tell me what I'm doing wrong?

//+------------------------------------------------------------------+
//|                                                   saveobject.mq5 |
//+------------------------------------------------------------------+
#include <\ChartObjects\ChartObjectsArrows.mqh>

//--- arrow A will be drawn on the chart and then saved to a file
CChartObjectArrow objArrow_A;
//--- arrow B will be loaded from the file and then drawn on the chart
CChartObjectArrow objArrow_B;
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- delete objects
   objArrow_A.Delete();
   objArrow_B.Delete();
   ChartRedraw();
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- retrieve properties of a visible bar
   long visibleBar=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR)-10;
   datetime tempTimeArray[];
   CopyTime(NULL,0,(int)visibleBar,1,tempTimeArray);
   double tempLowArray[];
   CopyLow(NULL,0,(int)visibleBar,1,tempLowArray);
//--- set properties of arrow A
   uchar arrowCode=233;
   objArrow_A.Create(0,"objArrow_2",0,tempTimeArray[0],tempLowArray[0],arrowCode);
   objArrow_A.Anchor(ANCHOR_TOP);
   objArrow_A.Width(3);
   objArrow_A.Color(clrRed);

//--- open file for saving
   int file_handle;
   file_handle=FileOpen("MyFile.bin",FILE_WRITE|FILE_BIN|FILE_ANSI);
   if(file_handle>=0)
     {
      if(!objArrow_A.Save(file_handle))
        {
         //--- file save error
         printf("File save: Error %d!",GetLastError());
         FileClose(file_handle);
         //---
         return;
        }
      FileClose(file_handle);
     }
//--- rename arrow A so that both arrow A and arrow B can be shown on the chart
   objArrow_A.Name("objArrow_1");
//--- redraw chart to show arrow A
   ChartRedraw();

//--- open file for loading
   file_handle=FileOpen("MyFile.bin",FILE_READ|FILE_BIN|FILE_ANSI);
   if(file_handle>=0)
     {
      if(!objArrow_B.Load(file_handle))
        {
         //--- file load error
         printf("File load: Error %d!",GetLastError());
         FileClose(file_handle);
         //---
         return;
        }
      FileClose(file_handle);
     }
//--- change arrow properties of arrow B to see the difference between arrow A and B
   double tmpPrice=objArrow_B.Price(0);
   objArrow_B.Price(0,tmpPrice-(100*Point()));
   objArrow_B.Color(clrAqua);
//--- redraw chart to show arrow B
   ChartRedraw();
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Types - Documentation on MQL5
/
To add comments, please log in or register