Detected Country () : English
Arrow Object - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Arrow Object

Level Advance [2018-02-05 14:15:11] - by hillzx
Featured Image

Usage of arrow is quite popular used by the user as indicator marker. The arrow icon is a character of the Wingdings font, see the following picture:

wingdings

The arrow points downwards using the 218th, while the arrow points upwards using the 217th. Write the code inside the variable in this way:

 

string RedArrow=CharToStr(218);
string GreenArrow=CharToStr(217);



OK, for example, we will try to practice making indicators to look for candles that have shadow / tail which is longer than body.

First we make the following variables:

 

string RedArrow=CharToStr(218);
string GreenArrow=CharToStr(217);
extern color ArrowColorUp=Lime;
extern color ArrowColorDown=Red;
extern int MinimumShadow=500;
int oldTime;

We are using pippete / points (5 decimal price digits). Here we take the minimum example of the candle length is 500 points. Variable oldtime is a variable to provide delay so that the process is executed only every new candle appears.

 

Next we write the code inside OnInit:

oldtime = current time set.

DeleteObjects = to clear the screen.

DoJob = Perform the shadow search process.

 

int OnInit()
  {
//--- indicator buffers mapping
   oldTime=Time[0];
   DeleteObjects();
   DoJob();
//---
   return(INIT_SUCCEEDED);
  }

Type the following code inside OnCalculate : 

 

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[])
  {
//---
if(oldTime != Time[0] )
{
 oldTime = Time[0];
 DoJob();
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }

The above code means execute DoJob Module every new candle turnover.



Continue with sub DeleteObjects to clear the screen of the object:

 

void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HiLLzXShadowFinder") >= 0) ObjectDelete(ObjectName(i));
  }
}

Continue with IsCandleTrendUp function to check if the candle is up or down:

 

bool IsCandleTrendUp(int index){

if (Open[index]>Close[index]){
   return false;
} else {
   return true;
}

}

Continue with CreateArrow sub to create an arrow:

 

void CreateArrow(bool isUpTrend,int index){
   if (isUpTrend){
      ObjectCreate("HiLLzXShadowFinder"+ index , OBJ_ARROW, 0, Time[index], Low[index]-40*Point); //draw an up arrow
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_COLOR,ArrowColorUp); 
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_WIDTH,6); 
   } else {
      ObjectCreate("HiLLzXShadowFinder"+ index , OBJ_ARROW, 0, Time[index], High[index]+350*Point); //draw an up arrow
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_COLOR,ArrowColorDown); 
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_WIDTH,6); 
   }
}

Continue with the IsShadowBiggerThanBody function to check whether the shadow is larger than the body:

 

bool IsShadowBiggerThanBody(bool isUpTrend, int index){
   int curShadowLength;
   int curBodyLength;
   
if (isUpTrend){
   curShadowLength=( Open[index] - Low[index] ) / Point;
   curBodyLength=( Close[index] - Open[index] ) / Point;
   Print("Shadow Length : " + curShadowLength);
   Print("Body Length : " + curBodyLength);
} else {
   curShadowLength=( High[index] - Open[index] ) / Point;
   curBodyLength=( Open[index] - Close[index] ) / Point;
   Print("Shadow Length : " + curShadowLength);
   Print("Body Length : " + curBodyLength);
}

if ( (curShadowLength>curBodyLength) && (curShadowLength>MinimumShadow) ){
   return true;
} else {
   return false;
}

}



Finally we create the DoJob sub as the main process:

 

void DoJob(){

for(int i=Bars-1;i>0;i--)
{
bool isUpTrend=IsCandleTrendUp(i);

   if (isUpTrend){
      if (IsShadowBiggerThanBody(isUpTrend,i)){
         CreateArrow(isUpTrend,i);
      } 
   } else {
      if (IsShadowBiggerThanBody(isUpTrend,i)){
         CreateArrow(isUpTrend,i);
      } 
   }


}

It's done, we can use this indicator on the chart:

shadowfinderinput

On the screen will appear green and red arrows on the candle that has shadow / tail at least 500 points AND shadow larger than the body.

shadowfinderchart

Here's complete source code:

 

//+------------------------------------------------------------------+
//|                                                Shadow Finder.mq4 |
//|                                                 hillzx@gmail.com |
//|                                           http://codingforex.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "http://codingforex.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

string RedArrow=CharToStr(218);
string GreenArrow=CharToStr(217);
extern color ArrowColorUp=Lime;
extern color ArrowColorDown=Red;
extern int MinimumShadow=500;
int oldTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   oldTime=Time[0];
   DeleteObjects();
   DoJob();
//---
   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[])
  {
//---
if(oldTime != Time[0] )
{
 oldTime = Time[0];
 DoJob();
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HiLLzXShadowFinder") >= 0) ObjectDelete(ObjectName(i));
  }
}

bool IsCandleTrendUp(int index){

if (Open[index]>Close[index]){
   return false;
} else {
   return true;
}

}

void CreateArrow(bool isUpTrend,int index){
   if (isUpTrend){
      ObjectCreate("HiLLzXShadowFinder"+ index , OBJ_ARROW, 0, Time[index], Low[index]-40*Point); //draw an up arrow
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_COLOR,ArrowColorUp); 
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_WIDTH,6); 
   } else {
      ObjectCreate("HiLLzXShadowFinder"+ index , OBJ_ARROW, 0, Time[index], High[index]+350*Point); //draw an up arrow
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_COLOR,ArrowColorDown); 
      ObjectSet("HiLLzXShadowFinder"+ index, OBJPROP_WIDTH,6); 
   }
}

bool IsShadowBiggerThanBody(bool isUpTrend, int index){
   int curShadowLength;
   int curBodyLength;
   
if (isUpTrend){
   curShadowLength=( Open[index] - Low[index] ) / Point;
   curBodyLength=( Close[index] - Open[index] ) / Point;
   Print("Shadow Length : " + curShadowLength);
   Print("Body Length : " + curBodyLength);
} else {
   curShadowLength=( High[index] - Open[index] ) / Point;
   curBodyLength=( Open[index] - Close[index] ) / Point;
   Print("Shadow Length : " + curShadowLength);
   Print("Body Length : " + curBodyLength);
}

if ( (curShadowLength>curBodyLength) && (curShadowLength>MinimumShadow) ){
   return true;
} else {
   return false;
}

}

void DoJob(){

for(int i=Bars-1;i>0;i--)
{
bool isUpTrend=IsCandleTrendUp(i);

   if (isUpTrend){
      if (IsShadowBiggerThanBody(isUpTrend,i)){
         CreateArrow(isUpTrend,i);
      } 
   } else {
      if (IsShadowBiggerThanBody(isUpTrend,i)){
         CreateArrow(isUpTrend,i);
      } 
   }


}

}


Good luck, do not forget to share and follow our official facebook and youtube, happy trading!



Previous Article

Featured Image

Text Object

Next Article

Featured Image

Button Object




- Provided by HiLLzSoft Studio -