-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Strategy, Custom Indicator, fixes
- Loading branch information
aLca
committed
Jun 5, 2024
1 parent
be3bd8a
commit 8c931d8
Showing
3 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import backtrader as bt | ||
|
||
class SuperTrend(bt.Indicator): | ||
lines = ('super_trend',) | ||
params = (('period', 7), | ||
('multiplier', 3), | ||
) | ||
plotlines = dict( | ||
super_trend=dict( | ||
_name='SuperTrend', | ||
color='cyan', | ||
alpha=1 | ||
) | ||
) | ||
plotinfo = dict(subplot=False) | ||
|
||
def __init__(self): | ||
self.st = [0] | ||
self.finalupband = [0] | ||
self.finallowband = [0] | ||
self.addminperiod(self.p.period) | ||
atr = bt.ind.ATR(self.data, period=self.p.period) | ||
self.upperband = (self.data.high + self.data.low) / 2 + self.p.multiplier * atr | ||
self.lowerband = (self.data.high + self.data.low) / 2 - self.p.multiplier * atr | ||
|
||
def next(self): | ||
pre_upband = self.finalupband[0] | ||
pre_lowband = self.finallowband[0] | ||
if self.upperband[0] < self.finalupband[-1] or self.data.close[-1] > self.finalupband[-1]: | ||
self.finalupband[0] = self.upperband[0] | ||
else: | ||
self.finalupband[0] = self.finalupband[-1] | ||
if self.lowerband[0] > self.finallowband[-1] or self.data.close[-1] < self.finallowband[-1]: | ||
self.finallowband[0] = self.lowerband[0] | ||
else: | ||
self.finallowband[0] = self.finallowband[-1] | ||
if self.data.close[0] <= self.finalupband[0] and ((self.st[-1] == pre_upband)): | ||
self.st[0] = self.finalupband[0] | ||
self.lines.super_trend[0] = self.finalupband[0] | ||
elif (self.st[-1] == pre_upband) and (self.data.close[0] > self.finalupband[0]): | ||
self.st[0] = self.finallowband[0] | ||
self.lines.super_trend[0] = self.finallowband[0] | ||
elif (self.st[-1] == pre_lowband) and (self.data.close[0] >= self.finallowband[0]): | ||
self.st[0] = self.finallowband[0] | ||
self.lines.super_trend[0] = self.finallowband[0] | ||
elif (self.st[-1] == pre_lowband) and (self.data.close[0] < self.finallowband[0]): | ||
self.st[0] = self.finalupband[0] | ||
self.lines.super_trend[0] = self.st[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import backtrader as bt | ||
from live_strategys.live_functions import BaseStrategy | ||
from custom_indicators.SuperTrend import SuperTrend | ||
|
||
class SuperSTrend_Scalper(BaseStrategy): | ||
params = ( | ||
("dca_threshold", 2.5), | ||
("take_profit", 4), | ||
('percent_sizer', 0.01), # 0.01 -> 1% | ||
# Trend Strenght | ||
("adx_period", 13), | ||
("adx_strength", 31), | ||
("di_period", 14), | ||
("adxth", 25), | ||
|
||
# Supertrends | ||
("st_fast", 2), | ||
('st_fast_multiplier', 3), | ||
("st_slow", 6), | ||
('st_slow_multiplier', 7), | ||
# RevFinder | ||
('reversal_lookback', 10), | ||
('reversal_malen', 40), | ||
('reversal_mult', 2.2), | ||
('reversal_rangethreshold', 0.9), | ||
('debug', False), | ||
("backtest", None) | ||
) | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.adx = bt.indicators.ADX(self.data, plot=False) | ||
self.plusDI = bt.indicators.PlusDI(self.data, plot=False) | ||
self.minusDI = bt.indicators.MinusDI(self.data, plot=False) | ||
self.supertrend_fast = SuperTrend(period=self.p.st_fast, multiplier=self.p.st_fast_multiplier, plotname='SuperTrend Fast: ', plot=True) | ||
self.supertrend_slow = SuperTrend(period=self.p.st_slow, multiplier=self.p.st_slow_multiplier, plotname='SuperTrend Slow: ', plot=True) | ||
self.supertrend_uptrend_signal = bt.indicators.CrossOver(self.supertrend_fast, self.supertrend_slow, plot=False) | ||
# self.supertrend_downtrend_signal = bt.indicators.CrossDown(self.supertrend_fast, self.supertrend_slow, plot=False) # NOT USED IN THIS EXAMPLE | ||
self.DCA = True | ||
self.buy_executed = False | ||
self.conditions_checked = False | ||
|
||
def buy_or_short_condition(self): | ||
if ( | ||
self.adx[0] >= self.params.adxth and \ | ||
self.minusDI[0] > self.params.adxth and \ | ||
self.plusDI[0] < self.params.adxth and \ | ||
self.supertrend_uptrend_signal | ||
): | ||
|
||
if self.params.backtest == False: | ||
self.entry_prices.append(self.data.close[0]) | ||
self.sizes.append(self.amount) | ||
self.load_trade_data() | ||
self.rabbit.send_jrr_buy_request(exchange=self.exchange, account=self.account, asset=self.asset, amount=self.amount) | ||
self.buy_executed = True | ||
self.conditions_checked = True | ||
elif self.params.backtest == True: | ||
self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) | ||
self.take_profit_price = self.data.close[-1] * (1 + self.params.take_profit / 100) | ||
self.buy_executed = True | ||
self.conditions_checked = True | ||
|
||
def dca_or_short_condition(self): | ||
if (self.position and \ | ||
self.adx[0] >= self.params.adxth and \ | ||
self.minusDI[0] > self.params.adxth and \ | ||
self.plusDI[0] < self.params.adxth and \ | ||
self.supertrend_uptrend_signal | ||
): | ||
|
||
if self.params.backtest == False: | ||
self.entry_prices.append(self.data.close[0]) | ||
self.sizes.append(self.amount) | ||
self.load_trade_data() | ||
self.rabbit.send_jrr_buy_request(exchange=self.exchange, account=self.account, asset=self.asset, amount=self.amount) | ||
self.buy_executed = True | ||
self.conditions_checked = True | ||
elif self.params.backtest == True: | ||
self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) | ||
self.take_profit_price = self.data.close[-1] * (1 + self.params.take_profit / 100) | ||
self.buy_executed = True | ||
self.conditions_checked = True | ||
|
||
|
||
def sell_or_cover_condition(self): | ||
if self.buy_executed and self.data.close[0] >= self.take_profit_price: | ||
if self.params.backtest == False: | ||
self.rabbit.send_jrr_close_request(exchange=self.exchange, account=self.account, asset=self.asset) | ||
elif self.params.backtest == True: | ||
self.close() | ||
self.reset_position_state() | ||
self.buy_executed = False | ||
self.conditions_checked = True | ||
|
||
def next(self): | ||
BaseStrategy.next(self) |