Since you are looking at the Current Daily Bar (as you used "0" for the shift in "iOpen()"), then the Begin and End times are as follows:
datetime BeginTime = iTime( NULL, PERIOD_D1, 0 ), // Open Time of the daily bar EndTime = TimeCurrent(); // Current Time as current bar has not closed yet
Alternative "EndTime":
datetime BeginTime = iTime( NULL, PERIOD_D1, 0 ), // Open Time of the daily bar EndTime = BeginTime + 86400; // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds
Also, use "PERIOD_D1" instead of 1440 in your "iOpen()";
Thanks again, FMIC! Once again you have pointed me in the right direction. Here is the code which will put a line at the open price of each day on any chart. Though it is best on daily and lower. Will only appear as dots within the candles on timeframes above the daily.
//+------------------------------------------------------------------+ //| Daily Open.mq4 | //| me | //| | //+------------------------------------------------------------------+ #property copyright " " #property link "" #property indicator_chart_window extern int Hour_Num = 0; extern int Minute_Num = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //ObjectsDeleteAll(); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int t = (-1); datetime BeginTime, EndTime; int Counted_bars=IndicatorCounted(); // Number of counted bars int i=Bars-Counted_bars-1; // Index of the first uncounted while(i>=0) // Loop for uncounted bars for (i = 0; i <=5; i++) { if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num) { t=t+1; Alert("Hour = ",Hour_Num," Minute = ",Minute_Num," i = ",i," t = ",t); double vO=iOpen(NULL, PERIOD_D1,t); // Daily Open if(t==0) { BeginTime = iTime( NULL, PERIOD_D1, 0 ); // Open Time of the daily bar EndTime = TimeCurrent(); // Current Time as current bar has not closed yet } if(i>0) { BeginTime = iTime( NULL, PERIOD_D1, t ); // Open Time of the daily bar EndTime = BeginTime + 86400; // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds } Alert("i = ",i," vO = ",vO," BeginTime = ",BeginTime," EndTime = ",EndTime); ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO ); // TimeCurrent() ObjectSet("Sm_Line"+t, OBJPROP_COLOR, Blue); ObjectSet("Sm_Line"+t, OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Sm_Line"+t, OBJPROP_WIDTH, 2); ObjectSet("Sm_Line"+t, OBJPROP_RAY, False); } i--; } //---- return(0); } //+------------------------------------------------------------------+
Your code will have the same problem as cashcube has here: Object is not automatically refreshing (Mimi) - MQL4 forum
Once you create the object
ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO ); // TimeCurrent()The end point (TimeCurrent) will not move because the object already exists.
Yes. I discovered, after posting, that when a new day begins, the line drawn, for the open of the day, wasn't. I'll figure it out, and post.
Thanks, again, for all the help!
O.k. I think that I have it. Use the following code for the past day(s) open values:
//+------------------------------------------------------------------+ //| Daily Open.mq4 | //| me | //| | //+------------------------------------------------------------------+ #property copyright " " #property link "" #property indicator_chart_window extern int Hour_Num = 0; extern int Minute_Num = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- ObjectsDeleteAll(); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int t = 0; datetime BeginTime, EndTime; int Counted_bars=IndicatorCounted(); // Number of counted bars int i=Bars-Counted_bars-1; // Index of the first uncounted // Loop for uncounted bars while(i>=0) { if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num) { t=t+1; double vO=iOpen(NULL, PERIOD_D1,t); // Daily Open if(i>0) { BeginTime = iTime( NULL, PERIOD_D1, t ); // Open Time of the daily bar EndTime = BeginTime + 86400; // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds } ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO ); ObjectSet("Sm_Line"+t, OBJPROP_COLOR, Blue); ObjectSet("Sm_Line"+t, OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Sm_Line"+t, OBJPROP_WIDTH, 2); ObjectSet("Sm_Line"+t, OBJPROP_RAY, False); } i--; } //---- return(0); } //+------------------------------------------------------------------+
And the following code for the current day's open value.
//+------------------------------------------------------------------+ //| Daily Open Current.mq4 | //| me | //| | //+------------------------------------------------------------------+ #property copyright " " #property link "" #property indicator_chart_window extern int Hour_Num = 0; extern int Minute_Num = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- ObjectDelete("Sm_Line"); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int t = (-1); datetime BeginTime, EndTime; int Counted_bars=IndicatorCounted(); // Number of counted bars int i=Bars-Counted_bars-1; // Index of the first uncounted // Loop for uncounted bars while(i>=0) { if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num) { double vO=iOpen(NULL, PERIOD_D1,0); // Daily Open { BeginTime = iTime( NULL, PERIOD_D1, 0 ); // Open Time of the daily bar EndTime = TimeCurrent(); // Current Time as current bar has not closed yet } ObjectCreate("Sm_Line", OBJ_TREND, 0, BeginTime, vO, EndTime, vO ); ObjectSet("Sm_Line", OBJPROP_COLOR, Blue); ObjectSet("Sm_Line", OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Sm_Line", OBJPROP_WIDTH, 2); ObjectSet("Sm_Line", OBJPROP_RAY, False); } i--; } //---- return(0); } //+------------------------------------------------------------------
// Loop for uncounted bars while(i>=0) { if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num) { t=t+1; double vO=iOpen(NULL, PERIOD_D1,t);
You are checking times starting from the most distant past, but getting the iOpen value from the opposite direction
ie Time from the oldest bar, price from the most recent.
In your second code, why the loop?
You have already been told that a line can only be created once, then it has to be moved
You are checking times starting from the most distant past, but getting the iOpen value from the opposite direction
ie Time from the oldest bar, price from the most recent.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to figure out how to draw a horizontal line from the start of a day to the end at the open price, of the day. Not sure how to represent start of day in the following code. Any thoughts would be greatly appreciated. I was thinking of converting time to seconds, but then, I would have to create a loop to establish the start of the day at zero seconds. Which I think would create conflict, in the loop, since I'm trying to draw a line to the end of the day.