Trading on specidifc hours

Created at 01 Mar 2013, 01:22
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!
condor's avatar

condor

Joined 28.02.2013

Trading on specidifc hours
01 Mar 2013, 01:22


Hi,

how can i set a robot trading only between 22:00 and 6:00 o'clock ?


@condor
Replies

cAlgo_Fanatic
01 Mar 2013, 11:51

Hello,

You can use Server.Time

See the sample code Trading Time Interval for reference.

Regards,


@cAlgo_Fanatic

condor
01 Mar 2013, 16:18

Thank you, but it seems that the solution in the sample doesn't works or maybe there is an error in my code, can you take a look :

    public class RSIBot : Robot
    {
        private RelativeStrengthIndex RSI;
        private StochasticOscillator Stoch;
        private HighLowBands HLBands;
        private Position _position;
        private DateTime startTime;
        private DateTime endTime;

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Begin Trading Hour", DefaultValue = 22.0)]
        public double Begin { get; set; }

        [Parameter("Ending Trading Hour", DefaultValue = 8.0)]
        public double Ending { get; set; }

        [Parameter("TP", DefaultValue = 2, MinValue = 1)]
        public int TakeProfit { get; set; }

        [Parameter("SL", DefaultValue = 5, MinValue = 3)]
        public int StopLoss { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }


        protected override void OnStart()
        {
            RSI = Indicators.RelativeStrengthIndex(Source, 7);
            Stoch = Indicators.StochasticOscillator(9, 3, 9, MovingAverageType.Simple);
            HLBands = Indicators.HighLowBands(14);

            startTime = Server.Time.Date.AddHours(Begin);
            endTime = Server.Time.Date.AddHours(Ending);

            Print("Start Time {0},", startTime);
            Print("End Time {0},", endTime);

        }

        protected override void OnTick()
        {
            if (Trade.IsExecuting) return;

            bool tradeTime = Server.Time >= startTime && Server.Time <= endTime;

            if (!tradeTime) return;




@condor

cAlgo_Fanatic
01 Mar 2013, 18:00

We apologize for this. It has been updated.


Please change

bool tradeTime = Server.Time >= startTime && Server.Time <= endTime;

 

to these lines:

var currentHours = Server.Time.TimeOfDay.TotalHours;

bool tradeTime = StartTime < StopTime 
    ? currentHours > StartTime && currentHours < StopTime
    : currentHours < StopTime || currentHours > StartTime;



 


@cAlgo_Fanatic

condor
01 Mar 2013, 18:12

Thanks, it's better but still doesn't works :(

The reason is that the "var" which is a double cannot be compared to a "DateTime".

And i've no idea how to cast a DateTime to a double.


@condor

condor
03 Mar 2013, 14:58

Finally i've found an easier way to do it :

			bool tradeTime = false;
			if(Begin < Ending) tradeTime = Server.Time.Hour >= Begin && Server.Time.Hour < Ending;
			if(Ending < Begin) tradeTime = Server.Time.Hour >= Begin || Server.Time.Hour <= Ending;
			
			if (!tradeTime) return;




@condor

cAlgo_Fanatic
04 Mar 2013, 10:10

RE:
condor said:

Thanks, it's better but still doesn't works :(

The reason is that the "var" which is a double cannot be compared to a "DateTime".

And i've no idea how to cast a DateTime to a double.

The example uses different variable names than the code you posted. Maybe that is the reason it didn't work. Server.Time.TimeOfDay.TotalHours is of type "double" and StartTime and StopTime are the input variables which are also double.


@cAlgo_Fanatic