Trouble with creating/placing objects in indicator

 

Hi all,

I'm trying to allow the indicator I'm building to signal entries for me by printing an object (an arrow) over/beneath the bar that the signal happened at.

Here's what I've got so far – it's not working, and I'm unsure where I'm off. My buySellSignal buffer is being filled with the proper values but the printing of an object on those candles is not. Here's the object code:

Any help is appreciated!

if (buySellSignal[i] == 1)
   {
      string ArrowName = StringConcatenate("Buy Signal" + Bars);
      ObjectCreate(ArrowName, OBJ_ARROW, 0, Time[i], Open[1] - 10 * Point);
      ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(ArrowName, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
      ObjectSet(ArrowName, OBJPROP_COLOR,ForestGreen);
      ObjectSet(ArrowName, OBJPROP_WIDTH,3);
   }
 
rrsch: I'm trying to allow the indicator I'm building to signal entries for me by printing an object (an arrow) over/beneath the bar that the signal happened at. Here's what I've got so far – it's not working, and I'm unsure where I'm off. My buySellSignal buffer is being filled with the proper values but the printing of an object on those candles is not. Here's the object code: Any help is appreciated!

If you are building an Indicator and all you want is to place an "arrow" character, then use a DRAW_ARROW or DRAW_COLOR_ARROW plot buffer instead of using graphical objects. It will save you a lot of hassle and be more code efficient.

EDIT: My apologies. I thought your question was about MQL5 and I reference the "DRAW_COLOUR_ARROW" which is not available on MQL4.

 

There are several "arrow" plot/buffer indicators in the CodeBase that you can use as a reference. Here is one I found on a quick search, for DRAW_ARROW :

Code Base

Strength Arrow

Khurram Mustafa, 2016.03.18 16:18

This is an indicator which totally based on RSI. The reason to amend this indicator is knowing the strength of a currency.
EDIT: I updated this post to reference MQL4 examples instead of my original MQL5 examples.

 
rrsch:

Hi all,

I'm trying to allow the indicator I'm building to signal entries for me by printing an object (an arrow) over/beneath the bar that the signal happened at.

Here's what I've got so far – it's not working, and I'm unsure where I'm off. My buySellSignal buffer is being filled with the proper values but the printing of an object on those candles is not. Here's the object code:

Any help is appreciated!

There is a problem in your code here (all arrows of the same type have the same name):

if (buySellSignal[i] == 1)
   {
      string ArrowName = StringConcatenate("Buy Signal" + Bars);
      //...
   }

For example, you can fix it like this:

if (buySellSignal[i] == 1)
   {
      string ArrowName = "Buy Signal" + TimeToString(Time[i]);
   }

But it will still be bad even if it starts to work. It's best to do this:

Forum on trading, automated trading systems and testing trading strategies

Trouble with creating/placing objects in indicator

Fernando Carreiro, 2022.08.24 00:34

If you are building an Indicator and all you want is to place an "arrow" character, then use a DRAW_ARROW or DRAW_COLOR_ARROW plot buffer instead of using graphical objects. It will save you a lot of hassle and be more code efficient.

 
Fernando Carreiro #:

If you are building an Indicator and all you want is to place an "arrow" character, then use a DRAW_ARROW or DRAW_COLOR_ARROW plot buffer instead of using graphical objects. It will save you a lot of hassle and be more code efficient.

EDIT: My apologies. I thought your question was about MQL5 and I reference the "DRAW_COLOUR_ARROW" which is not available on MQL4.

This option is working great, mostly. My only problem now is I'm unable to adjust the size of the arrow. As is it's pretty tiny. Entering a value for the width property seems to have no effect. I'm searching and don't see any other way to resize. Am I missing something?

SetIndexStyle(2, DRAW_ARROW,,5);

prints on my chart the same size as 

SetIndexStyle(2, DRAW_ARROW,,1);
 
rrsch #: This option is working great, mostly. My only problem now is I'm unable to adjust the size of the arrow. As is it's pretty tiny. Entering a value for the width property seems to have no effect. I'm searching and don't see any other way to resize. Am I missing something? prints on my chart the same size as 

It works just fine for me ...

// either this at the begining ...

   #property indicator_type2  DRAW_ARROW
   #property indicator_style2 STYLE_SOLID
   #property indicator_width2 3

// or this in OnInit() ...

   SetIndexStyle( 2, DRAW_ARROW, STYLE_SOLID, 3 ); 
EDIT: Remember to use "solid" for the width to have effect.
 
Fernando Carreiro #:

It works just fine for me ...

EDIT: Remember to use "solid" for the width to have effect.

Ah, the style property does it. Thank you!

 

A side question...

Normally when I create a comment after an input, the Inputs list in the indicator properties will show that text instead of the buffer name.

That's not happening for me in this indicator, and I'm unsure why. Is there some other property that needs to be enabled or anything?

input int per1 = 20;//Period 1
input double atrMult1 = 1;//ATR Multiplier 1
input int per2 = 55;//Period 2
input double atrMult2 = 2;//ATR Multiplier 2
input int arrowSize = 2;//Arrow Size

RF inputs

 
rrsch #: A side question... Normally when I create a comment after an input, the Inputs list in the indicator properties will show that text instead of the buffer name. That's not happening for me in this indicator, and I'm unsure why. Is there some other property that needs to be enabled or anything?


Have you tried placing a space on either side of the "//"?

Maybe something else in your code is causing the issue.

EDIT: And remember to use "#property strict".

 

#property strict makes the Properties dialog work, but now my indicator disappears from my chart!! I've compared it to other indicators that are working and I can't see what might be the problem!

#property indicator_chart_window
#property strict
#property indicator_buffers 9
#property indicator_color1 Silver
#property indicator_color2 White
#property indicator_color3 ForestGreen
#property indicator_color4 FireBrick

//---- input parameters
input int per1 = 20;//Fast Period
input double atrMult1 = 1;// Fast ATR Multiplier 
input int per2 = 55;// Slow Period
input double atrMult2 = 2;// Slow ATR Multiplier
//input int arrowSize = 5;// Arrow Size

//---- buffers
double rngfilt1[];
double rngfilt2[];

double buySignal[];
double sellSignal[];

double avrng1[];
double avrngEMA1[];
double smrng1[];

double avrng2[];
double avrngEMA2[];
double smrng2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   
   int draw = 0;
   
   SetIndexBuffer(0, rngfilt1);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "rngfilt1");
   
   SetIndexBuffer(1, rngfilt2);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexLabel(1, "rngfilt2");
   
   SetIndexBuffer(2, buySignal);
   SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 5);
   SetIndexArrow(2,233);
   SetIndexDrawBegin(2,draw);
   SetIndexLabel(2, "Buy Signal");
   
   SetIndexBuffer(3, sellSignal);
   SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 5);
   SetIndexArrow(3,234);
   SetIndexDrawBegin(3,draw);
   SetIndexLabel(3, "Sell Signal");
  
   SetIndexBuffer(4, avrng1);
   SetIndexLabel(4, "avrng1");
   
   SetIndexBuffer(5, avrngEMA1);
   SetIndexLabel(5, "avrngEMA1");
   
   SetIndexBuffer(6, smrng1);
   SetIndexLabel(6, "smrng1");
   
   SetIndexBuffer(7, avrng2);
   SetIndexLabel(7, "avrng2");
   
   SetIndexBuffer(7, avrngEMA2);
   SetIndexLabel(7, "avrngEMA2");
   
   SetIndexBuffer(8, smrng2);
   SetIndexLabel(8, "smrng2");

   
   
//----
   IndicatorShortName("TRF");
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
   int counted_bars = IndicatorCounted();
   int limit, i;
   limit = Bars - counted_bars - 1;

//---Create Smoothing 1
   for(i = Bars - 1; i >= 0; i--) 
   {
      avrng1[i] = MathAbs(Close[i] - Close[i+1]);
   }
   
   for(i = Bars - 1; i >= 0; i--) 
   {
      avrngEMA1[i] = iMAOnArray(avrng1, 0, per1, 0, MODE_EMA, i);
   }
   
   for(i = Bars - 1; i >= 0; i--) 
   {
      smrng1[i] = iMAOnArray(avrngEMA1, 0, per1 * 2 - 1, 0, MODE_EMA, i) * atrMult1;
   }

//---Create Range Filter Value 1
   for(i = Bars - 1; i >= 1; i--) 
   {
   rngfilt1[i] = Close[i];
    
   if (Close[i] > rngfilt1[i+1])
      {if (Close[i] - smrng1[i] < rngfilt1[i+1])
         {rngfilt1[i] = rngfilt1[i+1];}
      else
         {rngfilt1[i] = Close[i] - smrng1[i];}
      }      
   else if (Close[i] + smrng1[i] > rngfilt1[i+1])
      {rngfilt1[i] = rngfilt1[i+1];}   
   else
      {rngfilt1[i] = Close[i] + smrng1[i];}
   }
   
//---Create Smoothing 2 
   for(i = Bars - 1; i >= 0; i--) 
   {
      avrng2[i] = MathAbs(Close[i] - Close[i+1]);
   }
   
   for(i = Bars - 1; i >= 0; i--) 
   {
      avrngEMA2[i] = iMAOnArray(avrng1, 0, per2, 0, MODE_EMA, i);
   }
   
   for(i = Bars - 1; i >= 0; i--) 
   {
      smrng2[i] = iMAOnArray(avrngEMA2, 0, per2 * 2 - 1, 0, MODE_EMA, i) * atrMult2;
   }

//---Create Range Filter Value 2
   for(i = Bars - 1; i >= 1; i--) 
   {
   rngfilt2[i] = Close[i];
    
   if (Close[i] > rngfilt2[i+1])
      {if (Close[i] - smrng2[i] < rngfilt2[i+1])
         {rngfilt2[i] = rngfilt2[i+1];}
      else
         {rngfilt2[i] = Close[i] - smrng2[i];}
      }      
   else if (Close[i] + smrng2[i] > rngfilt2[i+1])
      {rngfilt2[i] = rngfilt2[i+1];}   
   else
      {rngfilt2[i] = Close[i] + smrng2[i];}
   }
   
   //---Create Buy/Sell Signals
   for(i = Bars - 1; i >= 1; i--) 
   {
   if (rngfilt1[i+1] < rngfilt2[i+1] && rngfilt1[i] > rngfilt2[i])
   {
      buySignal[i] = Low[i+1]-25*Point;
   }   
   if (rngfilt1[i+1] > rngfilt2[i+1] && rngfilt1[i] < rngfilt2[i])
   {
      sellSignal[i] = High[i+1]+25*Point;
   }
   }
   

   return(0);
}
 

You are still using the very old style of MQL4 coding (which I personally consider almost obsolete).

Please consider the more modern MQL4+ instead. By that I mean the use of OnInit(), OnCalclate(), etc.

Reason: