ToTime()

View as Markdown

Definition

Calculates an integer value representing a time.

Integer representation of time is in the format Hmmss where 7:30 AM would be 73000 and 2:15:12 PM would be 141512.

Method Return Value

An int value representing a time structure.

Syntax

ToTime(DateTime time)

ToTime(int hour, int minute, int second)

Parameters

ParameterDescription
timeA DateTime structure to calculate. Note: See also the Time property.
hourAn int value representing the hour used for the input.
minuteAn int value representing the minute used for the input.
secondAn int value representing the second used for the input.

NinjaScript uses the .NET DateTime structure which can be complicated for novice programmers. If you are familiar with C# you can directly use DateTime structure properties and methods for date and time comparisons; otherwise, use this method and the ToDay() method.

Examples

1// Only trade between 7:45 AM and 1:45 PM
2if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500)
3{
4 // Strategy logic goes here
5}
1// Store start time as an int variable to be compared
2int startTime = ToTime(9, 30, 00); // 93000
3
4// Only trade after 9:30 AM
5if (ToTime(Time[0]) >= startTime)
6{
7 // Strategy logic goes here
8}