{ "cells": [ { "cell_type": "markdown", "id": "25c8380d", "metadata": {}, "source": [ "# Seasonality Charts\n", "\n", "This script allows you to plot seasonality charts for Spark30S and Spark25S freight rates.\n", "\n", "This script uses elements from our API code samples. If you'd like a more basic and informative example of how to pull data via the Spark API, please visit our Github or API website:\n", "\n", "- Github: https://github.com/spark-commodities/api-code-samples/blob/master/jupyter_notebooks/\n", "- API Website: https://www.sparkcommodities.com/api/code-examples/jupyter.html\n", "\n", "\n", "### 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__\n", "\n", "or refer to our API website for more information about this endpoint:\n", "https://www.sparkcommodities.com/api/lng-freight/contracts.html" ] }, { "cell_type": "markdown", "id": "c5716130", "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": null, "id": "33fb0640", "metadata": {}, "outputs": [], "source": [ "import json\n", "import os\n", "import sys\n", "import pandas as pd\n", "import numpy as np\n", "from base64 import b64encode\n", "from pprint import pprint\n", "from urllib.parse import urljoin\n", "import datetime\n", "from io import StringIO\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\")\n", "\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", " 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", " else:\n", " raise ValueError('The format parameter only takes `csv` or `json` as inputs')\n", "\n", " # HTTP GET 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", " #status = response.status\n", "\n", " # The server must return HTTP 200. 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` 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\": \"Basic {}\".format(b64encode(payload).decode()),\n", " \"Accept\": \"application/json\",\n", " \"Content-Type\": \"application/json\",\n", " }\n", " body = {\n", " \"grantType\": \"clientCredentials\",\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\"]\n" ] }, { "cell_type": "markdown", "id": "691c889f", "metadata": {}, "source": [ "## Defining Fetch Request for Available Contracts\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5e341bdf", "metadata": {}, "outputs": [], "source": [ "def list_contracts(access_token):\n", " \"\"\"\n", " Fetch available contracts. Return contract ticker symbols\n", "\n", " # Procedure:\n", "\n", " Do a GET query to /v1.0/contracts/ with a Bearer token authorization HTTP header.\n", " \"\"\"\n", " content = do_api_get_query(uri=\"/v1.0/contracts/\", access_token=access_token)\n", "\n", " print(\">>>> All the contracts you can fetch\")\n", " tickers = []\n", " tick_names = []\n", " for contract in content[\"data\"]:\n", " print(contract[\"fullName\"])\n", " tickers.append(contract[\"id\"])\n", " tick_names.append(contract[\"fullName\"])\n", "\n", " return tickers,tick_names" ] }, { "cell_type": "markdown", "id": "fd3171a8", "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.\n", "\n", "The code then prints the available prices that are callable from the API, and their corresponding Python ticker names are displayed as a list at the bottom of the Output." ] }, { "cell_type": "code", "execution_count": null, "id": "fd7e89bf", "metadata": {}, "outputs": [], "source": [ "#Insert file 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)\n", "\n", "# Fetch all contracts:\n", "tickers, tick_names = list_contracts(access_token)\n" ] }, { "cell_type": "markdown", "id": "a0e0e030", "metadata": {}, "source": [ "# Importing Data\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ff4d0dcc", "metadata": {}, "outputs": [], "source": [ "def fetch_historical_price_releases(access_token, ticker, limit=4, offset=None, vessel=None):\n", " \"\"\"\n", " For a selected contract, this endpoint returns all the Price Releases you can\n", " access according to your current subscription, ordered by release date descending.\n", "\n", " **Note**: Unlimited access to historical data and full forward curves is only\n", " available to those with Premium access. Get in touch to find out more.\n", "\n", " **Params**\n", "\n", " limit: optional integer value to set an upper limit on the number of price\n", " releases returned by the endpoint. Default here is 4.\n", "\n", " offset: optional integer value to set from where to start returning data.\n", " Default is 0.\n", "\n", " # Procedure:\n", "\n", " Do GET queries to /v1.0/contracts/{contract_ticker_symbol}/price-releases/\n", " with a Bearer token authorization HTTP header.\n", " \"\"\"\n", " print(\">>>> Get price releases for {}\".format(ticker))\n", "\n", " query_params = \"?limit={}\".format(limit)\n", " if offset is not None:\n", " query_params += \"&offset={}\".format(offset)\n", " \n", " # '174-2stroke' or '160-tfde'\n", " if vessel is not None:\n", " query_params += \"&vessel-type={}\".format(vessel)\n", " \n", " \n", " content = do_api_get_query(\n", " uri=\"/v1.0/contracts/{}/price-releases/{}\".format(ticker, query_params),\n", " access_token=access_token,\n", " )\n", "\n", " my_dict = content['data']\n", " \n", " return my_dict\n" ] }, { "cell_type": "markdown", "id": "99be9416", "metadata": {}, "source": [ "### Formatting into a Pandas DataFrame\n", "\n", "Here we define a function that calls the data and formats it into a Pandas Dataframe." ] }, { "cell_type": "code", "execution_count": null, "id": "399cc46f", "metadata": {}, "outputs": [], "source": [ "def fetch_prices(ticker, my_lim, my_vessel=None):\n", " my_dict_hist = fetch_historical_price_releases(access_token, ticker, limit=my_lim, vessel=my_vessel)\n", " \n", " release_dates = []\n", " period_start = []\n", " ticker = []\n", " usd_day = []\n", " usd_mmbtu = []\n", "\n", " day_min = []\n", " day_max = []\n", " cal_month = []\n", "\n", " for release in my_dict_hist:\n", " release_date = release[\"releaseDate\"]\n", " ticker.append(release['contractId'])\n", "\n", " release_dates.append(release_date)\n", "\n", " data_points = release[\"data\"][0][\"dataPoints\"]\n", "\n", " for data_point in data_points:\n", " period_start_at = data_point[\"deliveryPeriod\"][\"startAt\"]\n", " period_start.append(period_start_at)\n", "\n", " usd_day.append(data_point['derivedPrices']['usdPerDay']['spark'])\n", " day_min.append(data_point['derivedPrices']['usdPerDay']['sparkMin'])\n", " day_max.append(data_point['derivedPrices']['usdPerDay']['sparkMax'])\n", "\n", " usd_mmbtu.append(data_point['derivedPrices']['usdPerMMBtu']['spark'])\n", " cal_month.append(datetime.datetime.strptime(period_start_at, '%Y-%m-%d').strftime('%b-%Y'))\n", "\n", "\n", "\n", " ## Storing values in a Pandas DataFrame\n", "\n", " historical_df = pd.DataFrame({\n", " 'ticker': ticker,\n", " 'Period Start': period_start,\n", " 'USDperday': usd_day,\n", " 'USDperdayMax': day_max,\n", " 'USDperdayMin': day_min,\n", " 'USDperMMBtu': usd_mmbtu,\n", " 'Release Date': release_dates})\n", "\n", " historical_df['USDperday'] = pd.to_numeric(historical_df['USDperday'])\n", " historical_df['USDperdayMax'] = pd.to_numeric(historical_df['USDperdayMax'])\n", " historical_df['USDperdayMin'] = pd.to_numeric(historical_df['USDperdayMin'])\n", " historical_df['USDperMMBtu'] = pd.to_numeric(historical_df['USDperMMBtu'])\n", "\n", " historical_df['Release Datetime'] = pd.to_datetime(historical_df['Release Date'])\n", " \n", " return historical_df" ] }, { "cell_type": "markdown", "id": "9dbbdc7f", "metadata": {}, "source": [ "# Yearly Comparisons\n", "Pulling Spark30s and Spark25s historical data using the import function defined above" ] }, { "cell_type": "code", "execution_count": null, "id": "b425755f", "metadata": {}, "outputs": [], "source": [ "spark30_174 = fetch_prices('spark30s', 1000, my_vessel='174-2stroke')\n", "spark30_160 = fetch_prices('spark30s', 1000, my_vessel='160-tfde')\n", "\n", "spark25_174 = fetch_prices('spark25s', 1000, my_vessel='174-2stroke')\n", "spark25_160 = fetch_prices('spark25s', 1000, my_vessel='160-tfde')" ] }, { "cell_type": "markdown", "id": "59589a6d", "metadata": {}, "source": [ "## Sorting Data\n", "\n", "We then create new columns to categorise the data we have. \n", "- By creating the 'Years' column, we can filter the data by year.\n", "- By creating the 'Day of Year' column, we can plot the yearly data on the same x-axis range.\n", "- Similarly, creating the 'Quarters' and 'Seasons' columns" ] }, { "cell_type": "code", "execution_count": null, "id": "a93ecb24", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "\n", "def sort_years(df):\n", " \n", " reldates = df['Release Date'].to_list()\n", "\n", " years = []\n", " months = []\n", " days = []\n", " monthday = []\n", " dayofyear = []\n", " for r in reldates:\n", " rsplit = r.split('-')\n", " years.append(rsplit[0])\n", " months.append(rsplit[1])\n", " days.append(rsplit[2])\n", "\n", " dayofyear.append(datetime.strptime(r, '%Y-%m-%d').timetuple().tm_yday)\n", "\n", " df['Year'] = years\n", " df['Month'] = months\n", " df['Day'] = days\n", " df['Day of Year'] = dayofyear\n", " \n", " seas_check = [['04','05','06','07','08','09'], ['10','11','12','01','02','03']]\n", " quart_check = [['01','02','03'],['04','05','06'],['07','08','09'],['10','11','12']]\n", "\n", " seasons = []\n", " quarters = []\n", "\n", " for i in df['Month'].to_list():\n", " if i in quart_check[0]:\n", " quarters.append('Q1')\n", " if i in quart_check[1]:\n", " quarters.append('Q2')\n", " if i in quart_check[2]:\n", " quarters.append('Q3')\n", " if i in quart_check[3]:\n", " quarters.append('Q4')\n", "\n", " if i in seas_check[0]:\n", " seasons.append('Summer')\n", " if i in seas_check[1]:\n", " seasons.append('Winter')\n", "\n", " df['Quarters'] = quarters\n", " df['Seasons'] = seasons\n", "\n", " return df" ] }, { "cell_type": "code", "execution_count": null, "id": "af3c1d24", "metadata": {}, "outputs": [], "source": [ "spark30_174 = sort_years(spark30_174)\n", "spark30_160 = sort_years(spark30_160)\n", "\n", "spark25_174 = sort_years(spark25_174)\n", "spark25_160 = sort_years(spark25_160)" ] }, { "cell_type": "markdown", "id": "d6209b17", "metadata": {}, "source": [ "# Plotting" ] }, { "cell_type": "code", "execution_count": null, "id": "647e2de6", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "sns.set_style()" ] }, { "cell_type": "code", "execution_count": null, "id": "c1b54663", "metadata": {}, "outputs": [], "source": [ "list(spark30_174['Year'].unique())" ] }, { "cell_type": "markdown", "id": "6f410c0d", "metadata": {}, "source": [ "## Spark30S" ] }, { "cell_type": "code", "execution_count": null, "id": "e8e314cb", "metadata": {}, "outputs": [], "source": [ "# setting up figure\n", "fig, ax = plt.subplots(figsize=(15,9))\n", "year_list = list(spark30_174['Year'].unique())\n", "\n", "# Iterating through years and plotting data\n", "for y in range(len(year_list)):\n", " hdf = spark30_174[spark30_174['Year']==year_list[y]]\n", " if y != 0: \n", " ax.plot(hdf['Day of Year'], hdf['USDperday'], alpha=0.4, label=year_list[y])\n", " else:\n", " ax.plot(hdf['Day of Year'], hdf['USDperday'], color = '#48C38D', linewidth=3.0, label=year_list[y])\n", "\n", " ax.plot(hdf['Day of Year'], hdf['USDperdayMin'], color = '#48C38D', alpha=0.1)\n", " ax.plot(hdf['Day of Year'], hdf['USDperdayMax'], color = '#48C38D', alpha=0.1)\n", " ax.fill_between(hdf['Day of Year'], hdf['USDperdayMin'], hdf['USDperdayMax'], color = '#48C38D', alpha=0.2)\n", "\n", "\n", "ax.set_title('Yearly Comparison of Spark30S')\n", "plt.xlabel('Price Release Date')\n", "plt.ylabel('USD per day')\n", "plt.legend()\n", "\n", "# Setting custom x-axis ticks location and labels.\n", "xlabels = ['January', 'March', 'May', 'July', 'September', 'November', 'Year End']\n", "\n", "# xpos gives the first day of every other month in terms of 'day of year'\n", "#xpos_allmonths = [1,32,60,91,121,152,182,213,244,274,305,335]\n", "xpos = [1,60,121,182,244,305,365]\n", "\n", "#plt.ylim(0, 100000)\n", "\n", "plt.xticks(xpos, xlabels)\n", "\n", "current_values = plt.gca().get_yticks()\n", "plt.gca().set_yticklabels(['$ {:,.0f}'.format(x) for x in current_values])\n", "\n", "\n", "sns.despine(left=True, bottom=True)\n", "\n", "plt.grid(True)" ] }, { "cell_type": "markdown", "id": "7e8bc38f", "metadata": {}, "source": [ "## Spark25S" ] }, { "cell_type": "code", "execution_count": null, "id": "95569391", "metadata": {}, "outputs": [], "source": [ "#setting up figure\n", "fig2, ax2 = plt.subplots(figsize=(15,9))\n", "year_list = list(spark25_174['Year'].unique())\n", "\n", "# iterating through years and plotting data\n", "for y in range(len(year_list)):\n", " hdf = spark25_174[spark25_174['Year']==year_list[y]]\n", " if y != 0: \n", " ax2.plot(hdf['Day of Year'], hdf['USDperday'], alpha=0.4, label=year_list[y])\n", " else:\n", " ax2.plot(hdf['Day of Year'], hdf['USDperday'], color = '#48C38D', linewidth=3.0, label=year_list[y])\n", "\n", " ax2.plot(hdf['Day of Year'], hdf['USDperdayMin'], color = '#48C38D', alpha=0.1)\n", " ax2.plot(hdf['Day of Year'], hdf['USDperdayMax'], color = '#48C38D', alpha=0.1)\n", " ax2.fill_between(hdf['Day of Year'], hdf['USDperdayMin'], hdf['USDperdayMax'], color = '#48C38D', alpha=0.2)\n", "\n", "\n", "ax2.set_title('Yearly Comparison of Spark25S')\n", "plt.xlabel('Price Release Date')\n", "plt.ylabel('USD per day')\n", "plt.legend()\n", "\n", "# Setting custom x-axis ticks location and labels.\n", "xlabels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Year End']\n", "#xlabels = ['January', 'March', 'May', 'July', 'September', 'November', 'Year End']\n", "\n", "# xpos gives the first day of every other month in terms of 'day of year'\n", "xpos = [1,32,60,91,121,152,182,213,244,274,305,335, 365]\n", "#xpos = [1,60,121,182,244,305,365]\n", "\n", "plt.xticks(xpos, xlabels)\n", "\n", "current_values = plt.gca().get_yticks()\n", "plt.gca().set_yticklabels(['$ {:,.0f}'.format(x) for x in current_values])\n", "\n", "\n", "sns.despine(left=True, bottom=True)\n", "plt.grid(True)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }