Hello there!
This is my first post, so I hope somebody is willing to help me. I'm having troubles trying to calculate the mean value of a parameter, in this case, the difference between high-low divided by a certain price
, as I'm trying to measure the volatility in this way. This certain price
i used as a parameter to compare different values, so I tested it using both the close price and the high price of the day.
I will rewrite it, cause I know it can be a bit confusing. tMintMaxtCloseMEAN
means:
tMin: Todays Min
tMax: Todays Max
tClose: Todays Close
MEAN: The mean value of this parameter over time
So, it should be calculated as:
tMintMaxtCloseMEAN = ( tMax - tMin ) / tClose
And I calculate this mean value using an EMA multiplier.
When I calculate tMintMaxtCloseMEAN it works ok, but when trying to calculate tMintMaxtMaxMEAN (which is exactly the same except that the denominator is the Max instead of the Close), it doesn't work as expected and it returns NaN.
What am I doing wrong?
I hope anybody helps me. Thaks in advance!!
study(Study
, overlay=true)
// Inputs de volatilidades
emaLength = input(365, title=EMA Length
, type= input.float)
i_tMintMaxtClose = input(true, title=(Max Hoy - Min Hoy) / Cierre Hoy
)
i_tMintMaxtMax = input(false, title=(Max Hoy - Min Hoy) / Max Hoy
)
// Variables
float tMintMaxtClose=0.0
var float tMintMaxtCloseMAX = 0.0
var float tMintMaxtCloseMEAN = 0.0
float tMintMaxtMax=0.0
var float tMintMaxtMaxMAX = 0.0
var float tMintMaxtMaxMEAN = 0.0
var float todayMax=0.0
var float todayMin=0.0
var float yesterdayMax = 0.0
var float yesterdayMin = 0.0
var int count = 0
var int day = 0
var int lastBarDay = 0
var float emaMultiplier = 2.0/(emaLength+1.0)
var bool isMarket=false
var bool lastBarWasMarket=false
var int lastMarketBar = 0
label2=label.new(bar_index, high, text = "", color=color.green)
// Cálculos
lastBarWasMarket := isMarket
isMarket := session.ismarket
lastBarDay := day
if(isMarket and lastBarWasMarket and lastBarDay != dayofweek)
lastMarketBar:= 1
if(not session.ismarket)
if(lastBarWasMarket)
lastMarketBar := 1
isMarket := false
else
lastMarketBar += 1
isMarket := true
if(session.ismarket)
if(day != dayofweek)
tMintMaxtClose := 100*(todayMax-todayMin)/close[lastMarketBar]
tMintMaxtCloseMAX := tMintMaxtClose > tMintMaxtCloseMAX ? tMintMaxtClose : tMintMaxtCloseMAX
tMintMaxtCloseMEAN := tMintMaxtClose * emaMultiplier + tMintMaxtCloseMEAN * (1 - emaMultiplier)
tMintMaxtMax := 100*(todayMax-todayMin)/todayMax
tMintMaxtMaxMAX := tMintMaxtMax > tMintMaxtMaxMAX ? tMintMaxtMax : tMintMaxtMaxMAX
tMintMaxtMaxMEAN := tMintMaxtMax * emaMultiplier + tMintMaxtMaxMEAN * (1 - emaMultiplier)
count+=1
if(session.ismarket and day != dayofweek)
day:= dayofweek
label.set_text(label2, "Volatility= " + tostring(round(tMintMaxtClose,2))+"\nMax= "+tostring(round(todayMax, 2))+"\nMin= "+tostring(round(todayMin, 2)) + "\ntMintMaxtMax= "+tostring(tMintMaxtMax)+ "\ntMintMaxtMaxMEAN= "+tostring(tMintMaxtMaxMEAN))
yesterdayMin:=todayMin
yesterdayMax:=todayMax
todayMax:=0.0
todayMin:=100000000.0
label.set_xy(label2, bar_index, high[0])
else
label.delete(label2)
if(session.ismarket)
todayMax := todayMax > high[0] ? todayMax : high[0]
todayMin := todayMin < low[0] ? todayMin : low[0]