0% found this document useful (0 votes)
80 views5 pages

NSDT 2

This Pine Script code is designed for trading analysis, specifically for the asset 'GOLD', utilizing moving averages for filtering and pivot points for entry and exit signals. It includes customizable inputs for moving average types, lengths, and conditions for buy/sell signals, alongside visual indicators for market trends. The script also incorporates alerts for buy and sell conditions based on the defined trading strategy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views5 pages

NSDT 2

This Pine Script code is designed for trading analysis, specifically for the asset 'GOLD', utilizing moving averages for filtering and pivot points for entry and exit signals. It includes customizable inputs for moving average types, lengths, and conditions for buy/sell signals, alongside visual indicators for market trends. The script also incorporates alerts for buy and sell conditions based on the defined trading strategy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// This Pine Script� code is subject to the terms of the Mozilla Public License 2.

0
at https://mozilla.org/MPL/2.0/
// � peppetrombetta

//@version=6
indicator('GOLD', overlay = true)

// == FILTERING ==
// Inputs
useMaFilter = input.bool(title = 'Use MA for Filtering?', defval = true, tooltip =
'Signals will be ignored when price is under this moving average. The intent is to
keep you out of bear periods and only buying when price is showing strength.',
group = 'Breakout Trend Follower')
maType = input.string(defval = 'SMA', options = ['EMA', 'SMA'], title = 'MA Type
For Filtering', group = 'Breakout Trend Follower')
maLength = input.int(defval = 50, title = 'MA Period for Filtering', minval = 1,
group = 'Breakout Trend Follower')

// Declare function to be able to swap out EMA/SMA


ma(maType, src, length) =>
maType == 'EMA' ? ta.ema(src, length) : ta.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.new(color.green, 50),
linewidth = 3, style = plot.style_line)

// 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 ===
// Change these values to adjust the look back and look forward periods for your
swing high/low calculations
pvtLenL = 3
pvtLenR = 3

// Get High and Low Pivot Points


pvthi_ = ta.pivothigh(high, pvtLenL, pvtLenR)
pvtlo_ = ta.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 = ta.barssince(not na(pvthi))
countlo = ta.barssince(not na(pvtlo))

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

// Display Pivot lines


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

// Stop Levels
stopLevel = ta.valuewhen(bool(pvtlo_), low[pvtLenR], 0) //Stop Level at Swing Low
plot(stopLevel, style = plot.style_line, color = color.new(color.orange, 50),
show_last = 1, linewidth = 1, trackprice = true)
buyLevel = ta.valuewhen(bool(pvthi_), high[pvtLenR], 0) //Buy level at Swing High
plot(buyLevel, style = plot.style_line, color = color.new(color.blue, 50),
show_last = 1, linewidth = 1, trackprice = true)

// Conditions for entry and exit


buySignal = high > buyLevel
buy = buySignal and buyLevel > maFilterCheck // All these conditions need to be met
to buy
sellSignal = low < stopLevel // 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')
// alertcondition(buyStudy or sellStudy, title='Breakout Trend Follower Buy/Sell',
message='Breakout Trend Follower Buy/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)

// // 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)

length = input.int(9, title = 'Length', group = 'OBOS')


ys1 = (high + low + close * 2) / 4
rk3 = ta.ema(ys1, length)
rk4 = ta.stdev(ys1, length)
rk5 = (ys1 - rk3) * 100 / rk4
rk6 = ta.ema(rk5, length)
up = ta.ema(rk6, length)
down = ta.ema(up, length)
Oo = up < down ? up : down
Hh = Oo
Ll = up < down ? down : up
Cc = Ll
// barcolor2=Oo[1]<Oo and Cc<Cc[1]?color.blue:up>down?color.green:color.red
barcolor2 = Oo[1] < Oo and Cc < Cc[1] ? 0 : up > down ? 1 : -1
// plotcandle(Oo,Hh,Ll,Cc,color=barcolor2)

len = input.int(title = 'Length', defval = 14, group = 'DI histo + adx')


th = input.int(title = 'threshold', defval = 25, group = 'DI histo + adx')

TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))),


math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high -
nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ?
math.max(nz(low[1]) - low, 0) : 0
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len +
TrueRange
SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) -
nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) -
nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100


DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)
ve = DIPlus > DIMinus + DIPlus * 10 / 100
ro = DIMinus > DIPlus + DIMinus * 10 / 100 and ADX > 11 and ADX <= 25
xve = DIPlus > DIMinus + DIPlus * 10 / 100 and ADX > 22
xro = DIMinus > DIPlus + DIMinus * 10 / 100 and ADX > 22
fl = DIPlus - DIMinus < math.abs(10) and ADX < 15

di = 25 + DIPlus - DIMinus
//vam=sma(1.2*(ADX-ADX[len]),3)+25
//vad=(vam+2*vam[1]+2*vam[2]+vam[3])/6

cy = di >= 25
// //plot(DIPlus, color=lime,linewidth=2, title="DI+")
// //plot(DIMinus, color=red, linewidth=2,title="DI-")
// plot(ADX, color=color.orange, linewidth=1, transp=0, title="ADX")
// //plot(vam,title="velocita",linewidth=1,transp=50,color=lime)
// plot(di, style=plot.style_histogram, linewidth=4, color=cy, transp=50,
histbase=25, title="DI")
// hline(th, color=color.gray)

//HAMA

//The follow gradient code is taken from the Pinecoders Gradient Framework example.
//https://www.tradingview.com/script/hqH4YIFa-Color-Gradient-Framework-PineCoders/
//Pro Advance/Decline Gradient

////////////////////
//GRADIENT AREA
////////////////////
f_c_gradientAdvDecPro(_source, _center, _steps, _c_bearWeak, _c_bearStrong,
_c_bullWeak, _c_bullStrong) =>
var float _qtyAdvDec = 0.
var float _maxSteps = math.max(1, _steps)
bool _xUp = ta.crossover(_source, _center)
bool _xDn = ta.crossunder(_source, _center)
float _chg = ta.change(_source)
bool _up = _chg > 0
bool _dn = _chg < 0
bool _srcBull = _source > _center
bool _srcBear = _source < _center
_qtyAdvDec := _srcBull ? _xUp ? 1 : _up ? math.min(_maxSteps, _qtyAdvDec + 1) :
_dn ? math.max(1, _qtyAdvDec - 1) : _qtyAdvDec : _srcBear ? _xDn ? 1 : _dn ?
math.min(_maxSteps, _qtyAdvDec + 1) : _up ? math.max(1, _qtyAdvDec - 1) :
_qtyAdvDec : _qtyAdvDec
var color _return = na
_return := _srcBull ? color.from_gradient(_qtyAdvDec, 1, _maxSteps,
_c_bullWeak, _c_bullStrong) : _srcBear ? color.from_gradient(_qtyAdvDec, 1,
_maxSteps, _c_bearWeak, _c_bearStrong) : _return
_return

//MA TYPES
mat(source, length, type) =>
type == 'SMA' ? ta.sma(source, length) : type == 'EMA' ? ta.ema(source, length)
: type == 'RMA' ? ta.rma(source, length) : type == 'WMA' ? ta.wma(source, length) :
type == 'VWMA' ? ta.vwma(source, length) : type == 'HMA' ? ta.hma(source, length) :
type == 'TMA' ? ta.sma(ta.sma(source, length), length) : na

//INPUTS
bull =color.rgb(0, 255, 0) //input(color.rgb(0, 255, 0), title='Bull Color')
bear = color.rgb(255, 0, 0)//input(color.rgb(255, 0, 0), title='Bear Color')
neutral = color.rgb(255, 255, 0, 0)//input(, title='Neutral Color')
show_ma = true
ma_type = 'WMA'
ma_source = close
ma_length = 55
UseGradient =true// input(true, title='Use Gradient Colors')
stepn =5// input(5, title='Max Gradient Steps')
ma = mat(ma_source, ma_length, ma_type)
col = f_c_gradientAdvDecPro(ma, ta.ema(ma, 3), stepn, neutral, bear, neutral, bull)

long = cy and barcolor2 == 1 and buyStudy and col==bull


short = not cy and barcolor2 == -1 and sellStudy and col==bear

plotshape(long, title = 'Buy', text = 'Buy', location = location.belowbar, style =


shape.labelup, size = size.tiny, color = color.new(color.green, 0), textcolor =
color.new(color.white, 0))
plotshape(short, title = 'Sell', text = 'Sell', location = location.abovebar, style
= shape.labeldown, size = size.tiny, color = color.new(color.red, 0), textcolor =
color.new(color.white, 0))

alertcondition(long, 'Buy')
alertcondition(short, 'Sell')

You might also like