{ "cells": [ { "cell_type": "markdown", "id": "59d19f73", "metadata": {}, "source": [ "# US Arb Freight Breakevens vs Spot Freight Rates\n", "\n", "This script is used to compare the US Arb Freight Breakevens with Spark30S Spot Freight Rates.\n", "\n", "For a full explanation of how to import our Arb Breakevens or Spark30S data, please refer to our Python Jupyter Notebook Code Samples:\n", "\n", "https://www.sparkcommodities.com/api/code-examples/jupyter.html\n", "\n" ] }, { "cell_type": "markdown", "id": "b0a05be4", "metadata": {}, "source": [ "### Have any questions?\n", "\n", "If you have any questions regarding our API, or need help accessing specific datasets, please contact us at:\n", "\n", "__data@sparkcommodities.com__" ] }, { "cell_type": "markdown", "id": "9e00ae34", "metadata": {}, "source": [ "## 1. Importing Data\n", "\n", "Here we define the functions that allow us to retrieve the valid credentials to access the Spark API.\n", "\n", "__This section can remain unchanged for most Spark API users.__" ] }, { "cell_type": "code", "execution_count": 71, "id": "705cbb25", "metadata": {}, "outputs": [], "source": [ "# Importing libraries for calling the API\n", "import json\n", "import os\n", "import sys\n", "import pandas as pd\n", "from base64 import b64encode\n", "from urllib.parse import urljoin\n", "\n", "\n", "try:\n", " from urllib import request, parse\n", " from urllib.error import HTTPError\n", "except ImportError:\n", " raise RuntimeError(\"Python 3 required\")" ] }, { "cell_type": "code", "execution_count": 72, "id": "1161e807", "metadata": {}, "outputs": [], "source": [ "# Defining functions for API request\n", "\n", "API_BASE_URL = \"https://api.sparkcommodities.com\"\n", "\n", "\n", "def retrieve_credentials(file_path=None):\n", " \"\"\"\n", " Find credentials either by reading the client_credentials file or reading\n", " environment variables\n", " \"\"\"\n", " if file_path is None:\n", " client_id = os.getenv(\"SPARK_CLIENT_ID\")\n", " client_secret = os.getenv(\"SPARK_CLIENT_SECRET\")\n", " if not client_id or not client_secret:\n", " raise RuntimeError(\n", " \"SPARK_CLIENT_ID and SPARK_CLIENT_SECRET environment vars required\"\n", " )\n", " else:\n", " # Parse the file\n", " if not os.path.isfile(file_path):\n", " raise RuntimeError(\"The file {} doesn't exist\".format(file_path))\n", "\n", " with open(file_path) as fp:\n", " lines = [l.replace(\"\\n\", \"\") for l in fp.readlines()]\n", "\n", " if lines[0] in (\"clientId,clientSecret\", \"client_id,client_secret\"):\n", " client_id, client_secret = lines[1].split(\",\")\n", " else:\n", " print(\"First line read: '{}'\".format(lines[0]))\n", " raise RuntimeError(\n", " \"The specified file {} doesn't look like to be a Spark API client \"\n", " \"credentials file\".format(file_path)\n", " )\n", "\n", " print(\">>>> Found credentials!\")\n", " print(\n", " \">>>> Client_id={}, client_secret={}****\".format(client_id, client_secret[:5])\n", " )\n", "\n", " return client_id, client_secret\n", "\n", "\n", "def do_api_post_query(uri, body, headers):\n", " \"\"\"\n", " OAuth2 authentication requires a POST request with client credentials before accessing the API. \n", " This POST request will return an Access Token which will be used for the API GET request.\n", " \"\"\"\n", " url = urljoin(API_BASE_URL, uri)\n", "\n", " data = json.dumps(body).encode(\"utf-8\")\n", "\n", " # HTTP POST request\n", " req = request.Request(url, data=data, headers=headers)\n", " try:\n", " response = request.urlopen(req)\n", " except HTTPError as e:\n", " print(\"HTTP Error: \", e.code)\n", " print(e.read())\n", " sys.exit(1)\n", "\n", " resp_content = response.read()\n", "\n", " # The server must return HTTP 201. Raise an error if this is not the case\n", " assert response.status == 201, resp_content\n", "\n", " # The server returned a JSON response\n", " content = json.loads(resp_content)\n", "\n", " return content\n", "\n", "\n", "def do_api_get_query(uri, access_token, format='json'):\n", " \"\"\"\n", " After receiving an Access Token, we can request information from the API.\n", " \"\"\"\n", " url = urljoin(API_BASE_URL, uri)\n", "\n", " if format == 'json':\n", " headers = {\n", " \"Authorization\": \"Bearer {}\".format(access_token),\n", " \"Accept\": \"application/json\",\n", " }\n", " elif format == 'csv':\n", " headers = {\n", " \"Authorization\": \"Bearer {}\".format(access_token),\n", " \"Accept\": \"text/csv\"\n", " }\n", "\n", " #headers = {\n", " # \"Authorization\": \"Bearer {}\".format(access_token),\n", " # \"Accept\": \"application/json\",\n", " #}\n", "\n", " # HTTP POST request\n", " req = request.Request(url, headers=headers)\n", " try:\n", " response = request.urlopen(req)\n", " except HTTPError as e:\n", " print(\"HTTP Error: \", e.code)\n", " print(e.read())\n", " sys.exit(1)\n", "\n", " resp_content = response.read()\n", "\n", " # The server must return HTTP 201. Raise an error if this is not the case\n", " assert response.status == 200, resp_content\n", "\n", " # Storing response based on requested format\n", " if format == 'json':\n", " content = json.loads(resp_content)\n", " elif format == 'csv':\n", " content = resp_content\n", "\n", " return content\n", "\n", "\n", "def get_access_token(client_id, client_secret):\n", " \"\"\"\n", " Get a new access_token. Access tokens are the thing that applications use to make\n", " API requests. Access tokens must be kept confidential in storage.\n", "\n", " # Procedure:\n", "\n", " Do a POST query with `grantType` and `scopes` in the body. A basic authorization\n", " HTTP header is required. The \"Basic\" HTTP authentication scheme is defined in\n", " RFC 7617, which transmits credentials as `clientId:clientSecret` pairs, encoded\n", " using base64.\n", " \"\"\"\n", "\n", " # Note: for the sake of this example, we choose to use the Python urllib from the\n", " # standard lib. One should consider using https://requests.readthedocs.io/\n", "\n", " payload = \"{}:{}\".format(client_id, client_secret).encode()\n", " headers = {\n", " \"Authorization\": b64encode(payload).decode(),\n", " \"Accept\": \"application/json\",\n", " \"Content-Type\": \"application/json\",\n", " }\n", " body = {\n", " \"grantType\": \"clientCredentials\",\n", " \"scopes\": \"read:netbacks,read:access,read:prices,read:routes\",\n", " }\n", "\n", " content = do_api_post_query(uri=\"/oauth/token/\", body=body, headers=headers)\n", "\n", " print(\n", " \">>>> Successfully fetched an access token {}****, valid {} seconds.\".format(\n", " content[\"accessToken\"][:5], content[\"expiresIn\"]\n", " )\n", " )\n", "\n", " return content[\"accessToken\"]" ] }, { "cell_type": "code", "execution_count": 73, "id": "ada4f167", "metadata": {}, "outputs": [], "source": [ "# Define the function for listing all netbacks\n", "def list_netbacks(access_token):\n", "\n", " content = do_api_get_query(\n", " uri=\"/v1.0/netbacks/reference-data/\", access_token=access_token\n", " )\n", "\n", " print(\">>>> All the routes you can fetch\")\n", " tickers = []\n", " fobPort_names = []\n", "\n", " availablevia = []\n", "\n", " for contract in content[\"data\"][\"staticData\"][\"fobPorts\"]:\n", " tickers.append(contract[\"uuid\"])\n", " fobPort_names.append(contract[\"name\"])\n", "\n", " availablevia.append(contract[\"availableViaPoints\"])\n", "\n", " reldates = content[\"data\"][\"staticData\"][\"sparkReleases\"]\n", "\n", " dicto1 = content[\"data\"]\n", "\n", " return tickers, fobPort_names, availablevia, reldates, dicto1" ] }, { "cell_type": "markdown", "id": "b46f962b", "metadata": {}, "source": [ "## N.B. Credentials\n", "\n", "Here we call the above functions, and input the file path to our credentials.\n", "\n", "N.B. You must have downloaded your client credentials CSV file before proceeding. Please refer to the API documentation if you have not dowloaded them already. Instructions for downloading your credentials can be found here:\n", "\n", "https://api.sparkcommodities.com/redoc#section/Authentication/Create-an-Oauth2-Client\n" ] }, { "cell_type": "code", "execution_count": 74, "id": "3acdfe86", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>> Found credentials!\n", ">>>> Client_id=01c23590-ef6c-4a36-8237-c89c3f1a3b2a, client_secret=80763****\n", ">>>> Successfully fetched an access token eyJhb****, valid 604799 seconds.\n", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoiYWNjZXNzVG9rZW4iLCJzdWIiOiIwMWMyMzU5MC1lZjZjLTRhMzYtODIzNy1jODljM2YxYTNiMmEiLCJzdWJUeXBlIjoib2F1dGgtY2xpZW50IiwiZXhwIjoxNzQ0MTE0MTI5LCJoYXNoZWRTZWNyZXQiOiJwYmtkZjJfc2hhMjU2JDYwMDAwMCRoTXRMNDlrMUZUaVVzTE42Njlqc2pPJHVCSXNxcml5b1NHVzJTS1AvaHVLNHh3eTZ4d3VDN001aUdGRm43N2l4S1U9Iiwib3JnVXVpZCI6IjQ5MzhiMGJiLTVmMjctNDE2NC04OTM4LTUyNTdmYmQzNTNmZiIsInNjb3BlcyI6WyJyZWFkOmFjY2VzcyIsInJlYWQ6cHJpY2VzIiwicmVhZDpyb3V0ZXMiLCJyZWFkOm5ldGJhY2tzIl0sImNsaWVudFR5cGUiOiJvYXV0aC1jbGllbnQifQ.29Nt_huWq6oV-H7R5j5aWpqgCdjP4COb-h3qqG2P6Mk\n" ] } ], "source": [ "# Input the path to your client credentials here\n", "client_id, client_secret = retrieve_credentials(file_path=\"/tmp/client_credentials.csv\")\n", "\n", "# Authenticate:\n", "access_token = get_access_token(client_id, client_secret)\n", "print(access_token)" ] }, { "cell_type": "markdown", "id": "7adcc5ed", "metadata": {}, "source": [ "### Fetching Netbacks reference data" ] }, { "cell_type": "code", "execution_count": 75, "id": "aaefce45", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>> All the routes you can fetch\n" ] } ], "source": [ "# Fetch all contracts:\n", "tickers, fobPort_names, availablevia, reldates, dicto1 = list_netbacks(access_token)\n", "\n", "# Define formatting data function\n", "def format_store(available_via, fob_names, tickrs):\n", " dict_store = {\n", " \"Index\": [],\n", " \"Ports\": [],\n", " \"Ticker\": [],\n", " \"Available Via\": []\n", " }\n", " \n", " c = 0\n", " for a in available_via:\n", " ## Check which routes have non-empty Netbacks data and save indices\n", " if len(a) != 0:\n", " dict_store['Index'].append(c)\n", "\n", " # Use these indices to retrive the corresponding Netbacks info\n", " dict_store['Ports'].append(fob_names[c])\n", " dict_store['Ticker'].append(tickrs[c])\n", " dict_store['Available Via'].append(available_via[c])\n", " c += 1\n", " # Show available Netbacks ports in a DataFrame (with corresponding indices)\n", " dict_df = pd.DataFrame(dict_store)\n", " return dict_df\n", "\n", "# Run formatting data function\n", "available_df = format_store(availablevia,fobPort_names,tickers)" ] }, { "cell_type": "markdown", "id": "e447d6b2", "metadata": {}, "source": [ "## Fetching Netback UUID for an individual port" ] }, { "cell_type": "code", "execution_count": 76, "id": "a4480909", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "003dec0a-ce8f-41db-8c24-4d7ef6addf70\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\Qasim\\AppData\\Local\\Temp\\ipykernel_15628\\3472757705.py:5: FutureWarning: Calling int on a single element Series is deprecated and will raise a TypeError in the future. Use int(ser.iloc[0]) instead\n", " ti = int(available_df[available_df[\"Ports\"] == port][\"Index\"])\n" ] } ], "source": [ "# Choose route ID and price release date\n", "\n", "# Here we define which port we want\n", "port = \"Sabine Pass\"\n", "ti = int(available_df[available_df[\"Ports\"] == port][\"Index\"])\n", "my_ticker = tickers[ti]\n", "my_via = 'cogh'\n", "\n", "print(my_ticker)" ] }, { "cell_type": "markdown", "id": "2d1bf4c0", "metadata": {}, "source": [ "## Data Import \n" ] }, { "cell_type": "code", "execution_count": 77, "id": "eb563eb4", "metadata": {}, "outputs": [], "source": [ "## Defining the function\n", "from io import StringIO\n", "\n", "def fetch_breakevens(access_token, ticker, nea_via=None, nwe_via=None, format='json'):\n", " \n", " query_params = \"?fob-port={}\".format(ticker)\n", " if nea_via is not None:\n", " query_params += \"&nea-via-point={}\".format(nea_via)\n", " if nwe_via is not None:\n", " query_params += \"&nwe-via-point={}\".format(nwe_via)\n", " \n", " \n", " content = do_api_get_query(\n", " uri=\"/beta/netbacks/arb-breakevens/{}\".format(query_params),\n", " access_token=access_token, format=format,\n", " )\n", " \n", " if format == 'json':\n", " my_dict = content['data']\n", " else:\n", " my_dict = content.decode('utf-8')\n", " my_dict = pd.read_csv(StringIO(my_dict))\n", "\n", " return my_dict\n" ] }, { "cell_type": "code", "execution_count": 78, "id": "30d317eb", "metadata": {}, "outputs": [], "source": [ "# Fetching data in CSV format\n", "\n", "break_df = fetch_breakevens(access_token, my_ticker, nea_via=my_via, format='csv')\n", "\n", "break_df['ReleaseDate'] = pd.to_datetime(break_df['ReleaseDate'])" ] }, { "cell_type": "code", "execution_count": 79, "id": "a3336112", "metadata": {}, "outputs": [ { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "int64", "type": "integer" }, { "name": "FobPortSlug", "rawType": "object", "type": "string" }, { "name": "NEAViaPoint", "rawType": "object", "type": "string" }, { "name": "NWEViaPoint", "rawType": "float64", "type": "float" }, { "name": "ReleaseDate", "rawType": "datetime64[ns]", "type": "datetime" }, { "name": "LoadMonthIndex", "rawType": "object", "type": "string" }, { "name": "LoadMonthStartDate", "rawType": "object", "type": "string" }, { "name": "LoadingDate", "rawType": "object", "type": "string" }, { "name": "NEADeliveryDate", "rawType": "object", "type": "string" }, { "name": "NWEDeliveryDate", "rawType": "object", "type": "string" }, { "name": "ArbUSDPerMBBtu", "rawType": "float64", "type": "float" }, { "name": "FreightBreakevenUSDPerDay", "rawType": "int64", "type": "integer" }, { "name": "JKMTTFSpreadBreakevenUSDPerMBBtu", "rawType": "float64", "type": "float" }, { "name": "FobPortUUID", "rawType": "object", "type": "string" } ], "conversionMethod": "pd.DataFrame", "ref": "5be1471b-6751-4821-9ac6-910496232f48", "rows": [ [ "0", "sabine-pass", "cogh", null, "2025-03-31 00:00:00", "M+1", "2025-05", "2025-05-15", "2025-06-28", "2025-06-01", "-0.426", "-2000", "0.087", "003dec0a-ce8f-41db-8c24-4d7ef6addf70" ], [ "1", "sabine-pass", "cogh", null, "2025-03-31 00:00:00", "M+2", "2025-06", "2025-06-15", "2025-07-29", "2025-07-02", "-0.393", "2000", "0.218", "003dec0a-ce8f-41db-8c24-4d7ef6addf70" ], [ "2", "sabine-pass", "cogh", null, "2025-03-31 00:00:00", "M+3", "2025-07", "2025-07-15", "2025-08-28", "2025-08-01", "-0.441", "-750", "0.441", "003dec0a-ce8f-41db-8c24-4d7ef6addf70" ] ], "shape": { "columns": 13, "rows": 3 } }, "text/html": [ "
\n", " | FobPortSlug | \n", "NEAViaPoint | \n", "NWEViaPoint | \n", "ReleaseDate | \n", "LoadMonthIndex | \n", "LoadMonthStartDate | \n", "LoadingDate | \n", "NEADeliveryDate | \n", "NWEDeliveryDate | \n", "ArbUSDPerMBBtu | \n", "FreightBreakevenUSDPerDay | \n", "JKMTTFSpreadBreakevenUSDPerMBBtu | \n", "FobPortUUID | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "sabine-pass | \n", "cogh | \n", "NaN | \n", "2025-03-31 | \n", "M+1 | \n", "2025-05 | \n", "2025-05-15 | \n", "2025-06-28 | \n", "2025-06-01 | \n", "-0.426 | \n", "-2000 | \n", "0.087 | \n", "003dec0a-ce8f-41db-8c24-4d7ef6addf70 | \n", "
1 | \n", "sabine-pass | \n", "cogh | \n", "NaN | \n", "2025-03-31 | \n", "M+2 | \n", "2025-06 | \n", "2025-06-15 | \n", "2025-07-29 | \n", "2025-07-02 | \n", "-0.393 | \n", "2000 | \n", "0.218 | \n", "003dec0a-ce8f-41db-8c24-4d7ef6addf70 | \n", "
2 | \n", "sabine-pass | \n", "cogh | \n", "NaN | \n", "2025-03-31 | \n", "M+3 | \n", "2025-07 | \n", "2025-07-15 | \n", "2025-08-28 | \n", "2025-08-01 | \n", "-0.441 | \n", "-750 | \n", "0.441 | \n", "003dec0a-ce8f-41db-8c24-4d7ef6addf70 | \n", "