need help on date time functions

Created at 27 Feb 2018, 06:36
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!
MA

marwaha1@gmail.com

Joined 27.02.2018

need help on date time functions
27 Feb 2018, 06:36


Hi everyone,

I am pretty new to the code and have worked around other things but this one thing i am getting stuck.

I need help with how to have the user select the specific time at which the trade must be taken and same for exiting the trades. 

for ex.

  if time is > x(in user parameters) and < than y(in user parameters) and indicator does this then buy/sell

  if time == z then close all positions

greatly appreciate help


@marwaha1@gmail.com
Replies

PanagiotisCharalampous
27 Feb 2018, 11:11

Hi marwaha1@gmail.com,

Thanks for posting in our forum. See below an example that might be helpful

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0)]
        public int Minute { get; set; }
        [Parameter(DefaultValue = 12)]
        public int Hour { get; set; }
        [Parameter(DefaultValue = 1)]
        public int Day { get; set; }
        [Parameter(DefaultValue = 1)]
        public int Month { get; set; }
        [Parameter(DefaultValue = 2018)]
        public int Year { get; set; }

        private DateTime _closeDateTime;
        protected override void OnStart()
        {
            _closeDateTime = new DateTime(Year, Month, Day, Hour, Minute, 0);
        }

        protected override void OnTick()
        {
            if (DateTime.Now > _closeDateTime)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

You can adjust accordingly based on your needs.

Best Regards,

Panagiotis


@PanagiotisCharalampous

marwaha1@gmail.com
28 Feb 2018, 14:26

RE:

Hi Panagiotis Charalampous,

Thanks for the help. Does this mean that i would have to manually change the Month and Day and Year in the cBot or is there a way to set the year, month,day to current values.

kind regards,

mohit marwaha

 

Panagiotis Charalampous said:

Hi marwaha1@gmail.com,

Thanks for posting in our forum. See below an example that might be helpful

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0)]
        public int Minute { get; set; }
        [Parameter(DefaultValue = 12)]
        public int Hour { get; set; }
        [Parameter(DefaultValue = 1)]
        public int Day { get; set; }
        [Parameter(DefaultValue = 1)]
        public int Month { get; set; }
        [Parameter(DefaultValue = 2018)]
        public int Year { get; set; }

        private DateTime _closeDateTime;
        protected override void OnStart()
        {
            _closeDateTime = new DateTime(Year, Month, Day, Hour, Minute, 0);
        }

        protected override void OnTick()
        {
            if (DateTime.Now > _closeDateTime)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

You can adjust accordingly based on your needs.

Best Regards,

Panagiotis

 


@marwaha1@gmail.com

PanagiotisCharalampous
02 Mar 2018, 12:56

Hi marwaha1@gmail.com,

You can use DateTime.Now to set the current values. But then the condition will be always true.

Best Regards,

Panagiotis


@PanagiotisCharalampous