Hey I have played with thinkscript a bit, most of my experience is in a normal language on indexed dataframes, not rolling data like thinkorswim uses. Anyway, I am looking to create labels on a chart that show how much volume a security had yesterday, at today's time. So, if the market has been open for 2 hours, I would like to create a label with the accrued volume of yesterday up until 2 hours in. I primarily daytrade on the 5min chart so I started this script using the "fold" function, which I am not super familar with, but I think it is very similar to a traditional for loop? Anyway, here is my code for this on a 5min chart. The idea is that there are 80 5min periods in a day of trading so I would take the current barnumber() on my chart, and add it to 80, which should bring me to the start of yesterday, and 80 periods behind the current period should be the same period from the current day. When I apply this to a label, the label will not appear, doesnt even give me a "NaN" or anything. I am guessing I am using the fucntion wrong or something. Any help would be much appreciated.
Thanks
Code:
def bar = barNumber();
def yesterday_start = barnumber() + 80;
def yesterday_stop = 80;
def v = fold index = yesterday_start to 80 with v = 0 do v + volume[index];
first, let me say good job on trying to do it and tackling the use of a fold. Fold loops are tricky and can take a while to get figured out, but it looks like you've got the basic set up pretty close.
1. There are 6.5 hours in a trading day, or 390 minutes.
So there will be 78 , 5 minute bars. At the closing bell that bar is not counted. that is an after hours.
2. Most of the time looking back X number of bars will work. but occasionally on some stocks there is a time period with no trades, so there won't be the usual quantity of bars per the day, and that will throw off studies like this.
3. In your fold loop the start number is bigger than the end number, so it won't even run through one iteration..
In a fold, think of it as running through a series of offset numbers. so maybe start at 0 or 1 to some bigger number.
4. When working in fold loops try to use getvalue to read data instead of the normal offset.
Instead of this,
do v + volume[index];
use this,
do v + getvalue(volume, index);
------------------------------
This version will have issues with missing bars during the day, ( no volume or halts), but should get you some numbers.
add up volume during the day, for each day.
i added code to calculate how many bars in a day, so it will work on any time, less than a day.
the chart doesn't have to be 5 minutes.
it looks back x bars and reads a volume total from yesterday.
Ruby:
# prev_day_vol_0
# [URL]https://usethinkscript.com/threads/getting-yesterdays-cumulative-volume-at-todays-time.10673/[/URL]
# Getting Yesterdays cumulative volume at today's time
# for charts with a period less than a day.
# get the chart agg time and convert to minutes. then calculate how many bars are in the trading day, on the chart.
def chartagg = GetAggregationPeriod();
def chartmin = chartagg / (1000 * 60);
# 6.5 hours in a normal trading day, 390 minutes.
def daybars = 390/chartmin;
addlabel(1, "bar min " + chartmin, color.yellow);
addlabel(1, "bars / day " + daybars, color.yellow);
# 78 for 5min bars
# def daybars = 78;
def isstart = If secondsfromtime(930) == 0 then 1 else 0;
def dayvol = If isstart then volume else if secondsfromtime(930) > 0 and secondstilltime(1600) > 0 then dayvol[1] + volume else dayvol[1];
#def prevdayvol = dayvol[daybars];
def prevdayvol = getvalue(dayvol, daybars);
addlabel(1, "yesterdays volume " + prevdayvol, color.yellow);
addlabel(1, "todays volume " + dayvol, color.yellow);
addlabel(1, "day to day vol factor " + round(dayvol/prevdayvol,2), color.yellow);
input test1_vol_bubbles = no;
addchartbubble(test1_vol_bubbles, volume,
prevdayvol + " prev\n" +
dayvol + " curr"
, (if isstart then color.yellow else color.gray), yes);
#
with test bubbles turned on , on a 30min chart
shows previous day accrued volume over current day accrued volume
getvalue has a 3rd parameter that should be used, max offset, to prevent bad code from causing endless loops.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue