Skip to content

Instantly share code, notes, and snippets.

View saleh-old's full-sized avatar

Saleh O saleh-old

View GitHub Profile
def filters(self):
return [
]
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
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
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()
def on_reduced_position(self):
self.stop_loss = self.position.qty, self.position.entry_price
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
@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
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
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:
# trading routes
routes = [
('Bitfinex', 'BTCUSD', '4h', 'TrendFollowingStrategy'),
]
# in case your strategy required extra candles, timeframes, ...
extra_candles = []