Rolling Pivot Range Indicator for ThinkorSwim

D

Danthaman

New member
Can anyone convert the Pinescript of TradingView to TOS below. Thanks.

Here is the actual 3 day rolling pivot range indicator as detailed in Mark Fisher's book The Logical Trader.

Code:
study(title="Rolling Pivot Indicator", shorttitle="RPR", overlay = true)
width = input(3, minval=1)
xHigh  = security(tickerid,"D", high[1])
xHigh2  = security(tickerid,"D", high[2])
xHigh3  = security(tickerid,"D", high[3])
xLow   = security(tickerid,"D", low[1])
xLow2   = security(tickerid,"D", low[2])
xLow3   = security(tickerid,"D", low[3])
xClose = security(tickerid,"D", close[1])

mh = if xHigh >= xHigh2 and xHigh>=xHigh3
    xHigh
else
    if xHigh2 >= xHigh and xHigh2>=xHigh3
        xHigh2
    else
        xHigh3
 
ml = if xLow <= xLow2 and xLow<=xLow3
    xLow
else
    if xLow2 <= xLow and xLow2<=xLow3
        xLow2
    else
        xLow3


cpp1 = (xClose + mh +ml)/3
cpp2 = (mh + ml)/2
diff = cpp1 -cpp2

PH = (cpp1 + diff)
PL = (cpp1 - diff)

plot(PH, color=yellow, title="PH", style = line, linewidth = width)
plot(PL, color=black, title="PL", style = line, linewidth = width)

positive = low>PL ? green : na
negative = high<PL ? red : na
bgcolor(positive, transp=75, offset=0)
bgcolor(negative, transp=75, offset=0)

//pos = low<PH and low>=PL ? olive : na
//neg = high<PL and high>=PH ? maroon : na
//bgcolor(pos, transp=75, offset=0)
//bgcolor(neg, transp=75, offset=0)
 
Last edited by a moderator:
BenTen

BenTen

Administrative
Staff
VIP
I was looking at this indicator a few days ago, decided to convert it for our Warehouse members. Since you asked, I'll just share it here with everyone.

Code:
# True Rolling Pivot Range Indicator
# Assembled by BenTen at useThinkScript.com
# Converted from https://www.tradingview.com/script/AAzaP2Rn-True-Rolling-Pivot-Range-Indicator/

input aggregationPeriod = AggregationPeriod.DAY;
def d_high = high(period = aggregationPeriod);
def d_low = low(period = aggregationPeriod);
def d_close = close(period = aggregationPeriod);

def xHigh = d_high[1];
def xHigh2 = d_high[2];
def xHigh3 = d_high[3];
def xLow = d_low[1];
def xLow2 = d_low[2];
def xLow3 = d_low[3];
def xClose = d_close[1];

def mh = if xHigh >= xHigh2 and xHigh>=xHigh3 then

    xHigh
else
    if xHigh2 >= xHigh and xHigh2>=xHigh3 then

        xHigh2
    else
        xHigh3;

def ml = if xLow <= xLow2 and xLow<=xLow3 then

    xLow
else
    if xLow2 <= xLow and xLow2<=xLow3 then

        xLow2
    else
        xLow3;


def cpp1 = (xClose + mh + ml) / 3;
def cpp2 = (mh + ml) / 2;
def diff = cpp1 - cpp2;

def PH = (cpp1 + diff);
def PL = (cpp1 - diff);

plot line_h = PH;
plot line_l = PL;

line_h.SetDefaultColor(GetColor(1));
line_l.SetDefaultColor(GetColor(0));
 
Last edited:
D

Danthaman

New member
AWESOME!!! Thanks so much!

I created the script and put aggregation period at 3 days. it is extending the range over 3 days of trading not taking the rolling pivots from the last three days. the plot is well below the price action for AAPL but in trading view, AAPL is trading in the pivot range and well above in TOS.
 
BenTen

BenTen

Administrative
Staff
VIP
@Danthaman There was an error in the code. I just fixed it. Try again. Keep in mind that the TradingView version uses the "Day" setting. And that's also the default for the ToS version. If you decided to change to "3 Days", it will not look the same as the one on TradingView.
 
U

Utajiri

New member
Very new to TOS scripts....Could anyone help with writing the script for pivot points on TOS the way it is calculated on TC2000?

TC2000 usually has it calculated based off the daily and it remains the same throughout the day....

Here is what it is like on TC2000.... I attached a link too. I do appreciate.

R3 = High + 2 * (Pivot - Low)

R2 = Pivot + (High - Low)

R1= (2 * Pivot) - Low

Pivot = (High + Low + Close) /3

S1 = (2 * Pivot) - High

S2 = Pivot - (High - Low)

S3 = Low - 2 * (High - Pivot)


https://help.tc2000.com/m/69445/l/755851-pivot-points
 
horserider

horserider

Well-known member
VIP
Ok here it is but why ??

Code:
input aggregationPeriod = AggregationPeriod.DAY;

def H1 = high(period = aggregationPeriod)[1];
def L1 = low(period = aggregationPeriod)[1];
def C1 = close(period = aggregationPeriod)[1];
plot R4;
plot R3;
plot R2;
plot R1;
plot HH;
plot PP;
plot LL;
plot S1;
plot S2;
plot S3;
plot S4;
HH = H1;
LL = L1;
PP = (H1 + L1 + C1) / 3;
R1 =(2 * H1 - L1 + 2 * C1) / 3;
R2 = (4 * H1 - 2 * L1 + C1) / 3;
R3 = (5 * H1 - 4 * L1 + 2 * C1) / 3;
R4 = (7 * H1 - 5 * L1 + C1) / 3;
S1 = (2 * L1 - H1 + 2 * C1) / 3;
S2 = (4 * L1 - 2 * H1 + C1) / 3;
S3 = (5 * L1 - 4 * H1 + 2 * C1) / 3;
S4 = (7 * L1 - 5 * H1 + C1) / 3;

R3.SetDefaultColor(GetColor(5));
R2.SetDefaultColor(GetColor(5));
R1.SetDefaultColor(GetColor(5));
HH.SetDefaultColor(GetColor(4));
PP.SetDefaultColor(GetColor(0));
LL.SetDefaultColor(GetColor(1));
S1.SetDefaultColor(GetColor(6));
S2.SetDefaultColor(GetColor(6));
S3.SetDefaultColor(GetColor(6));


R3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HH.SetStyle(Curve.MEDIUM_DASH);
LL.SetStyle(Curve.MEDIUM_DASH);

HH.Hide();
LL.Hide();
 
T

tomsk

Well-known member
VIP
@Utajiri I looked through the formulas you posted, those are essentially Generic pivots that you can plug in to almost any standard pivot study. TOS has about 5-6 standard pivot studies that you can easily adapt to suit your needs. Since you're new to scripting this is a great starter project to get you familiar with coding in ThinkScript. All the very best!
 
U

Utajiri

New member
Thanks a lot @horserider - I have actually used the pivots on TC2000 platform for years in conjunction with a different indicator I had built..... so I naturally wanted to see the same thing on TOS. I didn't like the calculation basis for the other pivots I saw in TOS, but found out after you coded this that the was basically checking out the SVE pivots on TOS pretty much is the same. Thanks for helping out. :)
Is there a way though for this script to calculate and show only today's lines without the multi-day showing it does?
 
U

Utajiri

New member
@Utajiri I looked through the formulas you posted, those are essentially Generic pivots that you can plug in to almost any standard pivot study. TOS has about 5-6 standard pivot studies that you can easily adapt to suit your needs. Since you're new to scripting this is a great starter project to get you familiar with coding in ThinkScript. All the very best!

Thank you sir.
Learning so much from here already. :)
 
T

tomsk

Well-known member
VIP
@Utajiri Since you're in learning mode, here is a copy of a script called "Generic Pivots" that I saved when I first started out leaning ThinkScript years ago. Run this on a 15 minute chart of /ES for example

Code:
input Aggregate = {W, default D, M};
input ShowValuesAtTop = no;
input ShowBubbles = yes;
input ShowOnDaily = yes;
input BubbleOffset = -3;

def today = if getDay() == getLastDay() then 1 else double.NaN;
def Agg = if Aggregate == Aggregate.D then AggregationPeriod.DAY else
    if Aggregate == Aggregate.W then AggregationPeriod.WEEK else
    if Aggregate == Aggregate.M then AggregationPeriod.MONTH else AggregationPeriod.MIN;

def highestBarNumber = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);

def isDaily = if !ShowOnDaily and GetAggregationPeriod() == AggregationPeriod.DAY then yes else no;

def thisOpen = open(period = Agg)[1];
def thisHigh = high(period = Agg)[1];
def thisLow = low(period = Agg)[1];
def thisClose = close(period = Agg)[1];

def thisPP = if isDaily or isNaN(Today) then Double.NaN else (thisHigh + thisLow + thisClose) / 3;

def thisR1 = if isDaily then Double.NaN else (2 * thisPP) - thisLow;
def thisR2 = if isDaily then Double.NaN else thisPP + (thisHigh - thisLow);
def thisR3 = if isDaily then Double.NaN else thisHigh + 2 * (thisPP - thisLow);
def thisR1Mid = if isDaily then Double.NaN else (thisR1 + thisPP) / 2;
def thisR2Mid = if isDaily then Double.NaN else (thisR1 + thisR2) / 2;

def thisS1 = if isDaily then Double.NaN else (2 * thisPP) - thisHigh;
def thisS2 = if isDaily then Double.NaN else thisPP - (thisHigh - thisLow);
def thisS3 = if isDaily then Double.NaN else thisLow - 2 * (thisHigh - thisPP);
def thisS1Mid = if isDaily then Double.NaN else (thisS1 + thisPP) / 2;
def thisS2Mid = if isDaily then Double.NaN else (thisS1 + thisS2) / 2;

plot R3 = thisR3;
plot R2 = thisR2;
plot R1 = thisR1;
plot PP = thisPP;
plot S1 = thisS1;
plot S2 = thisS2;
plot S3 = thisS3;

AddLabel(!isDaily and ShowValuesAtTop, "R3: " + R3, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R2: " + R2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R1: " + R1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "PP: " + PP, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S1: " + S1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S2: " + S2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S3: " + S3, Color.WHITE);


R3.DefineColor("R3", Color.WHITE);
R3.SetStyle(Curve.SHORT_DASH);
R3.SetLineWeight(1);
R3.HideBubble();
R2.DefineColor("R2", Color.WHITE);
R2.SetStyle(Curve.SHORT_DASH);
R2.SetLineWeight(1);
R2.HideBubble();
R1.DefineColor("R1", Color.WHITE);
R1.SetStyle(Curve.SHORT_DASH);
R1.SetLineWeight(1);
R1.HideBubble();
PP.DefineColor("PP", Color.WHITE);
PP.SetStyle(Curve.SHORT_DASH);
PP.SetLineWeight(1);
PP.HideBubble();
S1.DefineColor("S1", Color.WHITE);
S1.SetStyle(Curve.SHORT_DASH);
S1.SetLineWeight(1);
S1.HideBubble();
S2.DefineColor("S2", Color.WHITE);
S2.SetStyle(Curve.SHORT_DASH);
S2.SetLineWeight(1);
S2.HideBubble();
S3.DefineColor("S3", Color.WHITE);
S3.SetStyle(Curve.SHORT_DASH);
S3.SetLineWeight(1);
S3.HideBubble();


def BubbleBar = if ShowBubbles and (BarNumber() == highestBarNumber - BubbleOffset) then yes else no;

AddChartBubble(BubbleBar, R3,  " R3", R3.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R2, " R2", R2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R1,  " R1", R1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, PP, " PP", PP.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S1,  " S1", S1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S2,  " S2", S2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S3, " S3", S3.TakeValueColor(), yes);


# - - END SCRIPT
 
U

Utajiri

New member
@Utajiri Since you're in learning mode, here is a copy of a script called "Generic Pivots" that I saved when I first started out leaning ThinkScript years ago. Run this on a 15 minute chart of /ES for example

Code:
input Aggregate = {W, default D, M};
input ShowValuesAtTop = no;
input ShowBubbles = yes;
input ShowOnDaily = yes;
input BubbleOffset = -3;

def today = if getDay() == getLastDay() then 1 else double.NaN;
def Agg = if Aggregate == Aggregate.D then AggregationPeriod.DAY else
    if Aggregate == Aggregate.W then AggregationPeriod.WEEK else
    if Aggregate == Aggregate.M then AggregationPeriod.MONTH else AggregationPeriod.MIN;

def highestBarNumber = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);

def isDaily = if !ShowOnDaily and GetAggregationPeriod() == AggregationPeriod.DAY then yes else no;

def thisOpen = open(period = Agg)[1];
def thisHigh = high(period = Agg)[1];
def thisLow = low(period = Agg)[1];
def thisClose = close(period = Agg)[1];

def thisPP = if isDaily or isNaN(Today) then Double.NaN else (thisHigh + thisLow + thisClose) / 3;

def thisR1 = if isDaily then Double.NaN else (2 * thisPP) - thisLow;
def thisR2 = if isDaily then Double.NaN else thisPP + (thisHigh - thisLow);
def thisR3 = if isDaily then Double.NaN else thisHigh + 2 * (thisPP - thisLow);
def thisR1Mid = if isDaily then Double.NaN else (thisR1 + thisPP) / 2;
def thisR2Mid = if isDaily then Double.NaN else (thisR1 + thisR2) / 2;

def thisS1 = if isDaily then Double.NaN else (2 * thisPP) - thisHigh;
def thisS2 = if isDaily then Double.NaN else thisPP - (thisHigh - thisLow);
def thisS3 = if isDaily then Double.NaN else thisLow - 2 * (thisHigh - thisPP);
def thisS1Mid = if isDaily then Double.NaN else (thisS1 + thisPP) / 2;
def thisS2Mid = if isDaily then Double.NaN else (thisS1 + thisS2) / 2;

plot R3 = thisR3;
plot R2 = thisR2;
plot R1 = thisR1;
plot PP = thisPP;
plot S1 = thisS1;
plot S2 = thisS2;
plot S3 = thisS3;

AddLabel(!isDaily and ShowValuesAtTop, "R3: " + R3, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R2: " + R2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R1: " + R1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "PP: " + PP, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S1: " + S1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S2: " + S2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S3: " + S3, Color.WHITE);


R3.DefineColor("R3", Color.WHITE);
R3.SetStyle(Curve.SHORT_DASH);
R3.SetLineWeight(1);
R3.HideBubble();
R2.DefineColor("R2", Color.WHITE);
R2.SetStyle(Curve.SHORT_DASH);
R2.SetLineWeight(1);
R2.HideBubble();
R1.DefineColor("R1", Color.WHITE);
R1.SetStyle(Curve.SHORT_DASH);
R1.SetLineWeight(1);
R1.HideBubble();
PP.DefineColor("PP", Color.WHITE);
PP.SetStyle(Curve.SHORT_DASH);
PP.SetLineWeight(1);
PP.HideBubble();
S1.DefineColor("S1", Color.WHITE);
S1.SetStyle(Curve.SHORT_DASH);
S1.SetLineWeight(1);
S1.HideBubble();
S2.DefineColor("S2", Color.WHITE);
S2.SetStyle(Curve.SHORT_DASH);
S2.SetLineWeight(1);
S2.HideBubble();
S3.DefineColor("S3", Color.WHITE);
S3.SetStyle(Curve.SHORT_DASH);
S3.SetLineWeight(1);
S3.HideBubble();


def BubbleBar = if ShowBubbles and (BarNumber() == highestBarNumber - BubbleOffset) then yes else no;

AddChartBubble(BubbleBar, R3,  " R3", R3.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R2, " R2", R2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R1,  " R1", R1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, PP, " PP", PP.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S1,  " S1", S1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S2,  " S2", S2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S3, " S3", S3.TakeValueColor(), yes);


# - - END SCRIPT

Thank you so much @tomsk
This is quite helpful. Very clean! You rock!!!!
 
U

Utajiri

New member
Hi @tomsk , is possible to adjust the position of those (R&S) bubbles/labels on these pivots.... Tried doing so inside settings, but they seem to be permanent fixtures inside the code. Or can we at least move it to the left instead... they are a bit distracting.. I appreciate.
 
T

tomsk

Well-known member
VIP
Note that bubbles are specified using an X, Y axis where X is typically the barNumber() or horizontal location you are looking at and Y is the vertical or price location. Most traders want this to be located close to the price so they can keep track of price action.
 
U

Utajiri

New member
I see that. So if we wanted to take them away... so I just have the lines without the identifications.... is that possible?
 
T

tomsk

Well-known member
VIP
You can do as you please, to remove just delete the AddChartBubble statements
 
U

Utajiri

New member
Hi Tom, I wanted to make reference to your pivot script....
Works fine but I notice the price of the pivot lines are not highlighted on my charts... Is there anything I need to add to the script to make that happen?





@Utajiri Since you're in learning mode, here is a copy of a script called "Generic Pivots" that I saved when I first started out leaning ThinkScript years ago. Run this on a 15 minute chart of /ES for example

Code:
input Aggregate = {W, default D, M};
input ShowValuesAtTop = no;
input ShowBubbles = yes;
input ShowOnDaily = yes;
input BubbleOffset = -3;

def today = if getDay() == getLastDay() then 1 else double.NaN;
def Agg = if Aggregate == Aggregate.D then AggregationPeriod.DAY else
    if Aggregate == Aggregate.W then AggregationPeriod.WEEK else
    if Aggregate == Aggregate.M then AggregationPeriod.MONTH else AggregationPeriod.MIN;

def highestBarNumber = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);

def isDaily = if !ShowOnDaily and GetAggregationPeriod() == AggregationPeriod.DAY then yes else no;

def thisOpen = open(period = Agg)[1];
def thisHigh = high(period = Agg)[1];
def thisLow = low(period = Agg)[1];
def thisClose = close(period = Agg)[1];

def thisPP = if isDaily or isNaN(Today) then Double.NaN else (thisHigh + thisLow + thisClose) / 3;

def thisR1 = if isDaily then Double.NaN else (2 * thisPP) - thisLow;
def thisR2 = if isDaily then Double.NaN else thisPP + (thisHigh - thisLow);
def thisR3 = if isDaily then Double.NaN else thisHigh + 2 * (thisPP - thisLow);
def thisR1Mid = if isDaily then Double.NaN else (thisR1 + thisPP) / 2;
def thisR2Mid = if isDaily then Double.NaN else (thisR1 + thisR2) / 2;

def thisS1 = if isDaily then Double.NaN else (2 * thisPP) - thisHigh;
def thisS2 = if isDaily then Double.NaN else thisPP - (thisHigh - thisLow);
def thisS3 = if isDaily then Double.NaN else thisLow - 2 * (thisHigh - thisPP);
def thisS1Mid = if isDaily then Double.NaN else (thisS1 + thisPP) / 2;
def thisS2Mid = if isDaily then Double.NaN else (thisS1 + thisS2) / 2;

plot R3 = thisR3;
plot R2 = thisR2;
plot R1 = thisR1;
plot PP = thisPP;
plot S1 = thisS1;
plot S2 = thisS2;
plot S3 = thisS3;

AddLabel(!isDaily and ShowValuesAtTop, "R3: " + R3, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R2: " + R2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "R1: " + R1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "PP: " + PP, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S1: " + S1, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S2: " + S2, Color.WHITE);
AddLabel(!isDaily and ShowValuesAtTop, "S3: " + S3, Color.WHITE);


R3.DefineColor("R3", Color.WHITE);
R3.SetStyle(Curve.SHORT_DASH);
R3.SetLineWeight(1);
R3.HideBubble();
R2.DefineColor("R2", Color.WHITE);
R2.SetStyle(Curve.SHORT_DASH);
R2.SetLineWeight(1);
R2.HideBubble();
R1.DefineColor("R1", Color.WHITE);
R1.SetStyle(Curve.SHORT_DASH);
R1.SetLineWeight(1);
R1.HideBubble();
PP.DefineColor("PP", Color.WHITE);
PP.SetStyle(Curve.SHORT_DASH);
PP.SetLineWeight(1);
PP.HideBubble();
S1.DefineColor("S1", Color.WHITE);
S1.SetStyle(Curve.SHORT_DASH);
S1.SetLineWeight(1);
S1.HideBubble();
S2.DefineColor("S2", Color.WHITE);
S2.SetStyle(Curve.SHORT_DASH);
S2.SetLineWeight(1);
S2.HideBubble();
S3.DefineColor("S3", Color.WHITE);
S3.SetStyle(Curve.SHORT_DASH);
S3.SetLineWeight(1);
S3.HideBubble();


def BubbleBar = if ShowBubbles and (BarNumber() == highestBarNumber - BubbleOffset) then yes else no;

AddChartBubble(BubbleBar, R3,  " R3", R3.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R2, " R2", R2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, R1,  " R1", R1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, PP, " PP", PP.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S1,  " S1", S1.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S2,  " S2", S2.TakeValueColor(), yes);
AddChartBubble(BubbleBar, S3, " S3", S3.TakeValueColor(), yes);


# - - END SCRIPT

reference to your pivots scripts -
 
R

Raj

New member
Hi Ben,
Looks like you have already programmed the 3 day rolling pivot, which I am looking for TOS.
How can I get it, I am ready to pay for it.
Thanks
Raj
 
Last edited by a moderator:
YungTraderFromMontana

YungTraderFromMontana

Well-known member
Hi Ben,
Looks like you have already programmed the 3 day rolling pivot, which I am looking for TOS.
How can I get it, I am ready to pay for it.
Thanks
Raj
It's free it is the code posted.
 

Similar threads

Top