What are Timers for?
-
I saw
Timers
in the documentation and people asking questions about them, but I have not seen an explanation for why they exist. What's their purpose? What's an example usage case (not in a code sense, but in "I want aTimer
so I can do this" sense). -
One use case:
- You can implement a logic in your strategy which continuously checks the
time
of the day to see if only 5 minutes remain until the end of the session. This is the only moment in the session in which your fund executes the daily operations.
If this precise minute is missing (nothing was negotiated), your time comparison will fail. You can enhance the logic to not only check for
==
but also for>=
, but you then need abool
to understand if you have already acted.- The timer will be called only once and 5 minutes before the session end without a constant logic check in
next
and with nobool
involved, because it will only be called once.
The strategy contains the
datetime
information and things can be done manually. In general is a way to express things in a different way which may be more elegant.Some people want to do portfolio rotation on the 15th of each month, but that day may be a weekend or a bank holiday. The timer will be called with the next possible day, allowing the rotation to always happen on the 15th or the 1st trading day thereafter (which matches the reality of life)
- You can implement a logic in your strategy which continuously checks the