T106
invalid map key type
Severity Error
Map keys must be string, int or bool. Floats are rejected because two prices that print the same can differ in their last bit, so lookups would miss; colors, times and records are not supported as keys either.
For price-keyed maps, convert the price to an integer tick index (int(math.round(price / context.tickSize))) and use that as the key.
Failing example
script "Volume at price"
data chart = subscribe(data.ohlcv)
state byPrice = map<float, float>(2000)
on chart.close {
let c = chart.close
if c != null {
byPrice[c] = (byPrice[c] ?? 0.0) + (chart.volume ?? 0.0)
}
}Fixed example
script "Volume at price"
data chart = subscribe(data.ohlcv)
state byTick = map<int, float>(2000)
on chart.close {
let c = chart.close
if c != null {
let key = int(math.round(c / context.tickSize)) ?? 0
byTick[key] = (byTick[key] ?? 0.0) + (chart.volume ?? 0.0)
}
}