Convert TradingView Indicator to ThinkorSwim

javin82

New member
I need help converting a pine script indicator into a think script study for Thinkorswim. The name of the indicator is called Range. It is a reversal indicator and plots long and short signals. If you can help me with this, please let me know. Thank you!

PineScript code below:
Code:
//////////////
//@version=3
study("Range fv2",overlay=true)
showTrendLine = input(false, title="Show Trend Line")
colorBars = input(true, title="Color Bars?")
filterTrend = input(true, title="Filter Trend?")

//Source
src = close

//Sampling Period
per = input(defval=25, minval=1, title="Sampling Period")

//Range Multiplier
mult = input(defval=2, minval=0, title="Range Multiplier")

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Smooth Average Range
smoothrng(x, t, m)=>
    wper      = (t/3) - 1
    avrng     = sma(abs(x - x[1]), t)
    smoothrng = sma(avrng, wper)*m
    smoothrng
smrng = smoothrng(src, per, mult)

//Range Filter
rngfilt(x, r)=>
    rngfilt  = x
    rngfilt := x > nz(rngfilt[1]) ? ((x - r) < nz(rngfilt[1]) ? nz(rngfilt[1]) : (x - r)) : ((x + r) > nz(rngfilt[1]) ? nz(rngfilt[1]) : (x + r))
    rngfilt
filt = rngfilt(src, smrng)

//Filter Direction
upward   = 0.0
upward  := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])

//Target Bands
hband = filt + smrng
lband = filt - smrng

//Colors
filtcolor = upward > 0 ? lime : downward > 0 ? red : orange
barcolor  = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green : (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange
barcolor(colorBars ? barcolor : na)

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Range Filter
if filterTrend == true
    showTrendLine := false
filtplot = plot(filterTrend ? filt : na, color=filtcolor, linewidth=4, title="Range Filter")
//
Factor=input(3, minval=1,maxval = 100)
Pd=input(3, minval=1,maxval = 100)

Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
TrendUp = 0.0
TrendDown = 0.0
linecolor = na
up = na
down = na
Tsl = 0.0
Trend = 0.0
TrendUp := close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown := close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl := Trend==1? TrendUp: TrendDown
linecolor := Trend == 1 ? green : red
plot(showTrendLine ? Tsl : na, color = linecolor , style = line , linewidth = 3,title = "SuperTrend")

// Strategy


longCond = na
shortCond = na
longCond :=crossunder(Tsl,filt)
shortCond :=crossover(Tsl,filt)

// Count your long short conditions for more control with Pyramiding

sectionLongs = 0
sectionLongs := nz(sectionLongs[1])
sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if longCond
    sectionLongs := sectionLongs + 1
    sectionShorts := 0

if shortCond
    sectionLongs := 0
    sectionShorts := sectionShorts + 1

// Pyramiding

pyrl = 1


// These check to see your signal and cross references it against the pyramiding settings above

longCondition = longCond and sectionLongs <= pyrl
shortCondition = shortCond and sectionShorts <= pyrl

// Get the price of the last opened long or short

last_open_longCondition = na
last_open_shortCondition = na
last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1])

// Check if your last postion was a long or a short

last_longCondition = na
last_shortCondition = na
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])

in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Take profit

isTPl = input(true, "Take Profit Long")
isTPs = input(true, "Take Profit Short")
tp = input(5, "Take Profit ", type=float)
long_tp = isTPl and crossover(high, (1+(tp/100))*last_open_longCondition) and longCondition == 0 and in_longCondition  == 1
short_tp = isTPs and crossunder(low, (1-(tp/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1


// Create a single close for all the different closing conditions.

long_close = long_tp  ? 1 : 0
short_close = short_tp ? 1 : 0

// Get the time of the last close

last_long_close = na
last_short_close = na
last_long_close := long_close ? time : nz(last_long_close[1])
last_short_close := short_close ? time : nz(last_short_close[1])


// Alerts & Signals

bton(b) => b ? 1 : 0
plotshape(longCondition , title="Long", color=green, textcolor=green, transp=0,
          style=shape.triangleup, location=location.belowbar, size=size.small,text="LONG",offset=0)


plotshape(shortCondition, title="Short", color=red, textcolor=red, transp=0,
          style=shape.triangledown, location=location.abovebar, size=size.small,text="SHORT",offset=0)


//plotshape(longCondition, title = "BUY Signal", text = "Buy", style=shape.triangleup, location=location.belowbar, color = blue, editable = false, transp = 0)
//plotshape(shortCondition, title = "SELL Signal", text = "Sell", style=shape.triangledown, location=location.abovebar, color = black, editable = false, transp = 0)

plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="TP", title="Take Profit Long", style=shape.triangledown,
   location=location.abovebar, color = red, editable = false, transp = 0)
plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="TP", title="Take Profit Short", style=shape.triangleup,
   location=location.belowbar, color = lime, editable = false, transp = 0)

alertcondition(bton(longCondition), title="Long Alert")
alertcondition(bton(shortCondition), title="Short Alert")
alertcondition(bton(long_tp and last_longCondition > nz(last_long_close[1])), title="Take Profit Long")
alertcondition(bton(short_tp and last_shortCondition > nz(last_short_close[1])), title="Take Profit Short")

Any help I can get with this will be greatly appreciated! Thank you!

Admin's Notes:

You can now use this thread to make your own request. If there is a TradingView script that you would like to see in ThinkorSwim, please post the source code and link to that script below. We'll try our best to help you with the conversion.

Below are the TradingView scripts that have been converted to TOS.
 
Last edited by a moderator:

Nick

Active member
2019 Donor
VIP
I recently came across an interview with Matt Caruso who came in 4th place in the 2020 US Investing Championship. Though he came in 4th place, but he was leading all the way. It was only the last quarter, where a gap down in FSLY knock him off the 1st place position. Nevertheless, he handle the gap down flawlessly. The guy is brilliant!

You can see his interview here:


He uses an indicator called the ATRP to help him gauge whether volatility is low. When volatility is low, this is the best time buy into the stock, especially if you are the type who likes to do pullback.

I am wondering, does anyone come across this indicator. If yes, is it alright if you could share the code here? A copy of this indicator is shown below.

ckYiKt9.png
 

Volatility Trading Range

VTR is a momentum indicator that shows if a stock is overbought or oversold based on its Weekly and Monthly average volatility trading range.

Download the indicator

DJAlan

New member
@Nick Hey, I saw what you were looking for and tried to write it for you. (The other coders here are much better, But tell me if it's close)

Code:
###  ATR Mod   ###
declare lower;
input length = 20;
input length2 = 5;
input averageType = AverageType.WILDERS;
def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
def ATRA = MovingAverage(averageType, atr, length2);
plot ATRM = ((ATR- ATRA) /close)*100;
ATRM.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
ATRM.AssignValueColor(if ATRM >= 0 then Color.downtick else Color.uptick);
 

Chuck

Active member
Code:
//@version=2
study(title="Ichimoku Cloud Signal Score", shorttitle="Ichimoku (score)", overlay=false)

// == inputs ==

conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, minval=1, title="Displacement")

// == helpers ==

donchian(len) => avg(lowest(len), highest(len))

resolve(src, default) =>
    if na(src)
        default
    else
        src

// == generate ichimoku data ==

conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)

// NOTE: these senkou spans are for signal generation; not for plotting. this is
//       due to limitations of pinescript
//
// i.e. Senkou Span A
leadLine1 = offset(avg(conversionLine, baseLine), displacement)
// i.e. Senkou Span B
leadLine2 = offset(donchian(laggingSpan2Periods), displacement)


// == plot ichimoku ==

// // i.e. Tenkan Sen (turning line) (blue)
// plot(conversionLine, color=#0496ff, title="Turning/Conversion Line", linewidth=3)
// // i.e. Kijun Sen (base/standard line) (red)
// plot(baseLine, color=#991515, title="Standard/Base Line", linewidth=3)
// // i.g. Chikou Span (lagging line) (green)
// plot(close, offset = -displacement, color=#459915, title="Lagging Span", linewidth=3)

// plot(conversionLine, color=#0496ff, title="Conversion Line")
// plot(baseLine, color=#991515, title="Base Line")

// real_leadLine1 = avg(conversionLine, baseLine)
// real_leadLine2 = donchian(laggingSpan2Periods)

// // i.e. Senkou Span A
// p1 = plot(real_leadLine1, offset = displacement, color=green, title="Lead 1")
// // i.e. Senkou Span B
// p2 = plot(real_leadLine2, offset = displacement, color=red, title="Lead 2")

// // i.e. Kumo cloud colouring
// fill(p1, p2, color = real_leadLine1 > real_leadLine2 ? green : red)

// == ichimoku cloud signals ==
// source: [URL]http://www.ichimokutrader.com/signals.html[/URL]

// == Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==

// tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
// tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
tk_cross_bull = crossover(conversionLine, baseLine)
tk_cross_bear = crossunder(conversionLine, baseLine)

// get true y-coord of the cross
cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))

tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)

tk_cross_score = resolve(tk_cross_score[1], 0)

use_tk_cross = input(true, title="TS Cross")
tk_cross_weight = input(1.0, title="TS Cross Importance Weight", type=float, step=0.1)

tk_cross_weak_bullish_points = input(0.5, title="TS Cross Weak Bullish Points", type=float, step=0.1)
tk_cross_neutral_bullish_points = input(1.0, title="TS Cross Neutral Bullish Points", type=float, step=0.1)
tk_cross_strong_bullish_points = input(2.0, title="TS Cross Strong Bullish Points", type=float, step=0.1)

tk_cross_weak_bearish_points = input(-0.5, title="TS Cross Weak Bearish Points", type=float, step=0.1)
tk_cross_neutral_bearish_points = input(-1.0, title="TS Cross Neutral Bearish Points", type=float, step=0.1)
tk_cross_strong_bearish_points = input(-2.0, title="TS Cross Strong Bearish Points", type=float, step=0.1)

// == TK Cross / weak bullish (below kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score

// == TK Cross / neutral bullish (inside kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score

// == TK Cross / strong bullish (above kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score

// == TK Cross / strong bearish (below kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score

// == TK Cross / neutral bearish (inside kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score

// == TK Cross / weak bearish (above kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score


// == Price and Kijun Sen (standard line) Cross ==

//pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
//pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
pk_cross_bull = crossover(close, baseLine)
pk_cross_bear = crossunder(close, baseLine)

cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))

pk_cross_below_kumo = cross_pk_y <= leadLine2[1] and cross_pk_y <= leadLine1[1] and cross_pk_y <= leadLine2 and cross_pk_y <= leadLine1
pk_cross_above_kumo = cross_pk_y >= leadLine2[1] and cross_pk_y >= leadLine1[1] and cross_pk_y >= leadLine2 and cross_pk_y >= leadLine1
pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)

pk_cross_score = resolve(pk_cross_score[1], 0)

use_pk_cross = input(true, title="PS Cross")
pk_cross_weight = input(1.0, title="PS Cross Importance Weight", type=float, step=0.1)

pk_cross_weak_bullish_points = input(0.5, title="PS Cross Weak Bullish Points", type=float, step=0.1)
pk_cross_neutral_bullish_points = input(1.0, title="PS Cross Neutral Bullish Points", type=float, step=0.1)
pk_cross_strong_bullish_points = input(2.0, title="PS Cross Strong Bullish Points", type=float, step=0.1)

pk_cross_weak_bearish_points = input(-0.5, title="PS Cross Weak Bearish Points", type=float, step=0.1)
pk_cross_neutral_bearish_points = input(-1.0, title="PS Cross Neutral Bearish Points", type=float, step=0.1)
pk_cross_strong_bearish_points = input(-2.0, title="PS Cross Strong Bearish Points", type=float, step=0.1)

// == PK Cross / weak bullish (below kumo) ==
pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score

// == PK Cross / neutral bullish (inside kumo)
pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score

// == PK Cross / strong bullish (above kumo)
pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score

// == PK Cross / strong bearish (below kumo)
pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score

// == PK Cross / neutral bearish (inside kumo)
pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score

// == PK Cross / weak bearish (above kumo)
pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score

// == Kumo Breakouts ==

kumo_bull = (crossover(close, leadLine1) and leadLine1 > leadLine2) or (crossover(close, leadLine2) and leadLine2 > leadLine1)
kumo_bear = (crossunder(close, leadLine2) and leadLine1 > leadLine2) or (crossunder(close, leadLine1) and leadLine2 > leadLine1)

kumo_breakout_score = resolve(kumo_breakout_score[1], 0)

use_kumo_breakout = input(true, title="Kumo Breakout")
kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight", type=float, step=0.1)

kumo_breakout_bullish_points = input(1.0, title="Kumo Breakout Bullish Points", type=float, step=0.1)
kumo_breakout_bearish_points = input(-1.0, title="Kumo Breakout Bearish Points", type=float, step=0.1)

price_below_kumo = (close < leadLine2 and close < leadLine1)
price_above_kumo = (close > leadLine2 and close > leadLine1)
price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)

kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score

// == Senkou Span Cross ==
// The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
// NOTE: this cross occurs ahead of the price, since it's displaced to the right; this displacement must be removed

// i.e. Senkou Span A (no displacement)
no_dp_leadLine1 = avg(conversionLine, baseLine)
// i.e. Senkou Span B (no displacement)
no_dp_leadLine2 = donchian(laggingSpan2Periods)
// TODO: debug;remove
// plot(no_dp_leadLine1)
// plot(no_dp_leadLine2)

lead_line_cross_bull = crossover(no_dp_leadLine1, no_dp_leadLine2)
lead_line_cross_bear = crossunder(no_dp_leadLine1, no_dp_leadLine2)
price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
// price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)

span_cross_score = resolve(span_cross_score[1], 0)

use_span_cross = input(true, title="Span Cross")
span_cross_weight = input(1.0, title="Span Cross Importance Weight", type=float, step=0.1)

span_cross_weak_bullish_points = input(0.5, title="Span Cross Weak Bullish Points", type=float, step=0.1)
span_cross_neutral_bullish_points = input(1.0, title="Span Cross Neutral Bullish Points", type=float, step=0.1)
span_cross_strong_bullish_points = input(2.0, title="Span Cross Strong Bullish Points", type=float, step=0.1)

span_cross_weak_bearish_points = input(-0.5, title="Span Cross Weak Bearish Points", type=float, step=0.1)
span_cross_neutral_bearish_points = input(-1.0, title="Span Cross Neutral Bearish Points", type=float, step=0.1)
span_cross_strong_bearish_points = input(-2.0, title="Span Cross Strong Bearish Points", type=float, step=0.1)

// == Senkou Span Cross / weak bullish (price below kumo) ==
span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score

// == Senkou Span Cross / neutral bullish (price inside kumo) ==
span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score

// == Senkou Span Cross / strong bullish (price above kumo) ==
span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score

// == Senkou Span Cross / weak bearish (price above kumo) ==
span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score

// == Senkou Span Cross / neutral bearish (price inside kumo) ==
span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score

// == Senkou Span Cross / strong bearish (price below kumo) ==
span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score

// == Chikou Span Cross ==
// The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.

past_price = offset(close, displacement)
lag_line_bull_cross = close > close[displacement]
lag_line_bear_cross = close < close[displacement]

// TODO: debug; remove
// plot(close)
// plot(close[displacement], linewidth=2)

past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)

lag_line_cross_score = resolve(lag_line_cross_score[1], 0)

use_lag_line = input(true, title="Lag Line Cross")
lag_line_cross_weight = input(1.0, title="Lag Line Cross Importance Weight", type=float, step=0.1)

lag_line_cross_weak_bullish_points = input(0.5, title="Lag Line Cross Weak Bullish Points", type=float, step=0.1)
lag_line_cross_neutral_bullish_points = input(1.0, title="Lag Line Cross Neutral Bullish Points", type=float, step=0.1)
lag_line_cross_strong_bullish_points = input(2.0, title="Lag Line Cross Strong Bullish Points", type=float, step=0.1)

lag_line_cross_weak_bearish_points = input(-0.5, title="Lag Line Cross Weak Bearish Points", type=float, step=0.1)
lag_line_cross_neutral_bearish_points = input(-1.0, title="Lag Line Cross Neutral Bearish Points", type=float, step=0.1)
lag_line_cross_strong_bearish_points = input(-2.0, title="Lag Line Cross Strong Bearish Points", type=float, step=0.1)

// == Chikou Span Cross / weak bullish (price below kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bullish (price inside kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score

// == Chikou Span Cross / strong bullish (price above kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score

// == Chikou Span Cross / weak bearish (price above kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bearish (price inside kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score

// == Chikou Span Cross / strong bearish (price below kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score

// == lag line releative to cloud ==

use_lag_line_location = input(true, title="Lag Line Relative to Cloud")

lag_line_location_weight = input(1.0, title="Lag Line Relative to Cloud Importance Weight", type=float, step=0.1)

lag_line_location_above_points = input(1.0, title="Lag Line Above Cloud Points", type=float, step=0.1)
lag_line_location_inside_points = input(0, title="Lag Line Inside Cloud Points", type=float, step=0.1)
lag_line_location_below_points = input(-1.0, title="Lag Line Below Cloud Points", type=float, step=0.1)

lag_line_placement_score = resolve(lag_line_placement_score[1], 0)

lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score

// == price relative to cloud ==

price_placement_score = resolve(price_placement_score[1], 0)

use_price_location = input(true, title="Price Relative to Cloud")
price_location_weight = input(1.0, title="Price Relative to Cloud Importance Weight", type=float, step=0.1)

price_location_above_points = input(1.0, title="Price Above Cloud Points", type=float, step=0.1)
price_location_inside_points = input(0, title="Price Inside Cloud Points", type=float, step=0.1)
price_location_below_points = input(-1.0, title="Price Below Cloud Points", type=float, step=0.1)

price_below_kumo := (close < leadLine2 and close < leadLine1)
price_above_kumo := (close > leadLine2 and close > leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)

price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score

// == plot score ==

total_score = (use_tk_cross * tk_cross_weight * tk_cross_score) + (use_pk_cross * pk_cross_weight * pk_cross_score) + (use_kumo_breakout * kumo_breakout_weight * kumo_breakout_score) + (use_span_cross * span_cross_weight * span_cross_score) + (use_lag_line * lag_line_cross_weight * lag_line_cross_score) + (use_price_location * price_location_weight * price_placement_score)
total_score := total_score + (use_lag_line_location * lag_line_location_weight * lag_line_placement_score)

plot(0, style=line, title="Base")
plot(total_score, linewidth=3, title="Total Score")



// debug
//plotshape((should_plot_tk_nb_inside_kumo) ? leadLine1 : na, color=black, style=shape.arrowup, location=location.belowbar, size=size.huge)
 

Nick

Active member
2019 Donor
VIP
There is another indicator which Matt Caruso uses in the US Investing Championship, and that is the breadth indicator called net new high. A picture of it is below:

tvAdbSQ.png


There is already a script written for it but it is for TradingView, as shown here: https://www.tradingview.com/chart/FTCH/WywLQoxo-Nasdaq-Net-new-highs/

I am just wondering, does anyone out there has the script for the indicator but it is written for Thinkorswim?

Thank you so much :) :) 🙏🙏
 

barbaros

Active member
VIP
Code:
//@version=2
study(title="Ichimoku Cloud Signal Score", shorttitle="Ichimoku (score)", overlay=false)

// == inputs ==

conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, minval=1, title="Displacement")

// == helpers ==

donchian(len) => avg(lowest(len), highest(len))

resolve(src, default) =>
    if na(src)
        default
    else
        src

// == generate ichimoku data ==

conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)

// NOTE: these senkou spans are for signal generation; not for plotting. this is
//       due to limitations of pinescript
//
// i.e. Senkou Span A
leadLine1 = offset(avg(conversionLine, baseLine), displacement)
// i.e. Senkou Span B
leadLine2 = offset(donchian(laggingSpan2Periods), displacement)


// == plot ichimoku ==

// // i.e. Tenkan Sen (turning line) (blue)
// plot(conversionLine, color=#0496ff, title="Turning/Conversion Line", linewidth=3)
// // i.e. Kijun Sen (base/standard line) (red)
// plot(baseLine, color=#991515, title="Standard/Base Line", linewidth=3)
// // i.g. Chikou Span (lagging line) (green)
// plot(close, offset = -displacement, color=#459915, title="Lagging Span", linewidth=3)

// plot(conversionLine, color=#0496ff, title="Conversion Line")
// plot(baseLine, color=#991515, title="Base Line")

// real_leadLine1 = avg(conversionLine, baseLine)
// real_leadLine2 = donchian(laggingSpan2Periods)

// // i.e. Senkou Span A
// p1 = plot(real_leadLine1, offset = displacement, color=green, title="Lead 1")
// // i.e. Senkou Span B
// p2 = plot(real_leadLine2, offset = displacement, color=red, title="Lead 2")

// // i.e. Kumo cloud colouring
// fill(p1, p2, color = real_leadLine1 > real_leadLine2 ? green : red)

// == ichimoku cloud signals ==
// source: [URL]http://www.ichimokutrader.com/signals.html[/URL]

// == Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==

// tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
// tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
tk_cross_bull = crossover(conversionLine, baseLine)
tk_cross_bear = crossunder(conversionLine, baseLine)

// get true y-coord of the cross
cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))

tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)

tk_cross_score = resolve(tk_cross_score[1], 0)

use_tk_cross = input(true, title="TS Cross")
tk_cross_weight = input(1.0, title="TS Cross Importance Weight", type=float, step=0.1)

tk_cross_weak_bullish_points = input(0.5, title="TS Cross Weak Bullish Points", type=float, step=0.1)
tk_cross_neutral_bullish_points = input(1.0, title="TS Cross Neutral Bullish Points", type=float, step=0.1)
tk_cross_strong_bullish_points = input(2.0, title="TS Cross Strong Bullish Points", type=float, step=0.1)

tk_cross_weak_bearish_points = input(-0.5, title="TS Cross Weak Bearish Points", type=float, step=0.1)
tk_cross_neutral_bearish_points = input(-1.0, title="TS Cross Neutral Bearish Points", type=float, step=0.1)
tk_cross_strong_bearish_points = input(-2.0, title="TS Cross Strong Bearish Points", type=float, step=0.1)

// == TK Cross / weak bullish (below kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score

// == TK Cross / neutral bullish (inside kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score

// == TK Cross / strong bullish (above kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score

// == TK Cross / strong bearish (below kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score

// == TK Cross / neutral bearish (inside kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score

// == TK Cross / weak bearish (above kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score


// == Price and Kijun Sen (standard line) Cross ==

//pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
//pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
pk_cross_bull = crossover(close, baseLine)
pk_cross_bear = crossunder(close, baseLine)

cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))

pk_cross_below_kumo = cross_pk_y <= leadLine2[1] and cross_pk_y <= leadLine1[1] and cross_pk_y <= leadLine2 and cross_pk_y <= leadLine1
pk_cross_above_kumo = cross_pk_y >= leadLine2[1] and cross_pk_y >= leadLine1[1] and cross_pk_y >= leadLine2 and cross_pk_y >= leadLine1
pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)

pk_cross_score = resolve(pk_cross_score[1], 0)

use_pk_cross = input(true, title="PS Cross")
pk_cross_weight = input(1.0, title="PS Cross Importance Weight", type=float, step=0.1)

pk_cross_weak_bullish_points = input(0.5, title="PS Cross Weak Bullish Points", type=float, step=0.1)
pk_cross_neutral_bullish_points = input(1.0, title="PS Cross Neutral Bullish Points", type=float, step=0.1)
pk_cross_strong_bullish_points = input(2.0, title="PS Cross Strong Bullish Points", type=float, step=0.1)

pk_cross_weak_bearish_points = input(-0.5, title="PS Cross Weak Bearish Points", type=float, step=0.1)
pk_cross_neutral_bearish_points = input(-1.0, title="PS Cross Neutral Bearish Points", type=float, step=0.1)
pk_cross_strong_bearish_points = input(-2.0, title="PS Cross Strong Bearish Points", type=float, step=0.1)

// == PK Cross / weak bullish (below kumo) ==
pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score

// == PK Cross / neutral bullish (inside kumo)
pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score

// == PK Cross / strong bullish (above kumo)
pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score

// == PK Cross / strong bearish (below kumo)
pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score

// == PK Cross / neutral bearish (inside kumo)
pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score

// == PK Cross / weak bearish (above kumo)
pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score

// == Kumo Breakouts ==

kumo_bull = (crossover(close, leadLine1) and leadLine1 > leadLine2) or (crossover(close, leadLine2) and leadLine2 > leadLine1)
kumo_bear = (crossunder(close, leadLine2) and leadLine1 > leadLine2) or (crossunder(close, leadLine1) and leadLine2 > leadLine1)

kumo_breakout_score = resolve(kumo_breakout_score[1], 0)

use_kumo_breakout = input(true, title="Kumo Breakout")
kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight", type=float, step=0.1)

kumo_breakout_bullish_points = input(1.0, title="Kumo Breakout Bullish Points", type=float, step=0.1)
kumo_breakout_bearish_points = input(-1.0, title="Kumo Breakout Bearish Points", type=float, step=0.1)

price_below_kumo = (close < leadLine2 and close < leadLine1)
price_above_kumo = (close > leadLine2 and close > leadLine1)
price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)

kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score

// == Senkou Span Cross ==
// The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
// NOTE: this cross occurs ahead of the price, since it's displaced to the right; this displacement must be removed

// i.e. Senkou Span A (no displacement)
no_dp_leadLine1 = avg(conversionLine, baseLine)
// i.e. Senkou Span B (no displacement)
no_dp_leadLine2 = donchian(laggingSpan2Periods)
// TODO: debug;remove
// plot(no_dp_leadLine1)
// plot(no_dp_leadLine2)

lead_line_cross_bull = crossover(no_dp_leadLine1, no_dp_leadLine2)
lead_line_cross_bear = crossunder(no_dp_leadLine1, no_dp_leadLine2)
price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
// price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)

span_cross_score = resolve(span_cross_score[1], 0)

use_span_cross = input(true, title="Span Cross")
span_cross_weight = input(1.0, title="Span Cross Importance Weight", type=float, step=0.1)

span_cross_weak_bullish_points = input(0.5, title="Span Cross Weak Bullish Points", type=float, step=0.1)
span_cross_neutral_bullish_points = input(1.0, title="Span Cross Neutral Bullish Points", type=float, step=0.1)
span_cross_strong_bullish_points = input(2.0, title="Span Cross Strong Bullish Points", type=float, step=0.1)

span_cross_weak_bearish_points = input(-0.5, title="Span Cross Weak Bearish Points", type=float, step=0.1)
span_cross_neutral_bearish_points = input(-1.0, title="Span Cross Neutral Bearish Points", type=float, step=0.1)
span_cross_strong_bearish_points = input(-2.0, title="Span Cross Strong Bearish Points", type=float, step=0.1)

// == Senkou Span Cross / weak bullish (price below kumo) ==
span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score

// == Senkou Span Cross / neutral bullish (price inside kumo) ==
span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score

// == Senkou Span Cross / strong bullish (price above kumo) ==
span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score

// == Senkou Span Cross / weak bearish (price above kumo) ==
span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score

// == Senkou Span Cross / neutral bearish (price inside kumo) ==
span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score

// == Senkou Span Cross / strong bearish (price below kumo) ==
span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score

// == Chikou Span Cross ==
// The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.

past_price = offset(close, displacement)
lag_line_bull_cross = close > close[displacement]
lag_line_bear_cross = close < close[displacement]

// TODO: debug; remove
// plot(close)
// plot(close[displacement], linewidth=2)

past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)

lag_line_cross_score = resolve(lag_line_cross_score[1], 0)

use_lag_line = input(true, title="Lag Line Cross")
lag_line_cross_weight = input(1.0, title="Lag Line Cross Importance Weight", type=float, step=0.1)

lag_line_cross_weak_bullish_points = input(0.5, title="Lag Line Cross Weak Bullish Points", type=float, step=0.1)
lag_line_cross_neutral_bullish_points = input(1.0, title="Lag Line Cross Neutral Bullish Points", type=float, step=0.1)
lag_line_cross_strong_bullish_points = input(2.0, title="Lag Line Cross Strong Bullish Points", type=float, step=0.1)

lag_line_cross_weak_bearish_points = input(-0.5, title="Lag Line Cross Weak Bearish Points", type=float, step=0.1)
lag_line_cross_neutral_bearish_points = input(-1.0, title="Lag Line Cross Neutral Bearish Points", type=float, step=0.1)
lag_line_cross_strong_bearish_points = input(-2.0, title="Lag Line Cross Strong Bearish Points", type=float, step=0.1)

// == Chikou Span Cross / weak bullish (price below kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bullish (price inside kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score

// == Chikou Span Cross / strong bullish (price above kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score

// == Chikou Span Cross / weak bearish (price above kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bearish (price inside kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score

// == Chikou Span Cross / strong bearish (price below kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score

// == lag line releative to cloud ==

use_lag_line_location = input(true, title="Lag Line Relative to Cloud")

lag_line_location_weight = input(1.0, title="Lag Line Relative to Cloud Importance Weight", type=float, step=0.1)

lag_line_location_above_points = input(1.0, title="Lag Line Above Cloud Points", type=float, step=0.1)
lag_line_location_inside_points = input(0, title="Lag Line Inside Cloud Points", type=float, step=0.1)
lag_line_location_below_points = input(-1.0, title="Lag Line Below Cloud Points", type=float, step=0.1)

lag_line_placement_score = resolve(lag_line_placement_score[1], 0)

lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score

// == price relative to cloud ==

price_placement_score = resolve(price_placement_score[1], 0)

use_price_location = input(true, title="Price Relative to Cloud")
price_location_weight = input(1.0, title="Price Relative to Cloud Importance Weight", type=float, step=0.1)

price_location_above_points = input(1.0, title="Price Above Cloud Points", type=float, step=0.1)
price_location_inside_points = input(0, title="Price Inside Cloud Points", type=float, step=0.1)
price_location_below_points = input(-1.0, title="Price Below Cloud Points", type=float, step=0.1)

price_below_kumo := (close < leadLine2 and close < leadLine1)
price_above_kumo := (close > leadLine2 and close > leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)

price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score

// == plot score ==

total_score = (use_tk_cross * tk_cross_weight * tk_cross_score) + (use_pk_cross * pk_cross_weight * pk_cross_score) + (use_kumo_breakout * kumo_breakout_weight * kumo_breakout_score) + (use_span_cross * span_cross_weight * span_cross_score) + (use_lag_line * lag_line_cross_weight * lag_line_cross_score) + (use_price_location * price_location_weight * price_placement_score)
total_score := total_score + (use_lag_line_location * lag_line_location_weight * lag_line_placement_score)

plot(0, style=line, title="Base")
plot(total_score, linewidth=3, title="Total Score")



// debug
//plotshape((should_plot_tk_nb_inside_kumo) ? leadLine1 : na, color=black, style=shape.arrowup, location=location.belowbar, size=size.huge)

How do you use this indicator?
 

Chuck

Active member
Barbaros,

I have recently been converted to Ichimoku Kinko Hyo. After a few months studying it on my own I saw everything that just about every indicator in this forum shows in one simple system at one glance. I am completely blown away. Everything I have ever learned about the market in one system.


I see Support/Resistance, Value Area , Overbought /Oversold, Volitility, Momentum, Consolidation....etc.

To answer your question I have a system for prescreening and grading stocks that gives it a score. I use this for a swing trading weekly list.
I am learning to code a little now and was attempting to make a screener for Ichimoku Kinko Hyo and stumbled upon this.

I might be able to build it but it would probably take me a few months ...seing I am so new at coding. Just figured someone with experience could turn it out in a day or two and contribute to this community as well.

I plan on adding it to my existing code that grades stock ....using it to add a value from the Ichimoku standpoint.
 

Chuck

Active member
Got this much done...I think....any advice would be greatly appreciated.

Code:
#Ichimoku Cloud Signal Score
#START

input Conversion_Period = 9;
input minval = 1;
input title = "Conversion Line Periods";

input Base_Period = 26;
input minval = 1;
input title = "Base Line Periods";

input laggingSpan2Period = 52;
input minval = 1;
input title = "Lagging Span 2 Periods";
input displacement = 26;
input minval = 1;
input title = "Displacement";

#FINISH FIRST PASS, NEEDS CHECKING

##-------------- Helpers

donchian(len) => avg(lowest(len);
highest(len))

resolve(src;
default) =>
    if na(src)
        default
    else
        src

##-------------- generate ichimoku data ==

conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)

// NOTE: these senkou spans are for signal generation;
not for plotting. this is
//       due to limitations of pinescript
//
// i.e. Senkou Span A
leadLine1 = offset(avg(conversionLine;
baseLine);
displacement)
// i.e. Senkou Span B
leadLine2 = offset(donchian(laggingSpan2Periods); displacement)


##-------------- plot ichimoku ==

// // i.e. Tenkan Sen (turning line) (blue)
// plot(conversionLine; color=#0496ff;input title="Turning/Conversion Line"; linewidth=3)
// // i.e. Kijun Sen (base/standard line) (red)
// plot(baseLine; color=#991515;input title="Standard/Base Line"; linewidth=3)
// // i.g. Chikou Span (lagging line) (green)
// plot(close; offset = -displacement; color=#459915;input title="Lagging Span"; linewidth=3)

// plot(conversionLine; color=#0496ff;input title="Conversion Line")
// plot(baseLine; color=#991515;input title="Base Line")

// real_leadLine1 = avg(conversionLine; baseLine)
// real_leadLine2 = donchian(laggingSpan2Periods)

// // i.e. Senkou Span A
// p1 = plot(real_leadLine1; offset = displacement; color=green;input title="Lead 1")
// // i.e. Senkou Span B
// p2 = plot(real_leadLine2; offset = displacement; color=red;input title="Lead 2")

// // i.e. Kumo cloud colouring
// fill(p1; p2; color = real_leadLine1 > real_leadLine2 ? green : red)

##-------------- ichimoku cloud signals ==
// source: [URL]http://www.ichimokutrader.com/signals.html[/URL]

##-------------- Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==

// tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
// tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
tk_cross_bull = crossover(conversionLine; baseLine)
tk_cross_bear = crossunder(conversionLine; baseLine)

// get true y-coord of the cross
cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))

tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)

tk_cross_score = resolve(tk_cross_score[1]; 0)

use_tk_cross = input(true;input title="TS Cross")
tk_cross_weight = input(1.0;input title="TS Cross Importance Weight"; type=float; step=0.1)

tk_cross_weak_bullish_points = input(0.5;input title="TS Cross Weak Bullish Points"; type=float; step=0.1)
tk_cross_neutral_bullish_points = input(1.0;input title="TS Cross Neutral Bullish Points"; type=float; step=0.1)
tk_cross_strong_bullish_points = input(2.0;input title="TS Cross Strong Bullish Points"; type=float; step=0.1)

tk_cross_weak_bearish_points = input(-0.5;input title="TS Cross Weak Bearish Points"; type=float; step=0.1)
tk_cross_neutral_bearish_points = input(-1.0;input title="TS Cross Neutral Bearish Points"; type=float; step=0.1)
tk_cross_strong_bearish_points = input(-2.0;input title="TS Cross Strong Bearish Points"; type=float; step=0.1)

##-------------- TK Cross / weak bullish (below kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score

##-------------- TK Cross / neutral bullish (inside kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score

##-------------- TK Cross / strong bullish (above kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score

##-------------- TK Cross / strong bearish (below kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score

##-------------- TK Cross / neutral bearish (inside kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score

##-------------- TK Cross / weak bearish (above kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score


##-------------- Price and Kijun Sen (standard line) Cross ==

//pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
//pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
pk_cross_bull = crossover(close; baseLine)
pk_cross_bear = crossunder(close; baseLine)

cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))

pk_cross_below_kumo = cross_pk_y <= leadLine2[1] and cross_pk_y <= leadLine1[1] and cross_pk_y <= leadLine2 and cross_pk_y <= leadLine1
pk_cross_above_kumo = cross_pk_y >= leadLine2[1] and cross_pk_y >= leadLine1[1] and cross_pk_y >= leadLine2 and cross_pk_y >= leadLine1
pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)

pk_cross_score = resolve(pk_cross_score[1]; 0)

use_pk_cross = input(true;input title="PS Cross")
pk_cross_weight = input(1.0;input title="PS Cross Importance Weight"; type=float; step=0.1)

pk_cross_weak_bullish_points = input(0.5;input title="PS Cross Weak Bullish Points"; type=float; step=0.1)
pk_cross_neutral_bullish_points = input(1.0;input title="PS Cross Neutral Bullish Points"; type=float; step=0.1)
pk_cross_strong_bullish_points = input(2.0;input title="PS Cross Strong Bullish Points"; type=float; step=0.1)

pk_cross_weak_bearish_points = input(-0.5;input title="PS Cross Weak Bearish Points"; type=float; step=0.1)
pk_cross_neutral_bearish_points = input(-1.0;input title="PS Cross Neutral Bearish Points"; type=float; step=0.1)
pk_cross_strong_bearish_points = input(-2.0;input title="PS Cross Strong Bearish Points"; type=float; step=0.1)

##-------------- PK Cross / weak bullish (below kumo) ==
pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score

##-------------- PK Cross / neutral bullish (inside kumo)
pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score

##-------------- PK Cross / strong bullish (above kumo)
pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score

##-------------- PK Cross / strong bearish (below kumo)
pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score

##-------------- PK Cross / neutral bearish (inside kumo)
pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score

##-------------- PK Cross / weak bearish (above kumo)
pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score

##-------------- Kumo Breakouts ==

kumo_bull = (crossover(close; leadLine1) and leadLine1 > leadLine2) or (crossover(close; leadLine2) and leadLine2 > leadLine1)
kumo_bear = (crossunder(close; leadLine2) and leadLine1 > leadLine2) or (crossunder(close; leadLine1) and leadLine2 > leadLine1)

kumo_breakout_score = resolve(kumo_breakout_score[1]; 0)

use_kumo_breakout = input(true;input title="Kumo Breakout")
kumo_breakout_weight = input(1.0;input title="Kumo Breakout Importance Weight"; type=float; step=0.1)

kumo_breakout_bullish_points = input(1.0;input title="Kumo Breakout Bullish Points"; type=float; step=0.1)
kumo_breakout_bearish_points = input(-1.0;input title="Kumo Breakout Bearish Points"; type=float; step=0.1)

price_below_kumo = (close < leadLine2 and close < leadLine1)
price_above_kumo = (close > leadLine2 and close > leadLine1)
price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)

kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score

##-------------- Senkou Span Cross ==
// The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
// NOTE: this cross occurs ahead of the price; since it's displaced to the right; this displacement must be removed

// i.e. Senkou Span A (no displacement)
no_dp_leadLine1 = avg(conversionLine; baseLine)
// i.e. Senkou Span B (no displacement)
no_dp_leadLine2 = donchian(laggingSpan2Periods)
// TODO: debug;remove
// plot(no_dp_leadLine1)
// plot(no_dp_leadLine2)

lead_line_cross_bull = crossover(no_dp_leadLine1; no_dp_leadLine2)
lead_line_cross_bear = crossunder(no_dp_leadLine1; no_dp_leadLine2)
price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
// price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)

span_cross_score = resolve(span_cross_score[1]; 0)

use_span_cross = input(true;input title="Span Cross")
span_cross_weight = input(1.0;input title="Span Cross Importance Weight"; type=float; step=0.1)

span_cross_weak_bullish_points = input(0.5;input title="Span Cross Weak Bullish Points"; type=float; step=0.1)
span_cross_neutral_bullish_points = input(1.0;input title="Span Cross Neutral Bullish Points"; type=float; step=0.1)
span_cross_strong_bullish_points = input(2.0;input title="Span Cross Strong Bullish Points"; type=float; step=0.1)

span_cross_weak_bearish_points = input(-0.5;input title="Span Cross Weak Bearish Points"; type=float; step=0.1)
span_cross_neutral_bearish_points = input(-1.0;input title="Span Cross Neutral Bearish Points"; type=float; step=0.1)
span_cross_strong_bearish_points = input(-2.0;input title="Span Cross Strong Bearish Points"; type=float; step=0.1)

##-------------- Senkou Span Cross / weak bullish (price below kumo) ==
span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score

##-------------- Senkou Span Cross / neutral bullish (price inside kumo) ==
span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score

##-------------- Senkou Span Cross / strong bullish (price above kumo) ==
span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score

##-------------- Senkou Span Cross / weak bearish (price above kumo) ==
span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score

##-------------- Senkou Span Cross / neutral bearish (price inside kumo) ==
span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score

##-------------- Senkou Span Cross / strong bearish (price below kumo) ==
span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score

##-------------- Chikou Span Cross ==
// The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.

past_price = offset(close; displacement)
lag_line_bull_cross = close > close[displacement]
lag_line_bear_cross = close < close[displacement]

// TODO: debug; remove
// plot(close)
// plot(close[displacement]; linewidth=2)

past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)

lag_line_cross_score = resolve(lag_line_cross_score[1]; 0)

use_lag_line = input(true;input title="Lag Line Cross")
lag_line_cross_weight = input(1.0;input title="Lag Line Cross Importance Weight"; type=float; step=0.1)

lag_line_cross_weak_bullish_points = input(0.5;input title="Lag Line Cross Weak Bullish Points"; type=float; step=0.1)
lag_line_cross_neutral_bullish_points = input(1.0;input title="Lag Line Cross Neutral Bullish Points"; type=float; step=0.1)
lag_line_cross_strong_bullish_points = input(2.0;input title="Lag Line Cross Strong Bullish Points"; type=float; step=0.1)

lag_line_cross_weak_bearish_points = input(-0.5;input title="Lag Line Cross Weak Bearish Points"; type=float; step=0.1)
lag_line_cross_neutral_bearish_points = input(-1.0;input title="Lag Line Cross Neutral Bearish Points"; type=float; step=0.1)
lag_line_cross_strong_bearish_points = input(-2.0;input title="Lag Line Cross Strong Bearish Points"; type=float; step=0.1)

##-------------- Chikou Span Cross / weak bullish (price below kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score

##-------------- Chikou Span Cross / neutral bullish (price inside kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score

##-------------- Chikou Span Cross / strong bullish (price above kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score

##-------------- Chikou Span Cross / weak bearish (price above kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score

##-------------- Chikou Span Cross / neutral bearish (price inside kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score

##-------------- Chikou Span Cross / strong bearish (price below kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score

##-------------- lag line releative to cloud ==

use_lag_line_location = input(true;input title="Lag Line Relative to Cloud")

lag_line_location_weight = input(1.0;input title="Lag Line Relative to Cloud Importance Weight"; type=float; step=0.1)

lag_line_location_above_points = input(1.0;input title="Lag Line Above Cloud Points"; type=float; step=0.1)
lag_line_location_inside_points = input(0;input title="Lag Line Inside Cloud Points"; type=float; step=0.1)
lag_line_location_below_points = input(-1.0;input title="Lag Line Below Cloud Points"; type=float; step=0.1)

lag_line_placement_score = resolve(lag_line_placement_score[1]; 0)

lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score

##-------------- price relative to cloud ==

price_placement_score = resolve(price_placement_score[1]; 0)

use_price_location = input(true;input title="Price Relative to Cloud")
price_location_weight = input(1.0;input title="Price Relative to Cloud Importance Weight"; type=float; step=0.1)

price_location_above_points = input(1.0;input title="Price Above Cloud Points"; type=float; step=0.1)
price_location_inside_points = input(0;input title="Price Inside Cloud Points"; type=float; step=0.1)
price_location_below_points = input(-1.0;input title="Price Below Cloud Points"; type=float; step=0.1)

price_below_kumo := (close < leadLine2 and close < leadLine1)
price_above_kumo := (close > leadLine2 and close > leadLine1)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)

price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score

##-------------- plot score ==

total_score = (use_tk_cross * tk_cross_weight * tk_cross_score) + (use_pk_cross * pk_cross_weight * pk_cross_score) + (use_kumo_breakout * kumo_breakout_weight * kumo_breakout_score) + (use_span_cross * span_cross_weight * span_cross_score) + (use_lag_line * lag_line_cross_weight * lag_line_cross_score) + (use_price_location * price_location_weight * price_placement_score)
total_score := total_score + (use_lag_line_location * lag_line_location_weight * lag_line_placement_score)

plot(0; style=line;input title="Base")
plot(total_score; linewidth=3;input title="Total Score")



// debug
//plotshape((should_plot_tk_nb_inside_kumo) ? leadLine1 : na; color=black; style=shape.arrowup; location=location.belowbar; size=size.huge)
 

markos

Well-known member
VIP
@Nick Check this portion:

8qshtVi.png
@Nick Ben is correct. Above is where you should look.
I looked around and also found this:
tHnXNC4.png

Code:
#This indicator uses the Index New highs and Index New lows to help guage overall Market sentiment. 
It's a leading indicator.

# Index New High - New Low Indicator
# Mobius 2015

declare lower;
input Symb = {default "NYSE", "NASDQ", "AMEX", "ARCA", "ETF"};
input length = 10;
input OverSold = 20;
input OverBought = 80;
Input AvgType = AverageType.Hull;

def agg = AggregationPeriod.Day;
def NYSEH  = close(Symbol = "$NYHGH", period = agg);
def NYSEL  = close(Symbol = "$NYLOW", period = agg);
def NASDQH = close(Symbol = "$NAHGH", period = agg);
def NASDQL = close(Symbol = "$NALOW", period = agg);
def AMEXH  = close(Symbol = "$AMHGH", period = agg);
def AMEXL  = close(Symbol = "$AMLOW", period = agg);
def ARCAH  = close(Symbol = "$ARHGH", period = agg);
def ARCAL  = close(Symbol = "$ARLOW", period = agg);
def ETFH   = close(Symbol = "$ETFHGH", period = agg);
def ETFL   = close(Symbol = "$ETFLOW", period = agg);
def P;
Switch (Symb){ 
case "NYSE":
P = NYSEH / (NYSEH + NYSEL) * 100;
case "NASDQ":
P = NASDQH / (NASDQH + NASDQL) * 100;
case "AMEX":
P = AMEXH / (AMEXH + AMEXL) * 100;
case "ARCA":
P = ARCAH / (ARCAH + ARCAL) * 100;
case "ETF":
P = ETFH / (ETFH + ETFL) * 100;
}
def price = if isNaN(P) then price[1] else P;
plot data = if isNaN(close) then double.nan else price;
data.EnableApproximation();
data.SetDefaultColor(Color.Cyan);
plot avg = MovingAverage(AvgType, data, length);
avg.EnableApproximation();
avg.AssignValueColor(if between(avg, OverSold, OverBought)
                     then Color.yellow
                     else if avg >= OverBought
                          then Color.Green
                          else Color.Red);
avg.SetLineWeight(2);
plot OB = if isNaN(close) then double.nan else OverBought;
OB.SetDefaultColor(Color.Red);
plot OS = if isNaN(close) then double.nan else OverSold;
OS.SetDefaultColor(Color.Green);
plot neutral = if isNaN(close) then double.nan else 50;
neutral.SetdefaultColor(Color.Dark_Gray);
addCloud(0, OS, CreateColor(250,0,0), CreateColor(250,0,0));
addCloud(OS, neutral, createColor(50,0,0), createColor(50,0,0));
addCloud(neutral, OB, createColor(0,50,0), createColor(0,50,0));
addCloud(OB, 100, CreateColor(0,250,0), createColor(0,250,0));
Addlabel(yes,"NewHi vs NewLo " + avg,color.whiTE);
#
# End High - Low Index
 

markos

Well-known member
VIP
@Chuck Try to build off of this. Sorry, can't help further, not much Ichi out there.

Code:
# Ichimoku Demystifyed

# Nothing but the name have been changed to ones that have meaning

# Mobius



input Short_period = 9;

input Long_period = 26;



def h = high;

def l = low;

def c = close;

plot Short_Range = (Highest(h, Short_period) + Lowest(l, Short_period)) / 2;

plot Long_Range = (Highest(h, Long_period) + Lowest(l, Long_period)) / 2;

plot "Avg SR_LR" = (Short_Range[Long_period] + Long_Range[Long_period]) / 2;

plot "Avg SuperRange" = (Highest(h[Long_period], 2 * Long_period) + Lowest(l[Long_period], 2 * Long_period)) / 2;

plot DetrendedClose = c[-Long_period];



Short_Range.SetDefaultColor(GetColor(1));

Long_Range.SetDefaultColor(GetColor(2));

"Avg SR_LR".SetDefaultColor(GetColor(3));

"Avg SuperRange".SetDefaultColor(GetColor(4));

DetrendedClose.SetDefaultColor(GetColor(5));



DefineGlobalColor("Bullish", Color.YELLOW);

DefineGlobalColor("Bearish", Color.RED);

AddCloud("Avg SR_LR", "Avg SuperRange", globalColor("Bullish"), globalColor("Bearish"));
Good Luck
 

Bladeof300

New member
Can someone either tell me how to code this myself or translate it to thinkscript for me.

Code:
// © racer8
//@version=4
study("Monster Breakout Index",shorttitle="MBI")

per = input(12,title="Period")
H = highest(hl2,per)
hi = H[1]
L = lowest(hl2,per)
lo = L[1]
cl = close
ind = cl>hi? 1 : cl<lo? -1 : 0
c = ind>0? color.lime : ind<0? color.red : color.gray
plot (ind,color=c,style=plot.style_columns)
 

BenTen

Administrative
Staff
VIP
@Bladeof300 This one is really easy. If you want to learn, you can compare the thinkscript version below to the code posted above.

Code:
# Monster Breakout Index (MBI)
# Converted from https://www.tradingview.com/script/nFnRGiCl-Monster-Breakout-Index/
# Assembled by BenTen at UseThinkScript.com

declare lower;

input per = 12;
def H = highest(hl2,per);
def hi = H[1];
def L = lowest(hl2,per);
def lo = L[1];
def cl = close;
def ind = if cl>hi then 1 else if cl<lo then -1 else 0;
plot histogram = ind;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.AssignValueColor(if ind>0 then color.lime else if ind<0 then color.red else color.gray);
 

krish_555

New member
VIP
this indicator look very good.. i've followed for a while is have a good percent of winning trades.. if someone here can convert it into TOS, will be awesome for everyone here. the indicator is Call it: BREAK OUT TRADE FOLLOWER. here is the Trading View Code:

Code:
//@version=4

// Revision:        1
// Author:          @millerrh
// Strategy:  Enter long when recent swing high breaks out, using recent swing low as stop level.  Move stops up as higher lows print to act
// as trailing stops.  Ride trend as long as it is there and the higher lows aren't breached.
// Conditions/Variables
//    1. Can place a user-defined percentage below swing low and swing high to use as a buffer for your stop to help avoid stop hunts
//    2. Can add a filter to only take setups that are above a user-defined moving average (helps avoid trading counter trend)
//    3. Manually configure which dates to back test
//    4. Color background of backtested dates - allows for easier measuring buy & hold return of time periods that don't go up to current date  


// === CALL STRATEGY/STUDY, PROGRAMATICALLY ENTER STRATEGY PARAMETERS HERE SO YOU DON'T HAVE TO CHANGE THEM EVERY TIME YOU RUN A TEST ===
// (STRATEGY ONLY) - Comment out srategy() when in a study()
//strategy("Breakout Trend Follower", overlay=true, initial_capital=10000, currency='USD',
//   default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
// (STUDY ONLY) - Comment out study() when in a strategy()
study("Breakout Trend Follower", overlay=true)

// === BACKTEST RANGE ===
From_Year  = input(defval = 2019, title = "From Year")
From_Month = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
From_Day   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
To_Year    = input(defval = 9999, title = "To Year")
To_Month   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
To_Day     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
Start  = timestamp(From_Year, From_Month, From_Day, 00, 00)  // backtest start window
Finish = timestamp(To_Year, To_Month, To_Day, 23, 59)        // backtest finish window

// A switch to control background coloring of the test period - Use for easy visualization of backtest range and manual calculation of
// buy and hold (via measurement) if doing prior periods since value in Strategy Tester extends to current date by default
testPeriodBackground = input(title="Color Background - Test Period?", type=input.bool, defval=false)
testPeriodBackgroundColor = testPeriodBackground and (time >= Start) and (time <= Finish) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=95)

// == FILTERING ==
// Inputs
useMaFilter = input(title = "Use MA for Filtering?", type = input.bool, defval = false)
maType = input(defval="SMA", options=["EMA", "SMA"], title = "MA Type For Filtering")
maLength   = input(defval = 200, title = "MA Period for Filtering", minval = 1)

// Declare function to be able to swap out EMA/SMA
ma(maType, src, length) =>
    maType == "EMA" ? ema(src, length) : sma(src, length) //Ternary Operator (if maType equals EMA, then do ema calc, else do sma calc)
maFilter = ma(maType, close, maLength)
plot(maFilter, title = "Trend Filter MA", color = color.green, linewidth = 3, style = plot.style_line, transp = 50)

// Check to see if the useMaFilter check box is checked, this then inputs this conditional "maFilterCheck" variable into the strategy entry
maFilterCheck = if useMaFilter == true
    maFilter
else
    0

// === PLOT SWING HIGH/LOW AND MOST RECENT LOW TO USE AS STOP LOSS EXIT POINT ===
// Inputs
//pvtLenL       = input(3, minval=1, title="Pivot Length Left Hand Side") //use if you want to change this to an input
//pvtLenR       = input(3, minval=1, title="Pivot Length Right Hand Side") //use if you want to change this to an input
pvtLenL       = 3
pvtLenR       = 3

// Get High and Low Pivot Points
pvthi_ = pivothigh(high, pvtLenL, pvtLenR)
pvtlo_ = pivotlow(low, pvtLenL, pvtLenR)

// Force Pivot completion before plotting.
Shunt = 1 //Wait for close before printing pivot? 1 for true 0 for flase
maxLvlLen = 0 //Maximum Extension Length
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]

// Count How many candles for current Pivot Level, If new reset.
counthi = barssince(not na(pvthi))
countlo = barssince(not na(pvtlo))

pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = change(pvthis) != 0 ? na : color.maroon
lopc = change(pvtlos) != 0 ? na : color.green

// Display Pivot lines
plot((maxLvlLen == 0 or counthi < maxLvlLen) ? pvthis : na, color=hipc, transp=0, linewidth=1, offset=-pvtLenR-Shunt, title="Top Levels")
plot((maxLvlLen == 0 or countlo < maxLvlLen) ? pvtlos : na, color=lopc, transp=0, linewidth=1, offset=-pvtLenR-Shunt, title="Bottom Levels")
plot((maxLvlLen == 0 or counthi < maxLvlLen) ? pvthis : na, color=hipc, transp=0, linewidth=1, offset=0, title="Top Levels 2")
plot((maxLvlLen == 0 or countlo < maxLvlLen) ? pvtlos : na, color=lopc, transp=0, linewidth=1, offset=0, title="Bottom Levels 2")

// Stop Levels
stopBuff = input(0.0, minval=-2, title="Stop Loss Buffer off Swing Low (%)")
stopPerc = stopBuff*.01 // Turn stop buffer input into a percentage
stopLevel = valuewhen(pvtlo_, low[pvtLenR], 0) //Stop Level at Swing Low
stopLevel2 = stopLevel - stopLevel*stopPerc  // Stop Level with user-defined buffer to avoid stop hunts and give breathing room
plot(stopLevel2, style=plot.style_line, color=color.orange, show_last=1, linewidth=1, transp=50, trackprice=true)
buyLevel = valuewhen(pvthi_, high[pvtLenR], 0) //Buy level at Swing High
buyLevel2 = buyLevel + buyLevel*stopPerc // Buy-stop level with user-defined buffer to avoid stop hunts and give breathing room
plot(buyLevel2, style=plot.style_line, color=color.aqua, show_last=1, linewidth=1, transp=50, trackprice=true)

// Conditions for entry and exit
buySignal = high > buyLevel2
buy = buySignal and time > Start and time < Finish and buyLevel2 > maFilterCheck // All these conditions need to be met to buy
sellSignal = low < stopLevel2 // Code to act like a stop-loss for the Study

// (STRATEGY ONLY) Comment out for Study
// strategy.entry("Long", strategy.long, stop = buyLevel2, when = time > Start and time < Finish and buyLevel2 > maFilterCheck)
// strategy.exit("Exit Long", from_entry = "Long", stop=stopLevel2)

// == (STUDY ONLY) Comment out for Strategy ==
// Check if in position or not
inPosition = bool(na)
inPosition := buy[1] ? true : sellSignal[1] ? false : inPosition[1]
flat = bool(na)
flat := not inPosition
buyStudy = buy and flat
sellStudy = sellSignal and inPosition
//Plot indicators on chart and set up alerts for Study
plotshape(buyStudy, style = shape.triangleup, location = location.belowbar, color = #1E90FF, text = "Buy")
plotshape(sellStudy, style = shape.triangledown, location = location.abovebar, color = #EE82EE, text = "Sell")
alertcondition(buyStudy, title='Breakout Trend Follower Buy', message='Breakout Trend Follower Buy')
alertcondition(sellStudy, title='Breakout Trend Follower Sell', message='Breakout Trend Follower Sell')

// Color background when trade active (for easier visual on what charts are OK to enter on)
tradeBackground = input(title="Color Background for Trades?", type=input.bool, defval=true)
tradeBackgroundColor = tradeBackground and inPosition ? #00FF00 : na
bgcolor(tradeBackgroundColor, transp=95)
noTradeBackgroundColor = tradeBackground and flat ? #FF0000 : na
bgcolor(noTradeBackgroundColor, transp=90)
@FOTM_8888 I to tried this indicator in TradingView this looks great so far. But, sometimes i see fakeout signals. do you have updated script for this ?? as I see this is from Aug. If so, could you please share the latest script.

Thank you
 

barbaros

Active member
VIP
@FOTM_8888 I to tried this indicator in TradingView this looks great so far. But, sometimes i see fakeout signals. do you have updated script for this ?? as I see this is from Aug. If so, could you please share the latest script.

Thank you
Keep in mind this uses the pivotHigh and pivotLow functions in TradingView. These functions are laggy indicators. Meaning the pivot points are not drawn until x many bars passed to the function as a parameters. In this script pivot points are lagging 3 bars. What I have not looked at it how this is used in the code to generate buy or sell signals.
 

krish_555

New member
VIP
Keep in mind this uses the pivotHigh and pivotLow functions in TradingView. These functions are laggy indicators. Meaning the pivot points are not drawn until x many bars passed to the function as a parameters. In this script pivot points are lagging 3 bars. What I have not looked at it how this is used in the code to generate buy or sell signals.
@barbaros so, do you use any other indicators ? as you are saying this is lagging three candles.
 

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Similar threads

Top