Date time of Previous Year

 

Hi there,
I tried and tried to get the open date of the previous year.
Can you help me please ?

Now, i do it like this but, it's not "pro"

datetime  PREV_YEAR_OPEN_DATE=D'2017.01.001 0:00';
   
datetime PREV_MONTH_OPEN_DATE=iTime(Symbol(),PERIOD_MN1,1);

datetime PREV_WEEK_OPEN_DATE=iTime(Symbol(),PERIOD_W1,1);

datetime PREV_DAY_OPEN_DATE=iTime(Symbol(),PERIOD_D1,1);

Regards.

 
  1. Please use the SRC-button if you post code (more than one line)
  2. Have you checked the error messages?
  3. What did you get and what did you expect: PREV_YEAR_OPEN_DATE ?
  4. Maybe you shouldn't use: "2017.01.001 0:00" but try:
  5. datetime  PREV_YEAR_OPEN_DATE=D'2017.01.01 00:00';

 
Carl Schreiber:
  1. Please use the SRC-button if you post code (more than one line)
  2. Have you checked the error messages?
  3. What did you get and what did you expect: PREV_YEAR_OPEN_DATE ?
  4. Maybe you shouldn't use: "2017.01.001 0:00" but try:


Hello,
I do not get any error.
But i need it to be like the previous month (for example).

datetime PREV_MONTH_OPEN_DATE=iTime(Symbol(),PERIOD_MN1,1);

I find how to get the high and low price of the previous year

PREV_YEAR_HIGH = iHigh(NULL, PERIOD_MN1, iHighest(NULL, PERIOD_MN1, MODE_HIGH, 12, Month()));

And i need the open time now.

Thank you very much.

 
Thierry Ramaniraka: I find how to get the high and low price of the previous year
PREV_YEAR_HIGH = iHigh(NULL, PERIOD_MN1, iHighest(NULL, PERIOD_MN1, MODE_HIGH, 12, Month()));
And i need the open time now.
  1. You already know how. You posted the code. Since MT doesn't have yearly charts you have to find the MN1 indexes yourself.
    #define INDEX uint
    #define PRICE double
    datetime startOfMonth    = iTime(Symbol(),PERIOD_MN1,0);
    INDEX    iMnLastYearLast = TimeMonth(startOfMonth);       // MN1 last bar of last year.
    INDEX    iMnHighestMonth = iHighest(NULL, PERIOD_MN1, MODE_HIGH, 12, iMnLastYearLast);
    PRICE    HHlastYeat      = iHigh(NULL, PERIOD_MN1, iMnHighestMonth)

  2. On MT4: Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. "Open time" of what "now?" The year? iMNCurrYearFirst = iMNLastYearLast + 1
 
whroeder1:
  1. You already know how. You posted the code. Since MT doesn't have yearly charts you have to find the MN1 indexes yourself.

  2. On MT4: Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. "Open time" of what "now?" The year? iMNCurrYearFirst = iMNLastYearLast + 1

Thank you for your response.
Yes i need the open time of the previous year.

I tried so many way, but i failed.
If you can give me the answer, it would be very nice for me...

Regards.

 
Thierry Ramaniraka:

Thank you for your response.
Yes i need the open time of the previous year.

I tried so many way, but i failed.
If you can give me the answer, it would be very nice for me...

Regards.


   MqlDateTime time;
   TimeCurrent(time);
   time.year--;
   time.mon=1;
   time.day=1;
   time.min=0;
   time.sec=0;
   time.hour=0;
   
   Print("Last year's start time = ",StructToTime(time));
 
Thierry Ramaniraka: Yes i need the open time of the previous year. I tried so many way, but i failed. If you can give me the answer, it would be very nice for me...
If iMnLastYearLast is December of last year, isn't January iMnLastYearLast+11? When in doubt, think.
 

Nothing work to me...as my coding skills are limited.
I will put the date manually.

Anyway, thank to all 3 of you very much for your time.
Regards.

 
whroeder1:
If iMnLastYearLast is December of last year, isn't January iMnLastYearLast+11? When in doubt, think.

As I continue to learn, i finally got it from your code.
Thank you very much.

Reason: