Timeseries ranging from hour to day to month

 

I want to test a trading system with the strategy tester (30 Minute timeframe). Trading this system I first decide discretionally whether I determine the Market to be long or short. So for testing the system in the Strategy Tester I need to define a certein period of time, let's say from Jan. 1st 2017  20:00 h  until Feb. 20th 2017 10:00 h. I cant't work it out. I can programm in another programming language similar to Trade Station (called Equilla) but I'm new to mql4. I've tried in the file below to 'find' the period seareched for, but defined periods of time only work out for me for reoccuring events during the day, like every day from 9:00 to 18:00 and probably every week or  fixed dates during the months.

Here what I did to define the period I was looking for:

//+------------------------------------------------------------------+
//|                                                    test_time.mq4 |
//|                                                          bratshi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "bratshi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int color_Buy = Lime;
input double lots = 0.01;
input double takeprofit = 10;
input double stoploss = 10;
input int magic_number = 10101010;
bool time_wanted = false;

//______________________________________________________________________________________________________

void OnTick(void)
{
   double ma;
   string time1 = "2017.01.10 20:00";        // defining the date at which the system is supposed to start executing orders if the other conditions are met
   datetime first_date = StrToDouble(time1);// since datetime can be referred to as a 'string' datatype I convert the data information to the 'double' datatype
                                            // Compiler warns at the line above: possible loss of data due to type conversion
   string time2 = "2017.02.20 10:00";
   datetime second_date = StrToDouble(time2); // Compiler warns here: possible loss of data due to type conversion
   int order_buy;


   ma=iMA(NULL,0,21,13,MODE_SMA,PRICE_CLOSE,0);

   if (time2 > time1) // trying to define the Period from 2017.01.10 20:00 until 2017.02.20 10:00, which I was looking for.
                        //First I tried (time2 - time1 = time_wanted, which would give the double of the period searched for in terms of a double.
                        //But the data type for mql4 is only boolean, though I used the 'StrToDouble' function
                      
       time_wanted = true; // For a working trading system to which this filter is added:
                           // The Tester takes all data for the period clicked on in the tester's 'Use data' box,
                           // but not the time that was supposed to be selected
      {
      Print ("time_wanted",time_wanted);
      return;
      }

   if ((Open[1]>ma && Close[1]<ma) && (time_wanted))
      {
      order_buy =OrderSend(Symbol() ,OP_BUY, lots ,Ask, 5, stoploss, takeprofit, "", magic_number, 0, color_Buy);
      }
}



Is it possible to define a given period of time in mql4 that covers any time series, ranging from hour to day to month? Please..................

Regards bratshi

Files:
test_time.mq4  3 kb
 
bratshi: Is it possible to define a given period of time in mql4 that covers any time series, ranging from hour to day to month?
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. string time2 = "2017.02.20 10:00";
    datetime second_date = StrToDouble(time2); // Compiler warns here: possible loss of data due to type conversion
    Why are you trying to convert to a double (2017.02?) Why aren't you converting to a datetime (Feb 20, 2017 10AM?) StringToTime - Conversion Functions - MQL4 Reference

  3. //First I tried (time2 - time1 = time_wanted, which would give the double of the period searched for in terms of a double
    Time2 minus time1 (plus one) is the number of seconds between those times. Why do you care?
  4. //But the data type for mql4 is only boolean, though I used the 'StrToDouble' function
    No idea what you mean. Non-zero is true.

  5. You have two datetimes. That defines a given period. Check if the current time is between them.
 
whroeder1:

  1. Please edit your post.
    For large amounts of code, attach it

  2. string time2 = "2017.02.20 10:00";
    datetime second_date = StrToDouble(time2); // Compiler warns here: possible loss of data due to type conversion
    Why are you trying to convert to a double (2017.02?) Why aren't you converting to a datetime (Feb 20, 2017 10AM?) StringToTime - Conversion Functions - MQL4 Reference

  3. //First I tried (time2 - time1 = time_wanted, which would give the double of the period searched for in terms of a double
    Time2 minus time1 (plus one) is the number of seconds between those times. Why do you care?
  4. //But the data type for mql4 is only boolean, though I used the 'StrToDouble' function
    No idea what you mean. Non-zero is true.

  5. You have two datetimes. That defines a given period. Check if the current time is between them.

Hello whroeder,

thank you for helping so much.  I also found a good example here: https://www.mql5.com/en/articles/585.

Your Question: 'Time2 minus time1 (plus one) is the number of seconds between those times. Why do you care?'
Answer: I though maybe the program structure could identity by each second which period of time it  refers to. Just thinking......

For finding the defined period I tried to followed your instructions. But still it wouldn't work and gives me all trades for the period chosen in the Strategy Tester 'use date' box.

//+------------------------------------------------------------------+
//|                                                    test_time.mq4 |
//|                                                          bratshi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "bratshi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int color_Buy=Lime;
input double lots=0.01;
input double takeprofit=10;
input double stoploss=10;
input int magic_number=10101010;
bool time_wanted=false;

input string time1="2017.01.10 20:00";
input string time2="2017.02.20 10:00";
//--- convert the string expression of time to the datetime type
datetime first_time =StringToTime(time1);
datetime second_time=StringToTime(time2);

datetime time_current =TimeCurrent();

//______________________________________________________________________________________________________

void OnTick(void)
  {
   double ma;
   ma=iMA(NULL,0,21,13,MODE_SMA,PRICE_CLOSE,0);
   int order_buy;
   //Trying to find the period inbetween Jan.10th 10:00AM and Feb. 20th 8:00 PM
   if (first_time >0 && second_time > 0 && time_current != first_time && time_current != second_time)

          if  (first_time <= second_time && second_time >= first_time  )
             {
               time_wanted = true;
             }
  
   if (time_wanted)
      {
      if ((Open[1]>ma && Close[1]<ma))
         {
          order_buy=OrderSend(Symbol(),OP_BUY,lots,Ask,5,stoploss,takeprofit,"",magic_number,0,color_Buy);
         }
      }
   }
//+------------------------------------------------------------------+



Do you have another clou for finding a defined period of time? Syntax is quite a hard job for beginners.

Regards bratshi

 
  1. whroeder1:
    5. You have two datetimes. That defines a given period. Check if the current time is between them.

    Hello whroeder,

    thank you for helping so much. ...

    Don't add text inside quoted text, put it outside. MQL4 Forum editor problem - MQL4 forum
  2. datetime time_current =TimeCurrent();
    void OnTick(void)
    Do not do that. Your variable will never change and TimeCurrent isn't defined until you get a tick.
  3. bratshi: Do you have another clou for finding a defined period of time? Syntax is quite a hard job for beginners.
    What part of "Check if the current time is between them" was unclear?
    void OnTick(void){
       datetime now = TimeCurrent();
       bool time_wanted = first_time <= now && now < second_time;
      
       if (time_wanted ...
 
whroeder1:
  1. Don't add text inside quoted text, put it outside. MQL4 Forum editor problem - MQL4 forum
  2. datetime time_current =TimeCurrent();
    void OnTick(void)
    Do not do that. Your variable will never change and TimeCurrent isn't defined until you get a tick.
  3. What part of "Check if the current time is between them" was unclear?
    void OnTick(void){
       datetime now = TimeCurrent();
       bool time_wanted = first_time <= now && now < second_time;
      
       if (time_wanted ...

whroeder1: The strategy tester now works!!!!!!!!!!!! THANK YOU SO MUCH !!!!!!!!!!!!!!!!!

1. Sorry, but  the 'reply' button only appears at certain instances. I didn't find it and so I clicked on any button I could see.

2. I had a problem understanding the way ‌the function TimeCurrent() works. Quoting the documentation:

   'In the OnTick() handler, this function returns the time of the received handled tick.  In other cases (for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on) this is the time of the last quote receipt for any symbol available in the "Market Watch" window, the time shown in the title of this window.'

3. So it is neccessary to refer to the ‌TimeCurrent() for the program to 'find' the variables first_time and second_time unconditionally.‌ The variable 'now' at any rate no longer refers to time of the last tick the server receives. Instead the function TimeCurrent() with the variables 'now' ( with the variable 'first_time' and  'second_time') is found in a defined intervall of certain dates. Therefore it is not neccessary to define 'first_time < second_time' additionally.

That's at least how I understand it and I hope I didn't mix up my English.....

Regards bratshi

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Reference on algorithmic/automated trading language for MetaTrader 5