Ema mtf 8/20 /R/S Pine bar [traders journal]

Ce script Pine est un indicateur conçu par nos dévellopeurs pour TradingView qui combine plusieurs éléments tels que des moyennes mobiles exponentielles (EMA) sur différentes périodes, des configurations pour détecter les pin bars (barres à épingles) et des niveaux de support/résistance basés sur les points pivots. Je vais détailler le script en expliquant le rôle de chaque partie.

EMA MTF (Exponential Moving Average - Multi-Timeframe)
Le script commence par définir une fonction ema_custom pour calculer l'EMA avec une période spécifiée. Ensuite, il prend des paramètres pour la source de données (pouvant être ouverte, haute, basse, ou clôture), les périodes de l'EMA, et les unités de temps pour lesquelles afficher les EMAs (15 minutes, 1 heure, 4 heures).

Le script calcule ensuite les EMAs pour chaque unité de temps sélectionnée et combine ces valeurs pour obtenir une EMA globale. Il trace ensuite ces EMAs sur le graphique principal avec des couleurs différentes en fonction de la direction de la tendance.

Pin Bars
Ensuite, le script détecte les pin bars en calculant la taille du corps de la barre, les ombres supérieure et inférieure, et en vérifiant si les conditions d'une pin bar haussière (bullish) ou baissière (bearish) sont remplies. Les pin bars sont ensuite signalées sur le graphique avec des formes et des couleurs spécifiques.

Pivot Points
Le script utilise des points pivots pour déterminer les niveaux de support et de résistance. Il prend en compte plusieurs paramètres tels que la période de pivot, la source (haut/bas ou clôture/ouverture), la largeur maximale du canal, la force minimale requise pour un niveau de support/résistance, le nombre maximum de niveaux à afficher, et une période de retour en arrière.

Il trace les points pivots sur le graphique avec différentes formes et couleurs, et il peut également afficher des lignes de support/résistance basées sur ces points.

Moyennes Mobiles
Le script permet également d'afficher deux moyennes mobiles exponentielles supplémentaires (MA1 et MA2) avec des périodes et des types (SMA ou EMA) spécifiés par l'utilisateur.

Niveaux de Support/Résistance
Enfin, le script utilise les points pivots pour calculer et afficher les niveaux de support et de résistance en fonction des conditions spécifiées. Les niveaux les plus forts sont mis en évidence, et des boîtes sont tracées autour des canaux de support/résistance. Il vérifie également si le prix est dans un canal et s'il a rompu les niveaux de support ou de résistance, déclenchant des alertes en conséquence.

Conclusion
En résumé, ce script combine plusieurs indicateurs et techniques d'analyse technique pour aider les traders à identifier des opportunités potentielles sur le marché. Les paramètres comme les périodes des EMAs, les conditions des pin bars, et les configurations des points pivots sont tous ajustables pour s'adapter aux préférences individuelles des traders.
_____________________________________________________________________________________________code______________________________________________________________________

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © SESE04

//@version=5
indicator(" EMA MTF S/R[traders journal]", "Ema mtf 8/20 /R/S Pine bar [traders journal]"
, overlay = true
, max_labels_count = 500
, max_lines_count = 500
, max_boxes_count = 500
, max_bars_back = 500)

 

// EMA MTF

// Fonction pour calculer l'EMA avec la période spécifiée
ema_custom(src, length) =>
ta.ema(src, length)

// Définir la source de données
src = input(close, title="Source de Données")

// Paramètres pour la période de l'EMA et les unités de temps
ema_length = input.int(8, title="Période de l'EMA", minval=1)
ema1_length = input.int(20, title="Période de l'EMA1", minval=1)


timeframe_15min = input.bool(true, title="Afficher EMA 15min")
timeframe_1hour = input.bool(true, title="Afficher EMA 1 heure")
timeframe_4hour = input.bool(true, title="Afficher EMA 4 heures")

 

// Calculer les EMAs pour chaque unité de temps
ema_5min = ema_custom(src, ema_length)
ema_15min = timeframe_15min ? request.security(syminfo.tickerid, "15", ema_custom(src, ema_length)) : na
ema_1hour = timeframe_1hour ? request.security(syminfo.tickerid, "60", ema_custom(src, ema_length)) : na
ema_4hour = timeframe_4hour ? request.security(syminfo.tickerid, "240", ema_custom(src, ema_length)) : na

ema1_5min = ema_custom(src, ema1_length)
ema1_15min = timeframe_15min ? request.security(syminfo.tickerid, "15", ema_custom(src, ema1_length)) : na
ema1_1hour = timeframe_1hour ? request.security(syminfo.tickerid, "60", ema_custom(src, ema1_length)) : na
ema1_4hour = timeframe_4hour ? request.security(syminfo.tickerid, "240", ema_custom(src, ema1_length)) : na


// Calculer une moyenne des EMAs
ema_combined = (ema_5min + ema_15min + ema_1hour + ema_4hour) / 4

// Définir la couleur en fonction de la direction de la tendance
color_ema_combined = ema_combined > ema_combined[1] ? color.blue : color.red

// Tracer la ligne EMA combinée sur le graphique principal
plot(ema_combined, color=color_ema_combined, title="EMA Combined")


ema1_combined = (ema1_5min + ema1_15min + ema1_1hour + ema1_4hour) / 4

// Définir la couleur en fonction de la direction de la tendance
color_ema1_combined = ema1_combined > ema1_combined[1] ? color.blue : color.red

// Tracer la ligne EMA combinée sur le graphique principal
plot(ema1_combined, color=color_ema1_combined, title="EMA1 Combined")

// Entrée des pine bar

//PIN BAR
body = math.abs(close - open)
upshadow = open > close ? (high - open) : (high - close)
downshadow = open > close ? (close - low) : (open - low)
pinbar_h = close[1] > open[1] ? (body[1] > body ? (upshadow > 0.5 * body ? (upshadow > 2 * downshadow ? 1 : 0) : 0) : 0) : 0
pinbar_l = open[1] > close[1] ? (body[1] > body ? (downshadow > 0.5 * body ? (downshadow > 2 * upshadow ? 1 : 0) : 0) : 0) : 0

plotshape(pinbar_h, style=shape.triangledown, color=color.red)
plotshape(pinbar_l, style=shape.triangleup, color=color.blue, location=location.belowbar)
plotchar(pinbar_h, text="BEARISH PINBAR", char="", color=color.red)
plotchar(pinbar_l, text="BULLISH PINBAR", char="", color=color.blue, location=location.belowbar)

// PIVOT POINT
prd = input.int(defval=10, title='Pivot Period', minval=4, maxval=30, group='Settings 🔨', tooltip='Used while calculating Pivot Points, checks left&right bars')
ppsrc = input.string(defval='High/Low', title='Source', options=['High/Low', 'Close/Open'], group='Settings 🔨', tooltip='Source for Pivot Points')
ChannelW = input.int(defval=5, title='Maximum Channel Width %', minval=1, maxval=8, group='Settings 🔨', tooltip='Calculated using Highest/Lowest levels in 300 bars')
minstrength = input.int(defval=1, title='Minimum Strength', minval=1, group='Settings 🔨', tooltip='Channel must contain at least 2 Pivot Points')
maxnumsr = input.int(defval=6, title='Maximum Number of S/R', minval=1, maxval=10, group='Settings 🔨', tooltip='Maximum number of Support/Resistance Channels to Show') - 1
loopback = input.int(defval=290, title='Loopback Period', minval=100, maxval=400, group='Settings 🔨', tooltip='While calculating S/R levels it checks Pivots in Loopback Period')
res_col = input.color(defval=color.new(color.red, 75), title='Resistance Color', group='Colors red ')
sup_col = input.color(defval=color.new(color.blue, 75), title='Support Color', group='Colors blue')
inch_col = input.color(defval=color.new(color.gray, 75), title='Color When Price in Channel', group='Colors ')
showpp = input.bool(defval=false, title='Show Pivot Points', group='Extras ⏶⏷')
showsrbroken = input.bool(defval=false, title='Show Broken Support/Resistance', group='Extras ⏶⏷')
showthema1en = input.bool(defval=false, title='MA 1', inline='ma1')
showthema1len = input.int(defval=50, title='', inline='ma1')
showthema1type = input.string(defval='SMA', title='', options=['SMA', 'EMA'], inline='ma1')
showthema2en = input.bool(defval=false, title='MA 2', inline='ma2')
showthema2len = input.int(defval=200, title='', inline='ma2')
showthema2type = input.string(defval='SMA', title='', options=['SMA', 'EMA'], inline='ma2')

ma1 = showthema1en ? showthema1type == 'SMA' ? ta.sma(close, showthema1len) : ta.ema(close, showthema1len) : na
ma2 = showthema2en ? showthema2type == 'SMA' ? ta.sma(close, showthema2len) : ta.ema(close, showthema2len) : na

plot(ma1, color=not na(ma1) ? color.blue : na)
plot(ma2, color=not na(ma2) ? color.red : na)

// get Pivot High/low
float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
float src2 = ppsrc == 'High/Low' ? low : math.min(close, open)
float ph = ta.pivothigh(src1, prd, prd)
float pl = ta.pivotlow(src2, prd, prd)

// draw Pivot points
plotshape(ph and showpp, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd)
plotshape(pl and showpp, text='L', style=shape.labelup, color=na, textcolor=color.new(color.blue, 0), location=location.belowbar, offset=-prd)

//calculate maximum S/R channel width
prdhighest = ta.highest(300)
prdlowest = ta.lowest(300)
cwidth = (prdhighest - prdlowest) * ChannelW / 100

// get/keep Pivot levels
var pivotvals = array.new_float(0)
var pivotlocs = array.new_float(0)
if ph or pl
array.unshift(pivotvals, ph ? ph : pl)
array.unshift(pivotlocs, bar_index)
for x = array.size(pivotvals) - 1 to 0 by 1
if bar_index - array.get(pivotlocs, x) > loopback // remove old pivot points
array.pop(pivotvals)
array.pop(pivotlocs)
continue
break

//find/create SR channel of a pivot point
get_sr_vals(ind) =>
float lo = array.get(pivotvals, ind)
float hi = lo
int numpp = 0
for y = 0 to array.size(pivotvals) - 1 by 1
float cpp = array.get(pivotvals, y)
float wdth = cpp <= hi ? hi - cpp : cpp - lo
if wdth <= cwidth // fits the max channel width?
if cpp <= hi
lo := math.min(lo, cpp)
lo
else
hi := math.max(hi, cpp)
hi

numpp += 20 // each pivot point added as 20
numpp
[hi, lo, numpp]

// keep old SR channels and calculate/sort new channels if we met new pivot point
var suportresistance = array.new_float(20, 0) // min/max levels
changeit(x, y) =>
tmp = array.get(suportresistance, y * 2)
array.set(suportresistance, y * 2, array.get(suportresistance, x * 2))
array.set(suportresistance, x * 2, tmp)
tmp := array.get(suportresistance, y * 2 + 1)
array.set(suportresistance, y * 2 + 1, array.get(suportresistance, x * 2 + 1))
array.set(suportresistance, x * 2 + 1, tmp)

if ph or pl
supres = array.new_float(0) // number of pivot, strength, min/max levels
stren = array.new_float(10, 0)
// get levels and strengs
for x = 0 to array.size(pivotvals) - 1 by 1
[hi, lo, strength] = get_sr_vals(x)
array.push(supres, strength)
array.push(supres, hi)
array.push(supres, lo)

// add each HL to strengh
for x = 0 to array.size(pivotvals) - 1 by 1
h = array.get(supres, x * 3 + 1)
l = array.get(supres, x * 3 + 2)
s = 0
for y = 0 to loopback by 1
if high[y] <= h and high[y] >= l or low[y] <= h and low[y] >= l
s += 1
s
array.set(supres, x * 3, array.get(supres, x * 3) + s)

//reset SR levels
array.fill(suportresistance, 0)
// get strongest SRs
src = 0
for x = 0 to array.size(pivotvals) - 1 by 1
stv = -1. // value
stl = -1 // location
for y = 0 to array.size(pivotvals) - 1 by 1
if array.get(supres, y * 3) > stv and array.get(supres, y * 3) >= minstrength * 20
stv := array.get(supres, y * 3)
stl := y
stl
if stl >= 0
//get sr level
hh = array.get(supres, stl * 3 + 1)
ll = array.get(supres, stl * 3 + 2)
array.set(suportresistance, src * 2, hh)
array.set(suportresistance, src * 2 + 1, ll)
array.set(stren, src, array.get(supres, stl * 3))

// make included pivot points' strength zero
for y = 0 to array.size(pivotvals) - 1 by 1
if array.get(supres, y * 3 + 1) <= hh and array.get(supres, y * 3 + 1) >= ll or array.get(supres, y * 3 + 2) <= hh and array.get(supres, y * 3 + 2) >= ll
array.set(supres, y * 3, -1)

src += 1
if src >= 10
break

for x = 0 to 8 by 1
for y = x + 1 to 9 by 1
if array.get(stren, y) > array.get(stren, x)
tmp = array.get(stren, y)
array.set(stren, y, array.get(stren, x))
changeit(x, y)


get_level(ind) =>
float ret = na
if ind < array.size(suportresistance)
if array.get(suportresistance, ind) != 0
ret := array.get(suportresistance, ind)
ret
ret

get_color(ind) =>
color ret = na
if ind < array.size(suportresistance)
if array.get(suportresistance, ind) != 0
ret := array.get(suportresistance, ind) > close and array.get(suportresistance, ind + 1) > close ? res_col : array.get(suportresistance, ind) < close and array.get(suportresistance, ind + 1) < close ? sup_col : inch_col
ret
ret

var srchannels = array.new_box(10)
for x = 0 to math.min(9, maxnumsr) by 1
box.delete(array.get(srchannels, x))
srcol = get_color(x * 2)
if not na(srcol)
array.set(srchannels, x, box.new(left=bar_index, top=get_level(x * 2), right=bar_index + 1, bottom=get_level(x * 2 + 1), border_color=srcol, border_width=1, extend=extend.both, bgcolor=srcol))

resistancebroken = false
supportbroken = false

// check if it's not in a channel
not_in_a_channel = true
for x = 0 to math.min(9, maxnumsr) by 1
if close <= array.get(suportresistance, x * 2) and close >= array.get(suportresistance, x * 2 + 1)
not_in_a_channel := false
not_in_a_channel

// if price is not in a channel then check broken ones
if not_in_a_channel
for x = 0 to math.min(9, maxnumsr) by 1
if close[1] <= array.get(suportresistance, x * 2) and close > array.get(suportresistance, x * 2)
resistancebroken := true
resistancebroken
if close[1] >= array.get(suportresistance, x * 2 + 1) and close < array.get(suportresistance, x * 2 + 1)
supportbroken := true
supportbroken

alertcondition(resistancebroken, title='Resistance Broken', message='Resistance Broken')
alertcondition(supportbroken, title='Support Broken', message='Support Broken')
plotshape(showsrbroken and resistancebroken, style=shape.triangleup, location=location.belowbar, color=color.new(color.lime, 0), size=size.tiny)
plotshape(showsrbroken and supportbroken, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny)