Professional Documents
Culture Documents
Study
Study
transp=input(0)
Numbers=input(true)
SR=input(true)
Barcolor=input(true)
plotshape(Numbers?(TDUp==1?
true:na):na,style=shape.triangledown,text="1",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==2?
true:na):na,style=shape.triangledown,text="2",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==3?
true:na):na,style=shape.triangledown,text="3",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==4?
true:na):na,style=shape.triangledown,text="4",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==5?
true:na):na,style=shape.triangledown,text="5",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==6?
true:na):na,style=shape.triangledown,text="6",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==7?
true:na):na,style=shape.triangledown,text="7",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==8?
true:na):na,style=shape.triangledown,text="8",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDUp==9?
true:na):na,style=shape.triangledown,text="9",color=green,location=location.abovebar,transp=transp)
plotshape(Numbers?(TDDn==1?
true:na):na,style=shape.triangleup,text="1",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==2?
true:na):na,style=shape.triangleup,text="2",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==3?
true:na):na,style=shape.triangleup,text="3",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==4?
true:na):na,style=shape.triangleup,text="4",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==5?
true:na):na,style=shape.triangleup,text="5",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==6?
true:na):na,style=shape.triangleup,text="6",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==7?
true:na):na,style=shape.triangleup,text="7",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==8?
true:na):na,style=shape.triangleup,text="8",color=red,location=location.belowbar,transp=transp)
plotshape(Numbers?(TDDn==9?
true:na):na,style=shape.triangleup,text="9",color=red,location=location.belowbar,transp=transp)
//------------//
// Sell Setup //
//------------//
priceflip = barssince(close<close[4])
//----------//
// Buy setup//
//----------//
priceflip1 = barssince(close>close[4])
//----------//
// TD lines //
//----------//
TDbuyh = valuewhen(buy,high,0)
TDbuyl = valuewhen(buy,low,0)
TDsellh = valuewhen(sell,high,0)
TDselll = valuewhen(sell,low,0)
//----------//
// Plots //
//----------//
//Supertrend 1.0
Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
TrendUp=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
plot(close)
//@version=3
// TD Indicators:
// - TD Setup count up (green #, above bar, green diamond if count > 'Setup: Bars')
// - TD Setup count down (red #, above bar, red diamond if count > 'Setup: Bars')
// if Setup Up/Down Count == 2 * 'Setup: Bars', then the present Countdown is cancelled.
//
// Alerts Conditions:
// "Countdown Recycle Up" - Trigger an alert for the Countdown Recycle condition, where price is
moving up
// "Countdown Recycle Down" - Trigger an alert for the Countdown Recycle condition, where price is
moving down
//
// - "Price: Source", defines which bar price to use for price comparisions
// - TD Setups
// In this code, called the Buy/Sell Setup event, e.g. the last price up count
// "Setup: Lookback bars", defines the previous bar to compare for counting (traditionally 4).
// "Setup: Include Equal Price", If enabled, allow >= or <= in price comparisons.
// Traditionally not used (default is disabled). Might be useful for intraday charts.
// "Setup: Perfected Lookback", defines the previous count to evaluate for a perfected setup
// (this count and the next). Traditionally 3, i.e compare count 6 and 7 to count 8 or count 9.
// Note: Buy/Sell Setup events are not affected by this setting. They are always shown.
// If disabled, only look back to the beginning of this Buy/Sell Setup event
// If enabled, look back beyond this Buy/Sell Setup event to the previous
// Setup event of the same kind. (This capability has limitations... see code).
// "Setup Trend: Show", show/hide trend lines
// - TD Countdowns
// Called the Buy/Sell Countdown event in this code, i.e. the last up/down
// "Countdown: Lookback Bars", define previous bar to compare for counting (traditionally 2).
// "Countdown: Qualifier Bar", the bar in the Countdown sequence used to qualifiy the price
// qualification is disabled. Countdown events are still determined, just not qualified.
// "Countdown: Show Count", show/hide countdown numbers. Countdown events are always
shown.
// - TD Risk Level
// "Risk Level: Show", Show/hide TD Risk Level for setups, countdowns and recycled countdowns.
//
// If you want more flexibility in the user interface for plotting, set PlotEditEnable = true
//
// Coding notes:
// Ex) cntdwmCountUp -> <TD Countdown><counting for indicator><counting price moves up>
// - Variables that start with uppercase represent User input. Otherwise, lowercase. All variables
// are camelCase.
// faciliate quick/easy alterations. All plots are at the end of the script.
// - To pull off the logic wizardy of TD Indicators in the Pine Script language,
PlotEditEnable = false // show/hide some of the plots from Format window in the user interface.
// Create impulse series of price action. Compare where price is greater/less/equal than prior price.
// Plot parameters
// Setup Count Up plot (scup), Setup Count Down plot (scdp). Uses plotchar()
// Setup Sell plot (ssp), Setup Buy plot (sbp). Uses plotshape()
// Look for the establishment of momentum by counting consecutive up/down price moves.
// Up/down counters are mutually exclusive; only one is actively counting, while the other is in reset.
// Equal price ticks are captured separately so that up/down ticks aren't active on the
setupCountUp = na
setupCountUp := SetupEqualEnable ?
setupPriceUp ? nz(setupCountUp[1])+1 : 0
setupCountDown = na
setupCountDown := SetupEqualEnable ?
setupPriceDown ? nz(setupCountDown[1])+1 : 0
// Error check: make sure Setup Up and Down counts don't count on the same bar
setupSell = na
setupSellCount = barssince(setupSell)
setupBuyCount = barssince(setupBuy)
// Perfected Setups
// This consists of looking back a few bars and determining if the setup event's price is
// higher(sell)/lower(buy) than the lookback bars price. If not, a retest of the lookback
// SetupPerfLookback (user input) defines which bars to use for perfection evaluation.
// The evalution adheres to DeMark's original definiton where the bar before the setup event
// also qualifies a perfected setup, even if the setup event bar doesn't qualify.
// ...
// where n counts past the setup event, on setupCountUp bars. The evalution
// Cancelation rules for perfection evaluation aren't clear...(?) So here's a liberal approach:
// - A perfect price series, used for comparision to the setup event price or beyond
// After the mask is created, it overlays setup count series to plot visual indicators.
// For convenience, define const integer variables to translate the meaning of perfected/deffered
setupIsPerfected = 2
setupIsDeferred = 1
// Get the price for which Sell Setup perfection is evaluated. Stair-step series.
setupSellPerfPrice = na
setupSellPerfPrice := setupCountUp==SetupBars ?
valuewhen(setupCountUp==(SetupBars-SetupPerfLookback), high, 0 ) :
valuewhen(setupCountUp==(SetupBars-SetupPerfLookback+1), high, 0 )
) : nz(setupSellPerfPrice[1])
// Create mask to hold "perfected" decisions. This is like a state-machine, where new inputs
// First, cancellation
// - If a Buy Setup event occurs, cancel. This Sell Setup trend is over.
// - If a new Setup Sell event is present, start. Compare SetupBars and (SetupBars-1) to perf price.
// - If a perfected Sell Setup event NOT found, then seamlessly roll into the next Sell Setup event.
setupSellPerfMask = na
setupSellPerfMask :=
setupCountUp==SetupBars ?
setupIsPerfected : setupIsDeferred
na(setupSellPerfMask[1]) ? na :
setupBuyPerfPrice = na
setupBuyPerfPrice := setupCountDown==SetupBars ?
valuewhen(setupCountDown==(SetupBars-SetupPerfLookback), low, 0 ) :
valuewhen(setupCountDown==(SetupBars-SetupPerfLookback+1), low, 0 )
) : nz(setupBuyPerfPrice[1])
setupBuyPerfMask = na
setupBuyPerfMask :=
setupIsPerfected : setupIsDeferred
na(setupBuyPerfMask[1]) ? na :
// Plot parameters
// 2) The previous Setup of the same trend, e.g. if a Sell Setup, then low(support) from previous Sell
Setup
// if/else logic only allows for steps of "SetupBars" to look for lowest/highest,
// Stop at 10*SetupBars to avoid script consuming too much server juice... and rejected.
setupTrendSupport = na
setupTrendSupport := setupSell ?
(SetupTrendExtend ? (
: lowest(SetupBars) )
: nz(setupTrendSupport[1])
setupTrendResist = na
setupTrendResist := setupBuy ?
(SetupTrendExtend ? (
: highest(SetupBars))
: nz(setupTrendResist[1])
// Plot parameters
// Countdown Qualification Count plot (cqcp). Uses plotshape() ...only one defintion for both up/down
counts
// Countdown Count Recycle plot (cqcp). Uses plotshape() ...only one defintion for both up/down counts
csapText="aggr\ncount\ndown"
cbapText="aggr\ncount\ndown"
cntdwnCountUpRecycle = na
cntdwnCountUpRecycle := (setupCountUp==(2*SetupBars)) ?
valuewhen((setupCountUp==(2*SetupBars)), PriceSource, 0) : na
cntdwnCountDownRecycle = na
cntdwnCountDownRecycle := (setupCountDown==(2*SetupBars)) ?
valuewhen((setupCountDown==(2*SetupBars)), PriceSource, 0) : na
// Count up/down price moves. Price moves don't have to be consecutive for TD Countdowns
// "na" means no counting, integer values (including zero) mean a count in progress
cntdwnCountUp = na
// Convert stair-step values to impulse, i.e. only keep the initial price move
cntdwnCountUpImp = na
cntdwnCountDown = na
cntdwnCountDownImp = na
// Qualification of Countdowns: The last Countdown price must be greater/less than the CntdwnQualBar
price
cntdwnIsDeferred = 1
cntdwnSellQualPrice = na
cntdwnSellQualMask = na
cntdwnSellQualMaskImp = na
cntdwnBuyQualPrice = na
cntdwnBuyQualMask = na
cntdwnBuyQualMaskImp = na
cntdwnSellQualPrice := cntdwnCountUpImp==CntdwnQualBar ?
// Same routine as cntdwnCountUp... "na" means not qualifying. Integers mean qualifying in progress.
cntdwnSellQualMask :=
(nz(cntdwnSellQualMask[1])>=cntdwnIsQualified) or na(cntdwnCountUp) ? na :
(cntdwnCountUpImp==CntdwnBars ?
cntdwnIsQualified : cntdwnIsDeferred) :
(na(cntdwnSellQualMask[1]) ? na :
(cntdwnCountUpImp>CntdwnBars ?
cntdwnIsQualified : cntdwnSellQualMask[1]) :
cntdwnSellQualMask[1])))
cntdwnBuyQualPrice := cntdwnCountDownImp==CntdwnQualBar ?
valuewhen(cntdwnCountDownImp==CntdwnQualBar, PriceSource, 0) : nz(cntdwnBuyQualPrice[1])
cntdwnBuyQualMask :=
(nz(cntdwnBuyQualMask[1])>=cntdwnIsQualified) or na(cntdwnCountDown) ? na :
(cntdwnCountDownImp==CntdwnBars ?
cntdwnIsQualified : cntdwnIsDeferred) :
(na(cntdwnBuyQualMask[1]) ? na :
(cntdwnCountDownImp>CntdwnBars ?
cntdwnIsQualified : cntdwnBuyQualMask[1]) :
cntdwnBuyQualMask[1])))
else
cntdwnSell = na
cntdwnSellDefer = na
cntdwnSell := cntdwnSellQualMaskImp==cntdwnIsQualified ?
valuewhen(cntdwnSellQualMaskImp==cntdwnIsQualified, PriceSource, 0) : na
cntdwnSellDefer := cntdwnSellQualMaskImp==cntdwnIsDeferred ?
valuewhen(cntdwnSellQualMaskImp==cntdwnIsDeferred, PriceSource, 0) : na
cntdwnBuy = na
cntdwnBuyDefer = na
cntdwnBuy := cntdwnBuyQualMaskImp==cntdwnIsQualified ?
valuewhen(cntdwnBuyQualMaskImp==cntdwnIsQualified, PriceSource, 0) : na
cntdwnBuyDefer := cntdwnBuyQualMaskImp==cntdwnIsDeferred ?
valuewhen(cntdwnBuyQualMaskImp==cntdwnIsDeferred, PriceSource, 0) : na
//riskBar :=
// setupSell ? highest(SetupBars)
riskLevel :=
// Background plots come first. Shapes that need to be on top are last.
// TDST (Support/Resistance)
// TD Risk Levels
// bgcolor(setupEventColor)
plotshape(CntdwnShowCount?(cntdwnCountUpImp<CntdwnBars?true:na):na,
title="Countdowns Up < Last", style=ccupStyle, color=ccupColor, location=ccupLoc, size=ccupSize,
transp=Transp, editable=PlotEditEnable)
plotshape(CntdwnShowCount?(cntdwnCountUpImp==CntdwnBars?true:na):na,
plotshape(CntdwnShowCount?(cntdwnCountDownImp<CntdwnBars?true:na):na,
plotshape(CntdwnShowCount?(cntdwnCountDownImp==CntdwnBars?true:na):na,
plotshape(CntdwnShowCount?
((cntdwnCountUpImp==CntdwnQualBar)or(cntdwnCountDownImp==CntdwnQualBar)?true:na):na,
plotshape(CntdwnShowCount?(cntdwnCountUpRecycle or cntdwnCountDownRecycle):na,
// Note: this code is placed here so that setup shapes are placed on top of countdown shapes
// Setups, last
// For values > 9, use hexadecimal values since we can only plot one char
// For values > 9, use hexadecimal values since we can only plot one char
// plotchar(SetupShowCount?((setupCountDown==15)and(15<=SetupBars)?true:na):na, title="Setup
Count Down 15", char="F", color=scdpColor, location=scdpLoc, size=scdpSize, transp=Transp,
editable=PlotEditEnable)