History option on this indicator

 

Hello,

The purpose of this indicator is to draw a trendline between the close of 2 specific candle.

I have tried different thing to add history to this indicator without any success.

Like: extern int NumberOfDays = 5; ...

But no success.


If anybody can help me that would be great,

Thanks in advance


#property copyright "Copyright © 2011

#property link "http://www.metaquotes.net"

#property indicator_chart_window

extern string t1 = "02:45";
extern string t2 = "07:45";
string gs_92;

int init() {
gs_92 = TimeToStr(TimeCurrent(), TIME_DATE) + " ";
if (TimeHour(TimeCurrent()) < TimeHour(StrToTime(gs_92 + t2))) gs_92 = TimeToStr(iTime(NULL, PERIOD_D1, 1), TIME_DATE) + " ";
Comment(gs_92);
return (0);
}

int deinit() {
ObjectDelete("tr");
return (0);
}

int start() {
int l_ind_counted_0 = IndicatorCounted();
int l_shift_4 = iBarShift(NULL, 0, StrToTime(gs_92 + t1));
int l_shift_8 = iBarShift(NULL, 0, StrToTime(gs_92 + t2));
ObjectCreate("tr", OBJ_TREND, 0, StrToTime(gs_92 + t1), iClose(NULL, 0, l_shift_4), StrToTime(gs_92 + t2), iClose(NULL, 0, l_shift_8));
return (0);
}
 
gringoh:

Hello,

The purpose of this indicator is to draw a trendline between the close of 2 specific candle.

I have tried different thing to add history to this indicator without any success.

Like: extern int NumberOfDays = 5; ...

But no success.


If anybody can help me that would be great,

Thanks in advance

i believe this is want you wanted ...

#property indicator_chart_window

extern string start$ = "02:45";
extern string end$ =   "07:45";
string today$;

int init(){
   today$ = TimeToStr(TimeCurrent(), TIME_DATE) + " ";
   if( TimeHour(TimeCurrent()) < TimeHour(StrToTime(today$ + end$)))
      today$ = TimeToStr(iTime(NULL, PERIOD_D1, 1), TIME_DATE) + " ";   // start from yesterday instead
   
   Comment(today$);
   
   int startBar = iBarShift(NULL, 0, StrToTime(today$ + start$));
   int endBar =   iBarShift(NULL, 0, StrToTime(today$ + end$  ));
   Comment(today$);
   
   // for debug ...
   //Comment(today$ + "\nstartBar=" + startBar + "\nendbar=" + endBar);
   
   ObjectCreate("tr", OBJ_TREND, 0, Time[startBar], Close[startBar], Time[endBar], Close[endBar]);

   return (0);
}

int deinit(){
   ObjectDelete("tr");
   Comment("");
   return (0);
}

int start(){
   // All the work is done in the init function so there is no work to be done on a new tick
   return (0);
}
 
dabbler:

i believe this is want you wanted ...

I think that your code is better than mine.


In this code a trendline will be drawn today if the current time allow it otherwise yesterday.


But the question is how to display the 5 previous day for example.


Thanks for your help.

 
gringoh:

But the question is how to display the 5 previous day for example.


Try this then ...

#property indicator_chart_window

extern string start$ = "02:45";
extern string end$ =   "07:45";

#define DAYCOUNT 5

int init(){
   
   for( int n=0; n<DAYCOUNT; n++ ){
      datetime day = iTime(NULL,PERIOD_D1,n);
      string day$  = TimeToStr(day, TIME_DATE) + " ";
      int startBar = iBarShift(NULL, 0, StrToTime(day$ + start$));
      int endBar =   iBarShift(NULL, 0, StrToTime(day$ + end$  ));
   
      ObjectCreate("tr"+n, OBJ_TREND, 0, Time[startBar], Close[startBar], Time[endBar], Close[endBar]);
   }
   
   return (0);
}

int deinit(){
   for( int n=0; n<DAYCOUNT; n++ )
      ObjectDelete("tr"+n);

   Comment("");
   return (0);
}

int start(){
   // All the work is done in the init function so there is no work to be done on a new tick
   return (0);
}
 

Thank you very much for your help, it's just perfect,

Here the last version with my personnal modifications if it can help one of you ;-)



//+------------------------------------------------------------------+
//| Eagle Line.mq4 |
//| |
//| |
//+------------------------------------------------------------------+


#property indicator_chart_window

extern string Options = "//----- Eagle Line options --------//";
extern int NumberOfDays = 5;
extern string Start = "02:45";
extern string End = "07:45";
extern color Linecolor = White;

//+#define DAYCOUNT 5

int init(){

for( int n=0; n<NumberOfDays; n++ ){
datetime day = iTime(NULL,PERIOD_D1,n);
string day$ = TimeToStr(day, TIME_DATE) + " ";
int startBar = iBarShift(NULL, 0, StrToTime(day$ + Start));
int endBar = iBarShift(NULL, 0, StrToTime(day$ + End ));

ObjectCreate("tr"+n, OBJ_TREND, 0, Time[startBar], Close[startBar], Time[endBar], Close[endBar]);
ObjectSet("tr"+n, OBJPROP_RAY, 0);
ObjectSet("tr"+n, OBJPROP_COLOR, Linecolor);

}

return (0);
}

int deinit(){
for( int n=0; n<NumberOfDays; n++ )
ObjectDelete("tr"+n);

Comment("");
return (0);
}

int start(){
// All the work is done in the init function so there is no work to be done on a new tick
return (0);
}






 

  1. Instead of using strings to date, you can use doubles which can be optimized in the tester
        int         DOW = TimeDayOfWeek(now),   /* forum.mql4.com/33851
        // reports DayOfWeek() always returns 5 in the tester. No refresh?*/
                    DayMask = 1 << DOW;
        //                      #define DAYS_MAX    0x3F// 1<<6-1=63. (S-F)
        //extern int      Days.Mask               =  55;      // Not Wed
        if (Days.Mask & DayMask == 0){  EA.status="Day="+DOW;           return; }
        //extern double   TradeHr.UTC.Start   =   7.3;    // London-1
        //extern double   TradeHr.UTC.End     =  12.9;    // NY open
        int secStart    = 3600*TradeHr.UTC.Start,
            secEnd      = 3600*TradeHr.UTC.End,
            hrBeg       = (now-secStart+86400)%86400,
            hrEnd       = (now-secEnd  +86400)%86400;
        if (hrBeg > hrEnd){ double Tminus=hrBeg/3600.-24;
                                EA.status="HR"+DoubleToStr(Tminus,2);   return; }
    

Reason: