Get currencies live prices
Tags: #neo #bank #snippet
Author: Jeremy Ravenel

Input

Install packages

1
!pip install requests
Copied!

Import libraries

1
from pathlib import Path
2
import pandas as pd
3
import requests
Copied!

Model

API configurations

1
ccy_pair = 'EUR/CHF'
2
AUTH_HOST = "https://auth.getneo.com"
3
DATA_HOST = "https://data.getneo.com"
4
login_id = 'LOGIN'
5
api_key = 'PASSWORD'
6
bearer_token = None
Copied!

Output file config

1
output_dir = Path("../data_output/")
2
output_file = f"{ccy_pair.replace('/', '')}.csv"
Copied!

Authenticate to get token

1
if not bearer_token:
2
response = requests.get(f"{AUTH_HOST}/api/v1/auth/login/", params={"login_id": login_id, "api_key": api_key})
3
if response.status_code != 200:
4
raise PermissionError(f"Failed to authenticate as {login_id}")
5
print(f"Authenticated as {login_id}")
6
7
bearer_token = response.headers["Authorization"]
Copied!

Get data with token

1
response = requests.get(f"{DATA_HOST}/api/v1/prices/{ccy_pair}", headers={"Authorization": bearer_token})
2
if response.status_code != 200:
3
bearer_token = None
4
raise ValueError(f"Failed to retrieve {ccy_pair} prices")
Copied!

Output

Display result

1
prices_dict = response.json()
2
prices = pd.DataFrame(data=list(prices_dict.items()), columns=["DATE", "VALUE"])
3
oldest_date = prices["DATE"].min()
4
print(f"Closing price for {ccy_pair} on {oldest_date}: {prices_dict[oldest_date]}")
5
newest_date = prices["DATE"].max()
6
print(f"Closing price for {ccy_pair} on {newest_date}: {prices_dict[newest_date]}")
Copied!

Saving output to file

1
prices.to_csv(output_dir / output_file, index=False)
Copied!
Last modified 2mo ago
Copy link
Edit on GitHub