Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator that gets the cumulative volume of the previous day until the current time

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

    Indicator that gets the cumulative volume of the previous day until the current time

    Hi,

    I'm trying to calculate the cumulative volume of the previous day until the current time.
    Let's say its Monday at 9:05am, and I'd like to have the cumulative volume of the Friday session until 9:05am (so from Thursday 6pm until Friday 09:05am). Below is the code I'm using that is not working (giving me a fixed value for the volume). I struggle to navigate through the bars even though I read the documentation.

    I appreciate your help.


    DateTime currentDate = Bars.GetTime(CurrentBar);
    TimeSpan currentTime = Bars.GetTime(CurrentBar).TimeOfDay;

    DateTime startDate = currentDate.AddDays(-1);
    //Print("Current Date: " + Bars.GetTime(CurrentBar) + " || Current Time: " + currentTime + " || Prev. Day: " + startDate);

    int barsAgo = CurrentBar - Bars.GetBar(currentDate);

    int countSession = 0;
    double totalVolumePrevSession = 0;
    // loop through all of the viewable range of the chart
    for (int barIndex = barsAgo; barIndex <= ChartBars.ToIndex; barIndex++)
    {
    // print the High value for each index within the viewable range
    //Print(barIndex);
    if (Bars.IsFirstBarOfSession)
    Print(Bars.GetTime(barIndex) + " _ " + Bars.GetOpen(barIndex));
    countSession += 1;

    totalVolumePrevSession += Bars.GetVolume(barIndex);

    if (countSession > 1)
    break;
    }
    Print("Total cumulative volume in previous session: " + totalVolumePrevSession);

    #2
    Hello datlayzard,

    To get the previous session start time I would recommend using a SessionIterator.

    Code:
    sessionIterator.GetNextSession(Time[0], true);
    sessionIterator.GetNextSession(Bars.GetTime(Bars.GetBar(sessionIterator.ActualSessionBegin) - 1), true);
    Print(string.Format("Current bar: {0} | Get next session -1 bar ActualSessionBegin: {1}, ActualSessionBegin bar number: {2}", Time[0], sessionIterator.ActualSessionBegin, Bars.GetBar(sessionIterator.ActualSessionBegin)));
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Chelsea; it helped a lot. I can now get the previous day's cumulative volume up to a certain point, but it's a flat line (cf. attached email). I think it's because of the way I compare the time of the previous day's bars to the current ToTime[0], but I don't know what's the best way to fix it. Any idea on how to proceed? I attached a screenshot of what is being plotted on the chart. Many thanks.


      protected override void OnBarUpdate()
      {
      if (Bars.IsFirstBarOfSession)
      sessionVolume = 0;


      sessionVolume += Bars.GetVolume(CurrentBar);
      //Print("Session Volume: " + sessionVolume);

      sessionIterator.GetNextSession(Time[0], true);
      sessionIterator.GetNextSession(Bars.GetTime(Bars.G etBar(sessionIterator.ActualSessionBegin) - 1), true);
      Print(string.Format("Current bar: {0} | Get next session -1 bar ActualSessionBegin: {1}, ActualSessionBegin bar number: {2}", Time[0], sessionIterator.ActualSessionBegin, Bars.GetBar(sessionIterator.ActualSessionBegin)));

      int lastSessionStartIndex = Bars.GetBar(sessionIterator.ActualSessionBegin);
      for (int i = lastSessionStartIndex; i < CurrentBar; i++)
      {

      priorSessionVolume += Bars.GetVolume(i);
      Print(Bars.GetTime(i) + " - " + ToTime(Bars.GetTime(i)) + " - " + Bars.GetClose(i) + " - Current Time: " + ToTime(Time[0]) + " - length: " + ToTime(Time[0]).ToString().Length);
      Print("Cumulative Volume Prior Day: " + priorSessionVolume + " - " + sessionVolume);
      Print(ToTime(Bars.GetTime(i)) + " - " + ToTime(Time[0]));
      if (ToTime(Bars.GetTime(i)) >= ToTime(Time[0]) && (ToTime(Bars.GetTime(i)).ToString().Length == ToTime(Time[0]).ToString().Length))
      Print("BREAK: " + ToTime(Bars.GetTime(i)).ToString().Length + " - " + ToTime(Time[0]).ToString().Length);
      break;
      }
      // Plot the cumulative volume
      MyPlot2[0] = sessionVolume;
      MyPlot3[0] = priorSessionVolume;
      priorSessionVolume = 0;

      }​
      Attached Files

      Comment


        #4
        Hello datlayzard,

        I'm seeing this value is increasing on every new bar.

        Attached is an exported test script.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        646 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        367 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X