def handle(self, *args, **options): # instance variable unique to each instance xchange_coins = json.loads(self.getCoins()) for xchange_coin in xchange_coins: if xchange_coin['id'] in ['USD', 'EUR', 'GBP']: continue try: currency = Currency.objects.get(symbol=xchange_coin['id']) print(xchange_coin['id'] + " exists") except ObjectDoesNotExist as error: print(xchange_coin['id'] + " does not exist in our currency list..adding" + error) currency = Currency() symbol = SymbolName.SymbolName(xchange_coin['id']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=symbol) print("added") except: print("failed adding {0}".format(xchange_coin['id'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=7) while end_date < start_date: sleep(2) prices = self.getPrice( currency.symbol, start_date=self.__date_to_iso8601( date_time=(start_date - timedelta(days=1)).timetuple()), end_date=self.__date_to_iso8601( date_time=start_date.timetuple()), granularity=86400) coins = Coins.Coins() if len(prices) > 0: for price in prices: coin = coins.get_coin_type(symbol=currency.symbol, time=int(price[0]), exchange=self.xchange) coin.time = int(price[0]) coin.low = float(price[1]) coin.high = float(price[2]) coin.open = float(price[3]) coin.close = float(price[4]) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def handle(self, *args, **options): self.xchange = Xchange.objects.get(pk=XCHANGE['KRAKEN']) try: self.kraken = krakenex.API() except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) self.comparison_currency = 'USD' xchange_coins = self.getCoins() for xchange_coin in xchange_coins: try: currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['altname']) print(xchange_coins[xchange_coin]['altname'] + " exists") except ObjectDoesNotExist as error: print(xchange_coins[xchange_coin]['altname'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName( xchange_coins[xchange_coin]['altname']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['altname']) print("added") except: print("failed adding {0}".format( xchange_coins[xchange_coin]['altname'])) continue prices = self.getPrice(currency.symbol) if prices != 0: for price in prices: coins = Coins.Coins() coin = coins.get_coin_type( symbol=xchange_coins[xchange_coin]['altname'], time=int(price[0]), exchange=self.xchange) coin.time = int(price[0]) coin.open = float(price[1]) coin.close = float(price[4]) coin.high = float(price[2]) coin.low = float(price[3]) coin.volume = float(price[6]) coin.xchange = self.xchange coin.currency = currency coin.save()
def handle(self, *args, **options): xchange_coins = self.getCoins() for xchange_coin in xchange_coins: try: currency = Currency.objects.get(symbol=xchange_coins[xchange_coin]['Symbol']) print(xchange_coins[xchange_coin]['Symbol'] + " exists") except ObjectDoesNotExist as error: print(xchange_coins[xchange_coin]['Symbol'] + " does not exist in our currency list..adding" + error) continue currency = Currency() symbol = SymbolName.SymbolName(xchange_coins[xchange_coin]['Symbol']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=symbol) print("added") except: print("failed adding {0}".format(xchange_coins[xchange_coin]['Symbol'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=7) while end_date < start_date: start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol.replace('*', ''), start_date_ts) if len(prices) > 0: for price in prices: if prices[price] == 'Error': break coins = Coins.Coins() coin = coins.get_coin_type(symbol=currency.symbol.replace('*', ''), time=start_date_ts, exchange=self.xchange) coin.time = start_date_ts coin.close = float(prices[price]['USD']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1) return 0
def handle(self, *args, **options): # instance variable unique to each instance try: self.client = Client(self.xchange.api_key, self.xchange.api_secret, api_version='2018-01-14') except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format( error)) xchange_coins = json.loads(self.getCoins()) for xchange_coin in xchange_coins['data']: try: currency = Currency.objects.get(symbol=xchange_coin['id']) print(xchange_coin['id'] + " exists") except ObjectDoesNotExist as error: print(xchange_coin['id'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(xchange_coin['id']) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format(xchange_coin['id'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=10) while end_date < start_date: prices = self.getPrice(xchange_coin['id'], date=start_date.strftime('%Y-%m-%d')) coins = Coins.Coins() if len(prices) != 0: coin = coins.get_coin_type(symbol=currency.symbol, time=int(calendar.timegm(start_date.timetuple())), exchange=self.xchange) coin.time = int(calendar.timegm(start_date.timetuple())) coin.close = float(prices.amount) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def handle(self, *args, **options): exchange_coins = self.getCoins() for exchange_coin in exchange_coins: print(exchange_coin) try: currency = Currency.objects.get(symbol=exchange_coin) print(exchange_coin + " exists") except ObjectDoesNotExist as error: print(exchange_coin + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(exchange_coin) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format(exchange_coin)) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=5) start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol, start_date_ts, 9999999999, 14400) coins = Coins.Coin() for price in prices: print(price) utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.get_coin_type(symbol=currency.symbol, time=int(timestamp), exchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency #coin.save() return
def handle(self, *args, **options): self.valid_periods = ("oneMin", "fiveMin", "thirtyMin", "hour", "day") self.SHOW_ENDPOINTS = False self.TIMEOUT = 30 self.comparison_currency = 'USD' self.xchange = Xchange.objects.get(pk=XCHANGE['BITTREX']) try: self.bittrex = Bittrex_mod(api_key=self.xchange.api_key, api_secret=self.xchange.api_secret, timeout=self.TIMEOUT, debug_endpoint=self.SHOW_ENDPOINTS, parse_float=Decimal) except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) exchange_coins = self.getCoins() for exchange_coin in exchange_coins['result']: try: currency = Currency.objects.get( symbol=exchange_coin['Currency']) print(exchange_coin['Currency'] + " exists") except ObjectDoesNotExist as error: print(exchange_coin['Currency'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(exchange_coin['Currency']) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format( exchange_coin['Currency'])) continue prices = self.getPrice(currency.symbol) if prices == 0: continue coins = Coins.Coins() if prices == {} or prices == None or type(prices) == 'None': print(currency.symbol + " No prices found") continue for price in prices: utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.get_coin_type(symbol=currency.symbol, time=int(timestamp), exchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency coin.save() return
def parseHistoricalPage(self, currency_c, start_date, end_date): r = requests.get( 'https://coinmarketcap.com/currencies/{0}/historical-data/?start={1}&end={2}' .format(currency_c[2].lower(), start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d'))) if r.status_code != 200: print("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') try: timestamp = datetime.datetime.strptime(cells[0].text.strip(), "%b %d, %Y").date() except: timestamp = 0 try: open = float(cells[1].text.strip()) except: open = 0 try: high = float(cells[2].text.strip()) except: high = 0 try: low = float(cells[3].text.strip()) except: low = 0 try: close = float(cells[4].text.strip()) except: close = 0 try: volume = int(cells[5].text.replace(',', '')) except: volume = 0 try: market_cap = int(cells[6].text.replace(',', '')) except: market_cap = 0 new_symbol = SymbolName.SymbolName(currency_c[1]) try: currency = Currency.objects.get( symbol=new_symbol.parse_symbol()) except ObjectDoesNotExist as error: print(symbol + " does not exist in our currency list..continuing") continue currency = Currency() currency.symbol = new_symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=currency.symbol) print(symbol) except: print("failed adding {0}".format(currency.symbol)) continue coins = Coins.Coins() coin = coins.get_coin_type( symbol=new_symbol.symbol, time=int(calendar.timegm(timestamp.timetuple())), exchange=self.xchange) if coin is not None: coin.xchange = self.xchange coin.open = open coin.close = close coin.high = high coin.low = low coin.volume = volume coin.currency = currency coin.time = int(calendar.timegm(timestamp.timetuple())) coin.market_cap = market_cap coin.save() try: topCoin = TopCoins.objects.get( currency=currency, xchange=self.xchange, time=int(calendar.timegm(timestamp.timetuple()))) except ObjectDoesNotExist as error: topCoin = TopCoins() topCoin.currency = currency topCoin.market_cap = float(market_cap) topCoin.price = float(close) topCoin.xchange = self.xchange topCoin.time = int(calendar.timegm(timestamp.timetuple())) topCoin.save() else: print("no class " + symbol) return
def parse(self, start_date): r = requests.get('https://coinmarketcap.com/historical/{0}/'.format( start_date.strftime('%Y%m%d'))) if r.status_code != 200: print("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') symbol = cells[1].span.a.text symbol = cells[2].text.strip() market_cap = cells[3]['data-usd'] try: market_cap = float(market_cap) except: market_cap = 0 try: price = float(cells[4].a['data-usd']) except: price = 0 try: circulating_supply = cells[5].a['data-supply'] except: circulating_supply = cells[5].span['data-supply'] try: circulating_supply = int(float(circulating_supply)) except: circulating_supply = 0 new_symbol = SymbolName.SymbolName(symbol) try: currency = Currency.objects.get( symbol=new_symbol.parse_symbol()) except ObjectDoesNotExist as error: print(symbol + " does not exist in our currency list..continuing") continue currency = Currency() currency.symbol = new_symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=currency.symbol) print(symbol) except: print("failed adding {0}".format(currency.symbol)) continue coins = Coins.Coins() coin = coins.get_coin_type( symbol=symbol, time=int(calendar.timegm(start_date.timetuple())), exchange=self.xchange) if coin is not None: coin.xchange = self.xchange coin.close = price coin.currency = currency coin.time = int(calendar.timegm(start_date.timetuple())) coin.market_cap = market_cap coin.total_supply = circulating_supply coin.save() else: print("no class " + symbol) return