This file contains hidden or 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
def filters(self): | |
return [ | |
] |
This file contains hidden or 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
def reward_to_risk_filter(self): | |
profit = abs(self.average_entry_price - self.average_take_profit) | |
loss = abs(self.average_entry_price - self.average_stop_loss) | |
win_loss_ratio = profit / loss | |
return win_loss_ratio > 1 |
This file contains hidden or 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
def go_long(self): | |
# entry: the high of the current candle | |
entry = self.high | |
stop = entry - ta.atr(self.candles)*3 | |
qty = utils.risk_to_qty(self.capital, 5, entry, stop) | |
last_20_highs = self.candles[-20:, 3] | |
previous_high = np.max(last_20_highs) | |
self.buy = qty, entry |
This file contains hidden or 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
def update_position(self): | |
# the RSI logic is intended for the second half of the trade | |
if self.is_reduced: | |
rsi = ta.rsi(self.candles) | |
if self.is_long and rsi > 80: | |
# self.liquidate() closes the position with a market order | |
self.liquidate() | |
elif self.is_short and rsi < 20: | |
self.liquidate() |
This file contains hidden or 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
def on_reduced_position(self): | |
self.stop_loss = self.position.qty, self.position.entry_price |
This file contains hidden or 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
def go_long(self): | |
entry = self.high | |
stop = entry - ta.atr(self.candles)*3 | |
qty = utils.risk_to_qty(self.capital, 5, entry, stop, fee_rate=self.fee_rate) | |
# highest price of the last 20 bars | |
last_20_highs = self.candles[-20:, 3] | |
previous_high = np.max(last_20_highs) | |
self.buy = qty, entry |
This file contains hidden or 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
@property | |
def trend(self): | |
short_ema = ta.ema(self.candles, 50) | |
long_ema = ta.ema(self.candles, 100) | |
longer_ema = ta.ema(self.candles, 200) | |
if short_ema > long_ema > longer_ema: | |
return 1 | |
elif short_ema < long_ema < longer_ema: | |
return -1 |
This file contains hidden or 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
def should_long(self) -> bool: | |
return self.current_candle_touches_long_ema and self.trend == 1 | |
def should_short(self) -> bool: | |
return self.current_candle_touches_long_ema and self.trend == -1 |
This file contains hidden or 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
from jesse.strategies import Strategy | |
import jesse.indicators as ta | |
from jesse import utils | |
class TrendFollowingStrategy(Strategy): | |
def should_long(self) -> bool: | |
return False | |
def should_short(self) -> bool: |
This file contains hidden or 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
# trading routes | |
routes = [ | |
('Bitfinex', 'BTCUSD', '4h', 'TrendFollowingStrategy'), | |
] | |
# in case your strategy required extra candles, timeframes, ... | |
extra_candles = [] |