how to draw multiple horizontal lines into chart?

 

Hello,

I am trying to write simple EA for learning purposes, however, when I try to draw horizontal lines into chart indicating SL+TP prices of either buy and sell positions, there is some strange behavior. When i test my code in strategy tester, it draws only some lines, sometimes no lines at all... I googled and searched the forum but no luck :(

What I wanna do: when buy(or sell) is opened, I want my EA to draw two horizontal lines indicating the calculated SL and TP prices for opened position into chart. When the position is closed, I want the corresponding lines to disappear until new position is opened and so on.

How do I do that:

I declare the array as a private member of my EA class for four lines like this :

CChartObjectHLine m_chart_h_line[4];           // chart horizontal line object

  In the EA Init I do the following:

//-- create sl+tp lines for current chart  
    m_chart_h_line[0].Create(0,"buy SL",0,0);  // set price to 0 to hide the line
    m_chart_h_line[0].Color(clrDarkGoldenrod);
   
    m_chart_h_line[1].Create(0,"buy TP",0,0); 
    m_chart_h_line[1].Color(clrYellow);
   
    m_chart_h_line[2].Create(0,"sell TP",0,0);
    m_chart_h_line[2].Color(clrCyan);
       
    m_chart_h_line[3].Create(0,"sell SL",0,0);
    m_chart_h_line[3].Color(clrDarkCyan);


and then, when an buy position is being opened:

    m_chart_h_line[0].Price(0,SL); // set price to stoploss

    m_chart_h_line[1].Price(0,TP); // set price to takeprofit
    ChartRedraw(0);


and when closed:

    m_chart_h_line[0].Price(0,0); // set price to 0 to hide line

    m_chart_h_line[1].Price(0,0); // set price to 0 to hide line
    ChartRedraw(0);


But it behaves really randomly as said above - sometimes it show only one line, sometimes nothing.

What is the correct way to do such thing?


thanks

 
donmoron:

Hello,

I am trying to write simple EA for learning purposes, however, when I try to draw horizontal lines into chart indicating SL+TP prices of either buy and sell positions, there is some strange behavior. When i test my code in strategy tester, it draws only some lines, sometimes no lines at all... I googled and searched the forum but no luck :(

What I wanna do: when buy(or sell) is opened, I want my EA to draw two horizontal lines indicating the calculated SL and TP prices for opened position into chart. When the position is closed, I want the corresponding lines to disappear until new position is opened and so on.

How do I do that:

I declare the array as a private member of my EA class for four lines like this :

CChartObjectHLine m_chart_h_line[4];           // chart horizontal line object

  In the EA Init I do the following:

//-- create sl+tp lines for current chart  
    m_chart_h_line[0].Create(0,"buy SL",0,0);  // set price to 0 to hide the line
    m_chart_h_line[0].Color(clrDarkGoldenrod);
   
    m_chart_h_line[1].Create(0,"buy TP",0,0); 
    m_chart_h_line[1].Color(clrYellow);
   
    m_chart_h_line[2].Create(0,"sell TP",0,0);
    m_chart_h_line[2].Color(clrCyan);
       
    m_chart_h_line[3].Create(0,"sell SL",0,0);
    m_chart_h_line[3].Color(clrDarkCyan);


and then, when an buy position is being opened:

    m_chart_h_line[0].Price(0,SL); // set price to stoploss

    m_chart_h_line[1].Price(0,TP); // set price to takeprofit
    ChartRedraw(0);


and when closed:

    m_chart_h_line[0].Price(0,0); // set price to 0 to hide line

    m_chart_h_line[1].Price(0,0); // set price to 0 to hide line
    ChartRedraw(0);


But it behaves really randomly as said above - sometimes it show only one line, sometimes nothing.

What is the correct way to do such thing?

thanks

Hi donmoron

1. I don't like your user name, I hope that represent other people and not you.

2. Use SRC button to post your code

,

3. Try this instead - not tested though :)

    CChartObjectHLine m_chart_h_line;           // chart horizontal line object

    //-- create sl+tp lines for current chart   
    m_chart_h_line.Create(0,"buy SL",0,0);  // set price to 0 to hide the line
    m_chart_h_line.Color(clrDarkGoldenrod);
    
    m_chart_h_line.Create(0,"buy TP",0,0);  
    m_chart_h_line.Color(clrYellow);
    
    m_chart_h_line.Create(0,"sell TP",0,0);
    m_chart_h_line.Color(clrCyan);
        
    m_chart_h_line.Create(0,"sell SL",0,0);
    m_chart_h_line.Color(clrDarkCyan);

 4. Read point 1 and 2 on this link (https://www.mql5.com/en/forum/7115#comment_236917) and search the forum, there's plenty sample of drawing multiple object (like this https://www.mql5.com/en/forum/6983#comment_214634)

5. If we have problem with object, right click chart, select 'Object List' or just press Ctrl + B, click 'List All' button, and double click the object that confusing us.

6. Have fun !!!

:D 

 

thanks for the reply, i have managed that problem was supplying bad prices  - so the lines were drawn, but not visible because they were not in the range of prices currently displayed in the chart.

however, thanks for the "object list" tip, it is very useful!

Reason: