Skip to content

Timer

Summary

Schedules execution of virtual OnTimer method with specified interval.

Signature

1
public abstract interface Timer

Namespace

cAlgo.API

Methods

Name Description
Start Starts the Timer
Stop Stops the Timer

Properties

Name Description
Interval { get; } Gets the interval of timer. Returns -1 millisecond if the timer is stopped

Events

Name Description
TimerTick Occurs when the interval elapses

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 using cAlgo.API;
 using System;
 namespace cAlgo.Robots
 {
     // This sample cBot shows how to use the API Timer, this timer works both on live and back test
     // The timer is available for both cBots and indicators
     // Every cBot/indicator can have one single timer
     // You can also use the .NET timers if you want to but those will not work properly on back test
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class TimerSample : Robot
     {
         protected override void OnStart()
         {
             // You can start the cBot timer by calling Timer.Start method, for interval you can pass a time span or seconds
             Timer.Start(TimeSpan.FromSeconds(1));
             // You can also use the TimerTick event instead of OnTimer method
             Timer.TimerTick += Timer_TimerTick;
             // To stop the timer you can call Timer.Stop method anywhere on your cBot/indicator
             Timer.Stop();
         }
         private void Timer_TimerTick()
         {
             // Put your logic for timer elapsed event here
         }
         protected override void OnTimer()
         {
             // Put your logic for timer elapsed event here
         }
     }
 }

Last update: March 23, 2023