Trading Time Interval

01 Mar 2013, 11:47Trading Time Interval#1
Spotware Support posts: 400since: 01 Jan 2012

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™
09 Oct 2015, 13:35RE:#2
ironmineposts: 63since: 29 Sep 2015

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?

13 Oct 2015, 00:49#3
cTrader Teamposts: 3396since: 23 Sep 2013

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/
10 Dec 2015, 18:20RE:#4
Robot forexposts: 6since: 08 Dec 2015

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.

 

11 Dec 2015, 07:26RE: RE:#5
cTrader Teamposts: 3396since: 23 Sep 2013

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/
19 Jan 2016, 16:43Close the open position at a certain time#6
davidp13posts: 72since: 06 May 2014

Hi,

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

Thank you

20 Jan 2016, 07:57#7
cTrader Teamposts: 3396since: 23 Sep 2013

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/
09 Oct 2019, 18:09RE: Close the open position at a certain time#8
FireMystposts: 181since: 26 Mar 2019

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);
}