Implementing Double Supertrend Trading Strategy using Backtrader (2024)

Implementing Double Supertrend Trading Strategy using Backtrader (2)

The Double Supertrend strategy is a technical trading strategy that utilizes the Supertrend indicator, which is a trend-following indicator, twice in order to identify market trends and generate trading signals. The strategy uses one Supertrend indicator with default settings, and another with modified settings, in order to provide a more robust and accurate trend identification. By combining the signals from both indicators, the strategy aims to reduce the number of false signals and increase the chances of profitable trades. Additionally, the strategy also incorporates trendline analysis on the On-Balance Volume (OBV) indicator, which is used to confirm the trend and identify potential trend breaks. This strategy is suitable for traders who prefer a trend-following approach and are comfortable with using technical indicators.

Here is an example of how to implement theDouble Supertrend Trading Strategy using the backtrader library:

import backtrader as bt
import backtrader.indicators as btind

class DoubleSupertrend(bt.Strategy):
params = (
('atr_period', 14),
('atr_multiplier', 3),
('atr_multiplier2', 2),
)

def __init__(self):
self.obv = btind.OnBalanceVolume()
self.supertrend = btind.SuperTrend(self.data.close, self.data.high, self.data.low, period=self.params.atr_period, mul=self.params.atr_multiplier)
self.supertrend2 = btind.SuperTrend(self.data.close, self.data.high, self.data.low, period=self.params.atr_period, mul=self.params.atr_multiplier2)
self.order = None

def next(self):
# Check if OBV trendline breaks
if self.obv.lines.trendline.breakout_sig() != 0:
self.close()

# Check if Supertrends are in the same direction
if self.supertrend > self.data.close and self.supertrend2 > self.data.close:
self.order = self.sell()
elif self.supertrend < self.data.close and self.supertrend2 < self.data.close:
self.order = self.buy()

# Set stop loss and take profit
if self.order:
self.stop_loss(self.data.low[0])
self.take_profit(self.supertrend2[0])

The above code is a Python implementation of the Double Supertrend strategy using the Backtrader library. The strategy is defined in…

Implementing Double Supertrend Trading Strategy using Backtrader (2024)
Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5337

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.