Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Simple Algooo

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

//@version=5

indicator('SimpleAlgo.io V3', overlay=true)


import TradingView/ta/7

// Setting
showsignals = input(title='Show Buy & Sell', defval=true, group = 'Buy And Sell
SIgnals [AlgoPoInt]')
sensitivity = input.int(title='Sensitivity (1-15)', defval=7, minval = 1, maxval =
15, group = 'Buy And Sell SIgnals [AlgoPoInt]') + 1

levels = input.bool(false, "Take Profit/ Stop-Loss Areas" , group = "RISK


MANAGEMENT SETTINGS [AlgoPoInt]" , inline = "MMDB2")
lvlLines = true
linesStyle = "SOLID"
lvlDistance = input.int(20, "Distance", 1, inline="levels2", group = "RISK
MANAGEMENT SETTINGS [AlgoPoInt]")
lvlDecimals = input.int(2, " Decimals", 1, 8, inline="levels2", group = "RISK
MANAGEMENT SETTINGS [AlgoPoInt]")
atrRisk = input.int(1, "Risk % ", 1, group = "RISK MANAGEMENT SETTINGS
[AlgoPoInt]" , inline="levels3")
atrLen = input.int(14, " ATR Length", 1, group = "RISK MANAGEMENT SETTINGS
[AlgoPoInt]" , inline="levels3")

ShowSmartTrail = input(true,'Smart Trail', group = 'TOOLS [AlgoPoInt]')


show_rev = input.bool(true, "Reversal Cloud", group = 'TOOLS [AlgoPoInt]')
TrendFollower = input(true, 'Trend Follower', group='TOOLS [AlgoPoInt]')

Periods = sensitivity * 10
src = hl2
Multiplier = sensitivity / 2
changeATR = true
highlighting = false
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy Signal▲',
location=location.absolute, style=shape.labelup, size=size.normal, color=#00ff00,
textcolor=color.new(color.black, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell Signal▼',
location=location.absolute, style=shape.labeldown, size=size.normal, color=#ff0000,
textcolor=color.new(color.white, 0))

alertcondition(buySignal, title='SimpleAlgo Buy', message='SimpleAlgo Buy!')


alertcondition(sellSignal, title='SimpleAlgo Sell', message='SimpleAlgo Sell!')
changeCond = trend != trend[1]
alertcondition(changeCond, title='SimpleAlgo Direction Change', message='SimpleAlgo
has changed direction!')

// Candle Coloring
// Input
fastLength = 25
slowLength = 55
signalLength = 9

// Data reference
[macd, signal, hist] = ta.macd(src, fastLength, slowLength, signalLength)

// 4 level of green
greenHigh = #05df09
greenMidHigh = #05df09
greenMidLow = #05df09
greenLow = #673ab7

// Yellow
yellowLow = #673ab7

// 4 level of red
redHigh = #673ab7
redMidHigh = #ea0402
redMidLow = #ea0402
redLow = #ea0402

// Default color
candleBody = yellowLow

// Ranging trend
if hist > 0
if hist > hist[1] and hist[1] > 0
candleBody := greenLow

if hist < 0
if hist < hist[1] and hist[1] < 0
candleBody := redLow

// Bullish trend
if macd > 0 and hist > 0
candleBody := greenMidLow

if hist > hist[1] and macd[1] > 0 and hist[1] > 0


candleBody := greenMidHigh

if hist > hist[2] and macd[2] > 0 and hist[2] > 0


candleBody := greenHigh

// Bearish trend
if macd < 0 and hist < 0
candleBody := redMidLow

if hist < hist[1] and macd[1] < 0 and hist[1] < 0


candleBody := redMidHigh

if hist < hist[2] and macd[2] < 0 and hist[2] < 0


candleBody := redHigh

barcolor(candleBody) // Include suggestion by Shaheen204

// Smart Trail
// inputs //
//{

trailType = 'modified'
ATRPeriod = 13
ATRFactor = 4
smoothing = 8

norm_o = request.security(ticker.new(syminfo.prefix, syminfo.ticker),


timeframe.period, open)
norm_h = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, high)
norm_l = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, low)
norm_c = request.security(ticker.new(syminfo.prefix, syminfo.ticker),
timeframe.period, close)
//}

//////// FUNCTIONS //////////////


//{
// Wilders ma //
Wild_ma(_src, _malength) =>
_wild = 0.0
_wild := nz(_wild[1]) + (_src - nz(_wild[1])) / _malength
_wild

/////////// TRUE RANGE CALCULATIONS /////////////////


HiLo = math.min(norm_h - norm_l, 1.5 * nz(ta.sma(norm_h - norm_l, ATRPeriod)))

HRef = norm_l <= norm_h[1] ? norm_h - norm_c[1] : norm_h - norm_c[1] - 0.5 *


(norm_l - norm_h[1])

LRef = norm_h >= norm_l[1] ? norm_c[1] - norm_l : norm_c[1] - norm_l - 0.5 *


(norm_l[1] - norm_h)

trueRange = trailType == 'modified' ? math.max(HiLo, HRef, LRef) : math.max(norm_h


- norm_l, math.abs(norm_h - norm_c[1]), math.abs(norm_l - norm_c[1]))
//}

/////////// TRADE LOGIC ////////////////////////


//{
loss = ATRFactor * Wild_ma(trueRange, ATRPeriod)

Up = norm_c - loss
Dn = norm_c + loss

TrendUp = Up
TrendDown = Dn
Trend = 1

TrendUp := norm_c[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up


TrendDown := norm_c[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn

Trend := norm_c > TrendDown[1] ? 1 : norm_c < TrendUp[1] ? -1 : nz(Trend[1], 1)


trail = Trend == 1 ? TrendUp : TrendDown

ex = 0.0
ex := ta.crossover(Trend, 0) ? norm_h : ta.crossunder(Trend, 0) ? norm_l : Trend ==
1 ? math.max(ex[1], norm_h) : Trend == -1 ? math.min(ex[1], norm_l) : ex[1]
//}

// //////// PLOT TP and SL /////////////

////// FIBONACCI LEVELS ///////////


//{
state = Trend == 1 ? 'long' : 'short'

fib1Level = 61.8
fib2Level = 78.6
fib3Level = 88.6

f1 = ex + (trail - ex) * fib1Level / 100


f2 = ex + (trail - ex) * fib2Level / 100
f3 = ex + (trail - ex) * fib3Level / 100
l100 = trail + 0

fill(plot(ShowSmartTrail ? (ta.sma(trail, smoothing)) : na, 'Trailingstop',


style=plot.style_line, color=Trend == 1 ? color.new(#14ff34, 0) : Trend == -1 ?
color.new(#ff0015, 0) : na),
plot( ShowSmartTrail ? (ta.sma(f2, smoothing)) : na, 'Fib 2',
style=plot.style_line, display=display.none),
color=state == 'long' ? color.new(#14ff34, 80) : state == 'short' ?
color.new(#ff0015, 80) : na)
//}

// Reversal Band

//func
kama(src, len) =>
kama = 0.0
sum_1 = math.sum(math.abs(src - src[1]), len)
sum_2 = math.sum(math.abs(src - src[1]), len)
kama := nz(kama[1]) + math.pow((sum_1 != 0 ? math.abs(src - src[len]) / sum_2 :
0) * (0.288 - 0.0666) + 0.0666, 2) * (src - nz(kama[1]))
kama

//inputs

length = 2
bd1 = 2
bd2 = 3
bd3 = 4
smoothingband = 12
//logic
rg = kama(ta.tr, length)
basis = kama(close, length)
upper1 = basis + rg * bd1
upper2 = basis + rg * bd2
upper3 = basis + rg * bd3
lower1 = basis - rg * bd1
lower2 = basis - rg * bd2
lower3 = basis - rg * bd3
upper1ema = ta.dema2(upper1,smoothingband)
upper2ema = ta.dema2(upper2,smoothingband)
upper3ema = ta.dema2(upper3,smoothingband)
lower1ema = ta.dema2(lower1,smoothingband)
lower2ema = ta.dema2(lower2,smoothingband)
lower3ema = ta.dema2(lower3,smoothingband)
//ploting
p1 = plot(show_rev ? upper1ema : na, transp=100)
p2 = plot(show_rev ? upper2ema : na, transp=100)
p3 = plot(show_rev ? upper3ema : na, transp=100)
p4 = plot(show_rev ? lower1ema : na, transp=100)
p5 = plot(show_rev ? lower2ema : na, transp=100)
p6 = plot(show_rev ? lower3ema : na, transp=100)
fill(p1, p2, color=color.new(#ff0015, 85))
fill(p2, p3, color=color.new(#dc0000, 50))
fill(p4, p5, color=color.new(#21f945, 85))
fill(p5, p6, color=color.new(#30ff30, 50))

// Stop Loss and take profit

decimals = lvlDecimals == 1 ? "#.#" : lvlDecimals == 2 ? "#.##" : lvlDecimals == 3


? "#.###" : lvlDecimals == 4 ? "#.####" : lvlDecimals == 5 ? "#.#####" :
lvlDecimals == 6 ? "#.######" : lvlDecimals == 7 ? "#.#######" : "#.########"

bull = buySignal
bear = sellSignal

trigger2 = bull ? 1 : 0
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
atrBand = ta.atr(atrLen) * atrRisk
atrStop = trigger == 1 ? low - atrBand : high + atrBand
currentposition = countBull > countBear ? 'Sell' : 'Buy'

lastTrade(close) => ta.valuewhen(bull or bear , close, 0)

entry = levels ? label.new(time, close, "Entry: " + str.tostring(lastTrade(close),


decimals), xloc.bar_time, yloc.price, #787b8670, label.style_label_left,
color.white, size.normal) : na
label.set_x(entry, label.get_x(entry) + math.round(ta.change(time) * lvlDistance))
label.set_y(entry, lastTrade(close))
label.delete(entry[1])

stop_y = lastTrade(atrStop)
stop = levels ? label.new(time, close, "Stop Loss: " + str.tostring(stop_y,
decimals), xloc.bar_time, yloc.price, #fe000070, label.style_label_left,
color.white, size.normal) : na
label.set_x(stop, label.get_x(stop) + math.round(ta.change(time) * lvlDistance))
label.set_y(stop, stop_y)
label.delete(stop[1])

tp1Rl_y = (lastTrade(close)-lastTrade(atrStop))*1 + lastTrade(close)


tp1Rl = levels ? label.new(time, close, "Take Profit 1: " + str.tostring(tp1Rl_y,
decimals), xloc.bar_time, yloc.price, #14ff3470, label.style_label_left,
color.white, size.normal ) : na
label.set_x(tp1Rl, label.get_x(tp1Rl) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp1Rl, tp1Rl_y)
label.delete(tp1Rl[1])
tp2RL_y = (lastTrade(close)-lastTrade(atrStop))*2 + lastTrade(close)
tp2RL = levels ? label.new(time, close, "Take Profit 2: " + str.tostring(tp2RL_y,
decimals), xloc.bar_time, yloc.price, #14ff3470, label.style_label_left,
color.white, size.normal) : na
label.set_x(tp2RL, label.get_x(tp2RL) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp2RL, tp2RL_y)
label.delete(tp2RL[1])

tp3RL_y = (lastTrade(close)-lastTrade(atrStop))*3 + lastTrade(close)


tp3RL = levels ? label.new(time, close, "Take Profit 3: " + str.tostring(tp3RL_y,
decimals), xloc.bar_time, yloc.price, #14ff3470, label.style_label_left,
color.white, size.normal) : na
label.set_x(tp3RL, label.get_x(tp3RL) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp3RL, tp3RL_y)
label.delete(tp3RL[1])

style = linesStyle == "SOLID" ? line.style_solid : linesStyle == "DASHED" ?


line.style_dashed : line.style_dotted
lineEntry = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull :
countBear), lastTrade(close), bar_index + lvlDistance, lastTrade(close),
xloc.bar_index, extend.none, color.rgb(120, 123, 134, 50), style, 2) : na,
line.delete(lineEntry[1])
lineStop = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull :
countBear), stop_y, bar_index + lvlDistance, stop_y, xloc.bar_index, extend.none,
#fe010070, style, 2) : na, line.delete(lineStop[1])
lineTp1Rl = levels and lvlLines ? line.new(bar_index - (trigger == 0 ?
countBull : countBear), tp1Rl_y, bar_index + lvlDistance, tp1Rl_y, xloc.bar_index,
extend.none, #14ff3470, style, 2) : na, line.delete(lineTp1Rl[1])
lineTp2RL = levels and lvlLines ? line.new(bar_index - (trigger == 0 ?
countBull : countBear), tp2RL_y, bar_index + lvlDistance, tp2RL_y, xloc.bar_index,
extend.none, #14ff3470, style, 2) : na, line.delete(lineTp2RL[1])
lineTp3RL = levels and lvlLines ? line.new(bar_index - (trigger == 0 ?
countBull : countBear), tp3RL_y, bar_index + lvlDistance, tp3RL_y, xloc.bar_index,
extend.none, #14ff3470, style, 2) : na, line.delete(lineTp3RL[1])

// Trend Follower

smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng = smoothrng(close, 22, 6)

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(close, smrng)

//
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

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

filtcolor = upward > 0 ? color.new(#30ff30, 50) : downward > 0 ? color.new(#dc0000,


50) : color.new(#56328f, 0)

plot(TrendFollower ? filt : na, color=filtcolor, linewidth=1, title='Trend Tracer')

You might also like