Is there a better way to get the timespan of Chart.TimeFrame ?

Created at 27 Jul 2023, 23:55
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!
Capt.Z-Fort.Builder's avatar

Capt.Z-Fort.Builder

Joined 03.06.2020

Is there a better way to get the timespan of Chart.TimeFrame ?
27 Jul 2023, 23:55


Hello, 
Is there a better way to get the timespan of Chart.TimeFrame ?
Thanks.

        private TimeSpan TimeSpanTimeFrame()
        {
            if      ( Chart.TimeFrame == TimeFrame.Minute   ) { return new TimeSpan(0,1,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute2  ) { return new TimeSpan(0,2,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute3  ) { return new TimeSpan(0,3,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute4  ) { return new TimeSpan(0,4,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute5  ) { return new TimeSpan(0,5,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute6  ) { return new TimeSpan(0,6,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute7  ) { return new TimeSpan(0,7,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute8  ) { return new TimeSpan(0,8,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute9  ) { return new TimeSpan(0,9,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute10 ) { return new TimeSpan(0,10,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute15 ) { return new TimeSpan(0,15,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute20 ) { return new TimeSpan(0,20,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute30 ) { return new TimeSpan(0,30,0); }
            else if ( Chart.TimeFrame == TimeFrame.Minute45 ) { return new TimeSpan(0,45,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour     ) { return new TimeSpan(1,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour2    ) { return new TimeSpan(2,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour3    ) { return new TimeSpan(3,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour4    ) { return new TimeSpan(4,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour6    ) { return new TimeSpan(6,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour8    ) { return new TimeSpan(8,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Hour12   ) { return new TimeSpan(12,0,0); }
            else if ( Chart.TimeFrame == TimeFrame.Daily    ) { return new TimeSpan(24,0,0 ); }
            else if ( Chart.TimeFrame == TimeFrame.Day2     ) { return new TimeSpan(48,0,0 ); }
            else if ( Chart.TimeFrame == TimeFrame.Day3     ) { return new TimeSpan(72,0,0 ); }
            else if ( Chart.TimeFrame == TimeFrame.Weekly   ) { return new TimeSpan(7,0,0,0,0 ); }
            else if ( Chart.TimeFrame == TimeFrame.Monthly  ) { return new TimeSpan(30,0,0,0,0); }
            else return new TimeSpan(0,0,0);
        }

@Capt.Z-Fort.Builder
Replies

Capt.Z-Fort.Builder
28 Jul 2023, 17:22

Here is a method provided by a fellow (@TheLegitT) in the cTrader official group, though I didn't test, I believe it will work as suppose to be:

 private TimeSpan GetCurrentTimeSpan()
 {
     var TimeFrameName = Chart.TimeFrame.ToString();

     if (!int.TryParse(Regex.Replace(TimeFrameName, "[^0-9]", ""), out int prefix)) prefix = 1;

     if (TimeFrameName.Contains("Minute"))
         return TimeSpan.FromMinutes(prefix);
     else if (TimeFrameName.Contains("Hour"))
         return TimeSpan.FromHours(prefix);
     else if (TimeFrameName.Contains("Day") || TimeFrameName.Contains("Daily"))
         return TimeSpan.FromDays(prefix);
     else if (TimeFrameName == "Weekly")
         return TimeSpan.FromDays(7);
     else if (TimeFrameName == "Monthly")
         return TimeSpan.FromDays(31);
     else
         return TimeSpan.Zero;
 }

Usage:
 var CurrentTimeSpan = GetCurrentTimeSpan();

 if (CurrentTimeSpan == TimeSpan.Zero) // No supported TimeFrame available on Chart
 {

 }

 // TimeSpan is acquired properly

@Capt.Z-Fort.Builder