Pivot Pivots Confirmation with Trading Levels For ThinkOrSwim

J007RMC

Well-known member
2019 Donor
Great Script I added this to my chart...
Code:
input n = 5;#hint n: For pivot and standard deviation 
input addAtPercentStDev = 75;#hint addAtPercentStDev: Add at this percent of standard deviation below previous low
input initialLots = 4;#hint initialLots: Set to preference
input lotsToAdd = 2;#hint lotsToAdd: Number of lots to add on a pull back 
input stDevMult = 2.0;#hint stDevMult: trail stop multiplier 
input labels = yes;

def openingLots = Max(4, initialLots);
def addedlots = Max(2, lotsToAdd);
AddLabel(initialLots < openingLots, " Error: Initial lots must be 4 or more ", Color.Cyan);
AddLabel(lotsToAdd < addedLots, " Error: Lots to add must be 2 or more ", Color.Cyan);
def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def tick = TickSize();
def x = BarNumber();
def stDev = CompoundValue(1, StDev(c, n), nan);
def ll = l == Lowest(l, n);
def LPx = if ll then x else nan; 
def LP_low = if !IsNaN(LPx) then l else LP_Low[1]; 
def LP_High = if !IsNaN(LPx) then h else LP_High[1];
def confirmation_count = if ll then 0 else 
if c crosses above LP_high 
then confirmation_count[1] + 1 
else confirmation_count[1];
def confirmationX = if confirmation_count crosses above 0
then x else nan;
def confirmed = confirmation_count crosses above 0;

def ro;
def stc;
def trail;
def retrace;
if confirmed {
ro = Round((c + (c - LP_Low) / (openingLots - 2)) / tick, 0) * tick;
stc = LP_Low;
trail = LP_Low;
retrace = LP_Low;
}else{
ro = CompoundValue(1, ro[1], nan);
stc = stc[1];
trail = Round(Max(trail[1], l[1] - stDev[1] * stDevMult) / tick, 0) * tick; 
retrace = Round(Max(retrace[1], l[1] - StDev[1] * addAtPercentStDev / 100) / tick, 0) * tick; 
}

def ro_reached = if confirmed then 0 else 
if h > ro then 1 else ro_reached[1];
def added = if confirmed then 0 else
if l crosses below retrace then 1 else added[1];
def trail_hit = if confirmed then 0 else
if c < trail then 1 else trail_hit[1];
def stop_hit = if confirmed then 0 else
if l < stc then 1 else stop_hit[1];


plot 
PivotConfirmed = confirmed;
PivotConfirmed.SetDefaultColor(Color.Light_Green);
PivotConfirmed.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot
BuyToOpen = if confirmed then c else nan;
BuyToOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyToOpen.SetDefaultColor(Color.Light_Green);

plot 
SellToClose = if !stop_hit or stop_hit crosses above 0 then stc else nan;
SellToClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SellToClose.SetDefaultColor(Color.RED);

plot 
TrailingStop = if ! trail_hit or trail_hit crosses above 0 then trail else nan;
TrailingStop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TrailingStop.SetDefaultColor(Color.Pink);

plot
Add = if !added or added crosses above 0 then retrace else nan;
Add.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Add.SetDefaultColor(Color.Dark_Green);

plot
RiskOut = if !ro_reached or ro_reached crosses above 0 then ro else nan;
RiskOut.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RiskOut.SetDefaultColor(CreateColor(215, 215, 215));

AddLabel(labels and BuyToOpen, " BTO "+ openingLots +" = " + BuyToOpen + " ", Color.Light_Green);
Addlabel(labels and BuyToOpen, " ", CreateColor(0, 0, 0));
AddLabel(labels and ro_reached and Add, " Add "+ addedlots +" at = " + Add + " ", Color.Dark_Green);
AddLabel(labels and ro_reached and Add, " ", CreateColor(0, 0, 0));
AddLabel(labels and RiskOut, " Sell " + (openingLots - 2) + " = " + ro + " ", CreateColor(215, 215, 215));
AddLabel(labels and RiskOut, " ", CreateColor(0, 0, 0));
AddLabel(labels and ro_reached and TrailingStop, " Sell "+(1+addedLots-1)+" at = "+trail+" ", Color.Pink);
Addlabel(labels and ro_reached and TrailingStop, " ", CreateColor(0, 0, 0));
AddLabel(labels and SellToClose, " Sell All = " + stc + " ", Color.Red);
Addlabel(labels and SellToClose, " ", CreateColor(0, 0, 0));

#f/ Pivot Confirmation With Trading Levels
 
Last edited by a moderator:
Interesting to note the advanced market moves indicator fired along with the pivot script arrow on the hourly.
 
I am liking this. Thanks for sharing, J007RMC.

My coding skills are lacking - just starting to learn. I have tried to insert BUY / SELL commands to use the FloatingPL but just not getting it right. Would you please help? The following seems off and I know it can be written better to work with what you have done. I'm not sure how to adapt this to your initial order, the add to order, plus stops and trailing stops.

AddOrder(condition = confirmed, type = OrderType.BUY_TO_OPEN, price = close, name = "LE");
AddOrder(condition = stop_hit, type = OrderType.SELL_TO_CLOSE, price = close, name = "LX");
AddOrder(condition = trail_hit, type = OrderType.SELL_TO_CLOSE, price = close, name = "TrailStop");

Thanks,
Rick
 
This is a script I run every day I like it
 
Last edited by a moderator:
They do, I wait for the pivot shelf to settle with this script I use a swing hi/lo script catching the swings is important to me to look for the swing highs and lo's.... here is the kicker
 
Last edited by a moderator:
Heres the code for short entries if anyone is intereseted

Code:
input n = 5;#hint n: For pivot and standard deviation
input addAtPercentStDev = 75;#hint addAtPercentStDev: Add at this percent of standard deviation below previous low
input initialLots = 4;#hint initialLots: Set to preference
input lotsToAdd = 2;#hint lotsToAdd: Number of lots to add on a pull back
input stDevMult = 2.0;#hint stDevMult: trail stop multiplier
input labels = no;

def openingLots = Max(4, initialLots);
def addedlots = Max(2, lotsToAdd);
AddLabel(initialLots < openingLots, " Error: Initial lots must be 4 or more ", Color.Cyan);
AddLabel(lotsToAdd < addedLots, " Error: Lots to add must be 2 or more ", Color.Cyan);
def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def tick = TickSize();
def x = BarNumber();
def stDev = CompoundValue(1, StDev(c, n), nan);
def hh = h == Highest(h, n);
def LPx = if hh then x else nan;
def LP_low = if !IsNaN(LPx) then l else LP_Low[1];
def LP_High = if !IsNaN(LPx) then h else LP_High[1];
def confirmation_count = if hh then 0 else
if c crosses below LP_Low
then confirmation_count[1] + 1
else confirmation_count[1];
def confirmationX = if confirmation_count crosses above 0
then x else nan;
def confirmed = confirmation_count crosses above 0;

def ro;
def stc;
def trail;
def retrace;
if confirmed {
ro = Round((c - (LP_High - c) / (openingLots - 2)) / tick, 0) * tick;
stc = LP_High;
trail = LP_High;
retrace = LP_High;
}else{
ro = CompoundValue(1, ro[1], nan);
stc = stc[1];
trail = Round(Min(trail[1], h[1] + stDev[1] * stDevMult) / tick, 0) * tick;
retrace = Round(Min(retrace[1], h[1] + StDev[1] * addAtPercentStDev / 100) / tick, 0) * tick;
}

def ro_reached = if confirmed then 0 else
if l < ro then 1 else ro_reached[1];
def added = if confirmed then 0 else
if h crosses above retrace then 1 else added[1];
def trail_hit = if confirmed then 0 else
if c > trail then 1 else trail_hit[1];
def stop_hit = if confirmed then 0 else
if h > stc then 1 else stop_hit[1];


plot
PivotConfirmed = confirmed;
PivotConfirmed.SetDefaultColor(Color.Light_Red);
PivotConfirmed.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot
BuyToOpen = if confirmed then c else nan;
BuyToOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyToOpen.SetDefaultColor(Color.Light_Green);

plot
SellToClose = if !stop_hit or stop_hit crosses above 0 then stc else nan;
SellToClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SellToClose.SetDefaultColor(Color.RED);

plot
TrailingStop = if ! trail_hit or trail_hit crosses above 0 then trail else nan;
TrailingStop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TrailingStop.SetDefaultColor(Color.Pink);

plot
Add = if !added or added crosses above 0 then retrace else nan;
Add.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Add.SetDefaultColor(Color.Dark_Green);

plot
RiskOut = if !ro_reached or ro_reached crosses above 0 then ro else nan;
RiskOut.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RiskOut.SetDefaultColor(CreateColor(215, 215, 215));

AddLabel(labels and BuyToOpen, " BTO "+ openingLots +" = " + BuyToOpen + " ", Color.Light_Green);
Addlabel(labels and BuyToOpen, " ", CreateColor(0, 0, 0));
AddLabel(labels and ro_reached and Add, " Add "+ addedlots +" at = " + Add + " ", Color.Dark_Green);
AddLabel(labels and ro_reached and Add, " ", CreateColor(0, 0, 0));
AddLabel(labels and RiskOut, " Sell " + (openingLots - 2) + " = " + ro + " ", CreateColor(215, 215, 215));
AddLabel(labels and RiskOut, " ", CreateColor(0, 0, 0));
AddLabel(labels and ro_reached and TrailingStop, " Sell "+(1+addedLots-1)+" at = "+trail+" ", Color.Pink);
Addlabel(labels and ro_reached and TrailingStop, " ", CreateColor(0, 0, 0));
AddLabel(labels and SellToClose, " Sell All = " + stc + " ", Color.Red);
Addlabel(labels and SellToClose, " ", CreateColor(0, 0, 0));

#f/ Pivot Confirmation With Trading Levels
 
Help with turning a study into a scan. I can’t find who orginally posted this if it was here. There two seperate studies one for Longs one for Shorts. I’m trying to turn one into a scan: The scan says to remove labels. I did, must have at least one plot. This is where I’ve tried to alter the study to get results with no luck.

Here is the original clean code for the study. Can someone help me turn this into a scan please??
input n = 5;#hint n: For pivot and standard deviation
input addAtPercentStDev = 75;#hint addAtPercentStDev: Add at this percent of standard deviation below previous low
input initialLots = 4;#hint initialLots: Set to preference
input lotsToAdd = 2;#hint lotsToAdd: Number of lots to add on a pull back
input stDevMult = 2.0;#hint stDevMult: trail stop multiplier
input labels = no;

def openingLots = Max(4, initialLots);
def addedlots = Max(2, lotsToAdd);
AddLabel(initialLots < openingLots, " Error: Initial lots must be 4 or more ", Color.Cyan);
AddLabel(lotsToAdd < addedLots, " Error: Lots to add must be 2 or more ", Color.Cyan);
def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def tick = TickSize();
def x = BarNumber();
def stDev = CompoundValue(1, StDev(c, n), nan);
def hh = h == Highest(h, n);
def LPx = if hh then x else nan;
def LP_low = if !IsNaN(LPx) then l else LP_Low[1];
def LP_High = if !IsNaN(LPx) then h else LP_High[1];
def confirmation_count = if hh then 0 else
if c crosses below LP_Low
then confirmation_count[1] + 1
else confirmation_count[1];
def confirmationX = if confirmation_count crosses above 0
then x else nan;
def confirmed = confirmation_count crosses above 0;

def ro;
def stc;
def trail;
def retrace;
if confirmed {
ro = Round((c - (LP_High - c) / (openingLots - 2)) / tick, 0) * tick;
stc = LP_High;
trail = LP_High;
retrace = LP_High;
}else{
ro = CompoundValue(1, ro[1], nan);
stc = stc[1];
trail = Round(Min(trail[1], h[1] + stDev[1] * stDevMult) / tick, 0) * tick;
retrace = Round(Min(retrace[1], h[1] + StDev[1] * addAtPercentStDev / 100) / tick, 0) * tick;
}

def ro_reached = if confirmed then 0 else
if l < ro then 1 else ro_reached[1];
def added = if confirmed then 0 else
if h crosses above retrace then 1 else added[1];
def trail_hit = if confirmed then 0 else
if c > trail then 1 else trail_hit[1];
def stop_hit = if confirmed then 0 else
if h > stc then 1 else stop_hit[1];


plot
PivotConfirmed = confirmed;
PivotConfirmed.SetDefaultColor(Color.Light_Red);
PivotConfirmed.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot
BuyToOpen = if confirmed then c else nan;
BuyToOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyToOpen.SetDefaultColor(Color.Light_Green);
Ok these are the changes I made: created a study; and used the study and said TRUE: I only got a couple results on a D and not on smaller time frames. I think I may have figured this out and i’m so shocked and happy I’m finally learning this stuff. Let me know if there is something else I may have missed:

input n = 5;#hint n: For pivot and standard deviation
input addAtPercentStDev = 75;#hint addAtPercentStDev: Add at this percent of standard deviation below previous low
input initialLots = 4;#hint initialLots: Set to preference
input lotsToAdd = 2;#hint lotsToAdd: Number of lots to add on a pull back
input stDevMult = 2.0;#hint stDevMult: trail stop multiplier
input labels = no;

def openingLots = Max(4, initialLots);
def addedlots = Max(2, lotsToAdd);

def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def tick = TickSize();
def x = BarNumber();
def stDev = CompoundValue(1, StDev(c, n), nan);
def hh = h == Highest(h, n);
def LPx = if hh then x else nan;
def LP_low = if !IsNaN(LPx) then l else LP_Low[1];
def LP_High = if !IsNaN(LPx) then h else LP_High[1];
def confirmation_count = if hh then 0 else
if c crosses below LP_Low
then confirmation_count[1] + 1
else confirmation_count[1];
def confirmationX = if confirmation_count crosses above 0
then x else nan;
def confirmed = confirmation_count crosses above 0;

def ro;
def stc;
def trail;
def retrace;
if confirmed {
ro = Round((c - (LP_High - c) / (openingLots - 2)) / tick, 0) * tick;
stc = LP_High;
trail = LP_High;
retrace = LP_High;
}else{
ro = CompoundValue(1, ro[1], nan);
stc = stc[1];
trail = Round(Min(trail[1], h[1] + stDev[1] * stDevMult) / tick, 0) * tick;
retrace = Round(Min(retrace[1], h[1] + StDev[1] * addAtPercentStDev / 100) / tick, 0) * tick;
}

def ro_reached = if confirmed then 0 else
if l < ro then 1 else ro_reached[1];
def added = if confirmed then 0 else
if h crosses above retrace then 1 else added[1];
def trail_hit = if confirmed then 0 else
if c > trail then 1 else trail_hit[1];
def stop_hit = if confirmed then 0 else
if h > stc then 1 else stop_hit[1];


plot
PivotConfirmed = confirmed;
PivotConfirmed.SetDefaultColor(Color.Light_Red);
PivotConfirmed.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 

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
211 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