Crypto + Python = Simple percentage change alert

in #python7 years ago

In a fancy late i just recently discovered the exciting world of cryptocurrency and one of the first thing that jumped to my mind is to try and leverage some of my python knowledge into this world. So let try to create a (very) simple alert for price change

for this demo i used worldcoinindex.com api, the concept is pretty simple, the WCI api show changes every 5 minutes. so the plan is to sample the currency every 5 minutes and print alert when change (abs) bigger then 5%

import requests,json,time

key = 'YOUR API KEY'
alert_percentage = 5
data_arr = []

def get_change(current, previous):
    if current == previous:
        return 0
    try:
        result = ((abs(current - previous)) / previous) * 100
        return result
    except ZeroDivisionError:
        return 0

def get_data_arr():

    wci_url = "https://www.worldcoinindex.com/apiservice/json?key={key}".format(key=key)
    data = json.loads(requests.get(wci_url).content)

    for market in data['Markets']:
        Label = market['Label']
        Price_usd = market['Price_usd']

        data_arr.append({'Label': Label, 'Price_usd': Price_usd})

    return  data_arr

def main():
    base_data_arr = get_data_arr()

    while True:
        data_arr = get_data_arr()

        for i in range(len(data_arr)):
            current = data_arr[i]['Price_usd']
            previous = base_data_arr[i]['Price_usd']
            percentage_change = get_change(current, previous)

            if percentage_change > alert_percentage:
                print('alert {0} change in {1}'.format(str(percentage_change),data_arr[i]['Label']))

            base_data_arr[i]['Price_usd'] = current

        time.sleep(300)

main()

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.029
BTC 69212.15
ETH 2511.68
USDT 1.00
SBD 2.57