Not sure how to draw a box at the same time every day

 

I'm trying to draw a box at the same time every day. The start and end of the box will be at the same time but the top and bottom of the box will be of the highest high and lowest low during that time period, respectively. I'm still relatively new to writing code, which will probably be obvious and I've always especially struggled to understand using datetime variables. Any help would be appreciated.

input string boxStart = "07:00";
input string boxEnd = "9:00";

int candleStart = iBarShift(NULL,0,boxEnd,FALSE); 
int candleEnd = iBarShift(NULL,0,boxEnd,FALSE); 

//+------------------------------------------------------------------+

void OnDeinit(const int reason)
  {
  }
  
//+------------------------------------------------------------------+

void OnTick()
  {

      int boxCandleSize = candleStart - candleEnd;
   
      double boxTop, boxBottom;
      datetime now = __DATE__;
   
      if( boxEnd == TimeCurrent() )
      {
         boxTop = iHighest(NULL,0,MODE_HIGH,boxCandleSize,0);
         boxBottom = iLowest(NULL,0,MODE_LOW,boxCandleSize,0);
         
         ObjectCreate("Box" + now, OBJ_RECTANGLE, 0, boxStart ,boxTop , boxBottom, boxEnd);
         ObjectSet("Box" + now, OBJPROP_COLOR, clrRed);
      }
   
   }
  
 
dasilvja:

I'm trying to draw a box at the same time every day. The start and end of the box will be at the same time but the top and bottom of the box will be of the highest high and lowest low during that time period, respectively. I'm still relatively new to writing code, which will probably be obvious and I've always especially struggled to understand using datetime variables. Any help would be appreciated.

Hello you can try using the StringToTime() function.

Please see here https://www.mql5.com/en/articles/599

MQL5 Programming Basics: Time
MQL5 Programming Basics: Time
  • www.mql5.com
The article focuses on standard MQL5 functions for working with time, as well as programming techniques and practically useful functions for working with time that are required when creating Expert Advisors and indicators. Particular attention is paid to the general theory of time measurement. This article should be of interest primarily to novice MQL5 programmers.
 
int candleStart = iBarShift(NULL,0,boxEnd,FALSE); 
int candleEnd = iBarShift(NULL,0,boxEnd,FALSE); 

//+------------------------------------------------------------------+

void OnDeinit(const int reason)
  1. IBarShift takes a datetime not a string

  2. Two variables, same call.

  3. Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)


Reason: