heikin ashi calculation call with higher timeframe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © potatoshop

//@version=5
indicator("heikin ashi calculation call with higher timeframe","Pre_Heikin", overlay = true)

is_in_chart = time >= chart.left_visible_bar_time and chart.right_visible_bar_time >= time 
//Mathmatical Calcualtion to avoid the security function
heikin(Open,High,Low,Close) =>
    hClose  = (Open + High + Low + Close) / 4
    hOpen   = float(na)
    hOpen   := na(hOpen[1]) ? (Open + Close) / 2 : (nz(hOpen[1]) + nz(hClose[1])) / 2
    hHigh   = math.max(High, math.max(hOpen, hClose))
    hLow    = math.min(Low,  math.min(hOpen, hClose))
    [hOpen,hHigh,hLow,hClose]

Check = input.bool(true, "Use Heikin Ashi?")
interval = input.timeframe('60', "Resolution")
interval_ = if str.endswith(interval,"D")
    str.tonumber(str.substring(interval,0,str.pos(interval,"D")))*24*60
else if str.endswith(interval,"W")
    str.tonumber(str.substring(interval,0,str.pos(interval,"W")))*24*60*7
else if str.endswith(interval,"M")
    str.tonumber(str.substring(interval,0,str.pos(interval,"M")))*24*60*30
else
    str.tonumber(interval)

lookback = int(interval_/str.tonumber(timeframe.period))

// Global variables for previous value of Higher timeframe. 
_h_high = ta.highest(high,lookback)[1] , _h_low = ta.lowest(low,lookback)[1]
var h_high = float(na) , var h_low = float(na)
var HOpen = 0. , var HHigh = 0. ,var HLow = 0. , var HClose = 0.
var hkOpen = float(na),var hkHigh= float(na),var hkLow= float(na),var hkClose = float(na)

if timeframe.change(interval)
    HOpen := HClose[1],    HClose := close[1],     HHigh := _h_high,    HLow := _h_low
    [hkOpen_,hkHigh_,hkLow_,hkClose_] = heikin(HOpen,HHigh,HLow,HClose)
    hkOpen := hkOpen_,    hkHigh := hkHigh_,    hkLow := hkLow_,    hkClose := hkClose_
    
DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)

O = Check ? hkOpen : HOpen
H = Check ? hkHigh : HHigh
L = Check ? hkLow  : HLow
C = Check ? hkClose : HClose

Color = C > O ? DEFAULT_COLOR_GROWTH : DEFAULT_COLOR_FALL

check1 = input.bool(true,"Original Time View")
OPEN = plot(O,"Pre_Open",color =Color, style=plot.style_stepline  ,offset =check1? -lookback:0)
HIGH = plot(H,"Pre_High",color =Color, style=plot.style_stepline ,offset =check1? -lookback:0)
LOW = plot(L,"Pre_Low",color =Color, style=plot.style_stepline ,offset =check1? -lookback:0)
CLOSE = plot(C,"Pre_CLose",color =Color, style=plot.style_stepline ,offset =check1? -lookback:0)

fill(OPEN,CLOSE,color=Color)
fill(HIGH,LOW,color=Color)

is_new_period = timeframe.change(interval)
TMR = ((H-L)/L)*100 // Total Moving Range
Volatility = ((C-C[1])/C[1])*100

halflookback = int(lookback/2)
if is_new_period and is_in_chart
    label.new(bar_index-halflookback, H,style=label.style_none, text=str.tostring(Volatility,format.percent),textcolor = color.rgb(243, 245, 247, 45),color = color.rgb(27, 134, 45, 100),tooltip="TMR: "+ str.tostring(TMR,format.percent))

if barstate.islast
    line.new(last_bar_index-lookback,H,last_bar_index+lookback,H, color=color.new(Color,50),width=1)
    line.new(last_bar_index-lookback,O,last_bar_index+lookback,O, color=color.new(Color,50),width=1)
    line.new(last_bar_index-lookback,C,last_bar_index+lookback,C, color=color.new(Color,50),width=1)
    line.new(last_bar_index-lookback,L,last_bar_index+lookback,L, color=color.new(Color,50),width=1)

