Hi I'm trying to create a custom indicator, and I'm having trouble with what I need to do next in my code.
There is obviously a lot which I need to add and perhaps some that I need to leave out. However, I'm finding it difficult what I need to do next, at the moment my indicator compiles with no errors, but nothing happens when attached to a chart.
So what I want my indicator to do is:
check the last three candles o the chart so 0,1 and 2.
if the close of candle 1 is greater than the open of candle 1 AND the close of candle 2 is less than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Support. Red Line
if the close of candle 1 is less than the open of candle 1 AND the close of candle 2 is greater than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Resistance. Blue Line
I understand an indicator can have 8 buffers in MT4, so I would like a max of 4 of each on a chart at any one time. So if there are already 4 support lines on the chart and the indicator detects another support line to be drawn it will delete the oldest line then create the new one. Please look at the picture to see what I want.
I have attached the what I have so far of the indicator (which I know is not much) to this post for anyone that would be so kind enough to help me by telling what I need to do in order to get it working as I want.
I appreciate your time.
#property copyright"Copyright 2015, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"#property strict#property indicator_chart_window#property indicator_buffers8#property indicator_color1 clrRed// Color of the 1st line#property indicator_color2 clrBlue// Color of the 2nd lineexterncolor UpperClr=Blue;
externcolor LowerClr=Red;
externint LineStyle=STYLE_SOLID;
externint LineWidth=1;
double Support,Resistance;
int BarsOnChart;
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit()
{
/*--- indicator buffers mapping
SetIndexBuffer(0,Support);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrRed);// Line style
SetIndexLabel(0,"Support");
SetIndexBuffer(1,Resistance);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clrBlue);// Line style
SetIndexLabel(1,"Resistance");
*/return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+//the indicator programme should only run when a new candle is formed.bool IsNewCandle()
{
if (Bars==BarsOnChart)
return(false);
BarsOnChart = Bars;
return(true);
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+double draw_support()
{
ObjectCreate(ChartID(),"LowerLine",OBJ_HLINE,0,0,iLowest(NULL,0,3,2,0));
ObjectSet("LowerLine",OBJPROP_COLOR,LowerClr);
ObjectSet("LowerLine",OBJPROP_WIDTH,LineWidth);
ObjectSet("LowerLine",OBJPROP_STYLE,LineStyle);
return(0);
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+double draw_resistance()
{
ObjectCreate(ChartID(),"UpperLine",OBJ_HLINE,0,0,iHighest(NULL,0,3,2,0));
ObjectSet("UpperLine",OBJPROP_COLOR,LowerClr);
ObjectSet("UpperLine",OBJPROP_WIDTH,LineWidth);
ObjectSet("UpperLine",OBJPROP_STYLE,LineStyle);
return(0);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
int Uncounted_bars,Counted_bars;// Bar index, Number of counted barsint i;
Counted_bars=IndicatorCounted();
if(Counted_bars<0)return(-1);
if(Counted_bars>0) Counted_bars--;
Uncounted_bars=Bars-Counted_bars-1;
for(i=0; i<Uncounted_bars; i++)
{
Support=iLowest(NULL,0,3,2,0);
Resistance=iHighest(NULL,0,3,2,0);
}
{
if(IsNewCandle())
if(Close[1]>=Open[1] && Close[2]<=Open[2])draw_support();
}
{
if(IsNewCandle())
if(Close[1]<=Open[1] && Close[2]>=Open[2])draw_resistance();
}
return(rates_total);
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi I'm trying to create a custom indicator, and I'm having trouble with what I need to do next in my code.
There is obviously a lot which I need to add and perhaps some that I need to leave out. However, I'm finding it difficult what I need to do next, at the moment my indicator compiles with no errors, but nothing happens when attached to a chart.
So what I want my indicator to do is:
- check the last three candles o the chart so 0,1 and 2.
- if the close of candle 1 is greater than the open of candle 1 AND the close of candle 2 is less than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Support. Red Line
- if the close of candle 1 is less than the open of candle 1 AND the close of candle 2 is greater than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Resistance. Blue Line
I understand an indicator can have 8 buffers in MT4, so I would like a max of 4 of each on a chart at any one time. So if there are already 4 support lines on the chart and the indicator detects another support line to be drawn it will delete the oldest line then create the new one. Please look at the picture to see what I want.I have attached the what I have so far of the indicator (which I know is not much) to this post for anyone that would be so kind enough to help me by telling what I need to do in order to get it working as I want.
I appreciate your time.