in pinescript im[lement the folloing bollinger bands strategy
The middle track =the average of the last n columns of closing prices of the underlying sequence.
The upper track =the middle track + k * the standard deviation of the closing price of the last n columns.
The lower track=the middle track – k * the standard deviation of the closing price of the last n columns.
4) admission rules:
Long: When the price breaks through the upper track, long entry immediately.
Short: When the price breaks through the down track, short entry immediately.
5)stop-loss rules:
Long position: In the case of long positions, if the price is adjusted downward from the highest point by a certain
percentage k, then the position will be closed.
Short position: In the case of short position, if the price is adjusted upward from the lowest point by a certain percentage
k, then the position will be closed.
Assistant
Sure, here is the implementation of the Bollinger Bands strategy in PineScript based on your specifications:
pine
//@version=4
strategy("Bollinger Bands Strategy with Percentage Stop-Loss", overlay=true)
// User Inputs
len = input(20, title="BB Length")
k_std = input(2.0, title="BB Std Dev Multiplier")
sl_percent = input(5.0, title="Stop Loss Retracement Percent (%)", minval=0.0)
// Calculate Bollinger Bands
middle_band = sma(close, len)
std_dev = stdev(close, len)
upper_band = middle_band + k_std * std_dev
lower_band = middle_band - k_std * std_dev
// Plot Bollinger Bands
plot(middle_band, title="Middle Band", color=color.blue)
plot(upper_band, title="Upper Band", color=color.red)
plot(lower_band, title="Lower Band", color=color.green)
// Entry Conditions
long_entry = crossover(close, upper_band)
short_entry = crossunder(close, lower_band)
// Execute Entries
if (long_entry)
strategy.entry("Long", strategy.long)
if (short_entry)
strategy.entry("Short", strategy.short)
// Initialize variables to hold highest/lowest price since entry
var float highest_since_entry = na
var float lowest_since_entry = na
// Detect position entries and exits
entered_long = strategy.position_size > 0 and strategy.position_size[1] <= 0
entered_short = strategy.position_size < 0 and strategy.position_size[1] >= 0
exit_position = strategy.position_size == 0 and strategy.position_size[1] != 0
// Update highest price for long positions
if (entered_long)
highest_since_entry := high
else if (strategy.position_size > 0)
highest_since_entry := max(highest_since_entry, high)
else if (exit_position)
highest_since_entry := na
// Update lowest price for short positions
if (entered_short)
lowest_since_entry := low
else if (strategy.position_size < 0)
lowest_since_entry := min(lowest_since_entry, low)
else if (exit_position)
lowest_since_entry := na
// Calculate retracement for stop-loss
retracement_long = na
retracement_short = na
if (strategy.position_size > 0)
retracement_long := (highest_since_entry - close) / highest_since_entry * 100
if (retracement_long >= sl_percent)
strategy.close("Long", comment="Long Stop Loss")
if (strategy.position_size < 0)
retracement_short := (close - lowest_since_entry) / lowest_since_entry * 100
if (retracement_short >= sl_percent)
strategy.close("Short", comment="Short Stop Loss")
Explanation:
Inputs: We start by defining inputs for the Bollinger Bands length (len), the standard deviation multiplier (k_std), and the stop-loss percentage (sl_percent).
Calculations: We calculate the middle band (simple moving average), the standard deviation, and then the upper and lower bands according to your specifications.
Plotting: The middle, upper, and lower bands are plotted on the chart for visualization.
Entry Conditions:
Long Entry: We enter a long position when the price closes above the upper band.
Short Entry: We enter a short position when the price closes below the lower band.
Tracking Highest/Lowest Prices Since Entry:
For long positions, we keep track of the highest price since entering the trade.
For short positions, we keep track of the lowest price since entering the trade.
These are stored in the variables highest_since_entry and lowest_since_entry.
Calculating Retracement for Stop-Loss:
For long positions, we calculate how much the price has retraced from the highest point.
For short positions, we calculate how much the price has retraced from the lowest point.
If the retracement reaches or exceeds the sl_percent, we close the position.
Exits:
Positions are closed when the stop-loss condition is met based on the retracement percentage.
This script follows your strategy rules closely. It enters trades when the price breaks through the Bollinger Bands and sets a stop-loss based on a percentage retracement from the highest or lowest price since entry.