OPEN-SOURCE SCRIPT

Enhanced Bollinger Bands Strategy with SL/TP

This Pine Script code implements a Bollinger Bands trading strategy with several enhancements:

strategy(...): Initializes the strategy with a name, sets it to overlay the chart, and includes a slippage parameter (set to 2 ticks) to simulate real-world trading conditions.

Input Parameters:

length: The period for calculating the Simple Moving Average (SMA) that forms the basis of the Bollinger Bands (default: 20).

mult: The standard deviation multiplier that determines the width of the bands (default: 2.0).

enableLong: A boolean (true/false) input to enable or disable long (buy) trades.

enableShort: A boolean input to enable or disable short (sell) trades.

pipValue: Crucially, this input defines the value of a single pip. This is essential for making the strategy work correctly across different currency pairs (e.g., USDJPY, EURUSD, GBPUSD) and even other instruments that don't use "pips" in the same way. For example, on USDJPY, a pip is usually 0.01, while on EURUSD it's 0.0001. The user must set this correctly for the instrument they are trading.

slPips: The stop-loss distance in pips.

tpPips: The take-profit distance in pips.

showBands: A boolean to control whether the Bollinger Bands are plotted on the chart.

showSignals: A boolean to control whether entry signals (triangles) are shown on the chart.

Bollinger Bands Calculation:

basis: Calculates the SMA of the closing price (close) over the specified length.

dev: Calculates the standard deviation of the closing price over the length, multiplied by mult.

upper: Calculates the upper Bollinger Band (SMA + standard deviation).

lower: Calculates the lower Bollinger Band (SMA - standard deviation).

Plotting (Visualization):

plot(showBands ? basis : na, ...): Plots the basis (SMA) line only if showBands is true. na prevents plotting when showBands is false.

u = plot(...), l = plot(...): Plots the upper and lower bands, and stores the plot objects in variables u and l. This is necessary for the fill function.

fill(u, l, ...): Fills the area between the upper and lower bands with a semi-transparent purple color.

Entry Conditions:

longCondition: A long entry signal is generated when the closing price crosses above the lower Bollinger Band and the closing price is above the lower band. The and enableLong part allows the user to disable long entries. The close > lower part makes it less susceptible to false signals.

shortCondition: A short entry signal is generated when the closing price crosses below the upper Bollinger Band and the closing price is below the upper band. The and enableShort part allows the user to disable short entries. The close < upper part makes it less susceptible to false signals.

Position Management (SL/TP Calculation):

calcSlPrice(price, isLong): A function that calculates the stop-loss price. It takes the entry price (price) and a boolean isLong (true for long positions, false for short positions) as input. It correctly uses the pipValue to convert pips to price.

calcTpPrice(price, isLong): A function that calculates the take-profit price, similar to calcSlPrice.

Entry & Exit Logic:

if longCondition: If the long entry condition is met:

strategy.entry("Long", strategy.long, limit=lower): Enters a long position using a limit order at the lower band. This helps to get a slightly better fill price.

strategy.exit("Long Exit", "Long", stop=..., limit=...): Sets a stop-loss and take-profit for the long position, using the calculated SL and TP prices.

if shortCondition: If the short entry condition is met:

strategy.entry("Short", strategy.short, limit=upper): Enters a short position using a limit order at the upper band.

strategy.exit("Short Exit", "Short", stop=..., limit=...): Sets a stop-loss and take-profit for the short position.

Signal Visualization:

plotshape(...): Plots a green upward-pointing triangle below the bar when a long entry signal occurs, and a red downward-pointing triangle above the bar when a short entry signal occurs. The showSignals input controls whether these are displayed.

إخلاء المسؤولية