Day Trading with Pivots For ThinkOrSwim

Hi All,

Randomly came across this post and really liking this indicator! However, with the original code if you turn on previous days (change Show Today only from yes to no) all the lines of previous days would connect together

1736793643731.png


which I found to be cluttering to look at so I made a quick modification so each day has their own separate lines:

1736793688981.png


Just thought it looked a little cleaner and easier to look at. I didn't change any logic other than giving each day separate lines. If you happen to come across this post feel free to use this if you want!


Code:
# Complete Pivot Setup
# Created by @tony_futures
# Modified for horizontal lines
# Inspired from @PostyTrades and @SergeTrades

#declare hide_on_daily;
input aggregationPeriod = {default DAY, WEEK, MONTH, YEAR};
input showCPR = no;
input showPrevious = no;
input showReversalZone = yes;
input showLastStand = yes;
input showLabels = yes;
input showBullTargets = yes;
input showBearTargets = yes;
input showTodayOnly = yes;
input manualClose = 0.00;
input RoundLevel = 0;

def Today = if aggregationPeriod == AggregationPeriod.DAY and GetLastDay() == GetDay() then 1 else if aggregationPeriod == AggregationPeriod.MONTH and GetLastMonth() == GetMonth() then 1  else if aggregationPeriod == AggregationPeriod.WEEK and GetLastWeek() == GetWeek() then 1 else if aggregationPeriod == AggregationPeriod.YEAR and GetLastYEAR() == GetYear() then 1 else 0;

# Time-based conditions
def newDay = GetDay() <> GetDay()[1];
def endOfDay = SecondsTillTime(1600) == 0;
def isLastBar = !IsNaN(close) and IsNaN(close[1]);

def prevHigh = high(period = aggregationPeriod)[1];
def prevLow = low(period = aggregationPeriod)[1];
def prevClose1 = close(period = aggregationPeriod)[1];
def prevClose = if manualClose != 0 then manualClose else prevClose1;

def pivot = (prevHigh + prevLow + prevClose) / 3.0;
def bc = (prevHigh + prevLow) / 2.0;
def tc = (pivot - bc) + pivot;
def prevDiff = prevHigh - prevLow;
def prevDiff2 = prevDiff*1.1/2;
def prevDiff4 = prevDiff*1.1/4;
def prevDiff8 = prevDiff*1.1/8;

def smallR1 = prevClose + (prevDiff*1.1/ 12);
def smallS1 = prevClose - (prevDiff*1.1/ 12);

def R1 = prevDiff4 + prevClose;
def R3 = RoundDown(prevDiff2 + prevClose, RoundLevel);
def R2 = R3 - prevDiff8;
def R4 = RoundDown(prevDiff + prevClose,RoundLevel);
def R5 = RoundDown((R3 + (1.168 * (R3 - R1))),RoundLevel);

def S1 = PrevClose - prevDiff4;
def S3 = RoundUp(PrevClose - prevDiff2, RoundLevel);
def S2 = S3 + prevDiff8;
def S4 = RoundUp(prevClose - prevDiff, RoundLevel);
def S5 = RoundUp((S3 - (1.168 * ( S1 - S3))), RoundLevel);

# Modified plots for horizontal lines
plot PLine = if (showCPR and showTodayOnly and Today) or (showCPR and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else pivot
else Double.NaN;

plot BCLine = if (showCPR and showTodayOnly and Today) or (showCPR and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else bc
else Double.NaN;

plot TCLine = if (showCPR and showTodayOnly and Today) or (showCPR and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else tc
else Double.NaN;

PLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PLine.SetDefaultColor(Color.White);
BCLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BCLine.SetDefaultColor(Color.CYAN);
TCLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TCLine.SetDefaultColor(Color.MAGENTA);

plot PreviousHigh = if (showPrevious and showTodayOnly and Today) or (showPrevious and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else RoundDown(prevHigh,RoundLevel)
else Double.NaN;
PreviousHigh.SetDefaultColor(Color.GRAY);

plot PreviousLow = if (showPrevious and showTodayOnly and Today) or (showPrevious and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else RoundUp(prevLow,RoundLevel)
else Double.NaN;
PreviousLow.setDefaultColor(Color.GRAY);

# Reversal zones with proper breaks
AddCloud(
    if (showReversalZone and showTodayOnly and Today) or (showReversalZone and !showTodayOnly) then
        if newDay then Double.NaN
        else if endOfDay then Double.NaN
        else r1
    else Double.NaN,
    if (showReversalZone and showTodayOnly and Today) or (showReversalZone and !showTodayOnly) then
        if newDay then Double.NaN
        else if endOfDay then Double.NaN
        else r2
    else Double.NaN,
    Color.DARK_ORANGE,
    Color.DARK_ORANGE
);

AddCloud(
    if (showReversalZone and showTodayOnly and Today) or (showReversalZone and !showTodayOnly) then
        if newDay then Double.NaN
        else if endOfDay then Double.NaN
        else s1
    else Double.NaN,
    if (showReversalZone and showTodayOnly and Today) or (showReversalZone and !showTodayOnly) then
        if newDay then Double.NaN
        else if endOfDay then Double.NaN
        else s2
    else Double.NaN,
    Color.LIGHT_GREEN,
    Color.LIGHT_GREEN
);

AddLabel(showReversalZone and showLabels, "Bear zone: " + RoundDown(R1, RoundLevel) + " to " + RoundDown(R2, RoundLevel), Color.LIGHT_RED);
AddLabel(showReversalZone and showLabels, "Bull zone: " + RoundDown(S1, RoundLevel) + " to " + RoundDown(S2, RoundLevel), Color.LIGHT_GREEN);

plot bullLastStand = if (showLastStand and showTodayOnly and Today) or (showLastStand and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else S3
else Double.NaN;
bullLastStand.setDefaultColor(Color.GREEN);

plot bearLastStand = if (showLastStand and showTodayOnly and Today) or (showLastStand and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else R3
else Double.NaN;
bearLastStand.setDefaultColor(Color.RED);

input showBubblesLeft = no;
AddChartBubble(showBubblesLeft and showLastStand and Today and !Today[1], R3, "Bear Last Stand: " + R3, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showLastStand and Today and !Today[1], S3, "Bull Last Stand: " + S3, Color.GRAY, no);

AddChartBubble(showBubblesLeft and showPrevious and Today and !Today[1], PrevHigh, "Previous High: " + PrevHigh, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showPrevious and Today and !Today[1], PrevLow, "Previous Low: " + PrevLow, Color.GRAY, no);

plot bull1 = if (showBullTargets and showTodayOnly and Today) or (showBullTargets and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else R5
else Double.NaN;
bull1.setDefaultColor(Color.MAGENTA);

plot bear1 = if (showBearTargets and showTodayOnly and Today) or (showBearTargets and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else S5
else Double.NaN;
bear1.setDefaultColor(Color.CYAN);

AddChartBubble(showBubblesLeft and showBullTargets and Today and !Today[1], R5, "Bull Target 1: " + R5, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showBearTargets and Today and !Today[1], S5, "Bear Target 1: " + S5, Color.GRAY, no);

plot bull2 = if (showBullTargets and showTodayOnly and Today) or (showBullTargets and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else R4
else Double.NaN;
bull2.setDefaultColor(Color.MAGENTA);

plot bear2 = if (showBearTargets and showTodayOnly and Today) or (showBearTargets and !showTodayOnly) then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else S4
else Double.NaN;
bear2.setDefaultColor(Color.CYAN);

AddChartBubble(showBubblesLeft and showBullTargets and Today and !Today[1], R4, "Bull Target 2: " + R4, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showBearTargets and Today and !Today[1], S4, "Bear Target 2: " + S4, Color.GRAY, no);

input showOpen = yes;
input Open_Time = 0930;
AddVerticalLine(secondsFromTime(Open_Time)[1] < 0 and secondsFromTime(Open_Time) >= 0 and Today and showOpen, concat("Open", ""), Color.Green, curve.POINTS);

input showBubblesRight = Yes;
input displaceRightBubble = 3;
def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);
AddChartBubble(showBubblesRight and showLastStand and showBubbleNow[displaceRightBubble], R3, "Bear Last Stand: " + R3, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showLastStand and showBubbleNow[displaceRightBubble], S3, "Bull Last Stand: " + S3, Color.GRAY, no);

AddChartBubble(showBubblesRight and showPrevious and showBubbleNow[displaceRightBubble], PreviousHigh, "Previous High: " + RoundDown(previousHigh,RoundLevel), Color.GRAY, yes);
AddChartBubble(showBubblesRight and showPrevious and showBubbleNow[displaceRightBubble], PreviousLow, "Previous Low: " + PreviousLow, Color.GRAY, no);

AddChartBubble(showBubblesRight and showBullTargets and showBubbleNow[displaceRightBubble], R5, "Bull Target 1: " + R5, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showBearTargets and showBubbleNow[displaceRightBubble], S5, "Bear Target 1: " + S5, Color.GRAY, no);

AddChartBubble(showBubblesRight and showBullTargets and showBubbleNow[displaceRightBubble], R4, "Bull Target 2: " + R4, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showBearTargets and showBubbleNow[displaceRightBubble], S4, "Bear Target 2: " + S4, Color.GRAY, no);

input showWeeklyLabels = yes;
AddChartBubble(showWeeklyLabels and AggregationPeriod == AggregationPeriod.WEEK and Today and !Today[1], (R2 - R1)/2 + R1, "Weekly", Color.WHITE, yes);
AddChartBubble(showWeeklyLabels and AggregationPeriod == AggregationPeriod.WEEK and Today and !Today[1], (S1 - S2)/2 + S2, "Weekly", Color.WHITE, no);

input showSmallCamPoints = no;
plot smallCamResistance = if showSmallCamPoints and Today then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else smallR1
else Double.NaN;
smallCamResistance.setDefaultColor(Color.LIGHT_RED);

plot smallCamSupport = if showSmallCamPoints and Today then
    if newDay then Double.NaN
    else if endOfDay then Double.NaN
    else smallS1
else Double.NaN;
smallCamSupport.setDefaultColor(Color.LIGHT_GREEN);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
366 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top