RiskVar Python API

Follow the Steps below to Access the RiskVar Database via the following API:
Step 1: Copy the following Code or click to download python file RiskVar Python API
Step 2: Search RiskVar financial securities and macroeconomic indicators database by clicking here
Step 3: Insert your Query’s criteria in d=am.load by specifying the variable name, start and end date
Alternatively, you can access riskvar database using MS Excel


# -*- coding: utf-8 -*-
“””
RiskVar API Python Script
Upgrade your Account to gain unlimited access for $99/year ******* https://riskvar.com/subscriptions *******
Please find and search the full list of macroeconomic variables & financial variables at https://riskvar.com/financial-database-variables
Instructions: 1. Insert your username, password 2. variable name, start date, end date 3. Run Script
“””
import http.client
from urllib.parse import quote
import pandas as pd
class AssetMacro(object):
    user_name, password, conn, headers, payload = None, None, None, None, None
    def __init__(self, user_name, password):
        self.auth(user_name, password)
        self.conn = http.client.HTTPConnection(“api.riskvar.com:8080″)
    def auth(self, user_name, password):
        self.user_name = user_name
        self.password = password
        self.headers = {‘Authorization’: ‘{};{}’.format(self.user_name, self.password)}
    def get_vars(self):
        self.conn.request(method=”GET”, url=”/vars”, headers=self.headers)
        response = self.conn.getresponse()
        data = response.read()
        str_data = data.decode(“utf-8”)
        return str_data.split(‘,’)
    def load_data(self, var_name, start_date=None, end_date=None):
        v = self.get_vars()
        if var_name not in v:
            raise ValueError(‘Variable name not found, check the get_vars() result.’)
        params = ‘name={}’.format(quote(var_name))
        if start_date:
            params += ‘&start_date={}’.format(start_date)
        if end_date:
            params += ‘&end_date={}’.format(end_date)
        url = “/query?” + params
        self.conn.request(method=”GET”, url=url, headers=self.headers)
        response = self.conn.getresponse()
        if response.status == 200:
            data = response.read()
            str_data = data.decode(“utf-8”)
            data_rows = str_data.split(‘\r\n’)
            if len(data_rows) <= 1:
                return data_rows
            data_arr = []
            for d in data_rows:
                data_arr.append(d.split(‘,’))
            d = data_arr[:-1] # skip the last empty row
            return pd.DataFrame(d[2:], columns=d[0])
        else:
            raise ValueError(‘Server error {} {}’.format(response.status, response.reason))
            return None
if __name__ == ‘__main__’:
    am = AssetMacro(‘riskvartest’, ‘test’)
    v = am.get_vars()
    d = am.load_data(‘US-1Y’, ‘2010-01-03’, ‘2019-02-01’)
    print (d)