Hi guys,
I want to share with you my first trading strategy for BTC 4H.
Sorry for putting the code in Pine Script but I cant test it in 4h timeframe in PRT.
I just wanted to share with you in case it is interesting for someone that can translate it to PRT code and test it with more data.
//Juan Diaz Maldonado
//@version=6
strategy(title = "Bollinger Bands Width Strategy @ JDM", shorttitle = "BBW Strat @ JDM", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value = 100, initial_capital =1000, currency = currency.USD, slippage = 10, commission_type = strategy.commission.percent, commission_value = 0.1)
// ------------------------- Paramethers -------------------------
// Bollinger bands paramethers
length = input.int(22, minval=1, title="Length")
src = input.source(close, title="Source")
mult = input.float(2, minval=0.001, maxval=50, title="StdDev")
// Bollinger bands Width paramethers
ncandles = input.int(120, minval=1, title="n candles") // number of candles for normalized BB width
compthreshold = input.int(20, minval=1, title="Compresion Threshold") // compression band under which we will open trades
// ------------------------- Bollinger bands calculation -------------------------
// Bollinger bands
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
// Bollinger bands width
bbWidth = (upper - lower) / basis
// Normalize BB widht
bbWidthMax = ta.highest(bbWidth, ncandles) // maximum width of ncandles
bbWidthMin = ta.lowest(bbWidth, ncandles) // minimum width of ncandles
bbWidthNormalized = ((bbWidth - bbWidthMin) / (bbWidthMax - bbWidthMin)) * (100) // bollinger bands width normalized
// ------------------------- Long entry and exits and Stop Loss if any -------------------------
// Longs condition
longCondition = src > basis and src < upper and volume > ta.sma(volume, 20) and bbWidthNormalized < compthreshold
if (longCondition)
strategy.entry("Long", strategy.long)
// Exit longs condition
longExitCondition = ta.crossunder(src, lower)
if (longExitCondition)
strategy.close("Long")
// ------------------------- Gráficos -------------------------
// Representación de la amplitud normalizada (oscila entre -20 y 120)
plot(bbWidthNormalized, title="BB Width Normalized", color=color.new(color.orange, 0), linewidth=1)
// Líneas de referencia fijas en 100 y 0 para %B
hline(100, title="Upper Band", color=color.new(color.black, 0), linestyle=hline.style_solid)
hline(compthreshold, title="Threshold", color=color.new(color.blue, 0), linestyle=hline.style_solid)
hline(0, title="Lower Band", color=color.new(color.black, 0), linestyle=hline.style_solid)
Regards,