This example calculates the Crude Oil Tanker Freight Rates For Very Large Crude Carriers (VLCCs) From The Arabian Gulf To China
Setup
Install the Signal Ocean SDK:
InĀ [Ā ]:
Copied!
pip install signal-ocean
pip install signal-ocean
Import required libraries / APIs / packages:
Import Voyages Market Data API and some additional libraries
InĀ [1]:
Copied!
import pandas as pd
import seaborn as sns
from signal_ocean import Connection
from signal_ocean.voyages_market_data import VoyagesMarketDataAPI
import pandas as pd
import seaborn as sns
from signal_ocean import Connection
from signal_ocean.voyages_market_data import VoyagesMarketDataAPI
Fill in your personal SignalOcean API subscription key acquired here and open a connection:
InĀ [2]:
Copied!
signal_ocean_api_key = '' #replace with your subscription key
signal_ocean_api_key = '' #replace with your subscription key
Create a connection
InĀ [3]:
Copied!
connection = Connection(signal_ocean_api_key)
api = VoyagesMarketDataAPI(connection)
connection = Connection(signal_ocean_api_key)
api = VoyagesMarketDataAPI(connection)
Call the API to get market data
InĀ [2]:
Copied!
result = api.get_voyage_market_data(vessel_class_id=84, include_matched_fixture = True, filter_by_matched_fixture = True,
include_labels = True)
result = api.get_voyage_market_data(vessel_class_id=84, include_matched_fixture = True, filter_by_matched_fixture = True,
include_labels = True)
Get matched fixtures into a dataframe
InĀ [3]:
Copied!
fixtures = pd.DataFrame([r.matched_fixture.__dict__ for r in result])
fixtures = pd.DataFrame([r.matched_fixture.__dict__ for r in result])
Transform Data
Keep columns with rate info and rate type in WS (World Scale)
InĀ [4]:
Copied!
fixtures = fixtures[(~fixtures['rate'].isnull())&(fixtures['rate_type']=='WS')]
fixtures = fixtures[(~fixtures['rate'].isnull())&(fixtures['rate_type']=='WS')]
Keep columns with info on the desired itinerary: Arabian Gulf to China
InĀ [5]:
Copied!
fixtures = fixtures[(fixtures['load_name'] == 'Arabian Gulf')&(fixtures['discharge_name'] == 'China')]
fixtures = fixtures[(fixtures['load_name'] == 'Arabian Gulf')&(fixtures['discharge_name'] == 'China')]
Convert to datetime to use in aggregations
InĀ [6]:
Copied!
fixtures['laycan_from'] = pd.to_datetime(fixtures['laycan_from'], errors='coerce', utc=True)
fixtures.sort_values('laycan_from', inplace = True)
fixtures['laycan_from'] = pd.to_datetime(fixtures['laycan_from'], errors='coerce', utc=True)
fixtures.sort_values('laycan_from', inplace = True)
set date as index and calculate a the 7 days moving average - we use the lacan from as date
InĀ [7]:
Copied!
fixtures.set_index('laycan_from', inplace = True, drop=False)
fixtures['7_days_moving_average'] = fixtures['rate'].rolling('7D').mean()
fixtures.set_index('laycan_from', inplace = True, drop=False)
fixtures['7_days_moving_average'] = fixtures['rate'].rolling('7D').mean()
extract the day and keep only last done per day
InĀ [8]:
Copied!
fixtures['Date'] = fixtures['laycan_from'].dt.tz_localize(None).dt.to_period('D').dt.to_timestamp()
fixtures = fixtures.drop_duplicates(subset=['Date'], keep='last')
fixtures['Date'] = fixtures['laycan_from'].dt.tz_localize(None).dt.to_period('D').dt.to_timestamp()
fixtures = fixtures.drop_duplicates(subset=['Date'], keep='last')
Plot the rate moving average and last done
InĀ [9]:
Copied!
sns.set(rc = {'figure.figsize':(15,8)})
start_date = '2022-01-01'
sns.lineplot(data=fixtures[fixtures['Date']>start_date], x='Date', color='red', y='7_days_moving_average').set_title("Rate moving average and last done")
sns.scatterplot(data=fixtures[fixtures['Date']>start_date], x='Date', y='rate')
sns.set(rc = {'figure.figsize':(15,8)})
start_date = '2022-01-01'
sns.lineplot(data=fixtures[fixtures['Date']>start_date], x='Date', color='red', y='7_days_moving_average').set_title("Rate moving average and last done")
sns.scatterplot(data=fixtures[fixtures['Date']>start_date], x='Date', y='rate')
Out[9]:
<AxesSubplot:title={'center':'Rate moving average and last done'}, xlabel='Date', ylabel='7_days_moving_average'>