Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Rogers101, Today, 11:30 AM
        0 responses
        8 views
        0 likes
        Last Post Rogers101  
        Started by MGHORBEL, Today, 11:16 AM
        0 responses
        1 view
        0 likes
        Last Post MGHORBEL  
        Started by michi08, Today, 08:51 AM
        2 responses
        13 views
        0 likes
        Last Post michi08
        by michi08
         
        Started by flybuzz, Today, 10:33 AM
        0 responses
        5 views
        0 likes
        Last Post flybuzz
        by flybuzz
         
        Started by algospoke, 02-19-2024, 03:25 PM
        6 responses
        42 views
        0 likes
        Last Post Abiodun
        by Abiodun
         
        Working...
        X