I recently develop an indicator that detects support and resistance levels of the 100 previous peaks and troughs. I am trying to implement the data and calculations of this indicator to an EA I am trying to develop but I am having some problems. Since the indicator detects structure levels, it does not output data every candle (just when it identifies a new relevant structure levels).
So the problem I am having, as you might expect, is that when I try to set a buffer to retrieve the data from the indicator, using SetIndxBuffer(), I get my outputs (data/calculations) stored in the most recent candles of the market. By this I mean that the data points from my array SRLevels[], which stores the values where the structure levels were found, are stored within every new candle of the market instead of being stored in the candle of the chart where the new structure level was found (indexed in order). Below I leave attached a picture of my MT5 so that you can understand better (in green you can see the value that I am getting for the first index/candle of the chart). Since I am a newbie, I can understand all the code but I am not completely sure where to implement changes in it, so could you guys help me out?
#property indicator_buffers1#property indicator_applied_pricePRICE_TYPICAL#property indicator_plots1//ZigZag inputsinputint InpDepth = 12;
inputint InpDeviation = 5;
inputint InpBackstep = 3;
//Peak analysis inputsinputint InpGapPoints = 100; //gap between peak/trophs points inputint InpSensitivity = 2; //how many times it touches the levelinputint InpLookback = 50; //how many peaks we look back//Drawing Supports and Resistancesinputstring InpPrefix = "SRLevel_"; //name of the lineinputcolor InpColour = clrYellow;
inputint InpLineWidth = 2;
//Store Levels(supports and resistances)double SRLevels[];
//Store ZigZag datadouble Buffer[];
int Handle;
intOnInit()
{
Handle = iCustom(Symbol(), Period(), "Market\\ArrowZigZag",InpDepth, InpDeviation, InpBackstep); //ZigZag indicator for support and resistance levelsif(Handle == INVALID_HANDLE){
Print ("Could not create a handle to zigzag indicator");
return(INIT_FAILED);
}
ArraySetAsSeries (Buffer, true);
ArraySetAsSeries(SRLevels, true);
ObjectsDeleteAll(0,InpPrefix, 0, OBJ_HLINE);
ArrayResize(SRLevels, InpLookback);
SetIndexBuffer(0, SRLevels, INDICATOR_DATA);
string shortname;
StringConcatenate(shortname, "Structure Level (",InpDepth, ",", InpDeviation, ",",InpBackstep, ")" ); //set a label do display in DataWindowPlotIndexSetString(0,PLOT_LABEL,shortname); //set a name to show in a separate sub-window or a pop-up helpIndicatorSetString(INDICATOR_SHORTNAME,shortname); //set accuracy of displaying the indicator valuesIndicatorSetInteger(INDICATOR_DIGITS,5);
ChartRedraw(0);
return(INIT_SUCCEEDED);
}
voidOnDeinit(constint reason){
Print("EA STOPPED or DEINITIALIZED");
IndicatorRelease(Handle); //release zigzag indicatorObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE); //delete all objects in the chart
}
//STRUCTURE FINDER (support and resistance)intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spred[]){
//Convert pips to price gapstaticdouble levelGap = InpGapPoints*SymbolInfoDouble(Symbol(), SYMBOL_POINT);
//if there is an error with retrieving dataif (rates_total==prev_calculated) return(rates_total);
//Get data from peaksdouble zz = 0;
double zzPeaks [];
int zzCount = 0;
ArrayResize(zzPeaks, InpLookback);
ArrayInitialize(zzPeaks, 0.0);
int count = CopyBuffer(Handle, 0, 0, rates_total, Buffer);
if (count<0){
int err=GetLastError();
return (0);
}
for(int i=1; i<rates_total && zzCount<InpLookback; i++){
zz = Buffer[i];
if (zz!=0 && zz!=EMPTY_VALUE){
zzPeaks[zzCount] = zz;
zzCount++;
}
}
ArraySort(zzPeaks);
//Search for points where peaks touched as many times as previously inputedint srCounter = 0;
double price;
int priceCount = 0;
ArrayInitialize (SRLevels, 0.0);
for (int i=InpLookback-1; i>=0; i--){
price += zzPeaks[i];
priceCount++;
if (i==0 || (zzPeaks[i]-zzPeaks[i-1])>levelGap){
if (priceCount>=InpSensitivity){
price = price/priceCount;
SRLevels[srCounter] = price;
srCounter++;
}
price = 0;
priceCount = 0;
}
}
DrawLevels();
return (rates_total);
}
//fucntion to draw the different support/resistancesvoid DrawLevels(){
for (int i=0; i<InpLookback; i++){
string name = InpPrefix + IntegerToString(i);
if (SRLevels[i]==0){
ObjectDelete(0, name);
continue;
}
if (ObjectFind(0, name)<0){
ObjectCreate(0, name, OBJ_HLINE, 0, 0, SRLevels[i]);
ObjectSetInteger(0, name, OBJPROP_COLOR, InpColour);
ObjectSetInteger(0, name, OBJPROP_WIDTH, InpLineWidth);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);
} else{
ObjectSetDouble(0, name, OBJPROP_PRICE, SRLevels[i]);
}
}
ChartRedraw(0);
}
I have been looking around for other post or articles but I haven't been able to find an answer so if anyone knows how to solve this issue, please do not hesitate to give your thoughts.
Hi guys,
I recently develop an indicator that detects support and resistance levels of the 100 previous peaks and troughs. I am trying to implement the data and calculations of this indicator to an EA I am trying to develop but I am having some problems. Since the indicator detects structure levels, it does not output data every candle (just when it identifies a new relevant structure levels).
So the problem I am having, as you might expect, is that when I try to set a buffer to retrieve the data from the indicator, using SetIndxBuffer(), I get my outputs (data/calculations) stored in the most recent candles of the market. By this I mean that the data points from my array SRLevels[], which stores the values where the structure levels were found, are stored within every new candle of the market instead of being stored in the candle of the chart where the new structure level was found (indexed in order). Below I leave attached a picture of my MT5 so that you can understand better (in green you can see the value that I am getting for the first index/candle of the chart). Since I am a newbie, I can understand all the code but I am not completely sure where to implement changes in it, so could you guys help me out?
I have been looking around for other post or articles but I haven't been able to find an answer so if anyone knows how to solve this issue, please do not hesitate to give your thoughts.
Thank you