Script using CChartObjectArrow class fails to display an arrow on a chart?

 

Could someone please tell me why this simple script fails to draw an arrow on a chart that it is run on?  It has to be something simple, but I am unable to see it.  When debugged, it seems to create the chart object and set its values, but the arrow never is displayed on the chart....

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart() 
  { 
   CChartObjectArrow myarrow;
   double price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
   myarrow.Create(0,"test",0,0,0,0);
   myarrow.Time(TimeCurrent());
   myarrow.Price(price);
   myarrow.ArrowCode(233);
  }
//+------------------------------------------------------------------+

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
 

At the end of the OnStart function myarrow's destructor called which deletes created object

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
 

Thank you for your reply.  I see now why that happens.  It is a matter of SCOPE.  My purpose of this code was to explore how a simple call to create a class object and set its members functioned.  My real purpose is to include the creation and control of a class object in an indicator.

I see now that I need to create the class objects on a higher level, (from an Include file) outside of the OnCalculate() routine.  This way, the Destructor function of the object will not be called until the object goes out of scope.

Best regards,

DMMcCollum

 

You can use Detach method to avoid ObjectDelete in the destructor. Because destructor will be called in any case either at the end of OnStart or at the script ending