Operation on DateTime

O. N.  

Hello!

I am trying to understand the following operation:

int te, uv;

uv = Period()*60;

For(int i = 0; i < 10; i++)

{

  te = Time[0] - i*uv;

}

Time[0] is a DateTime  type; te is an integer.

I am just failling to unsderstand how and why such operation can be carried out with two variables of different types.

I wanted to replicate such operationn in C#, but I am failling.

Could someone help please?


Many thanks in advance!

Fernando Carreiro  

O. N.: I am trying to understand the following operation:

int te, uv;
uv = Period()*60;

For(int i = 0; i < 10; i++)
{
  te = Time[0] - i*uv;
}

Time[0] is a DateTime  type; te is an integer. I am just failling to unsderstand how and why such operation can be carried out with two variables of different types. I wanted to replicate such operationn in C#, but I am failling.

Could someone help please? Many thanks in advance!

Don't use "Period() * 60". Use PeriodSeconds() instead. Period() returns an enumeration and in MQL5 that does not always equate to minutes as it did in MQL4.

A datetime is also an integer based datatype, and the date and time are expressed in elapsed seconds, which facilitates the type of equation you are using.

O. N.  

Many thanks Carreiro for your quick reply.

I will implement the code again and revert to you if I have problem.


Many thanks!

O. N.  
Fernando Carreiro #:

Don't use "Period() * 60". Use PeriodSeconds() instead. Period() returns an enumeration and in MQL5 that does not always equate to minutes as it did in MQL4.

A datetime is also an integer based datatype, and the date and time are expressed in elapsed seconds, which facilitates the type of equation you are using.




Carreiro,

many thanks again for your help.

I am just failling to solve this problem. Since I am working in C#, the function  PeriodSeconds() does not exist in C#.

How can I avoid this function and still have the code implemented correctly?


Thanks in advance!

Documentation on MQL5: Common Functions / PeriodSeconds
Documentation on MQL5: Common Functions / PeriodSeconds
  • www.mql5.com
PeriodSeconds - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Fernando Carreiro  
O. N. #: Carreiro, many thanks again for your help. I am just failling to solve this problem. Since I am working in C#, the function  PeriodSeconds() does not exist in C#. How can I avoid this function and still have the code implemented correctly? Thanks in advance!

This site is for MetaTrader and its MQL language, not C#. MetaTrader does no offer an API or library for C#.

It is obvious that MetaTrader/MQL functionality is not available on standard C#.

Why are you trying to replicate it in C# in the first place, if it will never interface with MetaTrader?

O. N.  
Fernando Carreiro #:

This site is for MetaTrader and its MQL language, not C#. MetaTrader does no offer an API or library for C#.

It is obvious that MetaTrader/MQL functionality is not available on standard C#.

Why are you trying to replicate it in C# in the first place, if it will never interface with MetaTrader?

I am replicating it in C# because I am trading on Metatrader as well as on another platform where C# is used to develop tools. Metatader does not offer ticks charts and I need my tools on ticks chart as well.

Thanks for your reply!

Fernando Carreiro  
O. N. #: I am replicating it in C# because I am trading on Metatrader as well as on another platform where C# is used to develop tools. Metatader does not offer ticks charts and I need my tools on ticks chart.

MetaTrader 5 does not offer tick-charts as standard time-frames, but you can have them as Custom Symbols or simply process the tick data directly in the EA. It will certainly be much easier to do that than replicate MetaTrader/MQL functionality in C# on another platform.

Alternatively, just code the EA from scratch based on that other platform's functionality. Trying to retro-fit it is just going to make it highly inefficient.

Take the time to understand how that other platform works and write your code based on that functionality instead of MetaTrader's.

William Roeder  
O. N.: I am trying to understand the following operation:

int te, uv = Period()*60;

for(int i = 0; i < 10; i++){
  te = Time[0] - i*uv;
}
  1. PeriodSeconds() instead of Period()*60 has already been addressed.
  2. te is the start time of candle i, assuming that all candles exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

    Use Time[i] instead.

O. N.  

Many thanks Fernando, many thanks William for your reply.

I am stil struggling to unsderstad this operation. Allow me please to go step by step. 

In the following code

int te, uv = Period()*60;

for(int i = 0; i < 10; i++){
  te = Time[0] - i*uv;
}

uv is an integer, Time[0] a DateTime variable. i here represents the index of a bar. If this is correct, the substraction is occuring between a DateTime and an integer variables. Could you please correct me if I am mistaking? It is not really clear to me. I thought such substraction could be made between two DateTimes and not between a DateTime and an integer.

Willim has suggested to use Time[i], and that would be

te = Time[0] - Time[i];


But such operation is not working. Is there a formula to substract two DateTimes?


Many thanks in advance.

William Roeder  
O. N. #: But such operation is not working. Is there a formula to substract two DateTimes?

Yes, you subtract one from the other. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
     How To Ask Questions The Smart Way. (2004)
          When asking about code
          Be precise and informative about your problem

Reason: