Fetch Cryptopanic API for cryptocurrency news using Python

Words
131
Reading
1 min
Listen
Play
8y

I am running an automated trading bot on Binance and I have chosen to integrate Cryptopanic news to calculate scores based on which automatically trade.
Of course this is not the only parameter I am using, but this is the newest one.

First of all I have to:

  • call the API in my python program
  • reference my authentication token (You have to be registered on cryptopanic.com to have that, do not worry it is free).
  • load the JSON into a list of dictionaries to be able to interpret the data.
url_cryptopanic="https://cryptopanic.com/api/posts/?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxx"
for i in range(0,10):
    try:
        print("Getting data from cryptopanic.com")
        response_cryptopanic = urllib.urlopen(url_cryptopanic)
    except IOError as e:
        print("Could not get data from cryptopanic.com")
        continue
    break

try:
    data_cryptopanic = json.loads(response_cryptopanic.read())
except Exception as e:
    print(traceback.format_exc())

Once I have the data loaded in the list I can interpret the data as I want and use it to calculate scores:

for i in binance_coins_filtered:
    for k in results:
        if(i == k['Currency']):
            print (i+" found on Binance!, starting data collection...")
            k['Binance'] = True

            total_market_cap += float(k['Market cap'])
            important_votes = 0.0
            negative_votes = 0.0
            liked_votes = 0.0
            disliked_votes = 0.0
            toxic_votes = 0.0

            j=0
            while j < len(data_cryptopanic['results']):

                print("Scanning the news...")
                print(data_cryptopanic['results'][j])
                try:
                    for x in data_cryptopanic['results'][j]['currencies']:
                        print(k['Currency'], x['code'])
                        if k['Currency'] == x['code']:
                            important_votes = float(data_cryptopanic['results'][j]['votes']['important'])
                            if (important_votes == 0):
                                important_votes = 1

                            negative_votes = float(data_cryptopanic['results'][j]['votes']['negative'])
                            if (negative_votes == 0):
                                negative_votes = 1

                            positive_votes = float(data_cryptopanic['results'][j]['votes']['positive'])
                            if (positive_votes == 0):
                                positive_votes = 1

                            liked_votes = float(data_cryptopanic['results'][j]['votes']['liked'])
                            if (liked_votes == 0):
                                liked_votes = 1

                            disliked_votes = float(data_cryptopanic['results'][j]['votes']['disliked'])
                            if (disliked_votes == 0):
                                disliked_votes = 1

                            toxic_votes = float(data_cryptopanic['results'][j]['votes']['toxic'])
                            if (toxic_votes == 0):
                                toxic_votes = 1

                            print("Found news on "+k['Currency']+" calculating news score...")
                            k['Score news'] += important_votes/negative_votes*positive_votes*liked_votes/disliked_votes/toxic_votes
                            

                except Exception as e:
                    print("Not able to find news score for "+k['Currency'])
                    print(traceback.format_exc())

The next plan is to parse Blocktivity website data to integrate blockchain activity data into my score calculations.

Fetch Cryptopanic API for cryptocurrency news using Python | Ecency