how to draw an arrow with mql4 in all of the chart?

user3741124 picture user3741124 · Jun 24, 2017 · Viewed 7.7k times · Source

my problem is that i want to draw an up arrow(Green) and down arrow(Red) in bullish candle and bearish candle respectively in all of the history of specific currency chart here is my code so far

    //+------------------------------------------------------------------+
//|                                                  PriceAction.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

DrawArrowUp("up"+Bars,Close[1]+10*Point,Lime);

//---
   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---


//--- return value of prev_calculated for next call
   return(rates_total);
 }
//+------------------------------------------------------------------+
void DrawArrowUp(string ArrowName,double LinePrice,color LineColor)
{
ObjectCreate(ArrowName, OBJ_ARROW, 0, Time[0], LinePrice); //draw an up arrow
ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(ArrowName, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
ObjectSet(ArrowName, OBJPROP_COLOR,LineColor);
}

void DrawArrowDown(string ArrowName,double LinePrice,color LineColor)
{
ObjectCreate(ArrowName, OBJ_ARROW, 0, Time[0], LinePrice); //draw an up arrow
ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(ArrowName, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
ObjectSet(ArrowName, OBJPROP_COLOR,LineColor);
}

but it only draw the arrow on the last bar , and i want it in all of the chart candles thanks ,

Answer

Daniel Kniaz picture Daniel Kniaz · Jun 24, 2017

in your functions DrawArrowUp() and DrawArrowDn() you call mt4 function ObjectCreate() that requires name, object type, time and price. since you place all objects on Time[0] - maybe you can have many arrows on the same (last)candle.

const string PREFIX = "ALL_BARS_ARROWS";//to easily delete all objects in OnDeinit()
 void DrawArrow(double linePrice,datetime time,bool bullish){
    string name = PREFIX+"arrow"+(bullish?"up":"down")+IntegerToString(time);
    ObjectCreate(name,OBJ_ARROW,0,time,linePrice);
    ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet(name, OBJPROP_ARROWCODE, bullish?SYMBOL_ARROWUP:SYMBOL_ARROWDOWN);
    ObjectSet(name, OBJPROP_COLOR, bullish? clrLime : clrRed);
}

More options to create and edit properties of an arrow can be found here

Now in the OnCalculate() function:

int limit, i;
if(prev_calculated==0){
   limit = rates_total-1;
}else{
   limit = rates_total - prev_calculated;
}
bool isCandleBullish;
for(i=limit; i>0; i--){
   isCandleBullish = close[i]>open[i];//think of doji candles also
   DrawArrow(Close[i]+10*Point*(isCandleBullish?1:-1),time[i],isCandleBullish);
}