Moving HLine - page 2

 
Keith Watford:

What doesn't update?

Read through the code carefully and understand what it is doing.

Are you actually coding what you want it to do?

Why are you hard-coding the time-frame when you have it in the inputs?

Change it to


What is Candle?

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   if(!ObjectCreate(ChartID(),"High Line",OBJ_HLINE,0,0,0))
      Print(__FUNCTION__,": failed to create the High horizontal line! Error code = ",GetLastError());
   else
     {
      ObjectSetInteger(ChartID(),"High Line",OBJPROP_COLOR,clrWhite);
     }

   if(!ObjectCreate(ChartID(),"Low Line",OBJ_HLINE,0,0,0))
      Print(__FUNCTION__,": failed to create the Low horizontal line! Error code = ",GetLastError());
   else
     {
      ObjectSetInteger(ChartID(),"Low Line",OBJPROP_COLOR,clrWhite);
     }
   return(INIT_SUCCEEDED);
   }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
ObjectsDeleteAll(0,OBJ_HLINE);
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static datetime barTime=0;
   datetime nowTime=iTime(_Symbol,PERIOD_MN1,1);
   if(barTime!=nowTime)
     {
      barTime=nowTime;
      double LastOpen=iOpen(_Symbol,PERIOD_MN1,1);
      double LastClose=iClose(_Symbol,PERIOD_MN1,1);
      ObjectSetDouble(ChartID(),"High Line",OBJPROP_PRICE,LastOpen);
      ObjectSetDouble(ChartID(),"Low Line",OBJPROP_PRICE,LastClose);
     }
   
  }
//+------------------------------------------------------------------+

No update, try yourself.

 
David Diez:

No update, try yourself.

I have tried it myself and it DOES update.

Why do you insist on hard-coding the time-frame?

A simple input as in my code allows flexibility. When you want to expand your code, then you may end up with dozens or even hundreds of lines that use the time-frame. Do you really want to have to edit every line if you want to change the TF? This will often lead to mistakes.

ObjectsDeleteAll(0,OBJ_HLINE);

Bad practice. Make sure that you only delete objects created by your EA/indicator. Use of a prefix in the name (as in my code) simplifies this.

Give your objects descriptive names 

"High Line" suggests it is a high, but you are using the open price.

 
Keith Watford:

I have tried it myself and it DOES update.

Why do you insist on hard-coding the time-frame?

A simple input as in my code allows flexibility. When you want to expand your code, then you may end up with dozens or even hundreds of lines that use the time-frame. Do you really want to have to edit every line if you want to change the TF? This will often lead to mistakes.

Bad practice. Make sure that you only delete objects created by your EA/indicator. Use of a prefix in the name (as in my code) simplifies this.

Give your objects descriptive names 

"High Line" suggests it is a high, but you are using the open price.

ObjectMove missing in your code. Updates price values, not object.


Also have now the problem of supportline color that was coded to be blue.

   static datetime barTime=0;
   double LastOpen=0,LastClose=0;
   datetime nowTime=iTime(Symbol(),Candle,1);
   if(barTime!=nowTime){
      barTime=nowTime;
      LastOpen=iOpen(Symbol(),PERIOD_MN1,1);
      Print("LastOpen is ",LastOpen);
      LastClose=iClose(Symbol(),PERIOD_MN1,1);
      Print("LastClose is ",LastClose);
      }
   double Support=0,Resistance=0;
   string SupportLine="Support",ResistanceLine="Resistance";
   bool Bullish=LastClose>LastOpen,Bearish=LastClose<LastOpen;
   if(Bullish){Support=LastOpen;Resistance=LastClose;}
   if(Bearish){Support=LastClose;Resistance=LastOpen;}
   if(Support>0){
      if(!ObjectMove(ChartID(),SupportLine,OBJ_HLINE,0,Support)){
         Print("ObjectMove error ",GetLastError()); //NO PRINT
         if(!ObjectCreate(ChartID(),SupportLine,OBJ_HLINE,0,0,Support)){
            Print("ObjectCreate error ",GetLastError());
            if(!ObjectSet(SupportLine,OBJPROP_COLOR,clrBlue)){
               Print("ObjectSet error ",GetLastError());
               }
            }
         }
      else{ChartRedraw(0);}
      }
   if(Resistance>0){
      if(!ObjectMove(ChartID(),ResistanceLine,OBJ_HLINE,0,Resistance)){
         Print("ObjectMove error ",GetLastError()); // NO PRINT
         if(!ObjectCreate(ChartID(),ResistanceLine,OBJ_HLINE,0,0,Resistance)){
            Print("ObjectCreate error ",GetLastError());
            if(!ObjectSet(ResistanceLine,OBJPROP_COLOR,clrRed)){
               Print("ObjectSet error ",GetLastError());
               }
            }
         }
      else{ChartRedraw(0);}
      }
 
David Diez:

ObjectMove missing in your code. Updates price values, not object.

ObjectMove is not missing, it is not needed as I use ObjectSetDouble instead.

Have you even tried my code?

All you have to do is change the TF and then a simple matter to get the values of the open/close instead of the high/low.

 
Keith Watford:

ObjectMove is not missing, it is not needed as I use ObjectSetDouble instead.

Have you even tried my code?

All you have to do is change the TF and then a simple matter to get the values of the open/close instead of the high/low.

What happen, don't you see the image attached?
 
David Diez:
What happen, don't you see the image attached?

What is your image supposed to prove?

 
Keith Watford:

What is your image supposed to prove?

HLINES not corresponding to the current values as the prints are showing.

 
David Diez:

HLINES not corresponding to the current values as the prints are showing.

What has that got to do with my code?

 
Keith Watford:

What has that got to do with my code?

Your code doesn't mark lines at any price.
 
I give up!