Vline 50 bars after the current time/bar - page 2

 
Thierry Ramaniraka: Your solution deosn't seems to work in my case.
  1. I didn't provide a solution. I provided equivalent but simpler code. Why do you expect my code to work any different?

  2. datetime  Current_Time = TimeCurrent();
    
    int time2=Time[0]+(Datas_BarShift_Time-1)*PeriodSeconds();
    int time3=Time[0]+(Datas_BarShift_Time2-1)*PeriodSeconds();     
    
    These are all global variables. They are initialize at load with possible garbage. They are never updated in start.
    Don't try to use any price or server related functions in OnInit (or on load,) 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 and prices are valid.

  3. Start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  4. Always use strict. Fixing the warnings will save you hours of debugging.
              Program Properties (#property) - Preprocessor - Language Basics - MQL4 Reference
 
I will try with OnCalculate/OnTick.
Thank you.
 
Finally I succeed by redefine the shift parameter on new candle.
Thank you all for your help.
 
Thierry Ramaniraka:
Finally I succeed by redefine the shift parameter on new candle.
Thank you all for your help

Here is a more reusable solution...

#property strict
#property indicator_chart_window
#include <ChartObjects\ChartObjectsLines.mqh>
//+------------------------------------------------------------------+
class CFutureVline : public CChartObjectVLine
{
   int         m_bars;
   static int  s_instance;
public:
   CFutureVline():m_bars(0)
   {
      this.Create(0,StringFormat("__future_v_line_%d__",++s_instance),0,0);
   }
   void set_bars(int bars) { m_bars = bars; }
   void refresh()          { this.Time(0,Time[0]+PeriodSeconds(_Period)*m_bars);}
};
int CFutureVline::s_instance=0;
//+------------------------------------------------------------------+
CFutureVline line50;
//+------------------------------------------------------------------+
int init()
{
   line50.set_bars(50);
   return 0;
}
//+------------------------------------------------------------------+
int start()
{
   line50.refresh();
   return 0;
}
//+------------------------------------------------------------------+
 
aslkdjf:

Here is a more reusable solution...

Thank you.
I keep it for later in case.

Regards.

 

Hello,
I am trying to work with 

#property strict


And i get this error.
Can you tell me what is wrong please ?


 
Thierry Ramaniraka: And i get this error.
  1. Don't hijack other people's threads with your off topic post. Open your own.

  2. Don't double post!

              General rules and best pratices of the Forum. - General - MQL5 programming forum
 
whroeder1:
  1. Don't hijack other people's threads with your off topic post. Open your own.

  2. Don't double post!

              General rules and best pratices of the Forum. - General - MQL5 programming forum

Hello,
I didn't intended such bad things. It was just a corelation on both thread.

https://www.mql5.com/en/forum/233876/unread#unread

https://www.mql5.com/en/forum/235106/unread#unread

Regards

Vline 50 bars after the current time/bar
Vline 50 bars after the current time/bar
  • 2018.03.31
  • www.mql5.com
Hi there, I come to you to ask you : How can i draw a VLINE on the 50th (or Xth) bar in the future...
 
Thierry Ramaniraka: https://www.mql5.com/en/forum/233876
Please use the link button Use the link button See the difference? https://www.mql5.com/en/forum/233876
          Messages Editor
 
Thierry Ramaniraka:
Finally I succeed by redefine the shift parameter on new candle.
Thank you all for your help.

Hello I am learning the newest MT4 mql's language with "Oninit" and so on...
And my Vlines are not refreshing anymore. Any idea why ?

Nb : I have place it in "OnChartEvent" Because i planned to make them show/hide by a button.


#property strict
#property indicator_chart_window

bool Visible = true;

color  Separator_Color = Lavender;
extern int Datas_BarShift_Time = 35;
extern int Datas_BarShift_Time2 = 55;

datetime time2 = Time[0]+(Datas_BarShift_Time-1)*PeriodSeconds();
datetime time3 = Time[0]+(Datas_BarShift_Time2-1)*PeriodSeconds();
datetime Current_Time = TimeCurrent();

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // REINITIALIZE OBJECTS BY DELETING THEM
   ObjectDelete("Current_Time_Vline");
   ObjectDelete("Current_Time2_Vline");
   ObjectDelete("Current_Time3_Vline");
   // -------
  
   if(Volume[0]>1)
   {
      time2 = Time[0]+(Datas_BarShift_Time-1)*PeriodSeconds();
      time3 = Time[0]+(Datas_BarShift_Time2-1)*PeriodSeconds();
   }
    
   return(INIT_SUCCEEDED);
}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   Display_Vlines();
}

void Display_Vlines()
{
   if(Visible == true)
   {      
      ObjectCreate("Current_Time_Vline", OBJ_VLINE, 0, Current_Time, 0);
      ObjectCreate("Current_Time2_Vline", OBJ_VLINE, 0, time2, 0);
      ObjectCreate("Current_Time3_Vline", OBJ_VLINE, 0, time3, 0);
   }
   
   if(Visible == false)  
   {
      ObjectDelete("Current_Time_Vline");
      ObjectDelete("Current_Time2_Vline");
      ObjectDelete("Current_Time3_Vline");
   }
   
   Comment
   (
      string
      (
         "visible is " + string(Visible)
      )
   );
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Comment("");
}
Reason: