Add custom length = 30 for OBV formula (solved)

lmk99

Member
VIP
Hi All,

I made this OBV label as follows to show the DAY period OBV on a 1min chart as a label:



That OBV calculation is roughly correct, as the OBV for the DAY period. That is what appears right after loading the study.

However, several seconds later the label bizarrely changes to this value:



I can't understand how this is happening. This is the code for the label:

Code:
def Vol = volume(period = "DAY");
def OBV = TotalSum(Sign(close(period = "DAY") - close(period = "DAY")[1]) * Vol);
addlabel (yes, "OBV: " + (OBV), if OBV > 30000000 then Color.LIME else CreateColor(120,120,200));

Any ideas how this can be fixed or why this is happening?

Is it possible it's happening due to after hours period trades being calculated into the daily OBV?

EDIT: This is really dumb but there were a few reasons I had so much trouble with this. It's working now with this code:

Code:
def agg = aggregationperiod.day;
def c = close(period = agg);
def v = volume(period = agg);

def OBV = TotalSum(Sign(c - c[1]) * v);

AddLabel(yes, OBV, color.white);

I was getting discrepancies because when looking at a DAY chart, the length is always at least 30 days. Although the OBV code as I see it above appears to just refer back to the previous day (c[1]), obviously longer length makes a difference because having the 1min chart set to a 2 day length was causing discrepancies. If I have the 1min chart set to a 30 day length the value will be correct.

The weird change of the value from being correct in the first screenshot to wrong a few seconds later, as shown in the second screenshot, must have been happening because using period=day may have initially been calculating back 30 days by default but then the calculation was after a few moments refreshing to reflect the chart length and thus changing.
 
Last edited:

lmk99

Member
VIP
I am not editing the thread title as "solved" yet because I'm left with a problem that I still hope that I can get help with. I use some studies that only work well if I keep my 1min chart length at 1 day. But I need the OBV calculation to use a length of 30 days in order to be accurate. Is there a way to modify this OBV formula so that a length of 30 days is used on the 1min chart with a length of only 1 day?

Code:
def agg = aggregationperiod.day;
def c = close(period = agg);
def v = volume(period = agg);

def OBV = TotalSum(Sign(c - c[1]) * v);

AddLabel(yes, OBV, color.white);

The TotalSum function according to the documentation (https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/TotalSum): "The example returns the total accumulated volume for the time frame of the current chart." So it doesn't accept a length parameter. So I am at a loss for how a length parameter could be added to the OBV formula.

EDIT: I believe this is solved now, I just had to replace TotalSum with Sum which accepts a length of 30:

Code:
def agg = aggregationperiod.day;
def c = close(period = agg);
def v = volume(period = agg);

def OBV = Sum((Sign(c - c[1]) * v),30);
                
addlabel (yes, "OBV: " + (OBV), if OBV > 30000000 then Color.LIME else CreateColor(120,120,200));

I have therefore edited the thread title to say solved.
 
Last edited:

Similar threads

Top