The Crypto
Published in

The Crypto

Photo by Alex Kotliarskyi on Unsplash

Go Undercover by Scraping Cryptocurrency Market Metrics

Data-Informed Technique To Cryptocurrency Market Speculation using Python

Requirements

Don’t know which Crypto to buy? Do your own analysis. Photo by @cryptofunny

Preparing Your Data Environment

Install Python — Anaconda

Setup your Anaconda Project Environment

Interactive Jupyter Notebooks

Jupyter Notebook

Import Dependencies in Jupyter Notebook

import urllib.request
import ssl
import json
import time

Cryptocurrency Market Data API

Introduction to LunarCRUSH — Social Listening For Crypto

Create Account on LunarCRUSH
LunarCRUSH API Docs

Generate V2 API Key

Retrieve Cryptocurrency Data — Set API Key

api_key = "API_KEY_HERE"
# Allows adding as many coins as desired
coin_list = [
"BTC",
"ETH",
"LTC"
]
coins = ','.join(coin_list)
map = [
{"name":""},
{"symbol": ""},
{"price": " Price: "},
{"percent_change_24h": " - 24 Hour Percent Change: "},
]
def final_render(asset_coin, value, key, asset):
if key == 'symbol':
asset_coin += " (" + asset[key] + ")"
elif key == 'percent_change_24h':
asset_coin += value + str(asset[key]) + "%"
else:
asset_coin += value + str(asset[key])
return asset_coin
def main():url = "https://api.lunarcrush.com/v2?data=assets&key=" + api_key + "&symbol=" + coins
assets = json.loads(urllib.request.urlopen(url).read())
for asset in assets['data']:
asset_coin = ""
for field in map:
key = list(field.keys())[0]
value = list(field.values())[0]
asset_coin = final_render(asset_coin, value, key, asset)
print(asset_coin)
print(len(asset_coin))

Automating Our Code To Run Every 30 Minutes:

# Runs main() every 30 minutes
while True:
main()
time.sleep(1800)

Running Our Code

LunarCRUSH REST API ENDPOINTS

Analyzing Average Cryptocurrency Sentiment

{"average_sentiment": " Average Sentiment: "},
map = [
{"name":""},
{"symbol": ""},
{"price": " Price: "},
{"percent_change_24h": " - 24 Hour Percent Change: "},
{"average_sentiment": " Average Sentiment: "},
]
Bitcoin (BTC) Price: 6799.824241 - 24 Hour Percent Change: -0.84% Average Sentiment: 3.3Ethereum (ETH) Price: 143.297439 - 24 Hour Percent Change: -0.32% Average Sentiment: 3.5Litecoin (LTC) Price: 40.528366 - 24 Hour Percent Change: 0% Average Sentiment: 3.3

Additional Cryptocurrency Market Metrics

map = [
{“name”:””},
{“symbol”: “”},
{“price”: “ Price: “},
{“percent_change_24h”: “ — 24 Hour Percent Change: “},
{“market_cap”: “ Market Cap: “},
{“volume_24h”: “ 24 Hour Volume: “},
{“url_shares”: “ URL Shares: “},
{“reddit_posts”: “ Reddit Posts: “},
{“tweets”: “ Tweets: “},
{“galaxy_score”: “ Galaxy Score: “},
{“volatility”: “ Volatility: “},
{“social_volume”: “ Social Volume: “},
{“news”: “ News: “},
{“close”: “ Close: “},
]
Bitcoin (BTC) Price: 6783.203105 - 24 Hour Percent Change: -1.52% Market Cap: 123875196896 24 Hour Volume: 24893565282.51 URL Shares: 690 Reddit Posts: 16 Tweets: 5244 Galaxy Score: 58.5 Volatility: 0.00713268 Social Volume: 5744 News: 2 Close: 6783.203105Ethereum (ETH) Price: 142.949595 - 24 Hour Percent Change: -1.18% Market Cap: 15765467834 24 Hour Volume: 9016572873.14 URL Shares: 212 Reddit Posts: 3 Tweets: 464 Galaxy Score: 61 Volatility: 0.00844722 Social Volume: 646 News: 7 Close: 142.949595Litecoin (LTC) Price: 40.308414 - 24 Hour Percent Change: -1.03% Market Cap: 2592376506 24 Hour Volume: 2267773770.18 URL Shares: 40 Reddit Posts: 1 Tweets: 250 Galaxy Score: 60.5 Volatility: 0.00450274 Social Volume: 285 News: 1 Close: 40.308414

Full Code

import urllib.request
import ssl
import json
import time
import tweepy
ssl._create_default_https_context = ssl._create_unverified_contextapi_key = "API_KEY_HERE"# Allows adding as many coins as desired
coin_list = [
"LTC",
"ETH",
"BTC"
]
coins = ','.join(coin_list)
map = [
{"name":""},
{"symbol": ""},
{"price": " Price: "},
{"percent_change_24h": " - 24 Hour Percent Change: "},
{"market_cap": " Market Cap: "},
{"volume_24h": " 24 Hour Volume: "},
{"url_shares": " URL Shares: "},
{"reddit_posts": " Reddit Posts: "},
{"tweets": " Tweets: "},
{"galaxy_score": " Galaxy Score: "},
{"volatility": " Volatility: "},
{"social_volume": " Social Volume: "},
{"news": " News: "},
{"close": " Close: "},
]
def final_render(asset_coin, value, key, asset):

if key == 'symbol':
asset_coin += " (" + asset[key] + ")"
elif key == 'percent_change_24h':
asset_coin += value + str(asset[key]) + "%"
else:
asset_coin += value + str(asset[key])
return asset_coin
def main():url = "https://api.lunarcrush.com/v2?data=assets&key=" + api_key + "&symbol=" + coins
assets = json.loads(urllib.request.urlopen(url).read())
for asset in assets['data']:
asset_coin = ""
for field in map:
key = list(field.keys())[0]
value = list(field.values())[0]
asset_coin = final_render(asset_coin, value, key, asset)
print(asset_coin)
print(len(asset_coin))
# Runs main() every 30 minutes
while True:
main()
time.sleep(1800)

Pandas.Dataframe

def create_coins_df(assets):
global coins_df

for n in range(len(coin_list)):
data = assets['data'][n]
required_data = [{key: data[key] for key in fields}]

coins_df = coins_df.append(required_data, ignore_index = True)

coins_df['time'] = pandas.to_datetime(coins_df['time'], unit='s')

return coins_df
create_coins_df(main())
Dataframe Code & Output

Configuration Options

Final Thoughts

Thanks For Reading

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store