Skip to content

Sleep Functionality

The ability to stand by until a specific time is essential for cBots, especially for those that should be scheduled or activated upon the occurrence of some event (e.g., public announcement). The Sleep() method in cTrader allows for suspending algorithms until the specified time elapses or approaches.

Note

The sleep functionality is only available for cBots and indicators.

This API guide will introduce you to the Sleep() method and its usage peculiarities.

Sleep() Method in One Minute!

  • The Sleep() method enables developers to suspend the execution of an algorithm with flexible time parameters established.
  • If you know the date and time of a specific event to which your cBot should react with a tailored strategy, use the sleep functionality to trade news and react to scheduled events.
  • Set a period to elapse for the sleep functionality, from milliseconds to any timespan.
  • The Sleep() method is available only for .NET 6 algos and works as intended in backtesting.

How the Sleep() Method Works

The sleep functionality is available only for .NET 6 algos. While a cBot stays in sleep mode, all other events in the code will be ignored.

Example

If the BarOpened event occurred five times during sleep mode, none of the missed OnBar() handlers will be invoked after the cBot is awake. Only new upcoming events will be raised in this case.

Still, cBots process all data messages while staying in sleep mode (e.g., if RefreshData() is called).

Note

Specifying sleeping time, you set a minimum interval of the algorithm suspension. The actual sleeping time will always be equal to or greater than the requested suspension time.

To apply the Sleep() method, algorithm developers should use one of these signatures depending on their purpose.

To suspend the algorithm until the specified timespan elapses:

void Sleep(Timespan timespan)

To suspend the algorithm until the specified number of milliseconds elapses:

void Sleep(int milliseconds)

To suspend the algorithm until the specified time and date approach:

void Sleep(DateTime dateTime)

Example

Let’s assume that the next speech of the current Federal Reserve Chairman Jerome Powell is scheduled for the 11th of December 2023 at 11:00. You expect some announcements that would affect the EURUSD trend and have developed a cBot that would pursue a relevant strategy right on the speech start. In this instance, you can apply the Sleep() method as follows.

DateTime doomsday = new DateTime(2023, 12, 11, 11, 0, 0); 
Sleep(doomsday)

Warning

If the DateTime parameter is not from the algorithm timezone, an ArgumentException will be thrown.

The Sleep() method works as intended in backtesting. If you want to stop a sleeping cBot from the UI, you do not have to wait until it comes awake. Click on the 'Stop' button and it will cease operating without a timeout.

Creating an Example cBot

The following example cBot sleeps for 30 seconds after its launch, with the respective message printed in the log. Afterward, it places a buy market order for USDJPY.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SleepAndExecuteBot : Robot
    {
        protected override void OnStart()
        {
            Print("cBot will sleep for 30 seconds");
            Sleep(30000);

            var symbol = Symbols.GetSymbol("USDJPY");
            ExecuteMarketOrder(TradeType.Buy, symbol, 1000);
        }        
    }
}

Image title

Summary

To conclude, the sleep functionality allows algorithm developers to suspend the execution of other methods even if the cBot was launched. Applying the Sleep() method with its flexible time parameters, you will always react to scheduled events with your tailored strategy on time.