Trades at specific times

Created at 09 Nov 2020, 03:37
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!
SA

samuel.jus.cornelio

Joined 19.03.2020

Trades at specific times
09 Nov 2020, 03:37


 

(Hi Pani, I'm back, brow.) I wrote a code, whose goal should be to make trades only at specific times.

however my code continues to execute trades outside the determined times. how can I solve this problem.?

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.ESouthAmericaStandardTime, AccessRights = AccessRights.None)]
    public class bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

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


        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 10)]
        public double StopLoss { get; set; }

        [Parameter("Take_Profit", DefaultValue = 100, MinValue = 10)]
        public double TakeProfit { get; set; }


        [Parameter("Start Hour", DefaultValue = 5.0)]
        public double StartTime { get; set; }

        [Parameter("Stop Hour", DefaultValue = 18.0)]
        public double StopTime { get; set; }



        private DateTime _startTime;
        private DateTime _stopTime;

 

    private RelativeStrengthIndex _1hrsi1;
        private RelativeStrengthIndex _1hrsi2;
        private StochasticOscillator _1hstoch1;
        private StochasticOscillator _1hstoch2;

        protected override void OnStart()
        {
            _startTime = Server.Time.Date.AddHours(StartTime);


            _stopTime = Server.Time.Date.AddHours(StopTime);



        }



        protected override void OnTick()
        {



            {
                if (Trade.IsExecuting)
                    return;

                var currentHours = Server.Time.TimeOfDay.TotalHours;
                bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;

                if (!tradeTime)
                    return;

                if (Positions.Count != 0)
                    return;

            }

  var hour1 = MarketData.GetBars(TimeFrame.Hour);

    _1hstoch2 = Indicators.StochasticOscillator(hour1, 12, 3, 12, MovingAverageType.Simple);

  if (_1hstoch2.PercentK.LastValue > 80)

                                                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)
                                                    if (Positions.Count == 0)
                                                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);
 

 


@samuel.jus.cornelio
Replies

PanagiotisCharalampous
09 Nov 2020, 09:19

Hi samuel.jus.cornelio,

You need to provide the complete cBot code and steps to reproduce this behavior.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
09 Nov 2020, 15:00

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

You need to provide the complete cBot code and steps to reproduce this behavior.

Best Regards,

Panagiotis 

Join us on Telegram

 

Pani, the code is up there. Please, how can I write a line of code that makes my bot execute trades only from 5 am until 6 pm? Thanks man, you are the best


@samuel.jus.cornelio

PanagiotisCharalampous
09 Nov 2020, 15:06

Hi samuel.jus.cornelio,

You can try something like the below

                var tradingStarts = TimeSpan.ParseExact("17:00", "hh\\:mm", null);
                var tradingStops = TimeSpan.ParseExact("18:00", "hh\\:mm", null);

                if (Server.Time.TimeOfDay >= tradingStarts && Server.Time.TimeOfDay < tradingStops)
                {
                    // trade
                }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
09 Nov 2020, 18:53

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

You can try something like the below

                tradingStarts = TimeSpan.ParseExact("17:00", "hh\\:mm", null);
                tradingStops = TimeSpan.ParseExact("18:00", "hh\\:mm", null);

                if (Server.Time.TimeOfDay >= tradingStarts && Server.Time.TimeOfDay < tradingStops)
                {
                    // trade
                }

Best Regards,

Panagiotis 

Join us on Telegram

Pani, I tried exactly what you said and I still haven't been successful 

I received 4 error messages in the code :
Error CS0029: It is not possible to implicitly convert the type 'System.TimeSpan' to 'double'

Error CS0029: It is not possible to implicitly convert the type 'System.TimeSpan' to 'double'

Error CS0019: It is not possible to apply the operator '> =' to operands of type 'System.TimeSpan' and 'double'

Error CS0019: It is not possible to apply the operator '<' to operands of type 'System.TimeSpan' and 'double'

the code is below

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.ESouthAmericaStandardTime, AccessRights = AccessRights.None)]
    public class bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

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


        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 10)]
        public double StopLoss { get; set; }

        [Parameter("Take_Profit", DefaultValue = 100, MinValue = 10)]
        public double TakeProfit { get; set; }


        [Parameter(" tradingStarts", DefaultValue = 5.0)]
        public double tradingStarts { get; set; }

        [Parameter(" tradingStops", DefaultValue = 18.0)]
        public double tradingStops { get; set; }




        private StochasticOscillator _1hstoch2;

        protected override void OnStart()
        {



        }



        protected override void OnTick()
        {



            {


            }







            var day = Server.Time.DayOfWeek;

            var hour1 = MarketData.GetBars(TimeFrame.Hour);



            tradingStarts = TimeSpan.ParseExact("17:00", "hh\\:mm", null);
            tradingStops = TimeSpan.ParseExact("18:00", "hh\\:mm", null);








            _1hstoch2 = Indicators.StochasticOscillator(hour1, 12, 3, 12, MovingAverageType.Simple);



            if (_1hstoch2.PercentK.LastValue > 80)

                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)
                    if (Positions.Count == 0)

                        if (Server.Time.TimeOfDay >= tradingStarts && Server.Time.TimeOfDay < tradingStops)

                            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);











        }
    }
}


@samuel.jus.cornelio

PanagiotisCharalampous
10 Nov 2020, 08:12

Hi samuel.jus.cornelio,

The message clearly describes the problem

Error CS0029: It is not possible to implicitly convert the type 'System.TimeSpan' to 'double'

Your variable should be of type TimeSpan and not double.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
10 Nov 2020, 17:23

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

The message clearly describes the problem

Error CS0029: It is not possible to implicitly convert the type 'System.TimeSpan' to 'double'

Your variable should be of type TimeSpan and not double.

Best Regards,

Panagiotis 

Join us on Telegram

 

Pani, could you please give me an example of how this can be done? Thank you very much for all the help you have given me, you are the guy


@samuel.jus.cornelio

PanagiotisCharalampous
11 Nov 2020, 09:12

Hi samuel.jus.cornelio,

I am sorry but I am not sure what do you need. I provided an example above. Here it is again

                var tradingStarts = TimeSpan.ParseExact("17:00", "hh\\:mm", null);
                var tradingStops = TimeSpan.ParseExact("18:00", "hh\\:mm", null);

                if (Server.Time.TimeOfDay >= tradingStarts && Server.Time.TimeOfDay < tradingStops)
                {
                    // trade
                }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous