0% found this document useful (0 votes)
7 views

final kiran code

Uploaded by

lekhnath.regm
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

final kiran code

Uploaded by

lekhnath.regm
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © Algoryze

//@version=5
indicator("Kiran Indicator", overlay=true, max_lines_count=500,
max_boxes_count=500, max_labels_count=500)

// Function to check if a new day has started


_isNewDay(utcOffsetInMs) =>
dow = dayofweek(time + utcOffsetInMs)
dayChanged = dow != dow[1]
dayChanged

// Function to check if a new week has started


_isNewWeek(utcOffsetInMs) =>
woy = weekofyear(time + utcOffsetInMs)
weekChanged = woy != woy[1]
weekChanged

// Function to get the highest, lowest, and closing prices of the previous day
_getPreviousDayLevels(utcOffsetInMs) =>
dow = dayofweek(time + utcOffsetInMs)
var prevDayHigh = high
var prevDayLow = low
var prevDayClose = close
var dayHigh = high
var dayLow = low
var dayClose = close

if (dow != dow[1])
prevDayHigh := dayHigh[1]
prevDayLow := dayLow[1]
prevDayClose := close[1]
dayHigh := high
dayLow := low
dayClose := close
else
dayHigh := math.max(dayHigh, high)
dayLow := math.min(dayLow, low)
dayClose := close

[prevDayHigh, prevDayLow, prevDayClose]

// Function to get the day of the week text


_getDayOfWeekText(utcOffsetInMs) =>
var weekdayFullNames = array.from("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday")
weekdayText = array.get(weekdayFullNames, dayofweek(time + utcOffsetInMs) - 1)
weekdayText

// Input for custom UTC offset


utcOffset = input.int(0, title="UTC Offset (in hours)", minval=-12, maxval=12)
utcOffsetInMs = utcOffset * 60 * 60 * 1000

// Daily and Weekly separator inputs


dailySeparatorColor = input.color(color.orange, title='Daily Separator Color')
weeklySeparatorColor = input.color(color.gray, title='Weekly Separator Color')
// Variables for tracking mid-day index
var int midDayBarIndex = na
var float midDayPrice = na

// Reset midpoint calculation at the start of a new day


if _isNewDay(utcOffsetInMs)
midDayBarIndex := bar_index
midDayPrice := (high + low) / 2 // Start with the first midpoint

// Adjust mid-day position as time progresses


if not na(midDayBarIndex) and bar_index - midDayBarIndex < math.round((bar_index -
midDayBarIndex) / 2)
midDayBarIndex := bar_index
midDayPrice := (high + low) / 2

// Plot daily separator line


if _isNewDay(utcOffsetInMs)
line.new(bar_index, low, bar_index, high, color=dailySeparatorColor, width=1,
extend=extend.both)

// Plot weekly separator line


if _isNewWeek(utcOffsetInMs)
line.new(bar_index, low, bar_index, high, color=weeklySeparatorColor, width=3,
extend=extend.both)

// Get previous day high, low & close


[prevDayHigh, prevDayLow, prevDayClose] = _getPreviousDayLevels(utcOffsetInMs)

// Plot highest, lowest, and closing prices of the previous day


plot(prevDayHigh, title="Prev Day High", color=color.red,
style=plot.style_stepline)
plot(prevDayLow, title="Prev Day Low", color=color.green,
style=plot.style_stepline)
plot(prevDayClose, title="Prev Day Close", color=color.blue,
style=plot.style_stepline)

// Display labels for previous day's levels


label.new(bar_index, prevDayHigh, text="Prev HOD", textcolor=color.red,
style=label.style_label_down, size=size.tiny)
label.new(bar_index, prevDayLow, text="Prev LOD", textcolor=color.green,
style=label.style_label_up, size=size.tiny)
label.new(bar_index, prevDayClose, text="Prev Close", textcolor=color.blue,
style=label.style_label_left, size=size.tiny)

// Display day of the week label at mid-day region


if _isNewDay(utcOffsetInMs)
label.new(midDayBarIndex, midDayPrice, text=_getDayOfWeekText(utcOffsetInMs),
color=color.white, textcolor=color.blue, style=label.style_label_down)

// 🔹 EMA Calculation (User-Selectable Length)


emaLength = input.int(20, title="EMA Length", minval=1, maxval=200) // Users can
adjust EMA length
emaSource = close
emaOutput = ta.ema(emaSource, emaLength)
plot(emaOutput, title="EMA", color=color.blue, linewidth=2)

// 🔹 Optional Smoothing Moving Average


GRP = "Smoothing MA"
maTypeInput = input.string("None", "Type", options=["None", "SMA", "SMA + BB",
"EMA", "SMMA", "WMA", "VWMA"], group=GRP)
maLengthInput = input.int(20, "Smoothing MA Length", group=GRP)
bbMultInput = input.float(2.0, "BB Multiplier", minval=0.1, maxval=5, group=GRP)

// Function to calculate different MA types


ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
"SMA + BB" => ta.sma(source, length)
=> na

smoothingMA = ma(emaOutput, maLengthInput, maTypeInput)


smoothingStDev = maTypeInput == "SMA + BB" ? ta.stdev(emaOutput, maLengthInput) *
bbMultInput : na

plot(smoothingMA, title="Smoothed MA", color=color.yellow, linewidth=2,


display=maTypeInput != "None" ? display.all : display.none)

// Bollinger Bands (if selected)


bbUpper = plot(maTypeInput == "SMA + BB" ? smoothingMA + smoothingStDev : na,
title="Upper BB", color=color.green)
bbLower = plot(maTypeInput == "SMA + BB" ? smoothingMA - smoothingStDev : na,
title="Lower BB", color=color.green)
fill(bbUpper, bbLower, color=color.new(color.green, 90), title="BB Fill")

bool showSessionNames = input.bool(true, "Show session names")


bool showSessionOC = input.bool(true, "Draw session open and close lines")
bool showSessionTickRange = input.bool(true, "Show tick range for each session")
bool showSessionAverage = input.bool(true, "Show average price per session")

const string TZ_TOOLTIP_TEXT = "The session's time zone, specified in either GMT
notation (e.g., 'GMT-5') or as an IANA time zone database name (e.g.,
'America/New_York')."
+ " We recommend the latter since it includes other time-related changes, such as
daylight savings."

const string FIRST_SESSION_GROUP = "First Session"


showFirst = input.bool(true, "Show session", group = FIRST_SESSION_GROUP,
display = display.none)
firstSessionName = input.string("Tokyo", "Displayed name", group =
FIRST_SESSION_GROUP, display = display.none)
firstSessionTime = input.session("0900-1500", "Session time", group =
FIRST_SESSION_GROUP, display = display.none)
firstSessionTZ = input.string("Asia/Tokyo", "Session timezone", group =
FIRST_SESSION_GROUP, display = display.none, tooltip = TZ_TOOLTIP_TEXT)
firstSessionColor = input.color(color.new(#2962FF, 85), "Session color", group =
FIRST_SESSION_GROUP)

const string SECOND_SESSION_GROUP = "Second session"


showSecond = input.bool(true, "Show session", group = SECOND_SESSION_GROUP,
display = display.none)
secondSessionName = input.string("London", "Displayed name", group =
SECOND_SESSION_GROUP, display = display.none)
secondSessionTime = input.session("0830-1630", "Session time", group =
SECOND_SESSION_GROUP, display = display.none)
secondSessionTZ = input.string("Europe/London", "Session timezone", group =
SECOND_SESSION_GROUP, display = display.none, tooltip = TZ_TOOLTIP_TEXT)
secondSessionColor = input.color(color.new(#FF9800, 85), "Session color", group =
SECOND_SESSION_GROUP)

const string THIRD_SESSION_GROUP = "Third session"


showThird = input.bool(true, "Show session", group = THIRD_SESSION_GROUP,
display = display.none)
thirdSessionName = input.string("New York", "Displayed name", group =
THIRD_SESSION_GROUP, display = display.none)
thirdSessionTime = input.session("0930-1600", "Session time", group =
THIRD_SESSION_GROUP, display = display.none)
thirdSessionTZ = input.string("America/New_York", "Session timezone", group =
THIRD_SESSION_GROUP, display = display.none, tooltip = TZ_TOOLTIP_TEXT)
thirdSessionColor = input.color(color.new(#089981, 85), "Session color", group =
THIRD_SESSION_GROUP)

type SessionDisplay
box sessionBox
label sessionLabel
line openLine
line avgLine
line closeLine
float sumClose
int numOfBars

type SessionInfo
color color
string name
string session
string timezone
SessionDisplay active = na

method setName(SessionDisplay this, string name) =>


sessionLabel = this.sessionLabel
sessionBox = this.sessionBox
boxText = array.new<string>()
if showSessionTickRange
boxText.push("Range: " + str.tostring((sessionBox.get_top() -
sessionBox.get_bottom()) / syminfo.mintick, format.mintick))
if showSessionAverage
boxText.push("Avg: " + str.tostring(this.sumClose / this.numOfBars,
format.mintick))
if showSessionNames
boxText.push(name)

sessionLabel.set_y(sessionBox.get_bottom())
sessionLabel.set_text(array.join(boxText, "\n"))

method createSessionDisplay(SessionInfo this) =>


boxColor = this.color
opaqueColor = color.new(boxColor, 0)
dis = SessionDisplay.new(
sessionBox = box.new(bar_index, high, bar_index, low, bgcolor = boxColor,
border_color = na),
sessionLabel = label.new(bar_index, low, "", style =
label.style_label_upper_left, textalign = text.align_left, textcolor = opaqueColor,
color = color(na)),
openLine = showSessionOC ? line.new(bar_index, open, bar_index, open, color
= opaqueColor, style = line.style_dashed, width = 1) : na,
closeLine = showSessionOC ? line.new(bar_index, close, bar_index, close,
color = opaqueColor, style = line.style_dashed, width = 1) : na,
avgLine = showSessionAverage ? line.new(bar_index, close, bar_index,
close, style = line.style_dotted, width = 2, color = opaqueColor) : na,
sumClose = close,
numOfBars = 1
)
linefill.new(dis.openLine, dis.closeLine, boxColor)
dis.setName(this.name)
this.active := dis

method updateSessionDisplay(SessionInfo this) =>


sessionDisp = this.active
sessionBox = sessionDisp.sessionBox
openLine = sessionDisp.openLine
closeLine = sessionDisp.closeLine
avgLine = sessionDisp.avgLine
sessionDisp.sumClose += close
sessionDisp.numOfBars += 1

sessionBox.set_top(math.max(sessionBox.get_top(), high))
sessionBox.set_bottom(math.min(sessionBox.get_bottom(), low))
sessionBox.set_right(bar_index)
sessionDisp.setName(this.name)

if showSessionOC
openLine.set_x2(bar_index)
closeLine.set_x2(bar_index)
closeLine.set_y1(close)
closeLine.set_y2(close)

if showSessionAverage
avgLine.set_x2(bar_index)
avg = sessionDisp.sumClose / sessionDisp.numOfBars
avgLine.set_y1(avg)
avgLine.set_y2(avg)
sessionDisp

method update(SessionInfo this) =>


bool isChange = timeframe.change("1D")
if (not na(time("", this.session, this.timezone))) // inSession
if na(this.active) or isChange
this.createSessionDisplay()
else
this.updateSessionDisplay()
else if not na(this.active)
this.active := na

getSessionInfos()=>
array<SessionInfo> sessionInfos = array.new<SessionInfo>()
if showFirst
sessionInfos.push(SessionInfo.new(firstSessionColor, firstSessionName,
firstSessionTime, firstSessionTZ))
if showSecond
sessionInfos.push(SessionInfo.new(secondSessionColor, secondSessionName,
secondSessionTime, secondSessionTZ))
if showThird
sessionInfos.push(SessionInfo.new(thirdSessionColor, thirdSessionName,
thirdSessionTime, thirdSessionTZ))
sessionInfos

var array<SessionInfo> sessionInfos = getSessionInfos()


if timeframe.isdwm
runtime.error("This indicator can only be used on intraday timeframes.")

for info in sessionInfos


info.update()

You might also like