Gabri
Thank you sir. Do I need to import the Talib library or anything?
I attach the error code below and the strategy code as well.. sorry
============================== EXCEPTION TRACEBACK:
File "/usr/local/bin/jesse", line 8, in <module>
sys.exit(cli())
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/jesse/__init__.py", line 286, in backtest
backtest_mode.run(start_date, finish_date, chart=chart, tradingview=tradingview)
File "/usr/local/lib/python3.8/dist-packages/jesse/modes/backtest_mode/__init__.py", line 58, in run
simulator(candles)
File "/usr/local/lib/python3.8/dist-packages/jesse/modes/backtest_mode/__init__.py", line 225, in simulator
r.strategy._execute()
File "/usr/local/lib/python3.8/dist-packages/jesse/strategies/Strategy.py", line 817, in _execute
self._check()
File "/usr/local/lib/python3.8/dist-packages/jesse/strategies/Strategy.py", line 608, in _check
if self.should_short() and self.should_long():
File "/home/mybot/strategies/ExampleStrategySimpleEMA/__init__.py", line 14, in should_short
if self.price > self.EMA_fast:
File "/home/mybot/strategies/ExampleStrategySimpleEMA/__init__.py", line 65, in EMA_fast
ema_rsi = talib.EMA(rsi14, 20)
=========================================================================
Uncaught Exception: NameError: name 'talib' is not defined
root@cb969121dfbe:/home/mybot#
from jesse.strategies import Strategy
import jesse.indicators as ta
from jesse import utils
class ExampleStrategySimpleEMA(Strategy):
def should_long(self) -> bool:
# if the fast EMA is above the slow EMA we assume its a long trend
if self.price > self.EMA_fast:
return True
return False
def should_short(self) -> bool:
if self.price > self.EMA_fast:
return True
return False
def should_cancel(self) -> bool:
return True
def go_long(self):
entry = self.price
stop = self.price - self.atr * 2
qty = utils.risk_to_qty(self.capital, 2, entry, stop)
take_profit_price = entry + self.atr
self.buy = qty, entry
self.take_profit = qty, take_profit_price
self.stop_loss = qty, stop
def go_short(self):
# Just go in where the price is right now.
entry = self.price
# Average True Range for stop loss - if the market moves 2 times the ATR of the last 20 candles against my position get out.
stop = self.price + self.atr * 2
# To avoid losing everything in one trade, you should never risk all your money. That for we calculate the quantity we trade from the risk. In this case we go for a risk of 2.
qty = utils.risk_to_qty(self.capital, 2, entry, stop)
# Here we set the profit price, where we exit the trade. In this case we exit if the price moves 1 times the ATR in our direction.
take_profit_price = entry - self.atr
# here we set the stuff that we calculated before
self.sell = qty, entry
self.take_profit = qty, take_profit_price
self.stop_loss = qty, stop
# optionally you could remove the self.take_profit part and exit on a condition with update_position.
# def update_position(self):
# if self.is_long and self.EMA_fast < self.EMA_slow:
# self.liquidate()
#
# if self.is_short and self.EMA_fast > self.EMA_slow:
# self.liquidate()
################################################################
# # # # # # # # # # # # # indicators # # # # # # # # # # # # # #
################################################################
@property
def EMA_fast(self):
rsi14 = ta.rsi(self.candles, 14, True)
ema_rsi = talib.EMA(rsi14, 20)
print('rsi14:{} - ema_rsi:{}'.format(rsi14[-1], ema_rsi[-1]))
@property
def atr(self):
return ta.atr(self.candles, period=20)