Trading Time Interval

Spotware Support 's avatar
Spotware Support  since: 30 Nov -0001; 
 01 Mar 2013, 11:47
Trading Time Interval

The code executes only within a specific time interval of the day. The input parameters for the start and stop time are in hours. For instance, if the Start Hour is 10 and the Stop Hour is 12, the robot will only execute when the server time is between 10am and 12am.

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

namespace cAlgo.Robots
{
    [Robot]
    public class SampleTradingTime2 : Robot
    {
        private DateTime _startTime;
        private DateTime _stopTime;
        private BollingerBands _bollingerBands;

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

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


        protected override void OnStart()
        {
            // Start Time is the same day at 22:00:00 Server Time
            _startTime = Server.Time.Date.AddHours(StartTime);

            // Stop Time is the next day at 06:00:00
            _stopTime = Server.Time.Date.AddHours(StopTime);

            Print("Start Time {0},", _startTime);
            Print("Stop Time {0},", _stopTime);

            _bollingerBands = Indicators.BollingerBands(MarketSeries.Close, 20, 2, MovingAverageType.Simple);
        }

        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 top = _bollingerBands.Top.LastValue;
            var bottom = _bollingerBands.Bottom.LastValue;

            if (Symbol.Ask > top)                
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000);
            else if (Symbol.Bid < bottom)
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000);
        }
    }
}
TRADERS FIRST™
ironmine since: 29 Sep 2015; 
 09 Oct 2015, 13:35
RE:

admin said:

The code executes only within a specific time interval of the day. The input parameters for the start and stop time are in hours. For instance, if the Start Hour is 10 and the Stop Hour is 12, the robot will only execute when the server time is between 10am and 12am.

Hello!
How can I explain to a cBot that during optimization or backtesting it should refer not to the current server time hours, but to the server time which the historical tick data from the server are dated with?

cTrader Team's avatar
cTrader Team since: 23 Sep 2013; 
 13 Oct 2015, 00:49

Dear Trader,

You can use the Server.Time method instead of the DateTime.Now method. It will return the time of the historical tick when it is used in backtesting.

TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
Robot forex since: 08 Dec 2015; 
 10 Dec 2015, 18:20
RE:

Spotware,

is there a way to integrate the code inside another cBot?

Spotware said:

Dear Trader,

You can use the Server.Time method instead of the DateTime.Now method. It will return the time of the historical tick when it is used in backtesting.

cTrader Team's avatar
cTrader Team since: 23 Sep 2013; 
 11 Dec 2015, 07:26
RE: RE:

Dear stetrimo,

You can integrate any code inside other code.

However, currently we don't provide Users with any methods that will start/stop cBots programmatically. We will provide it in the future.

You can take advantage of the C# language used in cAlgo and create a workaround.

stetrimo said:

Spotware,

is there a way to integrate the code inside another cBot?

Spotware said:

Dear Trader,

You can use the Server.Time method instead of the DateTime.Now method. It will return the time of the historical tick when it is used in backtesting.

TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
davidp13 since: 06 May 2014; 
 19 Jan 2016, 16:43
Close the open position at a certain time

Hi,

What code should one add to close the open positions at lets say 3pm?

Thank you

cTrader Team's avatar
cTrader Team since: 23 Sep 2013; 
 20 Jan 2016, 07:57

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.

TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
firemyst since: 26 Mar 2019; 
 09 Oct 2019, 18:09
RE: Close the open position at a certain time

davidp13 said:

Hi,

What code should one add to close the open positions at lets say 3pm?

Thank you

//Rough sample to give you an idea to close at 3pm server time.
if (ServerTime.Hour == 15)
{
    foreach (Position p in Positions)
        ClosePosition(p);
}

//Rough sample to give you an idea to close 3pm according to your computer's time that's running the bot
if (DateTime.Now.Hour == 15)
{
    foreach (Position p in Positions)
        ClosePosition(p);
}