{ "cells": [ { "cell_type": "markdown", "id": "25c8380d", "metadata": {}, "source": [ "# FFA Seasonality Charts\n", "\n", "This script allows you to plot seasonality charts for a specific contract month from our Spark30FFA and Spark25FFA 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": "9a0e42dc", "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\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7d5fad75", "metadata": {}, "outputs": [], "source": [ "# Defining function for collecting the list of contracts\n", "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", " for contract in content[\"data\"]:\n", " print(contract[\"fullName\"])\n", " tickers.append(contract[\"id\"])\n", "\n", " return tickers" ] }, { "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. Instructions for downloading your credentials can be found here:\n", "\n", "https://www.sparkcommodities.com/api/request/authentication.html\n", "\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": "602d2492", "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 = list_contracts(access_token)" ] }, { "cell_type": "markdown", "id": "a0e0e030", "metadata": {}, "source": [ "# Data Import" ] }, { "cell_type": "code", "execution_count": null, "id": "3302a85f", "metadata": {}, "outputs": [], "source": [ "def fetch_historical_price_releases(access_token, ticker, limit, offset=None):\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", " 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": [ "### Function to call data and store as a DataFrame" ] }, { "cell_type": "code", "execution_count": null, "id": "32d6eb83", "metadata": {}, "outputs": [], "source": [ "# Defining the function\n", "def fetch_ffa_prices(my_tick, my_lim):\n", " print(my_tick)\n", "\n", " my_dict_hist = fetch_historical_price_releases(access_token, my_tick, limit=my_lim)\n", "\n", " release_dates = []\n", "\n", " period_start = []\n", " period_end = []\n", " period_name = []\n", " cal_month = []\n", " ticker = []\n", " usd_day = []\n", " day_min = []\n", " day_max = []\n", "\n", " for release in my_dict_hist:\n", " release_date = release[\"releaseDate\"]\n", " data = release[\"data\"]\n", "\n", " for d in data:\n", " data_points = d[\"dataPoints\"]\n", " for data_point in data_points:\n", " period_start_at = data_point[\"deliveryPeriod\"][\"startAt\"]\n", " period_start.append(period_start_at)\n", " period_end_at = data_point[\"deliveryPeriod\"][\"endAt\"]\n", " period_end.append(period_end_at)\n", " period_name.append(data_point[\"deliveryPeriod\"][\"name\"])\n", "\n", " release_dates.append(release_date) \n", " ticker.append(release[\"contractId\"])\n", " cal_month.append(\n", " datetime.datetime.strptime(period_start_at, \"%Y-%m-%d\").strftime(\"%b-%Y\")\n", " )\n", "\n", " usd_day.append(int(data_point[\"derivedPrices\"][\"usdPerDay\"][\"spark\"]))\n", " day_min.append(\n", " int(data_point[\"derivedPrices\"][\"usdPerDay\"][\"sparkMin\"])\n", " )\n", " day_max.append(\n", " int(data_point[\"derivedPrices\"][\"usdPerDay\"][\"sparkMax\"])\n", " )\n", " \n", "\n", "\n", " historical_df = pd.DataFrame(\n", " {\n", " \"Release Date\": release_dates,\n", " \"ticker\": ticker,\n", " \"Period Name\": period_name,\n", " \"Period Start\": period_start,\n", " \"Period End\": period_end,\n", " \"Calendar Month\": cal_month,\n", " \"Spark\": usd_day,\n", " \"SparkMin\": day_min,\n", " \"SparkMax\": day_max,\n", " }\n", " )\n", "\n", " historical_df['Release Date'] = pd.to_datetime(historical_df['Release Date'],format='%Y-%m-%d')\n", " \n", " historical_df['Period Start'] = pd.to_datetime(historical_df['Period Start'])\n", " historical_df['Period End'] = pd.to_datetime(historical_df['Period End'])\n", " historical_df['Time Diff'] = (historical_df['Release Date'] - historical_df['Period Start']).dt.days\n", "\n", " return historical_df" ] }, { "cell_type": "markdown", "id": "7135318a", "metadata": {}, "source": [ "### Call those functions for Spark30FFA and Spark25FFA\n", "\n", "We call the function defined above and create two dataframes:\n", "\n", "- spark25ffa - storing all historical Spark25FFA data\n", "- spark30ffa - storing all historical Spark30FFA data" ] }, { "cell_type": "code", "execution_count": null, "id": "b367acfe", "metadata": {}, "outputs": [], "source": [ "spark25ffa = fetch_ffa_prices(tickers[4], 900)\n", "\n", "spark30ffa = fetch_ffa_prices(tickers[8], 900)" ] }, { "cell_type": "markdown", "id": "9126aa2b", "metadata": {}, "source": [ "# FFA Contract Evolution + Seasonality\n", "\n", "Compare Spark30FFA December contracts from the past 3 years, and track how these contracts have priced in the year leading up to contract settlement" ] }, { "cell_type": "markdown", "id": "3676a9e1", "metadata": {}, "source": [ "## Sorting Data\n", "\n", "We then create new columns to categorise the data we have. By creating the 'Day of Year' column, we can plot the yearly data on the same x-axis range." ] }, { "cell_type": "code", "execution_count": null, "id": "e698cbfd", "metadata": {}, "outputs": [], "source": [ "def sort_years(df):\n", " \n", " reldates = df['Release Date'].to_list()\n", " startdates = df['Period Start'].to_list()\n", " calmonths = df['Calendar Month'].to_list()\n", "\n", " dayofyear = []\n", " \n", " for r in reldates:\n", " ir = reldates.index(r)\n", " if r.year - startdates[ir].year == -1:\n", " dayofyear.append(r.timetuple().tm_yday - 365)\n", " elif r.year - startdates[ir].year == -2:\n", " dayofyear.append(r.timetuple().tm_yday - 730)\n", " else:\n", " dayofyear.append(r.timetuple().tm_yday)\n", " \n", " df['Day of Year'] = dayofyear\n", "\n", " return df\n", "\n", "spark30ffa = sort_years(spark30ffa)" ] }, { "cell_type": "markdown", "id": "bbd5a39b", "metadata": {}, "source": [ "## Group Dataframe by CalMonth and choose contracts to compare" ] }, { "cell_type": "code", "execution_count": null, "id": "73853999", "metadata": {}, "outputs": [], "source": [ "# Grouping the Dataframe by Contract\n", "groups = spark30ffa.groupby('Calendar Month')\n", "years = list(spark30ffa['Release Date'].dt.year.unique())" ] }, { "cell_type": "markdown", "id": "97eaa066", "metadata": {}, "source": [ "### Input which month you'd like to compare contracts for" ] }, { "cell_type": "code", "execution_count": null, "id": "9e84e0cd", "metadata": {}, "outputs": [], "source": [ "month = 'Dec'" ] }, { "cell_type": "markdown", "id": "c354e2df", "metadata": {}, "source": [ "# Plotting" ] }, { "cell_type": "code", "execution_count": null, "id": "713215a3", "metadata": {}, "outputs": [], "source": [ "## Plotting\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import yarl\n", "\n", "sns.set_style()\n", "sns.set_theme(style=\"whitegrid\")\n", "\n", "fig, ax = plt.subplots(figsize=(14, 7))\n", "\n", "max_dates = []\n", "\n", "# iterating through years and plotting the data\n", "for y in years:\n", " ydf = groups.get_group(month + '-' + str(y))\n", " ydf = sort_years(ydf)\n", " ax.plot(ydf[\"Day of Year\"], ydf[\"Spark\"], label=month + str(y))\n", " max_dates.append(ydf[\"Day of Year\"].max())\n", "\n", "plt.xlabel(\"Release Date\")\n", "plt.ylabel(\"Cost in USD/day\")\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 = ['Y-1', 'March', 'May', 'July', 'September', 'November', 'Y+0', '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 = [0,32,60,91,121,152,182,213,244,274,305,335, 365]\n", "xpos = [-365,-305,-244,-183,-121,-60,0,60,121,182,244,305,365]\n", "\n", "current_values = plt.gca().get_yticks()\n", "plt.gca().set_yticklabels(['$ {:,.0f}'.format(x) for x in current_values])\n", "\n", "plt.xticks(xpos, xlabels)\n", "\n", "plt.xlim(-365,max(max_dates))\n", "\n", "# Setting the graph legend\n", "plt.legend()\n" ] } ], "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 }