Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Assigning historical bar date and time to a variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Assigning historical bar date and time to a variable

    Hello all,

    In building a utility, I am having difficulty assigning the DateTime value of the bar where conditions were met to a variable. Perhaps someone with more experience could provide the correct syntax to use in Sets to assign the value. I believe the variable is declared correctly and the Print statement is correct since it compiles.

    The purpose is simply to identify, record and print the time of day when the high or low price occurred. (Historical data only). Assigning a time window then a bool ensures only one High & one Low price (with their corresponding date and time values) are printed for each day.

    In the file: SampleDateTimeFunctions_NT8.zip located here: https://ninjatrader.com/support/help...lating_datetim e_objects.htm
    the programmer demonstrates various DateTime functions and output options. Unfortunately, I don't see a reference to capturing specific bar times to a variable.


    In this utility the DateTime variable is declared as "TimeStamp":

    public class HighLowTimeCapture : Strategy
    {
    private bool PeriodEnd;
    private double PriceHigh;
    private double PriceLow;
    private DateTime TimeStamp;


    The Print Statement looks like this:

    Print(@"High:" + Convert.ToString(PriceHigh) + " " + Convert.ToString(TimeStamp) + @" Low:" + Convert.ToString(PriceLow) + " " + Convert.ToString(TimeStamp));


    The above will compile, however once the DateTime value of bar [0] is assigned to the variable, it will not compile.

    Example: In the Set where High price conditions are met, the time of the current bar in the historical data must be saved to the user variable "TimeStamp" declared above.

    // Set 3
    if ((High[0] > PriceHigh)
    && (Times[0][0].TimeOfDay >= TimeStart.TimeOfDay)
    && (Times[0][0].TimeOfDay <= TimeStop.TimeOfDay))
    {
    PriceHigh = High[0];
    TimeStamp = DateTime[0];
    PeriodEnd = true;
    }


    Attempting to compile this syntax posts the error: 'System.DateTime' is a 'type' but is used like a 'variable'. It doesn't seem logical for "system" time to be relevant to historical bar data.

    How can the historical date and time of the bar when conditions were met be saved to the variable TimeStamp?

    Thanks for all replies.
    culpepper​

    #2
    Hello culpepper,

    Thank you for your post.

    I suspect that you will want to set TimeStamp to equal Times[0][0] which will be the timestamp for the current bar being processed when the condition from Set 3 is met. For more information on Time and Times from the TimeSeries<DateTime> which is a series that holds bar timestamp values, please see the following links:// Set 3
    if ((High[0] > PriceHigh)
    && (Times[0][0].TimeOfDay >= TimeStart.TimeOfDay)
    && (Times[0][0].TimeOfDay <= TimeStop.TimeOfDay))
    {
    PriceHigh = High[0];
    // set TimeStamp to equal Time[0] so that it holds the timestamp of the current bar whose High is greater than the previous value for PriceHigh
    TimeStamp = Time[0];
    PeriodEnd = true;
    }

    Please let us know if we may be of further assistance.​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    557 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X