Download MetaTrader 5

Newbie Trying To Make Simple Indicator

To add comments, please log in or register
uyin
14
uyin  

Hi,

Newbie here.

I want to make simple indicator but  error. I'm stuck. Actually this is my first indi to make.

 

Formula :  

Indicator Signal = ((Highest Hi previous 10 period  -  Lowest Low previous 10 period) / 3) +   Lowest Low previous 10 period 

 

Anyone can help me?

 ------------- edited by moderator to include SRC  -------------

//+------------------------------------------------------------------+
//|                                                     310-indi.mq5 |
//|                        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 indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Signal
#property indicator_label1  "Signal"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,SignalBuffer,INDICATOR_DATA);
  
  
//---
   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[])
  {
//---

double HH[], LL[], Range [];

ArraySetAsSeries(HH,true);
ArraySetAsSeries(LL,true);
ArraySetAsSeries(Range,true);


int i;
for(i=prev_calculated;i<rates_total;i++)
{

HH[i]  = ArrayMaximum (high,0,11);
LL[i] = ArrayMinimum (low,0,11);
Range[i] = round((HH[i]-LL[i])/3);
double newLL = LL[i]+Range[i];

SignalBuffer[i] = newLL;
}
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Files:
310-indi.mq5 3 kb
Alain Verleyen
Moderator
35118
Alain Verleyen  
There are so much problems in such a little code. You need to learn, see this to start.
What you should know about indicators
What you should know about indicators
  • www.mql5.com
The next article is Applying One Indicator to Another.
uyin
14
uyin  
Alain Verleyen:
There are so much problems in such a little code. You need to learn, see this to start.

Tq..I already add buffer..

 

But how to get Higher high in previous 10 data? Help me.. my loop problem.. 

Files:
310-indi.mq5 3 kb
Alain Verleyen
Moderator
35118
Alain Verleyen  
uyin:

Tq..I already add buffer..

 

But how to get Higher high in previous 10 data? Help me.. my loop problem.. 

You don't need CopyHigh/CopyLow as you already have all history available as OnCalculate() parameters.

ArrayMaximum/ArrayMinimum is the correct way (though not the fastest solution to apply it on each candle, but forget that for now). However you have to pay attention to the indexing, in MT5 arrays/buffers are not indexed as series by default (see ArraySetAseries()). Also these functions returns an index not the maximum itself.
int highest  = ArrayMaximum (high,i,10);

HH[i]=high[highest];
Mladen Rakic
72263
Mladen Rakic  
Skip the round in this line too :

Range[i] = round((HH[i]-LL[i])/3);
Your range will result in 0 almost all the time
uyin
14
uyin  
Ty all..I almost finish..but only fail at line:


CL[bar]= close[ArrayMinimum(close,bar-i,10)];

"Error : Array Out of Range"

Why this happen?
Files:
310_3.mq5 3 kb
Mladen Rakic
72263
Mladen Rakic  
uyin:
Ty all..I almost finish..but only fail at line:


CL[bar]= close[ArrayMinimum(close,bar-i,10)];

"Error : Array Out of Range"

Why this happen?
Wrong parameters and your ArrayMinimum() returns -1 as a result. Check the parameters for the function call
To add comments, please log in or register