how to keep saving an indicator value in mql4 - page 3

 

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 .

#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 
*/
input int InpDepth=12;     // Depth
input int InpDeviation=5;  // Deviation
input int 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 up
    void 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 them
struct 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 zigzags
             bool 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 place
             int 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 high
                 if(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 
                         else if(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 
                 else if(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 
                         else if(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                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  //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);
  }

void OnDeinit(const int reason)
  {
//---
  ObjectsDeleteAll(ChartID(),"MyZags_");
  LIST.reset(); 
  }

void OnTick()
  {

   
  }

so if you want to read the six latest stored zig zag levels 

you will :

  1. maintain the zigzag list
  2. if there was a change (return true on the check)
  3. are there 6 zigzags (or 6 new zigzags since last pattern check - which you will do its not in the code-)
  4. then check for your pattern
 
Lorentzos Roussos #:

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 :

  1. maintain the zigzag list
  2. if there was a change (return true on the check)
  3. are there 6 zigzags (or 6 new zigzags since last pattern check - which you will do its not in the code-)
  4. then check for your pattern

i need a simple trick 

all this code is hard for me to understand 

 
KIOS #:

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 ?

           double L0 = 0 , L1 = 0, L2=0, L3=0;
          
           if(arr[2]<arr[3] &&Close[1]<arr[2])
           {
           L1 = L1 + arr[1];
           L2 = L2 + arr[2];
           L3 = L3 + arr[3];
           
           Comment("\n",L1,"\n",L2,"\n",L3);
         
           }
Reason: