high/low in sepcified period

 

Hi everyone, very new to mql4. Hope someone here can help me out. 

I want to create an ea that measure the highest and lowest price within a specific time period every single day. For example from 8am to 12:00. I want to use the high and the low within this period to set pending buy/sell-stop orders in each direction. I came up with a code that compiles, but i realize that how i have written it, the system opens pending orders every time there is a new tick generated within the specified hour and minute. I feel like my approach is completely off. Does anyone have a better way to solve this? 


void OnTick()
  {
//---
   // get low and high price of last 4 candles
   
   double highestPrice = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,4,0));
   
   double lowestPrice = iLow(NULL,0,iLowest(NULL,0,MODE_LOW,4,0));
  
   
   
   // set pending orders at the highest and lowest price between time period)
   
   int hourOfDay = Hour();
   int minuteNow = Minute();
   
   if (hourOfDay == 11 && minuteNow == 59) 
   {
   OrderSend(NULL,OP_BUYSTOP,0.01,highestPrice,20,highestPrice-200*Point,highestPrice+300*Point,NULL,0,TimeCurrent()+480*60,clrNONE); //buystop at  high
   
   OrderSend(NULL,OP_SELLSTOP,0.01,lowestPrice,20,lowestPrice+200*Point,lowestPrice-300*Point,NULL,0,TimeCurrent()+480*60,clrNONE); //sellstop at  low
   
   
   }
   else
   {
   Print("waiting for setup");
   }
   
   
   
  }
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
traderjes:

Hi everyone, very new to mql4.

I want to create an ea that measure the highest and lowest price within a specific time period every single day. For example from 8am to 12:00.

i realize that how i have written it, the system opens pending orders every time there is a new tick generated within the specified hour and minute.

  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. You already did.
  4. Either check if you have orders set on the current symbol before opening more.
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  5. if (hourOfDay == 11 && minuteNow == 59) 
    Or instead of looking at a signal, act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
  6. Your test fails if there is no tick during that minute.
 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. You already did.
  4. Either check if you have orders set on the current symbol before opening more.
  5. Or instead of looking at a signal, act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
  6. Your test fails if there is no tick during that minute.

thanks for the inputs William. It is my first post here, so was not aware of these things. Will make sure to follow the rules in the future and find the right directory for my posts;) I edited the original post, but don't find a way for me to move it...

What you mention in point 6, that no orders will be send if there is no tick in that minute, makes me realize that i need to re-think this thing, come up with another solution. Thinking that i will make an indicator that draws out the high and low of the given timeperiod, and then make the ea place orders once the lines of the indicator is crossed.. Will go back to the drawing-board for now.. Cheers

 

You can use iBarShift() to get the right bar index for a time of day. Use StringToTime("08:00") to convert a time string into a datetime.

The issue with multiple ticks during a specified trigger time could be solved with a new bar function.

void OnTick()
  {
   bool isNewBar=IsNewBar();
   if(isNewBar && Hour()==12 && Minute()==00)
     {
      datetime dtfrom=StringToTime("08:00");
      datetime dttill=StringToTime("12:00");
      int ifrom=iBarShift(NULL,0,dtfrom);
      int itill=iBarShift(NULL,0,dttill);
      int ihighest=iHighest(NULL,0,MODE_HIGH,ifrom-itill,itill+1);
      int ilowest = iLowest(NULL,0,MODE_LOW, ifrom-itill,itill+1);
      double highestPrice=iHigh(NULL,0,ihighest);
      double lowestPrice = iLow(NULL,0,ilowest);
      OrderSend(NULL,OP_BUYSTOP,0.01,highestPrice,...); //buystop at  high
      OrderSend(NULL,OP_SELLSTOP,0.01,lowestPrice,...); //sellstop at  low
     }
  }
 
lippmaje:

You can use iBarShift() to get the right bar index for a time of day. Use StringToTime("08:00") to convert a time string into a datetime.

The issue with multiple ticks during a specified trigger time could be solved with a new bar function.

Thank you for your input lippmaje. I appreciate it a lot;) Will try to implement.

 
lippmaje:

You can use iBarShift() to get the right bar index for a time of day. Use StringToTime("08:00") to convert a time string into a datetime.

The issue with multiple ticks during a specified trigger time could be solved with a new bar function.

Thanks alot

Reason: