Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Identifying Daily High/Low

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

    Identifying Daily High/Low

    I am having trouble with this script that goes like this:

    - At midnight, a new "day" object is created.

    - I want that day object to save the high/low of the day along with which candle it happened.

    From my output, it seems that logic is getting messed up and I don't understand where from.

    This script:

    else if (State == State.Realtime)
    {
    foreach (var day in days.Where(day => day.date != Today))
    {
    Print($"{day.date} -> OHL_O: {day.MOP}, {day.High}, {day.Low}, Six Open: {day.open} -> High: {Time[day.HighBar]}, Low: {Time[day.LowBar]}");
    }
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1) return;

    Today = Time[0].Date;

    if (Time[0].TimeOfDay == SixOpen)
    {
    SixOpeningPrice = Open[0];
    }

    if (Time[0].TimeOfDay == MidnightOpen)
    {
    days.Add(new day
    {
    date = Time[0].Date,
    MOP = Open[0],
    open = SixOpeningPrice,
    High = High[0],
    Low = Low[0]
    });

    Print("New Day Started: " + Time[0].Date);
    }

    foreach (var day in days.Where(day => day.date == Today))
    {
    if (High[0] >= day.High)
    {
    day.High = High[0];
    day.HighBar = CurrentBar;
    //Draw.Line(this, "High", CurrentBar, High[0], CurrentBar - 5, High[0], Brushes.Green);
    }

    if (Low[0] <= day.Low)
    {
    day.Low = Low[0];
    day.LowBar = CurrentBar;
    //Draw.Line(this, "Low", -5, Low[0], CurrentBar, Low[0], Brushes.Red);
    }
    }
    }

    private class day
    {
    public DateTime date { get; set; } = DateTime.MaxValue;
    public double open { get; set; } = double.MinValue;
    public double MOP { get; set; } = double.MinValue;
    public double High { get; set; } = double.MinValue;
    public int HighBar { get; set; } = -1;
    public double Low { get; set; } = double.MaxValue;
    public int LowBar { get; set; } = -1;
    public bool TookPDH { get; set; } = false;
    public int PDHRaid { get; set; } = -1;
    public bool TookPDL { get; set; } = false;
    public int PDLRaid { get; set; } = -1;

    public bool Drawn { get; set; } = false;
    }​

    Returns this:

    6/12/2024 12:00:00 AM -> OHL_O: 5387, 5454.5, 5385, Six Open: 1.79769313486232E+308 -> High: 6/14/2024 3:30:00 AM, Low: 6/14/2024 3:30:00 PM
    6/13/2024 12:00:00 AM -> OHL_O: 5440.75, 5452.75, 5408.5, Six Open: 5436.25 -> High: 6/13/2024 10:15:00 AM, Low: 6/13/2024 6:15:00 AM
    6/14/2024 12:00:00 AM -> OHL_O: 5441.25, 5443, 5397.75, Six Open: 5436 -> High: 6/12/2024 4:30:00 PM, Low: 6/12/2024 11:45:00 AM​

    Disreguard the "Six Open:" property.

    As you can see, it is giving the high/low values from the 12th on the 14th day object, and vice versa.

    #2
    Hello olisav57,

    Thank you for your post.

    Have you tried using the CurrentDayOHLC indicator? This will return the current session OHLC values.




    If you want to stick with defining your session at midnight, I recommend further debugging the script using prints to understand the logic.

    Additionally, please note that the code when State == State.Realtime will only get called once if this is in OnStateChange(). It will only get called one time when the State reaches real time.



    To understand why the script is behaving as it is, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the script add prints (outside of any conditions) that print the values of every variable used in every condition along with the time of that bar.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

    I'm also including a link to a forum post with further suggestions on debugging a script.
    https://support.ninjatrader.com/s/ar...nd-TraceOrders​​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    56 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    132 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X