stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Time Lock Indicator
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4563
Page 1 of 1
Author:  xray2 [ Wed Jan 06, 2016 10:43 am ]
Post subject:  Time Lock Indicator

I am looking for a bit of advice on providing some code for a time lock on an indicator i have/am creating.
I'm not much of a programmer but with a bit of copy and paste I seem to manage to do basic stuff.
I am looking for some code to restrict the indicator to a particular date.
I have found some code which works on "old" mq4 but not on new mq4.

This is the code I have:

Code: Select all

//< Expiration time control >
static datetime exp = D'01.02.2016';
if (Time[0] > exp){ Alert("expired"); return; }
//</Expiration time control >
Is there anyone that help me with this or provide simialr code which will work.
Thanks in advance
Author:  renexxxx [ Wed Jan 06, 2016 11:26 am ]
Post subject:  Time Lock Indicator

Your are very close with your code, except that the date constant is expressed as: D'2016.02.01' (for the first of February, 2016).

I would do something like this:

Code: Select all

static const datetime expiryDate = D'2016.01.06';

int OnInit() {
   
   if ( TimeCurrent() > expiryDate ) {
      return(INIT_FAILED);
   }
   
   //---
   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[]) {

   //---
   if ( TimeCurrent() > expiryDate ) {
      MessageBox("Indicator has expired.","Expiry Error",0);
      ExpertRemove();
      return(0);
   }
   
   //--- return value of prev_calculated for next call
   return(rates_total);
}
Your use of Time[0] would work too, except that if the indicator was installed on a MN1 chart, it would still work until the end of the month. I use TimeCurrent() (i.e. server time) so that users can not get the indicator to work, by changing the computer clock. Also, I like to have a check inside the OnCalculate() (or OnStart()), so that it would be removed real-time once the clock ticks over beyond the expiry date.

Hope this helps.
Author:  xray2 [ Thu Jan 07, 2016 8:43 am ]
Post subject:  Time Lock Indicator

Thanks Rene for sharing you knowledge.
Hopefully I can follow your instructions.
All times are UTC Page 1 of 1