def rt_price_grab(ticker): baseURL =\ "https://min-api.cryptocompare.com/data/price?fsym="+ticker +\ "&tsyms=USD,BTC" request = tor_request(baseURL) try: data = request.json() except AttributeError: data = "ConnectionError" return (data)
def dojo_setup(): at = dojo_auth() try: status = dojo_status().json() except AttributeError: status = dojo_status() user_info = User.query.filter_by(username=current_user.username).first() form = DojoForm() if form.validate_on_submit(): api_keys_json = api_keys_class.loader() api_keys_json['dojo']['onion'] = form.dojo_onion.data api_keys_json['dojo']['api_key'] = form.dojo_apikey.data api_keys_json['dojo']['token'] = form.dojo_token.data api_keys_class.saver(api_keys_json) at = dojo_auth() elif request.method == "GET": at = dojo_auth() api_keys_json = api_keys_class.loader() form.dojo_onion.data = api_keys_json['dojo']['onion'] form.dojo_apikey.data = api_keys_json['dojo']['api_key'] try: form.dojo_token.data = at["authorizations"]["access_token"] except (KeyError, TypeError): form.dojo_token.data = "Error getting token" last_block = tor_request("https://api.oxt.me/lastblock") if last_block == "ConnectionError": last_block = " - " progress = "unknown" else: try: if status["blocks"]: last_block = last_block.json() progress = float(status["blocks"]) / float( last_block["data"][0]["height"]) else: progress = "unknown" except (KeyError, TypeError): progress = "unknown" return render_template( "dojo.html", title="Dojo Config and Check", form=form, at=at, user_info=user_info, status=status, last_block=last_block, progress=progress, )
def multiple_price_grab(tickers, fx): # tickers should be in comma sep string format like "BTC,ETH,LTC" baseURL = \ "https://min-api.cryptocompare.com/data/pricemultifull?fsyms="\ + tickers + "&tsyms=" + fx try: request = tor_request(baseURL) except requests.exceptions.ConnectionError: return ("ConnectionError") try: data = request.json() except AttributeError: data = "ConnectionError" return (data)
def request_data(self, ticker): data = None if self.base_url is not None: ticker = ticker.upper() globalURL = (self.base_url + "?" + self.ticker_field + "=" + ticker + self.url_args) # Some APIs use the ticker without a ticker field i.e. xx.xx./AAPL&... # in these cases, we pass the ticker field as empty if self.ticker_field == '': if self.url_args[0] == '&': self.url_args = self.url_args.replace('&', '?', 1) globalURL = (self.base_url + "/" + ticker + self.url_args) request = tor_request(globalURL) try: data = request.json() except Exception: try: # Try again - some APIs return a json already data = json.loads(request) except Exception as e: self.errors.append(e) return (data)
def price_data_rt_full(ticker, provider): # Function to get a complete data set for realtime prices # Loop through the providers to get the following info: # price, chg, high, low, volume, mkt cap, last_update, source # For some specific assets, a field 'note' can be passed and # will replace volume and market cap at the main page # ex: GBTC premium can be calculated here # returns a list with the format: # price, last_update, high, low, chg, mktcap, # last_up_source, volume, source, notes # All data returned in USD # ----------------------------------------------------------- # This function is used to grab a single price that was missing from # the multiprice request. Since this is a bit more time intensive, it's # separated so it can be memoized for a period of time (this price will # not refresh as frequently) # default: timeout=30 if provider == 'cc': multi_price = multiple_price_grab(ticker, 'USD,' + current_user.fx()) try: # Parse the cryptocompare data price = multi_price["RAW"][ticker][current_user.fx()]["PRICE"] price = float(price * current_user.fx_rate_USD()) high = float( multi_price["RAW"][ticker][current_user.fx()]["HIGHDAY"] * current_user.fx_rate_USD()) low = float( multi_price["RAW"][ticker][current_user.fx()]["LOWDAY"] * current_user.fx_rate_USD()) chg = multi_price["RAW"][ticker][ current_user.fx()]["CHANGEPCT24HOUR"] mktcap = multi_price["DISPLAY"][ticker][ current_user.fx()]["MKTCAP"] volume = multi_price["DISPLAY"][ticker][ current_user.fx()]["VOLUME24HOURTO"] last_up_source = multi_price["RAW"][ticker][ current_user.fx()]["LASTUPDATE"] source = multi_price["DISPLAY"][ticker][ current_user.fx()]["LASTMARKET"] last_update = datetime.now() notes = None return (price, last_update, high, low, chg, mktcap, last_up_source, volume, source, notes) except Exception: return (None) if provider == 'aa': try: globalURL = 'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&apikey=' globalURL += api_keys['alphavantage'][ 'api_key'] + '&symbol=' + ticker data = tor_request(globalURL).json() price = float(data['Global Quote'] ['05. price']) * current_user.fx_rate_USD() high = float( data['Global Quote']['03. high']) * current_user.fx_rate_USD() low = float( data['Global Quote']['04. low']) * current_user.fx_rate_USD() chg = data['Global Quote']['10. change percent'].replace('%', '') try: chg = float(chg) except Exception: chg = chg mktcap = '-' volume = '-' last_up_source = '-' last_update = '-' source = 'Alphavantage' notes = None # Start Notes methods for specific assets. For example, for # GBTC we report the premium to BTC if ticker == 'GBTC': fairvalue, premium = GBTC_premium( float(data['Global Quote']['05. price'])) fairvalue = "{0:,.2f}".format(fairvalue) premium = "{0:,.2f}".format(premium * 100) notes = f"Fair Value: {fairvalue}<br>Premium: {premium}%" return (price, last_update, high, low, chg, mktcap, last_up_source, volume, source, notes) except Exception: return None if provider == 'fp': try: globalURL = 'https://financialmodelingprep.com/api/v3/stock/real-time-price/' globalURL += ticker data = tor_request(globalURL).json() price = float(data['price']) * current_user.fx_rate_USD() high = '-' low = '-' chg = 0 mktcap = '-' volume = '-' last_up_source = '-' last_update = '-' source = 'FP Modeling API' notes = None return (price, last_update, high, low, chg, mktcap, last_up_source, volume, source, notes) except Exception: return None
def alphavantage_historical(id): # Downloads Historical prices from Alphavantage # Can handle both Stock and Crypto tickers - try stock first, then crypto # Returns: # - data matrix (prices) # - notification messages: error, stock, crypto # - Metadata: # "Meta Data": { # "1. Information": "Daily Prices and Volumes for Digital Currency", # "2. Digital Currency Code": "BTC", # "3. Digital Currency Name": "Bitcoin", # "4. Market Code": "USD", # "5. Market Name": "United States Dollar", # "6. Last Refreshed": "2019-06-02 (end of day)", # "7. Time Zone": "UTC" # }, # To limit the number of requests to ALPHAVANTAGE, if data is Downloaded # successfully, it will be saved locally to be reused during that day # Alphavantage Keys can be generated free at # https://www.alphavantage.co/support/#api-key user_info = User.query.filter_by(username=current_user.username).first() api_key = user_info.aa_apikey if api_key is None: return ("API Key is empty", "error", "empty") id = id.upper() filename = "thewarden/alphavantage_data/" + id + ".aap" meta_filename = "thewarden/alphavantage_data/" + id + "_meta.aap" try: # Check if saved file is recent enough to be used # Local file has to have a modified time in today today = datetime.now().date() filetime = datetime.fromtimestamp(os.path.getctime(filename)) if filetime.date() == today: logging.info("[ALPHAVANTAGE] Local file is fresh. Using it.") id_pickle = pd.read_pickle(filename) with open(meta_filename, 'rb') as handle: meta_pickle = pickle.load(handle) logging.info(f"Success: Open {filename} - no need to rebuild") return (id_pickle, "downloaded", meta_pickle) else: logging.info("[ALPHAVANTAGE] File found but too old" + " - downloading a fresh one.") except FileNotFoundError: logging.info(f"[ALPHAVANTAGE] File not found for {id} - downloading") baseURL = "https://www.alphavantage.co/query?" func = "DIGITAL_CURRENCY_DAILY" market = "USD" globalURL = baseURL + "function=" + func + "&symbol=" + id +\ "&market=" + market + "&apikey=" + api_key logging.info(f"[ALPHAVANTAGE] {id}: Downloading data") logging.info(f"[ALPHAVANTAGE] Fetching URL: {globalURL}") try: logging.info(f"[ALPHAVANTAGE] Requesting URL: {globalURL}") request = tor_request(globalURL) except requests.exceptions.ConnectionError: logging.error("[ALPHAVANTAGE] Connection ERROR " + "while trying to download prices") return ("Connection Error", 'error', 'empty') data = request.json() # Try first as a crypto request try: meta_data = (data['Meta Data']) logging.info(f"[ALPHAVANTAGE] Downloaded historical price for {id}") df = pd.DataFrame.from_dict( data['Time Series (Digital Currency Daily)'], orient="index") # Save locally for reuse today filename = "thewarden/alphavantage_data/" + id + ".aap" os.makedirs(os.path.dirname(filename), exist_ok=True) df.to_pickle(filename) meta_filename = "thewarden/alphavantage_data/" + id + "_meta.aap" with open(meta_filename, 'wb') as handle: pickle.dump(meta_data, handle, protocol=pickle.HIGHEST_PROTOCOL) logging.info(f"[ALPHAVANTAGE] {filename}: Filed saved locally") return (df, 'crypto', meta_data) except KeyError: logging.info( f"[ALPHAVANTAGE] Ticker {id} not found as Crypto. Trying Stock.") # Data not found - try as STOCK request func = "TIME_SERIES_DAILY_ADJUSTED" globalURL = baseURL + "function=" + func + "&symbol=" + id +\ "&market=" + market + "&outputsize=full&apikey=" +\ api_key try: request = tor_request(globalURL) except requests.exceptions.ConnectionError: logging.error("[ALPHAVANTAGE] Connection ERROR while" + " trying to download prices") return ("Connection Error", "error", "empty") data = request.json() try: meta_data = (data['Meta Data']) logging.info( f"[ALPHAVANTAGE] Downloaded historical price for stock {id}") df = pd.DataFrame.from_dict(data['Time Series (Daily)'], orient="index") # Save locally for reuse today filename = "thewarden/alphavantage_data/" + id + ".aap" os.makedirs(os.path.dirname(filename), exist_ok=True) df.to_pickle(filename) meta_filename = "thewarden/alphavantage_data/" + id + "_meta.aap" with open(meta_filename, 'wb') as handle: pickle.dump(meta_data, handle, protocol=pickle.HIGHEST_PROTOCOL) logging.info(f"[ALPHAVANTAGE] {filename}: Filed saved locally") return (df, "stock", meta_data) except KeyError: logging.warning( f"[ALPHAVANTAGE] {id} not found as Stock or Crypto" + " - INVALID TICKER") return ("Invalid Ticker", "error", "empty")