Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ccxt.async_support
- import time
- import asyncio
- import numpy as np
- import itertools
- import emails
- #EMAIL_ADDRESSES = ('carlo.revelli@berkeley.edu', 'idries.erfan@yahoo.co.uk',
- # 'jrctaylor98@googlemail.com')
- EMAIL_ADDRESSES = ('carlo.revelli@yahoo.co.uk',)
- EXCHANGES = []
- loop = asyncio.get_event_loop()
- def Meta(name):
- ex = type(name, (SubExchange, getattr(ccxt, name)), {})()
- EXCHANGES.append(ex)
- return ex
- class SubExchange:
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.async_version = getattr(ccxt.async_support, self.id)()
- async def get_week_opens(self, symbol):
- ohlcv = await self.async_version.fetch_ohlcv(symbol, '1d')
- prices = [(d[0], d[2]) for d in ohlcv] # use high/open for candles
- return [prices[i] for i in range(0, len(prices), 7)]
- async def get_consecutive_candles(self, symbol):
- prices = await self.get_week_opens(symbol)
- last_seven_weeks = sorted(prices, key=lambda x: x[0], reverse=True)[:7]
- offset = (now() - last_seven_weeks[0][0]) / (60 * 60 * 1000)
- print(f'{offset:.2f} hours since last candle')
- lsw_prices = np.array([i[1] for i in last_seven_weeks])
- diff = lsw_prices[:-1] - lsw_prices[1:]
- positive = diff > 0
- candles = count_consective(positive)
- print(f'{symbol} {candles} consecutive green candle(s) in the last week')
- return symbol, candles
- def all_cc(self):
- self.load_markets()
- result = loop.run_until_complete(asyncio.gather(
- *[self.get_consecutive_candles(m) for m in self.markets], return_exceptions=True
- ))
- output = {}
- for x in result:
- if not isinstance(x, tuple):
- # exception
- continue
- coin, no_candles = x
- output[coin] = no_candles
- return output
- def __repr__(self):
- return self.name
- def count_consective(l):
- """Takes a list and returns number of consecutive positive numbers
- >>> count_consective([True, True, True])
- 3
- >>> count_consective([False, True, False])
- 1
- """
- m = 0
- for group, it in itertools.groupby(l):
- if group:
- m = max(len(list(it)), m)
- return m
- def now():
- return int(time.time() * 1000)
- def close():
- for ex in EXCHANGES:
- loop.run_until_complete(ex.async_version.close())
- loop.close()
- while loop.is_running():
- time.sleep(0.1)
- if __name__ == '__main__':
- bittrex = Meta('bittrex')
- consecutive_candles = bittrex.all_cc()
- symbol = max(consecutive_candles, key=consecutive_candles.get)
- body = f"{symbol} has the most consecutive candles on {bittrex}" \
- f"({consecutive_candles[symbol]}) in the last seven week.\n\n{now()}"
- for email_address in EMAIL_ADDRESSES:
- emails.send_email(email_address, 'carlo.revelli@gicoconsultants.com', 'Daily market summary:', body)
- close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement