Download MetaTrader 5

Bar shifting and iLow/iLowest only work on full hours? (10pm, 11pm ...)

To add comments, please log in or register
Looking for like-minded people? You will find lots of them on the Forum!
Jan
175
Jan 2016.08.29 15:19 

Hey Guys,

I have one problem...  My EA only works on full hours. Like from 10pm to 11pm and so on. But if I input 10:15 or 10:54 to 11 it doesn't work...

 

The EA should operation so that he can give me the highest and lowest price between a specified timespan. But I don't know why it only works with full hours... I also testet it on the right Chart!
E.g. for 10:54-11:12 the M1 chart, for 10:05-11:00 the M5 chart, for 10:15-10:45 the M15 Chart and so on... 

 

This is my code: 

input int fromHour;
input int fromMinute;
input int toHour;
input int toMinute;

static bool succeeded = false;
static double lowestPrice;
static double highestPrice;
static double span;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit(){

   Print("Inputs are: fromHour:",fromHour," fromMinute:",fromMinute," toHour:",toHour," toMinute:",toMinute);
   
   string fromH = IntegerToString((fromHour)); //+1 because of the Time. Now 18 o'clock is 19 o'clock in the server time!
   string fromM = IntegerToString(fromMinute);
   string toH = IntegerToString((toHour)); //+1 because of the Time. Now 18 o'clock is 19 o'clock in the server time!
   string toM = IntegerToString(fromMinute);
   if(fromHour < toHour){
      succeeded = true;
   }else if(fromHour == toHour && fromMinute <= toMinute){
      succeeded = true;
   }else{
      Print("Error! - fromTime is bigger then toTime!");
   }
   
   if(fromHour == Hour() && fromMinute <= Minute() && toHour == Hour() && toMinute <= Minute()){
      succeeded = true;
   }else if(fromHour < Hour() && toHour == Hour() && toMinute <= Minute()){
      succeeded = true;
   }else if(fromHour < Hour() && toHour < Hour()){
      succeeded = true;
   }else{
      Print("Current Time is: ",Hour(),":",Minute());
      Print("Inserted Time isn't allowed! - Time must be in the past!");
   }
   
   Print("Success! - Now are watching for an breakout!");
   
   string point = ".";   
   string spacer = " ";
   string doublepoint = ":";
   string year = IntegerToString(Year());
   string month = IntegerToString(Month());
   string day = IntegerToString(Day());
  
   
   StringAdd(year, point);
   StringAdd(month, point);
   StringAdd(day, spacer);  
   StringAdd(fromH, doublepoint);
   StringAdd(toH, doublepoint);
   
   string fromFinal = year+month+day+fromH+fromM;
   string toFinal = year+month+day+toH+toM;
   Print("");
   Print(fromFinal);
   Print(toFinal);
   
   datetime lowest = StrToTime(fromFinal);
   datetime highest = StrToTime(toFinal);
   
   //Beginning
   int leftToShiftLowest = iBarShift(NULL, 0, lowest);
   
   //Ending
   int leftToShiftHighest = iBarShift(NULL, 0, highest);
   Print("index of the bar for the time ",TimeToStr(lowest)," is ",leftToShiftLowest);
   Print("index of the bar for the time ",TimeToStr(highest)," is ",leftToShiftHighest);
   
   //Shifts needed for highest and lowest price
   int lowestShifts = iLowest(NULL, 0, 1, leftToShiftLowest, leftToShiftHighest);
   int highestShifts = iHighest(NULL, 0, 2, leftToShiftLowest, leftToShiftHighest);
   
   Print("lowestShifts: ",lowestShifts," highestShifts: ",highestShifts);
   
   //lowest price
   lowestPrice = iLow(NULL, 0, lowestShifts); 
  
   //highest price
   highestPrice = iHigh(NULL, 0, highestShifts);
   
   span = (highestPrice - lowestPrice);
   datetime lowestBar = iTime(Symbol(), 0, lowestShifts);
   datetime highestBar = iTime(Symbol(), 0, highestShifts);
   
   ObjectCreate("Lowest", OBJ_HLINE, 0, Time[0], lowestPrice, 0, 0);
   ObjectCreate("Highest", OBJ_HLINE, 0, Time[0], highestPrice, 0, 0);
   ObjectCreate("TVK Buy", OBJ_HLINE, 0, Time[0], (highestPrice+span), 0, 0);
   ObjectCreate("TVK Sell", OBJ_HLINE, 0, Time[0], (lowestPrice-span), 0, 0);
   ObjectCreate("Range", OBJ_RECTANGLE, 0, lowestBar, lowestPrice, highestBar, highestPrice);

   
   return(INIT_SUCCEEDED);
  }

 

Greetings and thank you! 

peter.MT4Web
158
peter.MT4Web 2016.08.29 17:57  

Why are you not trying to test your code.

Then you may have found this:

   string fromM = IntegerToString(fromMinute);
   string toM = IntegerToString(fromMinute);

I have not checked all your code, because i think you have to do it.

Print your values or use the debugger.

Jan
175
Jan 2016.08.29 18:04  

I checked it but didn't found something... - But thank you a lot!

I will try it and check it again! - Sometimes you are just blind... hehe 

Jan
175
Jan 2016.08.29 18:31  

Okay... I think, I found my error... But nothing is wring in the code!

E.g.: If I type from 12-17, the EA marks a diffrent/wrong area... - 17 shifts for the 17 o'clock is right, but not 37 for 12 o'clock...
Does anybody know the solution?

The time outputs are right... So iBarShift is wrong or I just don't use it right... - Can that be? 

 

whroeder1
14519
whroeder1 2016.08.30 12:23  
  1. FrazeColder: E.g.: If I type from 12-17, the EA marks a diffrent/wrong area... - 17 shifts for the 17 o'clock is right, but not 37 for 12 o'clock...
    12-17 is 5 hours. 5 hours on the M15 is 20 bars. 17 + 20 = 37. Exactly correct.
  2. string fromH = IntegerToString((fromHour));
    string fromM = IntegerToString(fromMinute);
    string toH = IntegerToString((toHour));
    string toM = IntegerToString(fromMinute);
    :
    string point = ".";
    string spacer = " ";
    string doublepoint = ":";
    string year = IntegerToString(Year());
    string month = IntegerToString(Month());
    string day = IntegerToString(Day());
    
    StringAdd(year, point);
    StringAdd(month, point);
    StringAdd(day, spacer);
    StringAdd(fromH, doublepoint);
    StringAdd(toH, doublepoint);
    
    string fromFinal = year+month+day+fromH+fromM;
    string toFinal = year+month+day+toH+toM;
    :
    datetime lowest = StrToTime(fromFinal);
    datetime highest = StrToTime(toFinal);
    
    Simplify your date handling
    datetime lowest  = StrToTime(StringFormat("%i:%i", fromHour, fromMinute) );
    datetime highest = StrToTime(StringFormat("%i:%i",  toHour,    toMinute) );
    Or simplify your date handling by not using strings.
    #define HR2400 86400
    int     time(datetime when=0){
       return int(when == 0 ? TimeCurrent() : when) % HR2400;                   }
    datetime   date(datetime when=0){
       return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }
    datetime   tomorrow(datetime when=0){    // Weekends not accounted
       return date(when) + HR2400;                                              }
    /// Returns previous trading day.
    datetime   yesterday(datetime when=0)    /**< Previous relative to
                                                     * when. */{
       if(when==0) when = TimeCurrent();
       int      iD1   = iBarShift(NULL, PERIOD_D1,  when);   // Find today.
       return iTime(NULL, PERIOD_D1, iD1 + 1);               // Return yesterday.
    }
    //////////////////////////////////
    datetime now     = date();
    datetime lowest  = now + fromHour * 3600 + fromMinute * 60;
    datetime highest = now +   toHour * 3600 +   toMinute * 60;
    
Jan
175
Jan 2016.08.30 15:18  

Okay, then another example...

This time I typed from 12 to 16 o'clock.

The EA says that I need to shift 8 bars, to get to the 16 o'clock bar and 24 shifts to 12 o'clock. That's correct!
-> So the range for the lowest and highest price in this time should be between 8 till 24 bar shifts. Correct?

But why the hell is the EA saying, that the highest bar is at 25 bar shifts and the lowest by 31? - That's totally wrong!!!

 

And to you code: So I don't need 4 input variables for the time, just two. Right
For example: 12:15 and 16:30 - Right? - Because the "command" %i:%i splits the string at ":".
But why I need 4 variables then in the code?

datetime lowest  = StrToTime(StringFormat("%i:%i", fromHour, fromMinute) );
datetime highest = StrToTime(StringFormat("%i:%i",  toHour,    toMinute) );
peter.MT4Web
158
peter.MT4Web 2016.08.30 17:30  

Check the parameters of your function calls.

   int lowestShifts = iLowest(NULL, 0, 1, leftToShiftLowest, leftToShiftHighest);
   int highestShifts = iHighest(NULL, 0, 2, leftToShiftLowest, leftToShiftHighest);

There is no from - to. There is only count and start.

Jan
175
Jan 2016.08.30 19:07  

Ohhh... so I thought, that I start stands for the beginning of the shifting... :/

Any idea, how I can fix this problem? - I only want to check a specified area... So if we have 17:45 and I type fron 10-17 o'clock, I don't want, that oLowest/iHighest checks also the time area between 17 o'clock and now (17:45)... 

whroeder1
14519
whroeder1 2016.08.31 12:28  
FrazeColder: Any idea, how I can fix this problem?
int lowestShifts = iLowest(NULL, 0, 1, leftToShiftLowest, leftToShiftHighest);
int highestShifts = iHighest(NULL, 0, 2, leftToShiftLowest, leftToShiftHighest);
  1. You code it properly. As eddie already said, there is only count and start no from and to. When in doubt, think!
    int lowestShifts  =  iLowest(NULL, 0, MODE_LOW,  leftToShiftLowest - leftToShiftHighest + 1, leftToShiftHighest);
    int highestShifts = iHighest(NULL, 0, MODE_HIGH, leftToShiftLowest - leftToShiftHighest + 1, leftToShiftHighest);
  2. Don't hard code numbers, use the symbolic constants Series Array Identifiers - Indicator Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
Jan
175
Jan 2016.09.01 15:42  

Thank you a Lot WHRoeder!
But if it is possible could you help me by two more questions?

1. I already wrote:
"And to you code: So I don't need 4 input variables for the time, just two. Right

For example: 12:15 and 16:30 - Right? - Because the "command" %i:%i splits the string at ":".

But why I need 4 variables then in the code?"

datetime lowest  = StrToTime(StringFormat("%i:%i", fromHour, fromMinute) );
datetime highest = StrToTime(StringFormat("%i:%i",  toHour,    toMinute) );

 

2. I wanna try to change my system to a fully automatic system, so I don't need to input the times anymore...
But my problem is to "program the future". For example:

I have some fix times when I'am using my EA. We say, let's use 10am-12am. How can I programm the EA so, that the EA is looking every day, between 10am to 12am for the highest and lowest price at the market?
The specified range which I must enter in the EA at the moment!

whroeder1
14519
whroeder1 2016.09.01 15:55  
When in doubt think! We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
123
To add comments, please log in or register