This reads the zigzag and keeps the values in a list .
All you have to do is read the list you have when a change is detected live .
#property copyright"See The Discussion"#property link"https://www.mql5.com/en/forum/439613"#property version"1.00"#property strict//ZigZag Version Used : https://www.mql5.com/en/code/7796/*
First we need the zigZag parameters
*/inputint InpDepth=12; // Depthinputint InpDeviation=5; // Deviationinputint InpBackstep=3; // Backstep/*
then we create a point to know where it is
we also need to know what the point is high or low
lets define that
*/#define zzPointHigh 1#define zzPointLow 2/*
what else does a point have ? time and price
so , lets create our custom "type" to hold that
*/class a_zz_point{
public:
double price;
datetime time;
/* as we deploy this we realize we might also need the bar distance
so , we decide that our reader will have a unique id that increases only with each new bar
and that will represent a bar in a sequence of reading .
So if we compare 2 zz points , and subtract the new zz unique id from the old zz unique id
we can get the bar distance
*/int uid;
/* and of course the type */char type;
/* constructor = the default values when we create our custom type */
a_zz_point(void){reset();}
~a_zz_point(void){reset();}
void reset(){
price=0.0;
time=0;
uid=-1;
type=-1;
}
//we also need to set it upvoid setup(double _price,datetime _time,int _unique_id,char _type){
price=_price;
time=_time;
uid=_unique_id;
type=_type;
}
//some convenient functions bool isLowerThan(double _this){
return((price<_this));
}
bool isHigherThan(double _this){
return((price>_this));
}
};
//awesome we have points now we need a collector , a list , somewhere to store them as we find themstruct zz_points_list{
//what does a zz_points list have ? zz_points
a_zz_point points[];
//what else does it have ? the linear unique id int unique_id;
//and , a bar timer to know when the bar changed datetime barstamp;
zz_points_list(void){reset();}
~zz_points_list(void){reset();}
void reset(){
//delete list ArrayFree(points);
//reset unique id
unique_id=0;
//reset the timestamp
barstamp=0;
}
//now , we will need to add a point int add_point(double _price,datetime _time,int _unique_id,char _type){
int ns=ArraySize(points)+1;
ArrayResize(points,ns,0);
points[ns-1].setup(_price,_time,_unique_id,_type);
return(ns-1);
}
//and to check a bar bool check_bar(int latest_bar_or_simulation_of){
//this function will return true if there is a change in the zigzagsbool was_there_a_change=false;
//the latest bar or simulation of represents our position//in the past or live , we are at the open price and open time //of that bar ,which means , that bar just started so the //latest complete bar is that +1 , lets hold it in a more convenient placeint i=latest_bar_or_simulation_of+1;
//but first , did the bar change ? if(Time[latest_bar_or_simulation_of]>barstamp)
{
//now we need to check the zigzag indicator ResetLastError();
int errors=0;
double highs=iCustom(_Symbol,_Period,"ZigZag.ex4",InpDepth,InpDeviation,InpBackstep,1,i);errors+=GetLastError();ResetLastError();
double lows=iCustom(_Symbol,_Period,"ZigZag.ex4",InpDepth,InpDeviation,InpBackstep,2,i);errors+=GetLastError();ResetLastError();
//if no errors if(errors==0)
{
//update the barstamp
barstamp=Time[latest_bar_or_simulation_of];
//increase unique id
unique_id++;
//new highif(highs!=0.0){
//we have a new high at location i //so what do we do now ? //if the latest point we stored was also a high we change it //the location of the latest point is int last_point=ArraySize(points)-1;
if(last_point>=0&&points[last_point].type==zzPointHigh){
//change it //our new high is at location [i] so pass that high price and that time (and the type and the id)
points[last_point].setup(High[i],Time[i],unique_id,zzPointHigh);
//set the change switch on
was_there_a_change=true;
}
//but if the latest point we stored was not a high we create a new one elseif(last_point<0||points[last_point].type==zzPointLow){
//add a new one
add_point(High[i],Time[i],unique_id,zzPointHigh);
//set the change switch on
was_there_a_change=true;
}
}
//new low elseif(lows!=0.0){
//we have a new low at location i //if the latest point we stored was also a low we change it //the location of the latest point is int last_point=ArraySize(points)-1;
if(last_point>=0&&points[last_point].type==zzPointLow){
//change it //our new low is at location [i] so pass that low price and that time (and the type and the id)
points[last_point].setup(Low[i],Time[i],unique_id,zzPointLow);
//set the change switch on
was_there_a_change=true;
}
//but if the latest point we stored was not a low we create a new one elseif(last_point<0||points[last_point].type==zzPointHigh){
//add a new one
add_point(Low[i],Time[i],unique_id,zzPointLow);
//set the change switch on
was_there_a_change=true;
}
}
}
}
return(was_there_a_change);
}
int lastZigZag(){return(ArraySize(points)-1);}
};
/* now we have the storage of our points
lets declare it on the global scope
*/
zz_points_list LIST;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---//reset the list
LIST.reset();
//you should not check oninit but for this example we will do it //what do we need to do ? loop into the bars past to present and //catch the zigzags initially int from=Bars-2;
for(int i=from;i>=0;i--)
{
//and we just send the check instruction in the LIST
LIST.check_bar(i);
}
//lets draw some objects to debug it ObjectsDeleteAll(ChartID(),"MyZags_");
//loop to all the points in the list and link them//this is also the way you read the list (which is linear) //LIST.points[0] is the first zigzag in the list //and LIST.points[LIST.lastZigZag()] is the last one //for the drawing we will loop from [1] so as to link to each previous point for(int i=1;i<=LIST.lastZigZag();i++)
{
//create object string name="MyZags_"+IntegerToString(i);
//link from time and price of previous point to time and price of this point //if you draw live on tick you will need to be updating the last line because //zig zag repaints ObjectCreate(ChartID(),name,OBJ_TREND,0,LIST.points[i-1].time,LIST.points[i-1].price,LIST.points[i].time,LIST.points[i].price);
ObjectSetInteger(ChartID(),name,OBJPROP_RAY,false);
ObjectSetInteger(ChartID(),name,OBJPROP_RAY_RIGHT,false);
}
//---return(INIT_SUCCEEDED);
}
voidOnDeinit(constint reason)
{
//---ObjectsDeleteAll(ChartID(),"MyZags_");
LIST.reset();
}
voidOnTick()
{
}
so if you want to read the six latest stored zig zag levels
you will :
maintain the zigzag list
if there was a change (return true on the check)
are there 6 zigzags (or 6 new zigzags since last pattern check - which you will do its not in the code-)
Hey everyone, I'm posting here cause it's a similar topic... I would like to know how to save the value of the rsi as soon as a buy stop or a sell stop is triggered. My idea is to store the value or save it or consider the value of the rsi so that as soon as the rsi has a change of 4 point in opposite direction as the trade, so if it's a buy and the value is 60 when the trade is opened and the value reaches 56, the trade has to be closed. I would appreciate some help. thanks in advance!
Okay , here is a generic version .
This reads the zigzag and keeps the values in a list .
All you have to do is read the list you have when a change is detected live .
so if you want to read the six latest stored zig zag levels
you will :
Okay , here is a generic version .
This reads the zigzag and keeps the values in a list .
All you have to do is read the list you have when a change is detected live .
so if you want to read the six latest stored zig zag levels
you will :
i need a simple trick
all this code is hard for me to understand
i need a simple trick
all this code is hard for me to understand
All this code is the door to the simple trick . 😊
Also what are you catching here ? arr[2]<arr[3] means 2 is a low , why ignore 4+5 ?
Hey everyone, I'm posting here cause it's a similar topic...
I would like to know how to save the value of the rsi as soon as a buy stop or a sell stop is triggered.
My idea is to store the value or save it or consider the value of the rsi so that as soon as the rsi has a change of 4 point in opposite direction as the trade, so if it's a buy and the value is 60 when the trade is opened and the value reaches 56, the trade has to be closed.
I would appreciate some help.
thanks in advance!