Advertisement
Guest User

cryptolords

a guest
Nov 24th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import ccxt.async_support
  2.  
  3. import time
  4. import asyncio
  5. import numpy as np
  6. import itertools
  7.  
  8. import emails
  9.  
  10. #EMAIL_ADDRESSES = ('carlo.revelli@berkeley.edu', 'idries.erfan@yahoo.co.uk',
  11. #                   'jrctaylor98@googlemail.com')
  12. EMAIL_ADDRESSES = ('carlo.revelli@yahoo.co.uk',)
  13. EXCHANGES = []
  14.  
  15. loop = asyncio.get_event_loop()
  16.  
  17.  
  18. def Meta(name):
  19.     ex = type(name, (SubExchange, getattr(ccxt, name)), {})()
  20.     EXCHANGES.append(ex)
  21.     return ex
  22.  
  23.  
  24. class SubExchange:
  25.     def __init__(self, *args, **kwargs):
  26.         super().__init__(*args, **kwargs)
  27.         self.async_version = getattr(ccxt.async_support, self.id)()
  28.  
  29.     async def get_week_opens(self, symbol):
  30.         ohlcv = await self.async_version.fetch_ohlcv(symbol, '1d')
  31.         prices = [(d[0], d[2]) for d in ohlcv]  # use high/open for candles
  32.         return [prices[i] for i in range(0, len(prices), 7)]
  33.  
  34.     async def get_consecutive_candles(self, symbol):
  35.         prices = await self.get_week_opens(symbol)
  36.         last_seven_weeks = sorted(prices, key=lambda x: x[0], reverse=True)[:7]
  37.         offset = (now() - last_seven_weeks[0][0]) / (60 * 60 * 1000)
  38.         print(f'{offset:.2f} hours since last candle')
  39.         lsw_prices = np.array([i[1] for i in last_seven_weeks])
  40.         diff = lsw_prices[:-1] - lsw_prices[1:]
  41.         positive = diff > 0
  42.         candles = count_consective(positive)
  43.         print(f'{symbol} {candles} consecutive green candle(s) in the last week')
  44.         return symbol, candles
  45.  
  46.     def all_cc(self):
  47.         self.load_markets()
  48.         result = loop.run_until_complete(asyncio.gather(
  49.             *[self.get_consecutive_candles(m) for m in self.markets], return_exceptions=True
  50.         ))
  51.         output = {}
  52.         for x in result:
  53.             if not isinstance(x, tuple):
  54.                 # exception
  55.                 continue
  56.             coin, no_candles = x
  57.             output[coin] = no_candles
  58.         return output
  59.  
  60.     def __repr__(self):
  61.         return self.name
  62.  
  63.  
  64. def count_consective(l):
  65.     """Takes a list and returns number of consecutive positive numbers
  66.    >>> count_consective([True, True, True])
  67.    3
  68.    >>> count_consective([False, True, False])
  69.    1
  70.    """
  71.     m = 0
  72.     for group, it in itertools.groupby(l):
  73.         if group:
  74.             m = max(len(list(it)), m)
  75.     return m
  76.  
  77.  
  78. def now():
  79.     return int(time.time() * 1000)
  80.  
  81.  
  82. def close():
  83.     for ex in EXCHANGES:
  84.         loop.run_until_complete(ex.async_version.close())
  85.     loop.close()
  86.     while loop.is_running():
  87.         time.sleep(0.1)
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     bittrex = Meta('bittrex')
  92.     consecutive_candles = bittrex.all_cc()
  93.     symbol = max(consecutive_candles, key=consecutive_candles.get)
  94.     body = f"{symbol} has the most consecutive candles on {bittrex}" \
  95.            f"({consecutive_candles[symbol]}) in the last seven week.\n\n{now()}"
  96.     for email_address in EMAIL_ADDRESSES:
  97.         emails.send_email(email_address, 'carlo.revelli@gicoconsultants.com', 'Daily market summary:', body)
  98.     close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement