OPEN-SOURCE SCRIPT

Bollinger Bands Strategy with SL/TP

Title: Bollinger Bands Strategy with Stop Loss/Take Profit

Description:

This strategy implements a classic Bollinger Bands trading system with integrated Stop Loss (SL) and Take Profit (TP) levels. It identifies potential entry points based on price crossing the upper or lower Bollinger Bands and automatically sets SL/TP orders to manage risk and secure profits.

Key Features:

Bollinger Bands Calculation: Calculates the upper and lower Bollinger Bands using a Simple Moving Average (SMA) of the price and a standard deviation multiplier.

Customizable Parameters:

Source: Price source to use for calculations (default: close price).

Length: SMA period for the Bollinger Bands (default: 20).

Mult: Standard deviation multiplier for the Bollinger Bands (default: 2.0).

Strategy Direction: Specifies the trading direction:

-1: Short only.

0: Long and Short (both directions).

1: Long only.

Stop Loss (pips): Stop Loss distance from entry price in pips (default: 10).

Take Profit (pips): Take Profit distance from entry price in pips (default: 20).

Entry Logic:

Long Entry: Enters a long position when the price crosses above the lower Bollinger Band.

Short Entry: Enters a short position when the price crosses below the upper Bollinger Band.

Exit Logic (Stop Loss/Take Profit):

Automatically places Stop Loss and Take Profit orders upon entry.

SL/TP levels are calculated based on the user-defined sl_pips and tp_pips from the entry price.

Uses strategy.exit with stop and limit parameters to define the SL/TP levels.

Order Cancellation:

Uses oca_type=strategy.oca.cancel to ensure that if one entry order is filled, the other is automatically cancelled. This avoids unwanted positions.

Pip to Price Conversion:

The f_pips_to_price function converts the pip value to the corresponding price change for the specific instrument, ensuring accurate SL/TP placement.

Strategy Direction Control:

The direction input allows users to specify whether the strategy should trade long, short, or both directions. This is helpful for adapting the strategy to different market conditions or biases.

How to Use:

Add the strategy to your TradingView chart.

Adjust the input parameters (Length, Mult, SL pips, TP pips, direction) to suit your trading style and the specific market you are trading.

Backtest the strategy on historical data to evaluate its performance.

Use the strategy with caution and always manage your risk appropriately.

Disclaimer:

This strategy is for educational and informational purposes only. Past performance is not indicative of future results. Trading involves risk, and you should only trade with capital you can afford to lose. Always conduct thorough research and due diligence before making any trading decisions. The user assumes all responsibility for any gains or losses incurred using this strategy.

Aviso legal