TimespanMT4(int) convert to Datetime(datetime)cTrader

Created at 04 Oct 2014, 20:49
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MI

mijo212

Joined 12.10.2013

TimespanMT4(int) convert to Datetime(datetime)cTrader
04 Oct 2014, 20:49


Hello,

how can i convert datetime(int) in format MT4 to datetime cTrader ?

thx Mijo.


@mijo212
Replies

Invalid
06 Oct 2014, 09:14

RE:
public static DateTime UnixTimeStampToDateTime( int unixTimeStamp )
{
    // Unix timestamp is seconds past epoch
    var dateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
    dateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
    return dateTime;
}

 

mijo212 said:

Hello,

how can i convert datetime(int) in format MT4 to datetime cTrader ?

thx Mijo.

 


@Invalid

algotrader
06 Oct 2014, 09:25

class Mq4TimeSeries
    {
        private readonly TimeSeries _timeSeries;
        private static readonly DateTime StartDateTime = new DateTime(1970, 1, 1);

        public Mq4TimeSeries(TimeSeries timeSeries)
        { 
            _timeSeries = timeSeries;
        }

        public static int ToInteger(DateTime dateTime)
        {
          return (int)(dateTime - StartDateTime).TotalSeconds;
        }
            
        public static DateTime ToDateTime(int seconds)
        {
          return StartDateTime.AddSeconds(seconds);
        }
                
        public int this[int index]
        {
            get 
            { 
                if (index < 0 || index >= _timeSeries.Count)
                  return 0;
                  
                DateTime dateTime = _timeSeries[_timeSeries.Count - 1 - index];

                return ToInteger(dateTime); 
            }
        }
    }

Example of usage:

DateTime cSharpTime = Mq4TimeSeries.ToDateTime(mt4Time);

Taken from https://github.com/kt5ov/2calgo.Library/blob/master/Presenter/Templates/Common/OuterParts/Mq4TimeSeries.c


@algotrader

algotrader
06 Oct 2014, 09:26

class Mq4TimeSeries
    {
        private static readonly DateTime StartDateTime = new DateTime(1970, 1, 1);
 
        public static int ToInteger(DateTime dateTime)
        {
          return (int)(dateTime - StartDateTime).TotalSeconds;
        }
             
        public static DateTime ToDateTime(int seconds)
        {
          return StartDateTime.AddSeconds(seconds);
        }
    }

Example of usage:

DateTime cSharpTime = Mq4TimeSeries.ToDateTime(mt4Time);

DateTime cSharpTime = Mq4TimeSeries.ToDateTime(mt4Time);

Taken from https://github.com/kt5ov/2calgo.Library/blob/master/Presenter/Templates/Common/OuterParts/Mq4TimeSeries.c


@algotrader

mijo212
08 Oct 2014, 17:55

 thanks very much


@mijo212