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);
Comment