Removing old support/resistance levels at expiration

 

Hi folks,

Help needed.

My advisor creates support and resistance levels in the main window as the day goes.. Those levels have certain life duration, which I define. When TimeCurrent() > exp..., the lines are removed. Everything works fine, but only when there is just one pair of levels on the chart. If there is more than 1pair of levels, only the last pair gets removed, the older ones remain, even if "expired". I need to keep on the chart only the levels which are not expired. Apparently, I need to create an array for the levels I create and then check the levels in cycle for expiration, but I am still learning how to do it in the code. Could not google the solution either. Any guidance or help would be greatly appreciated.

my code is below:

 ObjectCreate(name_objectb,OBJ_TREND,0,tsup,psup,expsup,psup);
ObjectSet(name_objectb,OBJPROP_COLOR,PaleGreen);
ObjectSet(name_objectb,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(name_objectb,OBJPROP_WIDTH,1);
ObjectSet(name_objectb,OBJPROP_RAY,False);

ObjectSet(name_objectb,OBJPROP_BACK,false);

ObjectCreate(name_objects,OBJ_TREND,0,tres,pres,expres,pres);
ObjectSet(name_objects,OBJPROP_COLOR,LightPink);
ObjectSet(name_objects,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(name_objects,OBJPROP_WIDTH,1);
ObjectSet(name_objects,OBJPROP_RAY,False);
ObjectSet(name_objects,OBJPROP_BACK,false);

if ( (TimeCurrent() > expsup) || (TimeCurrent() >expres) )
{
ObjectDelete(name_objects); ObjectDelete(name_objectb); return(0);
}


Cheers,


Sam

 
13toi:

Hi folks,

Help needed.

My advisor creates support and resistance levels in the main window as the day goes.. Those levels have certain life duration, which I define. When TimeCurrent() > exp..., the lines are removed. Everything works fine, but only when there is just one pair of levels on the chart. If there is more than 1pair of levels, only the last pair gets removed, the older ones remain, even if "expired". I need to keep on the chart only the levels which are not expired. Apparently, I need to create an array for the levels I create and then check the levels in cycle for expiration, but I am still learning how to do it in the code. Could not google the solution either. Any guidance or help would be greatly appreciated.

my code is below:

<CODE REMOVED>

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

 
RaptorUK:

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.



any specific posting, you have in mind...


you see, that is what I wrote, I could not find the solution in the posts

I tried:

int obj_total=ObjectsTotal();
string name;
for(int i=obj_total-1; i>=0; i--)
{
name=ObjectName(i);
if(ObjectType(name)!=OBJ_TREND)

int nchs=StringFind(name,"LINE_RES_",0);
int nchb=StringFind(name,"LINE_SUP_",0);
if (nchs>=0 || nchb>=0)
{

if ( TimeCurrent() > ObjectGet(name, OBJPROP_TIME2) )

ObjectDelete(name);

}

}

but it does not work, I guess, because the ObjectGet is a double and TimeCurrent() is datetime, so I'm stuck, pls help

thanks

 
13toi: any specific posting, you have in mind...
Yes, your first post. What part of EDIT it was unclear?
Yes, your new post. What part of EDIT it was unclear?
 
13toi:

any specific posting, you have in mind...


you see, that is what I wrote, I could not find the solution in the posts

I tried:

int obj_total=ObjectsTotal();
string name;
for(int i=obj_total-1; i>=0; i--)
{
name=ObjectName(i);
if(ObjectType(name)!=OBJ_TREND)

int nchs=StringFind(name,"LINE_RES_",0);
int nchb=StringFind(name,"LINE_SUP_",0);
if (nchs>=0 || nchb>=0)
{

if ( TimeCurrent() > ObjectGet(name, OBJPROP_TIME2) )

ObjectDelete(name);

}

}

but it does not work, I guess, because the ObjectGet is a double and TimeCurrent() is datetime, so I'm stuck, pls help

thanks


Try changing your code just a little by putting in continue, without it the next line will only be executed when the object is NOT a trendline.

Obviously, that is not what you intended.

int obj_total=ObjectsTotal();
string name;
for(int i=obj_total-1; i>=0; i--)
{
name=ObjectName(i);
if(ObjectType(name)!=OBJ_TREND)
continue;                       //<---------------- continue added here                              
int nchs=StringFind(name,"LINE_RES_",0);
int nchb=StringFind(name,"LINE_SUP_",0);
if (nchs>=0 || nchb>=0)
{

if ( TimeCurrent() > ObjectGet(name, OBJPROP_TIME2) )

ObjectDelete(name);

}

}
 
//+------------------------------------------------------------------+
//|                                                   a_mainside.mq4 |
//|                                                             self |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "self"
#property link      "none"

extern double Lot=0.1;
extern int TP=25;
extern int SL=20;
extern int Magic=1000;
double VB=0, VS=0;
string name_objectb;
string name_objects;
datetime tVB;
datetime tVS;
datetime expVS;
datetime expVB;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
  double sl, tp;
  int sdel=0, otl=0;
// datetime exp=Period()*60*3;    
//----
  if (Digits==3||Digits==5) 
  {sl=SL*10*Point; tp = TP*10*Point;} 
  else
  if (Digits==2||Digits==4) 
  {sl=SL*Point; tp = TP*Point;} 


  if (OrdersTotal()==0 && VS ==0 && VB==0)
  {
  if (High[1]<=High[2] && Low[1]>=Low[2] && Volume[0]<2)
  {
  VB=High[1]+10*Point; Print ("Виртуальный VB: ", VB);  tVB=Time[1];  expVB= tVB+Period()*60*6; 
  name_objectb="LINE_VB_"+" "+Symbol()+" "+Period()+" "+tVB+" "+expVB+" "+VB;                                         //ideally need to add a coounter to the name
  ObjectCreate(name_objectb,OBJ_TREND,0,tVB,VB,expVB,VB);
                  ObjectSet(name_objectb,OBJPROP_COLOR,PaleGreen);
                  ObjectSet(name_objectb,OBJPROP_STYLE,STYLE_DOT);
                  ObjectSet(name_objectb,OBJPROP_WIDTH,1);
                  ObjectSet(name_objectb,OBJPROP_RAY,False);
                  ObjectSet(name_objectb,OBJPROP_BACK,false);

  VS=Low[1]-10*Point; Print ("Виртуальный VS: ", VS);  tVS=Time[1];  expVS= tVS+Period()*60*6;
  name_objects="LINE_VS_"+" "+Symbol()+" "+Period()+" "+tVS+" "+expVS+" "+VS;
  ObjectCreate(name_objects,OBJ_TREND,0,tVS,VS,expVS,VS);
                  ObjectSet(name_objects,OBJPROP_COLOR,LightPink);
                  ObjectSet(name_objects,OBJPROP_STYLE,STYLE_DOT);
                  ObjectSet(name_objects,OBJPROP_WIDTH,1);
                  ObjectSet(name_objects,OBJPROP_RAY,False);
                  ObjectSet(name_objects,OBJPROP_BACK,false);
 
  }
  }
 /*
 int obj_total=ObjectsTotal();                                                               //in fact what needs to be deleted at expiration is VB, VS, LINE_VB_, LINE_VS_
for(int i=obj_total-1; i>=0; i--)
{
name=ObjectName(i);
if(ObjectType(name)!=OBJ_TREND)

int nchs=StringFind(name,"LINE_VS_",0);
int nchb=StringFind(name,"LINE_VB_",0);
if (nchs>=0 || nchb>=0)
{

if ( TimeCurrent() > ObjectGet(name, OBJPROP_TIME2) )

ObjectDelete(name);

}

}*/
  
  if (OrdersTotal()==0 && VS >0 && VB>0) 
   {
   if ( (TimeCurrent() > expVB) || (TimeCurrent() >expVS) )
   {
   VS=0; VB=0; ObjectDelete(name_objects); ObjectDelete(name_objectb); return(0);
   }
   if (Close[1]<VS && Volume[0]<2 )
   {  OrderSend(Symbol(),OP_SELL,Lot,Bid,20,Bid+sl,Bid-tp,NULL,Magic,0,Red);  VS=0; ObjectDelete(name_objects); Print(TimeCurrent()-tVS); return(0);}
   else if (Close[1]>VB && Volume[0]<2 )
   {  OrderSend(Symbol(),OP_BUY,Lot,Ask,20,Ask-sl,Ask+tp,NULL,Magic,0,Green);  VB=0; ObjectDelete(name_objectb); Print(TimeCurrent()-tVB); return(0);}
   }
  if (OrdersTotal()==1 && (VS >0 || VB>0))
   {
   if ( (TimeCurrent() > expVB) || (TimeCurrent() >expVS) )
   {
   VS=0; VB=0; ObjectDelete(name_objects); ObjectDelete(name_objectb); return(0);
   }
   OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
   if(OrderType()==OP_BUYSTOP)
   {
   if (Close[1]<VS && Volume[0]<2  )
   {  OrderSend(Symbol(),OP_SELL,Lot,Bid,20,Bid+sl,Bid-tp,NULL,Magic,0,Red);  VS=0; ObjectDelete(name_objects); Print(TimeCurrent()-tVS); return(0);}
   }
   else if(OrderType()==OP_SELLSTOP)
   {
   if (Close[1]>VB && Volume[0]<2  )
   {  OrderSend(Symbol(),OP_BUY,Lot,Ask,20,Ask-sl,Ask+tp,NULL,Magic,0,Green);  VB=0; ObjectDelete(name_objectb); Print(TimeCurrent()-tVB); return(0);}
   }
   else if (OrderType()==OP_BUY) 
   {
   VS=0; ObjectDelete(name_objects); return(0);
   }
   else if (OrderType()==OP_SELL) 
   {
   VB=0; ObjectDelete(name_objectb); return(0);
   }
  }
 
 if (OrdersTotal()==2)
   {
   VS=0; VB=0;
   for (int pos=0; pos<OrdersTotal(); pos++)
   {
   OrderSelect(pos, SELECT_BY_POS);
   if (OrderType()<2) sdel++; else otl++;
   }
   if (sdel==1)
     {
     for (pos=0; pos<OrdersTotal(); pos++)
         {
         OrderSelect(pos, SELECT_BY_POS);
         if (OrdersTotal()>1) OrderDelete(OrderTicket());
         }
        }

       }
  
   return(0);
  }
//+------------------------------------------------------------------+

 

RaptorUK, WHRoeder, above is the complete code, sorry, yesterday I was really slow, thanks



 

It is difficult to see without running it to my MT4 what name the lines get

I like to see a few examples of the names what this code

  VB=High[1]+10*Point; Print ("Виртуальный VB: ", VB);  tVB=Time[1];  expVB= tVB+Period()*60*6; 
  name_objectb="LINE_VB_"+" "+Symbol()+" "+Period()+" "+tVB+" "+expVB+" "+VB;                                         //ideally need to add a coounter to the name
  ObjectCreate(name_objectb,OBJ_TREND,0,tVB,VB,expVB,VB);

is creating

deleting can if you do something like

//---- Deleting wrong Objects     
   int objectnumber, ot=ObjectsTotal()-1;   
   string id;
   
   //----
   for(objectnumber=ot;objectnumber>=0;objectnumber--)
    {id=ObjectName(objectnumber);
     if(StringSubstr(id,0,6)=="LINE_V")
        {
        Print(StringSubstr(id,10,8);
        //if(StringSubstr(id,10,8)<TimeToStr(TimeCurrent()))ObjectDelete(id);
        }
    }      
//----     

see if you can find it... expsup expres value

then you know what to do

 

GumRai, deVries, thanks a lot for the guidance and help. Both suggestions arrive to the same results, catching different variables.

Here is the code I use currently, it does remove "expired" objects.

int obj_total=ObjectsTotal();                                                              
for(int i=obj_total-1; i>=0; i--)
{
string name=ObjectName(i);
if(ObjectType(name)!=OBJ_TREND)
continue;
int nchs=StringFind(name,"LINE_VS_",0);
int nchb=StringFind(name,"LINE_VB_",0);
if (nchs>=0 || nchb>=0)
{

if ( TimeCurrent() > ObjectGet(name, OBJPROP_TIME2) )

{ObjectDelete(name);}

}

}

But with the VB and VS levels, it does not seem to work. Those I will need to turn into some arrays. Here is how I try to do it. Still learning the arrays, pls advise if my line of thought is valid and if the syntax of assigning the value to the array element makes sense.

Thank you in advance.

double VB[];    //in the declaration part of the code
double VS[];
datetime tVB[];
datetime tVS[];

...............

 ArrayInitialize(VB,0);   // in the init part of the code
 ArrayInitialize(VS,0);
 ArrayInitialize(tVS,0);
 ArrayInitialize(tVS,0);

.................
if (High[1]<=High[2] && Low[1]>=Low[2] && Volume[0]<2)                       //in the expert body
{
  VB[]=High[1]+10*Point; Print ("Виртуальный VB: ", VB);  tVB[]=Time[1];  expVB= tVB+Period()*60*6;     //  <------ would that be the right syntax to assign value to the first element of each array (VB and tBS)
                  ArrayResize(timeVB,ArraySize(timeVB)+1);
                  timeVB[ArraySize(timeVB)-1]=Time[1];
                  ArrayResize(VB,ArraySize(VB)+1);
                  VB[ArraySize(VB)-1]=0;
 



 

So what kind of names do the Objects get you want delete at expiretime ???

Give examples i'm not running your program ...... might be if you change the name of the object you can easily use the method i gave

Reason: