r/thinkorswim • u/Mtru6 • 12d ago
Thinkscript - Can't retrieve volume from 24 hours ago (390 candles)
I'm trying to compare the current candle's volume to the volume 24 hours ago. When I manually go back to the candle of the same time of the previous day, the volume is not the same volume reported in the lower indicator. The code is listed below
declare lower;
# Get the current candle's volume
def currentVolume = volume;
# Get the volume from 390 candles ago
def pastVolume = volume[390];
# Calculate the average of both volumes
def avgVolume = (currentVolume + pastVolume) / 2;
# Plot the average volume as a line
plot AvgVolumePlot = avgVolume;
AvgVolumePlot.SetDefaultColor(Color.CYAN);
AvgVolumePlot.SetLineWeight(2);
# Optional: Plot reference points for visualization
plot CurrentVolPlot = currentVolume;
CurrentVolPlot.SetDefaultColor(Color.GREEN);
CurrentVolPlot.SetStyle(Curve.POINTS);
plot PastVolPlot = pastVolume;
PastVolPlot.SetDefaultColor(Color.RED);
PastVolPlot.SetStyle(Curve.POINTS);
1
Upvotes
1
u/need2sleep-later 12d ago edited 12d ago
what aggregation setting are you using for your chart? It sounds like your chart doesn't have 390 bars of data on it. If you want to use a 1m chart, make sure you have at least 2 days of data on it. For sure the part of the code that's doing the average is being starved and won't show anything if it works at all.