// This source code is subject to the terms of the Mozilla Public License 2.
0 at
https://mozilla.org/MPL/2.0/
// © World_of_Indicators
//@version=4
//eeping in mind of getting full potential of Donchian Channels , As part of this
script, Linear Regression is used as primary source to identify trend and execute
the trades.
// Hull Moving Average given as alternative option in place of Linear regression .
// Linear Regression:
// Linear regression used to identify trend, trade setup, and stop. Based on this,
Color fillings on Donchian channels is updated. That will give clear idea of
strength or weakness in any trading instruments.
// HMA:
// HMA used as alternative to identify trend, trade setup, and stop. Based on this,
Color fillings on Donchian channels is updated. That will give clear idea of
strength or weakness in any trading instruments.
// Donchian Channels:
// As it is one of the oldest trend riding system, combined here with
differentiation in color will help to go with the trend.
// Inputs:
// Input periods can be changed by users/traders as per their understanding and
observations.
/////////////////////////////////////////////////
// Using the ratio of donchian channel from early script we can calculate factor Z
which is the estimate of the high ratio and the low ratio
// crossing it with +100 and -100 we can produce the signals with addition on non
repaint donchian ratio channel
// inside the script one can take profit for both long and short , buy again and
sell again with stop loss if needed
// by the same logic we can calculate factor to z to any channel we want once we
create the correct ratio to it
// add max and low point of the donchian channel ratio
// This are the poits where the channel at max low or high , after we expect change
of trend
// there is no alerts for this but one can make it in the code
study("BankNifty 365 Algo", overlay=true)
tf = input(450)
lookBack = timeframe.isintraday and timeframe.multiplier >= 1 ?
tf / timeframe.multiplier * 7 :
timeframe.isintraday and timeframe.multiplier < 60 ?
60 / timeframe.multiplier * 24 * 7 : 7
High = highest(high, lookBack)
Low = lowest(low, lookBack)
highRatio = (close - Low) / (High - Low) * 500
lowRatio = -((High - close) / (High - Low)) * 500
highColor = High > High[1] ? #006400 : #90EE90
lowRatio1 = Low < Low[1] ? #8B0000 : #EDAFAF
h = High > High[1]
l = Low < Low[1]
HIGH = input(false, "HIGH")
LOW = input(false, "LOW")
plotshape(HIGH and h, title="high channel", location=location.abovebar,
color=color.black, style=shape.arrowup, text="M")
plotshape(LOW and l, title="low channel", location=location.belowbar,
color=color.black, style=shape.arrowup, text="L")
z = highRatio + lowRatio
showZones = input(true, title="Show Bullish/Bearish Zones")
// bullish signal rule:
bullishRule = z >= 100
// bearish signal rule:
bearishRule = z <= -100
// current trading State
ruleState = 0
ruleState := bullishRule ? 1 : bearishRule ? -1 : nz(ruleState[1])
bgcolor(showZones ? ruleState == 1 ? color.green : ruleState == -1 ? color.red :
color.yellow : na, title=" Bullish/Bearish Zones", transp=90)
// Conditions
longCond = bool(na)
shortCond = bool(na)
longCond := crossover(z, 100)
shortCond := crossunder(z, -100)
// 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
sectionShorts
if shortCond
sectionLongs := 0
sectionShorts := sectionShorts + 1
sectionShorts
// Pyramiding
pyrl = 1
// These check to see your signal and cross references it against the pyramiding
settings above
// 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 = float(na)
last_open_shortCondition = float(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 = float(na)
last_shortCondition = float(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(2, "Take Profit %", type=input.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
// Stop Loss
isSLl = input(false, "Stop Loss Long")
isSLs = input(false, "Stop Loss Short")
sl = 0.0
sl := input(3, "Stop Loss %", type=input.float)
long_sl = isSLl and crossunder(low, (1 - sl / 100) * last_open_longCondition) and
longCondition == 0 and in_longCondition == 1
short_sl = isSLs and crossover(high, (1 + sl / 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 or long_sl ? 1 : 0
short_close = short_tp or short_sl ? 1 : 0
// Get the time of the last close
last_long_close = float(na)
last_short_close = float(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="buy alert", color=color.green,
textcolor=color.green, transp=0, style=shape.triangleup,
location=location.belowbar, size=size.small, text="LONG", offset=0)
plotshape(shortCondition, title="sell alert", color=color.red, textcolor=color.red,
transp=0, style=shape.triangledown, location=location.abovebar, size=size.small,
text="SHORT", offset=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=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=color.lime, editable=false, transp=0)
ltp = iff(long_tp and last_longCondition > nz(last_long_close[1]), (1 + tp / 100) *
last_open_longCondition, na)
plot(ltp, style=plot.style_cross, linewidth=3, color=color.white, editable=false)
stp = iff(short_tp and last_shortCondition > nz(last_short_close[1]), (1 - tp /
100) * last_open_shortCondition, na)
plot(stp, style=plot.style_cross, linewidth=3, color=color.white, editable=false)
plotshape(long_sl and last_longCondition > nz(last_long_close[1]), text="SL",
title="Stop Loss Long", style=shape.triangledown, location=location.abovebar,
color=color.red, editable=false, transp=0)
plotshape(short_sl and last_shortCondition > nz(last_short_close[1]), text="SL",
title="Stop Loss Short", style=shape.triangleup, location=location.belowbar,
color=color.lime, editable=false, transp=0)
lsl = iff(long_sl and last_longCondition > nz(last_long_close[1]), (1 - sl / 100) *
last_open_longCondition, na)
plot(lsl, style=plot.style_cross, linewidth=3, color=color.white, editable=false)
ssl = iff(short_sl and last_shortCondition > nz(last_short_close[1]), (1 + sl /
100) * last_open_shortCondition, na)
plot(ssl, style=plot.style_cross, linewidth=3, color=color.white, editable=false)
//
alertcondition(bton(longCondition), title="Buy Alert")
alertcondition(bton(shortCondition), title="Sell 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")
alertcondition(bton(long_sl and last_longCondition > nz(last_long_close[1])),
title="Stop Loss Long")
alertcondition(bton(short_sl and last_shortCondition > nz(last_short_close[1])),
title="Stop Loss Short")
//
// Conditions
longCond1 = bool(na)
shortCond1 = bool(na)
longCond1 := isTPl and crossover(high, (1 + tp / 100) * last_open_longCondition)
and
longCondition == 0 and in_longCondition == 1
shortCond1 := isTPs and crossunder(low, (1 - tp / 100) * last_open_shortCondition)
and
shortCondition == 0 and in_shortCondition == 1
// Count your long short conditions for more control with Pyramiding
sectionLongs1 = 0
sectionLongs1 := nz(sectionLongs1[1])
sectionShorts1 = 0
sectionShorts1 := nz(sectionShorts1[1])
if longCond1
sectionLongs1 := sectionLongs1 + 1
sectionShorts1 := 0
sectionShorts1
if shortCond1
sectionLongs1 := 0
sectionShorts1 := sectionShorts1 + 1
sectionShorts1
// Pyramiding
pyrl1 = 1
longCondition1 = longCond1 and sectionLongs1 <= pyrl1
shortCondition1 = shortCond1 and sectionShorts1 <= pyrl1
// Get the price of the last opened long or short
last_open_longCondition1 = float(na)
last_open_shortCondition1 = float(na)
last_open_longCondition1 := longCondition1 ? open : nz(last_open_longCondition1[1])
last_open_shortCondition1 := shortCondition1 ? open :
nz(last_open_shortCondition1[1])
// Check if your last postion was a long or a short
last_longCondition1 = float(na)
last_shortCondition1 = float(na)
last_longCondition1 := longCondition1 ? time : nz(last_longCondition1[1])
last_shortCondition1 := shortCondition1 ? time : nz(last_shortCondition1[1])
in_longCondition1 = last_longCondition1 > last_shortCondition1
in_shortCondition1 = last_shortCondition1 > last_longCondition1
// Take profit
isTPl1 = input(true, "Take Profit Long1")
isTPs1 = input(true, "Take Profit Short1")
tp1 = input(2, "Take Profit %", type=input.float)
long_tp1 = isTPl1 and crossover(high, (1 + tp / 100) * last_open_longCondition1)
and
longCondition1 == 0 and in_longCondition1 == 1
short_tp1 = isTPs1 and crossunder(low, (1 - tp / 100) * last_open_shortCondition1)
and
shortCondition1 == 0 and in_shortCondition1 == 1
// Create a single close for all the different closing conditions.
long_close1 = long_tp1 ? 1 : 0
short_close1 = short_tp1 ? 1 : 0
// Get the time of the last close
last_long_close1 = float(na)
last_short_close1 = float(na)
last_long_close1 := long_close1 ? time : nz(last_long_close1[1])
last_short_close1 := short_close1 ? time : nz(last_short_close1[1])
// Alerts & Signals
buy1 = input(false, "buy again")
sell1 = input(false, "sell again")
bton1(b1) =>
b1 ? 1 : 0
plotshape(buy1 and longCondition1, title="Buy again", text="b",
style=shape.triangleup, location=location.belowbar, color=color.blue,
editable=false, transp=0)
plotshape(sell1 and shortCondition1, title="Sell again", text="s",
style=shape.triangledown, location=location.abovebar, color=color.black,
editable=false, transp=0)
plotshape(long_tp1 and last_longCondition1 > nz(last_long_close1[1]), text="TP1",
title="Take Profit Long1", style=shape.triangledown, location=location.abovebar,
color=color.red, editable=false, transp=0)
plotshape(short_tp1 and last_shortCondition1 > nz(last_short_close1[1]),
text="TP1", title="Take Profit Short1", style=shape.triangleup,
location=location.belowbar, color=color.lime, editable=false, transp=0)
alertcondition(bton1(longCondition1), title="Buy Again")
alertcondition(bton1(shortCondition1), title="Sell Aagin")
alertcondition(bton1(long_tp1 and last_longCondition1 > nz(last_long_close1[1])),
title="Take Profit Long1")
alertcondition(bton1(short_tp1 and last_shortCondition1 >
nz(last_short_close1[1])), title="Take Profit Short1")
isHMA = input(false, title="Use HMA as primary source")
//Linear Lines
//Linear Stop
srcStop = input(close, title="Linear Stop Source")
lengthStop = input(type=input.integer, defval=25, title="Stop Period")
offsetStop = input(type=input.integer, defval=0, title="Stop Offset")
linearStop = linreg(srcStop, lengthStop, offsetStop)
plot((isHMA==false) and linearStop ? linearStop : na, color=color.blue,
style=plot.style_circles, linewidth=2, title="Linear Stop")
//Linear Trade Setup
src2Stop = input(close, title="Linear Setup Source")
length2Stop = input(type=input.integer, defval=75, title="Setup Period")
offset2Stop = input(type=input.integer, defval=0, title="Setup Offset")
linear2Stop = linreg(src2Stop, length2Stop, offset2Stop)
plot((isHMA==false) and linear2Stop ? linear2Stop : na, color=color.fuchsia,
style=plot.style_stepline, linewidth=2, title="Linear Trade Setup")
//Linear Trend Line
src3Stop = input(close, title="Linear Trend Source")
length3Stop = input(type=input.integer, defval=300, title="Trend Period")
offset3Stop = input(type=input.integer, defval=0, title="Trend Offset")
linear3Stop = linreg(src3Stop, length3Stop, offset3Stop)
linearTrendColor = (close > linear3Stop) ? color.green : color.red
plot((isHMA==false) and linear3Stop ? linear3Stop : na, color=linearTrendColor,
style=plot.style_line, linewidth=3, title="Linear Trend Line")
//HMA Lines
//HMA Stop
hmaIntermediatePeriod = input(30, minval=1, title="HMA Stop Period")
hmaIntermediate = hma(input(close), hmaIntermediatePeriod)
plot(isHMA and hmaIntermediate ? hmaIntermediate : na, color=color.blue,
linewidth=2, title="HMA Stop")
//HMA Trade Setup
hmaMediumPeriod = input(100, minval=1, title="HMA Trade Period")
hmaMedium = hma(input(close), hmaMediumPeriod)
plot(isHMA and hmaMedium ? hmaMedium : na, color=color.fuchsia, linewidth=2,
title="HMA Trade Setup")
//HMA Trend Line
hmaLongPeriod = input(300, minval=1, title="HMA Trend Period")
hmaLong = hma(input(close), hmaLongPeriod)
hmaTrendColor = (close > hmaLong) ? color.green : color.red
plot(isHMA and hmaLong ? hmaLong : na, color=hmaTrendColor, linewidth=3, title="HMA
Trend Line")
//Donchian 50
DCLength = input(50, minval=1)
DCLower = lowest(DCLength)
DCUpper = highest(DCLength)
DCBasis = avg(DCUpper, DCLower)
maColor = (close > DCBasis) ? color.lime : color.red
plot(DCBasis, "DC Basis", color=maColor, linewidth=2)
fillColor = isHMA ? hmaTrendColor : linearTrendColor
upperBoundary = plot(DCUpper, "DC Upper", color=fillColor, linewidth=2)
lowerBoundary = plot(DCLower, "DC Lower", color=fillColor, linewidth=2)
fill(upperBoundary, lowerBoundary, color=fillColor, transp=95, title="DC
Background")
//1 EMA - Long Term
showEMA1 = input(false, title="Exponential Moving Average - Long Term")
emaLongerPeriod = input(200, minval=1, title="EMA Longer Period")
emaLong = ema(input(close), emaLongerPeriod)
plot(showEMA1 and emaLong ? emaLong : na, color=color.aqua, linewidth=2, title="EMA
Longer Period")
//1 SMA - Medium Term
showSMA1 = input(false, title="Simple Moving Average - Medium Term")
smaMediumPeriod = input(50, minval=1, title="SMA Medium Period")
smaMedium = sma(input(close), smaMediumPeriod)
plot(showSMA1 and smaMedium ? smaMedium : na, color=color.maroon, linewidth=2,
title="SMA Medium Period")