Guest User

jesse_ema_50_4h_strategy

a guest
Feb 10th, 2021
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2. import sys
  3. import datetime
  4. from jesse.strategies import Strategy
  5. import jesse.indicators as ta
  6. from jesse import utils
  7.  
  8.  
  9. class ExampleStrategy(Strategy):
  10.     def __init__(self):
  11.         super().__init__()
  12.         print('Initiated ExampleStrategy')
  13.  
  14.         self.description = ''
  15.         self.vars['last_balance'] = 4000.00
  16.         self.vars['last_position_qty'] = 0.00
  17.  
  18.     def terminate(self):
  19.         print('Backtest done')
  20.  
  21.     ##### INDICATORS #####
  22.     # determines uptrend or downtrend based on ema 50 4h
  23.     @property
  24.     def anchor_trend_ema_50_4h(self):
  25.         # use self.get_candles() to get the candles for the anchor timeframe
  26.         ema = ta.ema(self.candles, 50, source_type="close", sequential=True)
  27.  
  28.         if self.price >= ema[-1]:
  29.             return 1, ema  # uptrend
  30.         else:
  31.             return -1, ema  #downtrend
  32.  
  33.  
  34.     ##### HYPERPARAMETERS #####
  35.     def hyperparameters(self):
  36.         return []
  37.  
  38.  
  39.     ##### ENTRY RULES #####
  40.     def should_buy_long(self) -> bool:
  41.         anchor_trend_ema_50_4h, ema_value_50_4h = self.anchor_trend_ema_50_4h
  42.         # if we dont have a buy
  43.         if self.is_long == False:
  44.             # if price > ema_50_4h  in current candle, we buy
  45.             if self.price > ema_value_50_4h[-1]:
  46.                 return True
  47.  
  48.         return False
  49.  
  50.     def should_sell_long(self) -> bool:
  51.         anchor_trend_ema_50_4h, ema_value_50_4h = self.anchor_trend_ema_50_4h
  52.         timeValue = datetime.datetime.fromtimestamp(self.current_candle[0] / 1000)
  53.  
  54.         if self.position.is_open == True:
  55.             # https://forum.jesse.trade/d/12-what-is-the-code-for-the-emas-crossed-above-and-crossed-below-function-please
  56.             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))
  57.  
  58.             if self.price < ema_value_50_4h[-1]:
  59.                 # 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))
  60.                 self.description = '- EMA_50_4h crossed downtrend\n'
  61.                 return True
  62.         else:
  63.             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))
  64.  
  65.         return False
  66.  
  67.     # si no tenemos una compra (posición abierta)
  68.     def should_long(self) -> bool:
  69.         if self.should_buy_long() == True:
  70.             return True
  71.         return False
  72.  
  73.     def should_short(self) -> bool:
  74.         return False
  75.  
  76.     def should_cancel(self) -> bool:
  77.         return False
  78.  
  79.     # 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.
  80.     def update_position(self):
  81.         if self.should_buy_long() == True:
  82.             self.go_buy_long(self.position.qty)
  83.  
  84.         if self.should_sell_long() == True:
  85.             self.vars['last_balance'] = self.update_description_after_should_sell()
  86.             self.take_profit = self.position.qty, self.price
  87.  
  88.  
  89.     ##### POSITION SIZE #####
  90.     def go_buy_long(self, input_qty):
  91.         qty = self.vars['last_balance'] / self.price
  92.         self.vars['last_position_qty'] = qty
  93.         entry = self.price
  94.         stop = entry - ((20*self.price) / 100)  # 20% stop loss test
  95.         self.buy = qty, entry
  96.         self.stop_loss = qty, stop
  97.  
  98.         self.description = '\n--- Result Buy ---\n'
  99.         self.description += '- Initial capital: ' + str(self.vars['last_balance']) + '\n'
  100.         self.description += '- Qty bought: ' + str(qty) + '\n'
  101.  
  102.     def go_long(self):
  103.         self.go_buy_long(1)
  104.  
  105.     def go_short(self):
  106.         pass
  107.  
  108.  
  109.     ############### DESCRIPTION UPDATE (for tradingview) #################
  110.     def update_description_after_should_sell(self) -> float:
  111.         self.description += '\n--- Result Sell ---\n'
  112.  
  113.         # self.position.entry_price
  114.         # fee = self.balance * self.fee_rate
  115.         # dinero obtenido
  116.         total_after_sell = (self.position.qty * self.price) - ((self.position.qty * self.price) * self.fee_rate)
  117.         total_after_sell = round(total_after_sell, 2)
  118.  
  119.         if total_after_sell > self.vars['last_balance']:
  120.             total_result_obtained = round(total_after_sell - self.vars['last_balance'], 2)
  121.             self.description += '- Initial capital: ' + str(self.vars['last_balance']) + '\n'
  122.             self.description += '- Total won: +' + str(total_result_obtained) + '\n'
  123.             self.description += '- Result after sell: ' + str(total_after_sell) + '\n'
  124.         else:
  125.             total_result_obtained = round(self.vars['last_balance'] - total_after_sell, 2)
  126.             self.description += '- Initial capital: ' + str(self.vars['last_balance'] ) + '\n'
  127.             self.description += '- Total lost: -' + str(total_result_obtained) + '\n'
  128.             self.description += '- Result after sell: ' + str(total_after_sell) + '\n'
  129.  
  130.         return total_after_sell
  131.  
  132.  
  133.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×