Trading Indicator 2
Trading Indicator 2
Trading Indicator 2
//------------------------------------------------------------------------------
// Settings for UT Bot Alerts
//------------------------------------------------------------------------------
a = input(1, group="UT Bot", title='Key Value. \'This changes the sensitivity\'')
c = input(10, group="UT Bot", title='ATR Period')
h_ut = input(false, group="UT Bot", title='Signals from Heikin Ashi Candles')
xATR = ta.atr(c)
nLoss = a * xATR
xATRTrailingStop = 0.0
iff_1 = src_ut > nz(xATRTrailingStop[1], 0) ? src_ut - nLoss : src_ut + nLoss
iff_2 = src_ut < nz(xATRTrailingStop[1], 0) and src_ut[1] < nz(xATRTrailingStop[1],
0) ? math.min(nz(xATRTrailingStop[1]), src_ut + nLoss) : iff_1
xATRTrailingStop := src_ut > nz(xATRTrailingStop[1], 0) and src_ut[1] >
nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src_ut - nLoss) :
iff_2
pos_ut = 0
iff_3 = src_ut[1] > nz(xATRTrailingStop[1], 0) and src_ut < nz(xATRTrailingStop[1],
0) ? -1 : nz(pos_ut[1], 0)
pos_ut := src_ut[1] < nz(xATRTrailingStop[1], 0) and src_ut >
nz(xATRTrailingStop[1], 0) ? 1 : iff_3
ema_ut = ta.ema(src_ut, 1)
above_ut = ta.crossover(ema_ut, xATRTrailingStop)
below_ut = ta.crossover(xATRTrailingStop, ema_ut)
//------------------------------------------------------------------------------
// Settings for Nadaraya-Watson Envelope
//------------------------------------------------------------------------------
length_nwe = input.float(500, group="Nadaraya-Watson Envelope [LuxAlgo]",
title='Window Size', maxval=500, minval=0)
h_nwe = input.float(8, group="Nadaraya-Watson Envelope [LuxAlgo]",
title='Bandwidth')
mult_nwe = input.float(3, group="Nadaraya-Watson Envelope [LuxAlgo]")
src_nwe = input.source(close, group="Nadaraya-Watson Envelope [LuxAlgo]",
title='Source')
if barstate.isfirst
for i = 0 to length_nwe/k_nwe-1
array.push(upper_nwe, line.new(na, na, na, na))
array.push(lower_nwe, line.new(na, na, na, na))
//----
line up_nwe = na
line dn_nwe = na
//----
cross_up_nwe = 0.
cross_dn_nwe = 0.
if barstate.islast
y_nwe = array.new_float(0)
sum_e_nwe = 0.
for i = 0 to length_nwe-1
sum_nwe = 0.
sumw_nwe = 0.
for j = 0 to length_nwe-1
w_nwe = math.exp(-(math.pow(i-j,2)/(h_nwe*h_nwe*2)))
sum_nwe += src_nwe[j]*w_nwe
sumw_nwe += w_nwe
y2_nwe = sum_nwe/sumw_nwe
sum_e_nwe += math.abs(src_nwe[i] - y2_nwe)
array.push(y_nwe, y2_nwe)
mae_nwe = sum_e_nwe/length_nwe*mult_nwe
for i = 1 to length_nwe-1
y2_nwe = array.get(y_nwe, i)
y1_nwe = array.get(y_nwe, i-1)
//----
var tb_nwe = table.new(position.top_right, 1, 1
, bgcolor = #35202b)
////////////////=///////////
coeff = input.float(1, group= 'AlphaTrend', title = 'Multiplier', step=0.1)
AP = input(14, 'Common Period')
ATR = ta.sma(ta.tr, AP)
src = input(close)
showsignalsk = input(title='Show Signals?', defval=true)
novolumedata = input(title='Change calculation (no volume data)?', defval=false)
upT = low - ATR * coeff
downT = high + ATR * coeff
AlphaTrend = 0.0
AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT
< nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ?
nz(AlphaTrend[1]) : downT
K1 = ta.barssince(buySignalk)
K2 = ta.barssince(sellSignalk)
O1 = ta.barssince(buySignalk[1])
O2 = ta.barssince(sellSignalk[1])
//
atrMultiplier = input.float(title='ATR Band Scale Factor', defval=2.5, step=0.1,
minval=0.01, group="ATR Bands Standard Settings", tooltip="Scaling factor (aka
multiplier) for the ATR to use for plotting the ATR bands. " +
"This will usually be between 1 and 3.\n\nDefault: 2.5")
// On second thought, I'm going to nix this setting and force it to be the "close"
source. Having the ability to offset based on the wicks was a nice idea, but
doesn't really seem to have any notable practical application.
atrSourceRef = "close"
//atrSourceRef = input.string(title='ATR Upper Offset Source', defval="close",
options=["close","wicks"], group="ATR Bands Standard Settings", tooltip="This
setting determines the offset point for ATR bands. " +
// "The default value 'close' should be your go-to, but 'wicks'
might provide a bit more breathing room in securities that tend to have large
wicks.")
//
// See above - these are deprecated and no longer used...
//
// atrMultiplierLower = input.float(title='ATR Lower Band Scale Factor',
defval=2.5, step=0.1, minval=0.01, group="ATR Lower Band Settings",
tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR
bands. " +
// "This will usually be between 1 and 3.")
// srcLower = input.source(title='ATR Lower Offset Source', defval=close,
group="ATR Lower Band Settings", tooltip="This setting determines the offset point
for ATR bands. " +
// "For this band, 'low' and 'close' (default) are generally
the most appropriate values.")
//
//
// Take-Profit band settings
showTPBands = input.bool(title="Show opposite bands for take-profit zones",
defval=false, tooltip="If enalbled, the existing ATR bands will be treated as
'stop-loss' bands, and 'take-profit' bands will be plotted " +
"to depict potential take-profit targets that are scaled based
on the 'stop-loss' band and an additional reward/risk scaling factor (see below).\
n\nDefault: Unchecked", group="Take-Profit Settings")
tpScaleFactor = input.float(title="Take-Profit Scale Factor", defval=1.5, minval=1,
step=0.1, tooltip="This is a secondary scaling factor used based on the 'stop-loss'
ATR bands to calculate and plot a potential take-profit target.\n\n" +
"The easiest way to think of this is as a desired reward/risk
ratio, where the primary ATR Bands represent the risk/stop-loss.\n\nDefault: 1.5")
//
//
// As an added bonus, give the option to plot a table containing exact figures for
all of the bands...
// Functional settings
showTable = input.bool(title="Show Table for Stops and Targets", defval=false,
group="Table Settings", tooltip="If enabled, a table will be placed on-chart
showing exact values for both stop bands, as well as optional take-profit
bands/targets.\n\n" +
"Note: Take-profit values are based on the 'Take-Profit Scale
Factor' setting above.\n\nDefault: Unchecked")
allowTableRepainting = input.bool(title="Allow Table Repainting", defval=false,
group="Table Settings", tooltip="If enabled, table data will show real-time values,
which will inherently repaint. This may be desirable for people preparing to enter
prior " +
"to candle close, but should be used with extreme caution.\n\
nDefault: Unchecked")
showTPinTable = input.bool(title="Include additional rows/columns to display take-
profit values.", defval=false, group="Table Settings", tooltip="If enabled,
additional table rows/columns will be drawn and populated with take-profit
band/target values.\n\n" +
"Note: Take-profit values are based on the 'Take-Profit Scale
Factor' setting above.\n\nDefault: Unchecked")
//
// Display settings
alignTableVertically = input.bool(title="Align Table Vertically", defval=true,
group="Table Settings", tooltip="If enabled, the table will be re-aligned to
display vertically (headers to the left) instead of horizontally (headers on top).\
n\nDefault: Checked")
tablePosition = input.string(title="Table Location", defval="Bottom Right",
options=["Top Right","Top Left","Top Center","Middle Right","Middle Left","Middle
Center","Bottom Right","Bottom Left","Bottom Center"], group="Table Settings",
tooltip='This setting controls the position on the chart where
the table will be located.\n\nDefault: Bottom Right')
tableColor = input.color(title="Table Color: ", defval=color.rgb(0, 175, 200, 20),
group="Table Settings", inline="A")
tableTextHeaderColor = input.color(title="Table Header Color: ",
defval=color.rgb(255, 255, 0, 0), group="Table Settings", tooltip="These settings
determine the colors used for the table cell borders/outlines, and the text inside
the table cells used as data headers.", inline="A")
tableTextColor = input.color(title="Table Text Color: ", defval=color.rgb(255, 255,
255, 0), group="Table Settings", tooltip="This setting determines the color used
for the text inside the table cells.", inline="B")
// tableTooltipColor = input.color(title="Table Tooltip Color: ",
defval=color.rgb(255, 75, 255, 0), group="Table Display Settings", tooltip="This
setting determines the color used for any cell tooltips.") // Not used
tableLongBGColor = input.color(title="Table Background Color - Long: ",
defval=color.rgb(0, 255, 0, 90), group="Table Settings", inline="C")
tableShortBGColor = input.color(title="Short: ", defval=color.rgb(255, 0, 0, 80),
group="Table Settings", tooltip="These settings determine the background fill
colors used for long/short position info.", inline="C")
// Functions
//
// Function to convert the input "source" to a proper "source"
getBandOffsetSource(srcIn, isUpperBand) =>
// Initialize the return to our fail-safe 'close', which is also the default
input, then update thru the switch statement
ret = close
switch srcIn
"close" => ret := close
"wicks" => ret := isUpperBand ? high : low
=> ret := close
ret
//
// Function to convert table position input to a an appropriate argument
getTablePosition(posIn) =>
posOut = position.bottom_right
switch (posIn)
"Top Right" => posOut := position.top_right
"Top Left" => posOut := position.top_left
"Top Center" => posOut := position.top_center
"Middle Right" => posOut := position.middle_right
"Middle Left" => posOut := position.middle_left
"Middle Center" => posOut := position.middle_center
"Bottom Right" => posOut := position.bottom_right
"Bottom Left" => posOut := position.bottom_left
"Bottom Center" => posOut := position.bottom_center
=> posOut := position.bottom_right
posOut
// ATR
atr = ta.atr(atrPeriod)
scaledATR = atr * atrMultiplier
upperATRBand = getBandOffsetSource(atrSourceRef, true) + scaledATR
lowerATRBand = getBandOffsetSource(atrSourceRef, false) - scaledATR
//
// Since we can calcualte ATR bands based on either close or wicks, we need to be
sure to normalize the true distance
// from the close to the "stop band" before we can then apply our take-profit
scaler and calculate the TP bands...
scaledTPLong = close + ((close - lowerATRBand) * tpScaleFactor)
scaledTPShort = close - ((upperATRBand - close) * tpScaleFactor)
// TP band plots
plot(showTPBands ? scaledTPLong : na, title="NEGREITAI", color=color.rgb(255, 255,
255, 80), linewidth=1)
plot(showTPBands ? scaledTPShort : na, title="NEGREITAI", color=color.rgb(255, 255,
0, 80), linewidth=1)
//
// Horizontal orientation
else
// Define the Title/Header cells
table.cell(atrTable, 0, 0, text="Long ATR Stop",
text_color=tableTextHeaderColor, bgcolor=tableLongBGColor, tooltip="Test")
table.cell(atrTable, 1, 0, text="Long ATR Stop Dist",
text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
if (showTPinTable)
table.cell(atrTable, 2, 0, text="Long ATR TP",
text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
// If the TP scale factor is exactly 1, we can nix the TP
distance columns as it will be exactly the same as the stop distance.
if (tpScaleFactor != 1)
table.cell(atrTable, 3, 0, text="Long ATR TP Dist",
text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
table.cell(atrTable, 4, 0, text="Short ATR Stop",
text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
table.cell(atrTable, 5, 0, text="Short ATR Stop Dist",
text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
if (showTPinTable)
table.cell(atrTable, 6, 0, text="Short ATR TP",
text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
// If the TP scale factor is exactly 1, we can nix the TP
distance columns as it will be exactly the same as the stop distance.
if (tpScaleFactor != 1)
table.cell(atrTable, 7, 0, text="Short ATR TP Dist",
text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
//
// Now for table values for each header...
// Start with Long position...
table.cell(atrTable, 0, 1, text=str.tostring(allowTableRepainting ?
lowerATRBand : lowerATRBand[1], format.mintick), text_color=tableTextColor,
bgcolor=tableLongBGColor, tooltip="Test")
table.cell(atrTable, 1, 1,
text=str.tostring(math.round_to_mintick(allowTableRepainting ? close - lowerATRBand
: close[1] - lowerATRBand[1])), text_color=tableTextColor,
bgcolor=tableLongBGColor)
if (showTPinTable)
table.cell(atrTable, 2, 1, text=str.tostring(allowTableRepainting
? scaledTPLong : scaledTPLong[1], format.mintick), text_color=tableTextColor,
bgcolor=tableLongBGColor)
// If the TP scale factor is exactly 1, we can nix the TP
distance columns as it will be exactly the same as the stop distance.
if (tpScaleFactor != 1)
table.cell(atrTable, 3, 1,
text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR *
tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor,
bgcolor=tableLongBGColor)
// Now the Short position...
table.cell(atrTable, 4, 1, text=str.tostring(allowTableRepainting ?
upperATRBand : upperATRBand[1], format.mintick), text_color=tableTextColor,
bgcolor=tableShortBGColor, tooltip="Test 2")
table.cell(atrTable, 5, 1,
text=str.tostring(math.round_to_mintick(allowTableRepainting ? upperATRBand - close
: upperATRBand[1] - close[1])), text_color=tableTextColor,
bgcolor=tableShortBGColor)
if (showTPinTable)
table.cell(atrTable, 6, 1, text=str.tostring(allowTableRepainting
? scaledTPShort : scaledTPShort[1], format.mintick), text_color=tableTextColor,
bgcolor=tableShortBGColor)
// If the TP scale factor is exactly 1, we can nix the TP
distance columns as it will be exactly the same as the stop distance.
if (tpScaleFactor != 1)
table.cell(atrTable, 7, 1,
text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR *
tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor,
bgcolor=tableShortBGColor)