Drawing objects in MQL5

To add comments, please log in or register
gammag
635
gammag  

Hello all,

I'm struggling already for several days with how to draw objects within Expert Advisor in MQL5. First goal is to draw objects and have them visible within Strategy Tester.
I searched and read various articles in the forum, but still, it's not clear to me what is the logic behind drawing objects, especially within Strategy Tester.

Design of my Expert Advisor uses MQL5 classes, in particular, I have a class derived from CExpertSignal where I declared a member variable:

CChartObjectVLine *m_open_line;

Then, within ShortCondition() function, after all preconditions are fulfilled to open a trade, I want to draw a vertical line at the timepoint when the trade has been opened:

// signal preconditions check
(...)
// signal preconditions fulfilled

m_open_line=new CChartObjectVLine;
m_open_line.Create(0,"Line",0,TimeCurrent());
m_open_line.Color(clrRed);
              
ChartRedraw();

result = m_pattern_0;
(...)
return (result);

Issues:

1. if I run the EA from the MetaEditor, in debug mode:

    - I can see a red vertical line within Strategy Tester Visualization chart, but only for the first trade, not for the subsequent trades. This shows me that the problem is not about the parameters in the VLine Create() function.

2. if I run the EA directly from the StrategyTester

    - I don't see any vertical line within Strategy Tester Visualization chart, neither for the first trade, nor for the subsequent trades.

I suspect, this is something about how and where the graphical objects exists and/or how are they redrawn. I intentionally created the object dynamically with "new: to avoid situation that the objects will be removed because a scope of a given function is closed. Still, the issue persists.

Can you give me a hint, what am I doing wrong?

PS this is my first post in the forum. Please be patient with me, even if I'm asking obvious questions... I really tried to solve it on my own, but apparently I'm missing something
PS2 I can think of creating an indicator, and use array within the indicator to handle multiple objects for drawing, but it seems to be an overkill for me. I want to draw more objects, so I would end up with multiple indicators. I guess, there must be a simpler solution?

Ernst Van Der Merwe
6477
Ernst Van Der Merwe  

Each line has to have a unique name.

CChartObjectVLine *m_open_line;
CArrayObj m_open_lines;
m_open_line=new CChartObjectVLine;
m_open_line.Create(0,"Line"+(string)TimeCurrent(),0,TimeCurrent());
m_open_line.Color(clrRed);
m_open_lines.Add(m_open_line);           
      
ChartRedraw();
gammag
635
gammag  
Ernst Van Der Merwe:

Each line has to have a unique name.

Ernst, thank you so much, it works!

PS I double checked the documentation, there is nothing about this requirement. how did you know about it? trial and error? or is there a better source of documentation than mql5 site?

[Edit]: not for me, but maybe it can help someone in the future. This requirement needs to be fulfilled for each object within chart and is documented here:

https://www.mql5.com/en/docs/objects/objectcreate

Documentation on MQL5: Object Functions / ObjectCreate
Documentation on MQL5: Object Functions / ObjectCreate
  • www.mql5.com
The function creates an object with the specified name, type, and the initial coordinates in the specified chart subwindow. During creation up to 30 coordinates can be specified. The function returns true if the command has been successfully added to the queue of the specified chart, or false otherwise. If an object has already been created, an...
gammag
635
gammag  
gammag:

Ernst, thank you so much, it works!

PS I double checked the documentation, there is nothing about this requirement. how did you know about it? trial and error? or is there a better source of documentation than mql5 site?

PS it's a good idea to have a dynamic array of such objects. Even if I expect at DeInit() everything will be removed from the memory (?), it is good to have a clean code, thanks for this idea as well.

gammag
635
gammag  

I still have issues.

This is the question that I asked in the first post:

"1. if I run the EA from the MetaEditor, in debug mode:

    - I can see a red vertical line within Strategy Tester Visualization chart, but only for the first trade, not for the subsequent trades. This shows me that the problem is not about the parameters in the VLine Create() function.

2. if I run the EA directly from the StrategyTester

    - I don't see any vertical line within Strategy Tester Visualization chart, neither for the first trade, nor for the subsequent trades."


The first part of my question is solved now (each line has now unique name and I can see lines in the debug mode, within Strategy Tester Visualization chart). Great.

However, when I run EA directly from the Strategy Tester (not from MetaEditor), I still don't see any line (neither for the first nor any other trades).


Can you please give me further hints? thank you so much in advance.

Ernst Van Der Merwe
6477
Ernst Van Der Merwe  
gammag:

I still have issues.

This is the question that I asked in the first post:

"1. if I run the EA from the MetaEditor, in debug mode:

    - I can see a red vertical line within Strategy Tester Visualization chart, but only for the first trade, not for the subsequent trades. This shows me that the problem is not about the parameters in the VLine Create() function.

2. if I run the EA directly from the StrategyTester

    - I don't see any vertical line within Strategy Tester Visualization chart, neither for the first trade, nor for the subsequent trades."


The first part of my question is solved now (each line has now unique name and I can see lines in the debug mode, within Strategy Tester Visualization chart). Great.

However, when I run EA directly from the Strategy Tester (not from MetaEditor), I still don't see any line (neither for the first nor any other trades).


Can you please give me further hints? thank you so much in advance.

Seems to work in strategy tester on my side. Keep in mind that CArrayObj() class cleans up all dynamic pointers at program termination(tester stop), else there would be leaked memory.


gammag
635
gammag  
Ernst Van Der Merwe:

Seems to work in strategy tester on my side. Keep in mind that CArrayObj() class cleans up all dynamic pointers at program termination(tester stop), else there would be leaked memory.


it also works for my within Strategy Tester Visualization window:


However, where I don't see any graphic objects is within Strategy Tester integrated chart window:

Maybe it is not designed for this, though indicators which are used withing EA are plotted, so I don't understand the logics behind?

Ernst Van Der Merwe
6477
Ernst Van Der Merwe  
gammag:

it also works for my within Strategy Tester Visualization window:


However, where I don't see any graphic objects is within Strategy Tester integrated chart window:

Maybe it is not designed for this, though indicators which are used withing EA are plotted, so I don't understand the logics behind?

Try:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m_open_lines.FreeMode(false);     
gammag
635
gammag  

thanks, unfortunately, it does not change anything.

any other ideas?

To add comments, please log in or register