Can this VWAP ATR script be converted to TOS?


Active member
Can this script be converted to TOS?
TOS doesn't let you re-use variables, or assign more than once, and my attempts to convert this always hit one error or another.

The other issue seems to also be using "stop" on line 23, when it is set again on line 25. I think Pine Script has the := to set immediately, but I think TOS works differently...

Anyone care to tackle this one, or have ideas how to work around this code construct in TOS?


Well-known member
@korygill The only way I know how to get around "re-using" variables is via a switch statement or an if-then-else construct such as the following. Otherwise it's not possible to re-use variables as you've noted

def variable1;
def variable2;

if close > open
then {
    variable1 = 1;
    variable2 = 2;
} else if close < open
then {
    variable1 = 2;
    variable2 = 1;
} else {
    variable1 = variable1[1];
    variable2 = variable2[1];
Last edited:


Active member
Thanks @tomsk. The issue I have is

hi:= hi[1]? high[1]>=stop[1] ? false : true : low[1]<=stop[1] ? true : false
stop:=hi ? max : min
stop:= hi ? hi[1]==false ? stop : stop>stop[1] ? stop[1] : stop : hi[1] ? stop : stop<stop[1] ? stop[1] : stop

Line 3, stop used before declared or assigned (by TOS)
Line 4 assignment
Line 5, TOS does not like stop = stop

I've even tried

if BarNumber() == 1
init vars
other logic

But no matter what, TOS throws a fit on one line or another. I've even tried creating intermediate vars, etc.

I've studied that indicator, and not sure how else to express it.

TOS is great for the most part, but TradingView and Sierra are light years ahead of thinkscript.


@RobertPayne, do you have any guidance here for this type of code and thinkscript?

The CompoundValue() function allows you to set an initial value then change that value based on whatever criteria you set. I am not familiar with Pine script, but I believe this snippet can be translated in the following manner.


def hi = CompoundValue(true, if hi[1] then (if high[1] >= stop[1] then false else true) else (if low[1] <= stop[1] then true else false), 1);

I haven't loaded this into TOS and am just throwing out an idea. You'll have to play around and test it yourself.

Similar threads