// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cryptohaltero
//@version=5
indicator("Déviation Dynamique BTC CryptoHaltéro - mai 2025", overlay=true)
// === Constantes temporelles ===
e = math.exp(-5)
y = year + (month - 1) / 12 + (dayofmonth - 1) / 365
// === Modèle personnalisé ===
model = e * math.pow(y - 2009, 6)
// === Accumulation min/max du modèle après 2009 ===
var float model_min = na
var float model_max = na
if year >= 2009
model_min := na(model_min) ? model : math.min(model_min, model)
model_max := na(model_max) ? model : math.max(model_max, model)
// === Déviation dynamique basée sur l'historique depuis 2009 ===
deviation = (model_max - model_min) / 2
// === Bornes hautes et basses ===
upper = model + deviation
lower = model - deviation
// === Position du prix BTC par rapport au modèle ===
btc_price = close
last_position = ((btc_price - model) / model) * 100
objectif_top1 = ((upper - btc_price) / btc_price) * 100
// === Tracés principaux ===
plot(model, color=#ffffff, title="Modèle (Formule)", linewidth=3)
p1 = plot(upper, color=color.red, title="Haut du Range")
p2 = plot(lower, color=color.rgb(255, 251, 4), title="Bas du Range")
fill(p1, p2, color=color.new(color.gray, 85), title="Zone de Range")
// === Zones -1.5 x déviation + 2 x déviation ===
zone_low = model - 1.5 * deviation
zone_high = model + 2 * deviation
objectif_top2 = ((zone_high - btc_price) / btc_price) * 100
p22 = plot(zone_low, color=color.rgb(76, 175, 79), title="Zone -")
p11 = plot(zone_high, color=color.rgb(249, 82, 255), title="Zone +")
fill(p1, p11, color=color.rgb(236, 135, 190, 71), title="Zone de Range Haute")
fill(p22, p2, color=color.rgb(184, 236, 135, 71), title="Zone de Range basse")
// === Étiquette texte sur la dernière bougie ===
var label lastLabel = na
isLastBar = bar_index == ta.highest(bar_index, 100)
if isLastBar
label.delete(lastLabel)
xOffset = 150 // décale de 200 barres vers la droite
yOffset = 200 // décale de 100 unités de prix vers le bas
lastLabel := label.new(x=bar_index + xOffset, y=close - yOffset, xloc = xloc.bar_index, yloc = yloc.price, text="BTC: " + str.tostring(btc_price, "#.##") + "\n" + "Model: " + str.tostring(model, "#.##") + "\n" + "Objectif TOP 1 : " + str.tostring(objectif_top1, "#.##") + " % (" + str.tostring(upper, "#.##") + ")\n" + "Objectif TOP 2 : " + str.tostring(objectif_top2, "#.##") + " % (" + str.tostring(zone_high, "#.##") + ")\n" + "Variation / moyenne : " + str.tostring(last_position, "#.##") + " % (" + str.tostring(model, "#.##") + ")", style=label.style_label_up, textcolor=color.white, size=size.large, color=#2195f383)