Need help with time functions

Created at 01 Nov 2014, 08:42
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!
AP

apresau

Joined 31.03.2014

Need help with time functions
01 Nov 2014, 08:42


Hi,

I'm trying to code a simple indicator to insert vertical lines on the chart at specific hours.

However I don't understand time functions and am struggling to find tutorials / examples to illustrate

the various functions.

 

Any help with my broken code would me much appreciated.

Thanks

apresau

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TimeMarkers : Indicator
    {
        [Parameter(DefaultValue = 12)]
        public int Hour_1 { get; set; }

        [Parameter(DefaultValue = 16)]
        public int Hour_2 { get; set; }

        private DateTime _Hour_1, _Hour_2;

        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            _Hour_1 = new DateTime(Hour_1, 0, 0);
            _Hour_2 = new DateTime(Hour_2, 0, 0);

            if (Server.Time.Date.AddHours(0) == _Hour_1)
                ChartObjects.DrawVerticalLine("vLine_1", index, Colors.Red, 1, LineStyle.Lines);

            if (Server.Time.Date.AddHours(0) == _Hour_2)
                ChartObjects.DrawVerticalLine("vLine_2", index, Colors.Gold, 1, LineStyle.Lines);

        }
    }
}

 


@apresau
Replies

Invalid
03 Nov 2014, 09:17

RE:

DateTime initialization is wrong.

Currently you use this one (Year, Month, Hour). You need to create a date time using (year, month, day, hour, minutes, seconds)

apresau said:

Hi,

I'm trying to code a simple indicator to insert vertical lines on the chart at specific hours.

However I don't understand time functions and am struggling to find tutorials / examples to illustrate

the various functions.

 

Any help with my broken code would me much appreciated.

Thanks

apresau

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TimeMarkers : Indicator
    {
        [Parameter(DefaultValue = 12)]
        public int Hour_1 { get; set; }

        [Parameter(DefaultValue = 16)]
        public int Hour_2 { get; set; }

        private DateTime _Hour_1, _Hour_2;

        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            _Hour_1 = new DateTime(Hour_1, 0, 0);
            _Hour_2 = new DateTime(Hour_2, 0, 0);

            if (Server.Time.Date.AddHours(0) == _Hour_1)
                ChartObjects.DrawVerticalLine("vLine_1", index, Colors.Red, 1, LineStyle.Lines);

            if (Server.Time.Date.AddHours(0) == _Hour_2)
                ChartObjects.DrawVerticalLine("vLine_2", index, Colors.Gold, 1, LineStyle.Lines);

        }
    }
}

 

 


@Invalid

apresau
03 Nov 2014, 16:46

Thankyou, invalid.

I now understand that function and realise that it isn't what I need to use -

I was trying to apply the lines to the full chart history and obviously need to use a different function.

Any ideas would be appreciated.

apresau


@apresau