Download MetaTrader 5

MQL5: Buy or user error using CChartObjectTrendByAngle object

To add comments, please log in or register
imamushroom
157
imamushroom  
Have I found a bug?

I'm having difficulty using the CChartObjectTrendByAngle class as it is not doing what I expect it to do. Have I found a bug or am I doing something wrong?

My code is this

   int i = 0;

   datetime t0 = iTime(NULL,0,i);
   datetime t1 = iTime(NULL,0,i+1);
   
   double o0 = iOpen(NULL,0,i);
   double o1 = iOpen(NULL,0,i+1);
   
   long chartid = ChartID();
   
   CChartObjectTrendByAngle* L1;

   L1 = new CChartObjectTrendByAngle();
        
   L1.Create(chartid,"L1",0,t1,o1,t0,o0);
   L1.Selectable(true);
   L1.Selected(true);
   L1.Hidden(false);
   L1.Color(clrBlack);
   L1.RayRight(true);

   PrintFormat("Angle = %f",L1.Angle());

   delete L1;

I find that point2 of the Create command doesn't follow the open price of o0. If I add this line 

 
 L1.SetDouble(OBJPROP_PRICE2,o0);

after the L1.Create command the line seems to be drawn correctly.

Also, the Angle printformat statement says that the angle of the line is 0.000 which is also incorrect.

I've tried a test using the ObjectCreate command directly and the points are drawn correctly,however, the angle retrieved from the command

ObjectGetDouble(chartid,"Test",OBJPROP_ANGLE)

(where "Test" is the name of the line created using the ObjectCreate command) still reports 0.000

Any ideas what I should do?

Thanks in advance.

imamushroom
157
imamushroom  

Here's a more complete code as defined in a cllaa (updated to use CopyOpen and CopyTime commands)

#include <Kerching!/libIndicators.mqh>
#include <ChartObjects/ChartObjectsLines.mqh>

class CPlayable : CCustomIndicator
{
   private:
      bool _selectable;
      bool _hidden;
      color _l1Colour;
      color _l2Colour;
      bool _playable;        
      datetime _t[];
      double _o[];

      CChartObjectTrendByAngle* _L1;
      CChartObjectTrendByAngle* _L2; 
   
      double GetMa(int pAppliedPrice, int pShift);
      void UpdateMa(int pShift);
      
   public:
      void CPlayable(string pSymbol, int pTimeframe, bool pSelectable, bool pHidden, color pL1Colour, color pL2Colour);
      void ~CPlayable(void);
      bool Playable(int pShift);     
      int Direction(int pShift); 
};

void CPlayable::CPlayable(string pSymbol, int pTimeframe, bool pSelectable, bool pHidden, color pL1Colour, color pL2Colour)
{
   Init(pSymbol,pTimeframe);
   
   _selectable = pSelectable;
   _hidden = pHidden;
   _l1Colour = pL1Colour;
   _l2Colour = pL2Colour;
   _playable = false;

   
   _L1 = new CChartObjectTrendByAngle();
   _L2 = new CChartObjectTrendByAngle();

   
   const int m = 3;
   
   ArrayResize(_t,m);
   ArrayResize(_o,m);
}

void CPlayable::~CPlayable(void)
{
   delete _L1;
   delete _L2;
}

bool CPlayable::Playable(int pShift)
{
   if( _L1 != NULL ) _L1.Delete();
   if( _L2 != NULL ) _L2.Delete();
   
   int mt = ArraySize(_t);
   int mo = ArraySize(_o);
   
   CopyOpen(_symbol,_timeFrame,0,mt,_o);
   CopyTime(_symbol,_timeFrame,0,mo,_t);
   
   mt--;
   mo--;

   long chartid = ChartID();
   
   _L1.Create(chartid,"L1",0,_t[mt-1],_o[mo-1],_t[mt],_o[mo]);   // displays example screenshot
//   L1.SetDouble(OBJPROP_PRICE2,o0);  //works with this line but shouldn't need it 
   _L1.Selectable(_selectable);
   _L1.Selected(true);
   _L1.Hidden(_hidden);
   _L1.Color(_l1Colour);
   _L1.RayRight(true);
   
   _L2.Create(chartid,"L2",0,_t[mt-2],_o[mo-2],_t[mt-1],_o[mo-1]);
//   L2.SetDouble(OBJPROP_PRICE2,o1);
   _L2.Selectable(_selectable);
   _L2.Selected(true);
   _L2.Hidden(_hidden);
   _L2.Color(_l2Colour);
   _L2.RayRight(true);

   ObjectCreate(chartid,"Test",OBJ_TRENDBYANGLE,0,_t[mt-1],iClose(_symbol,_timeFrame,pShift+1),_t[mt],iClose(_symbol,_timeFrame,pShift));  //works
   
   double l1angle = _L1.Angle();

   PrintFormat("&& l1angle = %f, l2angle = %f, initalAngle = %i",l1angle,_L2.Angle());    // displaying 0.00
   PrintFormat("&& ObjectGetDouble(m_chart_id,m_name,OBJPROP_ANGLE) = %f,",ObjectGetDouble(chartid,"L1",OBJPROP_ANGLE));    // displaying 0.00
   PrintFormat("&& ObjectGetDouble(Close) = %f,",ObjectGetDouble(chartid,"Test",OBJPROP_ANGLE));  // displaying 0.00  

   
   return _playable;
}

 

 And the output

Output 

Ernst Van Der Merwe
4999
Ernst Van Der Merwe  

Coordinates should be set after creation.

   line1.Create(0,"line1",0,0,0,0,0);
   line1.Color(clrRed);
   line1.SetInteger(OBJPROP_TIME1,time[3]);
   line1.SetDouble(OBJPROP_PRICE1,close[3]);
   line1.SetInteger(OBJPROP_TIME2,time[50]);
   line1.SetDouble(OBJPROP_PRICE2,close[50]);
   printf("line angle: %.1f",line1.GetDouble(OBJPROP_ANGLE));
imamushroom
157
imamushroom  
Thanks I'll give that a go
To add comments, please log in or register