Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import sys
- import datetime
- from jesse.strategies import Strategy
- import jesse.indicators as ta
- from jesse import utils
- class ExampleStrategy(Strategy):
- def __init__(self):
- super().__init__()
- print('Initiated ExampleStrategy')
- self.description = ''
- self.vars['last_balance'] = 4000.00
- self.vars['last_position_qty'] = 0.00
- def terminate(self):
- print('Backtest done')
- ##### INDICATORS #####
- # determines uptrend or downtrend based on ema 50 4h
- @property
- def anchor_trend_ema_50_4h(self):
- # use self.get_candles() to get the candles for the anchor timeframe
- ema = ta.ema(self.candles, 50, source_type="close", sequential=True)
- if self.price >= ema[-1]:
- return 1, ema # uptrend
- else:
- return -1, ema #downtrend
- ##### HYPERPARAMETERS #####
- def hyperparameters(self):
- return []
- ##### ENTRY RULES #####
- def should_buy_long(self) -> bool:
- anchor_trend_ema_50_4h, ema_value_50_4h = self.anchor_trend_ema_50_4h
- # if we dont have a buy
- if self.is_long == False:
- # if price > ema_50_4h in current candle, we buy
- if self.price > ema_value_50_4h[-1]:
- return True
- return False
- def should_sell_long(self) -> bool:
- anchor_trend_ema_50_4h, ema_value_50_4h = self.anchor_trend_ema_50_4h
- timeValue = datetime.datetime.fromtimestamp(self.current_candle[0] / 1000)
- if self.position.is_open == True:
- # https://forum.jesse.trade/d/12-what-is-the-code-for-the-emas-crossed-above-and-crossed-below-function-please
- print("\nTime {0:%Y-%m-%d %H:%M:%S}: We have a buy, ema_value_50_4h = {1!r}, price = {2!r}, position = {3!r}".format(timeValue, ema_value_50_4h[-1], self.price, self.position.pnl))
- if self.price < ema_value_50_4h[-1]:
- # print("\nTime {0:%Y-%m-%d %H:%M:%S}: Selling at ema_value_50_4h = {1!r}, price = {2!r}, position = {3!r}".format(timeValue, ema_value_100_1D, self.price, self.position.pnl))
- self.description = '- EMA_50_4h crossed downtrend\n'
- return True
- else:
- print("\nTime {0:%Y-%m-%d %H:%M:%S}: We DO NOT have a buy, ema_value_50_4h = {1!r}, price = {2!r}, position = {3!r}".format(timeValue, ema_value_50_4h[-1], self.price, self.position.pnl))
- return False
- # si no tenemos una compra (posición abierta)
- def should_long(self) -> bool:
- if self.should_buy_long() == True:
- return True
- return False
- def should_short(self) -> bool:
- return False
- def should_cancel(self) -> bool:
- return False
- # Assuming there's an open position, this method is used to update exit points or to add to the size of the position if needed.
- def update_position(self):
- if self.should_buy_long() == True:
- self.go_buy_long(self.position.qty)
- if self.should_sell_long() == True:
- self.vars['last_balance'] = self.update_description_after_should_sell()
- self.take_profit = self.position.qty, self.price
- ##### POSITION SIZE #####
- def go_buy_long(self, input_qty):
- qty = self.vars['last_balance'] / self.price
- self.vars['last_position_qty'] = qty
- entry = self.price
- stop = entry - ((20*self.price) / 100) # 20% stop loss test
- self.buy = qty, entry
- self.stop_loss = qty, stop
- self.description = '\n--- Result Buy ---\n'
- self.description += '- Initial capital: ' + str(self.vars['last_balance']) + '\n'
- self.description += '- Qty bought: ' + str(qty) + '\n'
- def go_long(self):
- self.go_buy_long(1)
- def go_short(self):
- pass
- ############### DESCRIPTION UPDATE (for tradingview) #################
- def update_description_after_should_sell(self) -> float:
- self.description += '\n--- Result Sell ---\n'
- # self.position.entry_price
- # fee = self.balance * self.fee_rate
- # dinero obtenido
- total_after_sell = (self.position.qty * self.price) - ((self.position.qty * self.price) * self.fee_rate)
- total_after_sell = round(total_after_sell, 2)
- if total_after_sell > self.vars['last_balance']:
- total_result_obtained = round(total_after_sell - self.vars['last_balance'], 2)
- self.description += '- Initial capital: ' + str(self.vars['last_balance']) + '\n'
- self.description += '- Total won: +' + str(total_result_obtained) + '\n'
- self.description += '- Result after sell: ' + str(total_after_sell) + '\n'
- else:
- total_result_obtained = round(self.vars['last_balance'] - total_after_sell, 2)
- self.description += '- Initial capital: ' + str(self.vars['last_balance'] ) + '\n'
- self.description += '- Total lost: -' + str(total_result_obtained) + '\n'
- self.description += '- Result after sell: ' + str(total_after_sell) + '\n'
- return total_after_sell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement