Up down arrow not getting visible on every tick in mql5

To add comments, please log in or register
jaffer wilson
769
jaffer wilson  

I am trying to get the up and down buy/sell arrows on the graph but not able to place it at every tick. Here is the code for my logic:

 bool ArrowSellCreate(const long            chart_ID=0,        // chart's ID 
                     const string          name="ArrowSell",  // sign name 
                     const int             sub_window=0,      // subwindow index 
                     datetime              time=0,            // anchor point time 
                     double                price=0,           // anchor point price 
                     const color           clr=C'225,68,29',  // sign color 
                     const ENUM_LINE_STYLE style=STYLE_SOLID, // line style (when highlighted) 
                     const int             width=1,           // line size (when highlighted) 
                     const bool            back=false,        // in the background 
                     const bool            selection=false,   // highlight to move 
                     const bool            hidden=true,       // hidden in the object list 
                     const long            z_order=0)         // priority for mouse click 
  { 
//--- set anchor point coordinates if they are not set 
   ChangeArrowEmptyPoint(time,price); 
//--- reset the error value 
   ResetLastError(); 
//--- create the sign 
   if(!ObjectCreate(chart_ID,name,OBJ_ARROW_SELL,sub_window,time,price)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create \"Sell\" sign! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- set a sign color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
//--- set a line style (when
Kindly, suggest me what will make the code show up down arrow onTick function.
highlighted)

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style); //--- set a line size (when highlighted)    ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width); //--- display in the foreground (false) or background (true)    ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); //--- enable (true) or disable (false) the mode of moving the sign by mouse    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); //--- hide (true) or display (false) graphical object name in the object list    ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); //--- set the priority for receiving the event of a mouse click in the chart    ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); //--- successful execution    return(true);   } //+------------------------------------------------------------------+ //| Move the anchor point                                            | //+------------------------------------------------------------------+ bool ArrowSellMove(const long   chart_ID=0,       // chart's ID                    const string name="ArrowSell", // object name                    datetime     time=0,           // anchor point time coordinate                    double       price=0)          // anchor point price coordinate   { //--- if point position is not set, move it to the current bar having Bid price    if(!time)       time=TimeCurrent();    if(!price)       price=SymbolInfoDouble(Symbol(),SYMBOL_BID); //--- reset the error value    ResetLastError(); //--- move the anchor point    if(!ObjectMove(chart_ID,name,0,time,price))      {       Print(__FUNCTION__,             ": failed to move the anchor point! Error code = ",GetLastError());       return(false);      } //--- successful execution    return(true);   } //+------------------------------------------------------------------+ //| Delete Sell sign                                                 | //+------------------------------------------------------------------+ bool ArrowSellDelete(const long   chart_ID=0,       // chart's ID                      const string name="ArrowSell") // sign name   { //--- reset the error value    ResetLastError(); //--- delete the sign    if(!ObjectDelete(chart_ID,name))      {       Print(__FUNCTION__,             ": failed to delete \"Sell\" sign! Error code = ",GetLastError());       return(false);      } //--- successful execution    return(true);   } //+------------------------------------------------------------------+ //| Check anchor point values and set default values                 | //| for empty ones                                                   | //+------------------------------------------------------------------+ void ChangeArrowEmptyPoint(datetime &time,double &price)   { //--- if the point's time is not set, it will be on the current bar    if(!time)       time=TimeCurrent(); //--- if the point's price is not set, it will have Bid value    if(!price)       price=SymbolInfoDouble(Symbol(),SYMBOL_BID);   } bool ArrowBuyCreate(const long            chart_ID=0,        // chart's ID                     const string          name="ArrowBuy",   // sign name                     const int             sub_window=0,      // subwindow index                     datetime              time=0,            // anchor point time                     double                price=0,           // anchor point price                     const color           clr=C'3,95,172',   // sign color                     const ENUM_LINE_STYLE style=STYLE_SOLID, // line style (when highlighted)                     const int             width=1,           // line size (when highlighted)                     const bool            back=false,        // in the background                     const bool            selection=false,   // highlight to move                     const bool            hidden=true,       // hidden in the object list                     const long            z_order=0)         // priority for mouse click   { //--- set anchor point coordinates if they are not set    ChangeArrowEmptyPoint(time,price); //--- reset the error value    ResetLastError(); //--- create the sign    if(!ObjectCreate(chart_ID,name,OBJ_ARROW_BUY,sub_window,time,price))      {       Print(__FUNCTION__,             ": failed to create \"Buy\" sign! Error code = ",GetLastError());       return(false);      } //--- set a sign color    ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); //--- set a line style (when highlighted)    ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style); //--- set a line size (when highlighted)    ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width); //--- display in the foreground (false) or background (true)    ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); //--- enable (true) or disable (false) the mode of moving the sign by mouse    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); //--- hide (true) or display (false) graphical object name in the object list    ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); //--- set the priority for receiving the event of a mouse click in the chart    ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); //--- successful execution    return(true);   } //+------------------------------------------------------------------+ //| Move the anchor point                                            | //+------------------------------------------------------------------+ bool ArrowBuyMove(const long   chart_ID=0,      // chart's ID                   const string name="ArrowBuy", // object name                   datetime     time=0,          // anchor point time coordinate                   double       price=0)         // anchor point price coordinate   { //--- if point position is not set, move it to the current bar having Bid price    if(!time)       time=TimeCurrent();    if(!price)       price=SymbolInfoDouble(Symbol(),SYMBOL_BID); //--- reset the error value    ResetLastError(); //--- move the anchor point    if(!ObjectMove(chart_ID,name,0,time,price))      {       Print(__FUNCTION__,             ": failed to move the anchor point! Error code = ",GetLastError());       return(false);      } //--- successful execution    return(true);   } //+------------------------------------------------------------------+ //| Delete Buy sign                                                  | //+------------------------------------------------------------------+ bool ArrowBuyDelete(const long   chart_ID=0,      // chart's ID                     const string name="ArrowBuy") // sign name   { //--- reset the error value    ResetLastError(); //--- delete the sign    if(!ObjectDelete(chart_ID,name))      {       Print(__FUNCTION__,             ": failed to delete \"Buy\" sign! Error code = ",GetLastError());       return(false);      } //--- successful execution    return(true);   }

Kindly, suggest me what will make the code show up down arrow onTick function.

Lakshan Perera
5728
Lakshan Perera  
jafferwilson:

I am trying to get the up and down buy/sell arrows on the graph but not able to place it at every tick. Here is the code for my logic:

Kindly, suggest me what will make the code show up down arrow onTick function.

Where is the code in the OnTick() 
you need to Create an Arrow by calling ArrowSellCreate(with parameters); in the OnTick()

jaffer wilson
769
jaffer wilson  
Lakshan Perera:

Where is the code in the OnTick() 
you need to Create an Arrow by calling ArrowSellCreate(with parameters); in the OnTick()

I simply did this:  

void OnTick()
{
ArrowSellCreate();
ArrowBuyCreate();
}

It should give both arrows on each bar

aslkdjf
22
aslkdjf  

There's a couple of libraries that do what you're attempting to write from scratch. I'd recommend the CChartObject classes. Here's a quickie example of repurposing the CChartObjectArrow class...

//+------------------------------------------------------------------+
//|                                                   TickArrows.mq5 |
//|                                  Copyright 2018, ;alskdjf;lakjsd |
//|                                                         dasfasdf |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, ;alskdjf;lakjsd"
#property link      "dasfasdf"
#property version   "1.00"
#property indicator_chart_window

#include <ChartObjects\ChartObjectsArrows.mqh>

class CTickArrow : public CChartObjectArrow 
{
   double      m_last_bid;
   datetime    m_last_time;
 public:
   void        refresh(double bid,datetime bar_time);
   bool        create();
};
//+------------------------------------------------------------------+
CTickArrow arrow;
int OnInit()
  {
   if(!arrow.create())
      return INIT_FAILED;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   arrow.refresh(
      close[rates_total-1],
      time[rates_total-1]
   );
   return(rates_total);
  }
//+------------------------------------------------------------------+

//src
void CTickArrow::refresh(double bid,datetime bar_time)
{  
   if(!(bid != m_last_bid || TimeCurrent() > m_last_time + PeriodSeconds(_Period)))
      return;
   bool up = bid >= m_last_bid;
   m_last_bid = bid;
   m_last_time=bar_time;
   this.Price(0,bid);
   this.Time(0,bar_time+PeriodSeconds(_Period));
   this.ArrowCode(up?(char)233:(char)234);
   this.Color(up?clrLime:clrRed);
   this.Anchor(up?ANCHOR_BOTTOM:ANCHOR_TOP);
}
//+------------------------------------------------------------------+
bool CTickArrow::create()
{
   return this.Create(0,"display_arrow",0,0,0.0,char(233));
}
Lakshan Perera
5728
Lakshan Perera  
jafferwilson:

I simply did this:  

It should give both arrows on each bar

This is because name of each arrow should be unique. In your case all the Sell arrows has the same name, "ArrowSell" , therefore you will encounter the error 4200..
A new arrow named "ArrowSell" cannot be made  if there is an arrow already existing on the chart named "ArrowSell"... I suggest you to use loop to change the name of the arrows or if this is for an indicator(I know in this particular case you use OnTick() so it is for an EA) use buffers(arrays[]).. Hope that's clear.

To add comments, please log in or register