iBarShift seems to be returning the wrong value

 

Hello all, hope everybody is safe and sound


i am tring to generate and alert when the candle crosses below and close below a horizontal line , while at the same time i am reading the string from the description and converting it to time and then converting that time value to the candle id using the ibarshift but the variable i am storing it to always returns zero 

here is the code :

 if(ObjectsTotal()>0)
   {
     for(int j=0;j<ObjectsTotal();j++)
   {
    string objname= ObjectName(j);
    int objectdes = ObjectDescription(objname);
    double objpric= ObjectGet(objname, OBJPROP_PRICE1);  
    if(StringFind(objectdes,"Done")<0)
      {
        if(Close[1]<objpric && Open[1]>objpric)
         {
         datetime timeforcid=StrToTime(objectdes);
         int candleid=iBarShift(NULL,PERIOD_CURRENT,timeforcid);
         Print(candleid,"and", timeforcid);
         //Alert (Symbol(),candleid);
         ObjectSetText(objname,"strrrDone",10,"Times New Roman",Green);
         }
      }

   }
   }

can anyone point out why the candleid always stays 0?

thanks in advance 

 
Arad_1:

Hello all, hope everybody is safe and sound


i am tring to generate and alert when the candle crosses below and close below a horizontal line , while at the same time i am reading the string from the description and converting it to time and then converting that time value to the candle id using the ibarshift but the variable i am storing it to always returns zero 

here is the code :

can anyone point out why the candleid always stays 0?

thanks in advance 

datetime timeforcid=StrToTime(objectdes); // this is not valid

datetime timeforcid=StringToTime(objectdes);     // is the correct syntax for conversion (see note below)					

however, the string objectdes has to be a date string in order to convert it, not the name of an object


The point the price goes below your line will surely be on the current bar as your indicator/EA monitors the price over time, so you know the time at that point and the bar is [0].

If you want to look backwards in time for it passing below the line, then you should loop through a number of bars and check the values accordingly.

so either way you will know the bar and time

StringToTime - Conversion Functions - MQL4 Reference
StringToTime - Conversion Functions - MQL4 Reference
  • docs.mql4.com
StringToTime - Conversion Functions - MQL4 Reference
 
Arad_1:

Hello all, hope everybody is safe and sound


i am tring to generate and alert when the candle crosses below and close below a horizontal line , while at the same time i am reading the string from the description and converting it to time and then converting that time value to the candle id using the ibarshift but the variable i am storing it to always returns zero 

here is the code :

can anyone point out why the candleid always stays 0?

thanks in advance 

int  iBarShift( 
   const string        symbol,          // Symbol 
   ENUM_TIMEFRAMES     timeframe,       // Period 
   datetime            time,            // Time 
   bool                exact=false      // Mode 
   );

exact=false

[in]  A return value, in case the bar with the specified time is not found. If exact=false, iBarShift returns the index of the nearest bar, the Open time of which is less than the specified time (time_open<time). If such a bar is not found (history before the specified time is not available), then the function returns -1. If exact=true, iBarShift does not search for a nearest bar but immediately returns -1.


 
Paul Anscombe:

 

however, the string objectdes has to be a date string in order to convert it, not the name of an object



thanks a lot @Paul@Paul Anscombe   for the valuable input  , Highly appereciated
and yeah the objectdes is in the date string format , i am  setting it up in an other section of the code :

    double Highs= High[i];
    double  Lows= Low [i];
    int ttt = Time[i];
    string var = TimeToStr(ttt,TIME_DATE|TIME_SECONDS);
    if(Open[1]<Highs && Close[1]>Highs)
      {
            string name= StringConcatenate("1bl",i);
            ObjectCreate(0,name,OBJ_HLINE,0,0,Lows);
            ObjectSetInteger(0,name,OBJPROP_COLOR,clrRed);
            ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID); 
            ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
            ObjectSetInteger(0,name,OBJPROP_BACK,true);
            ObjectSetText(name,var,10,"Times New Roman",Green);
      }
    
    }
 
Roberto Jacobs:

exact=false

[in]  A return value, in case the bar with the specified time is not found. If exact=false, iBarShift returns the index of the nearest bar, the Open time of which is less than the specified time (time_open<time). If such a bar is not found (history before the specified time is not available), then the function returns -1. If exact=true, iBarShift does not search for a nearest bar but immediately returns -1.


Thanks @Roberto Jacobs , Duly noted 

 
thanks a lot @Paul@Paul Anscombe   for the valuable input  , Highly appereciated
and yeah the objectdes is in the date string format , i am  setting it up in an other section of the code :

I think you are mistaken, you are setting objectdes as an integer 


 if(ObjectsTotal()>0)
   {
     for(int j=0;j<ObjectsTotal();j++)
   {
    string objname= ObjectName(j);
    int objectdes = ObjectDescription(objname);
    double objpric= ObjectGet(objname, OBJPROP_PRICE1);  
    if(StringFind(objectdes,"Done")<0)
      {

   }
Reason: