Skip to main content

Code AI Conversations

Real interations with the code assistant for generating and using the ODSL language

Code Assistant

Your programming expert for ODSL development, scripting, and technical implementation.

Expertise Areas
  • ODSL language syntax and best practices
  • Custom analytics development
  • Data transformation scripting
  • Code debugging and optimization
  • Algorithm implementation
  • Integration code examples

Analysing Scripts

The code assistant can pull stored user scripts from the platform and analyse the functionality.

Can you analyse the script MyFunctions to see what it does?
Answer from assistant: Code

Perfect! I found the MyFunctions script. Let me decode and analyze it for you:

Script Analysis: MyFunctions

Script Type: ODSL
Version: 2
Last Updated: 14 January 2022

What It Does:

This script contains a single function called timespread that calculates calendar spreads from a bootstrapped forward curve.

Function Details:

Function Name: timespread(input)

Purpose: Creates a new curve containing the spreads (price differences) between consecutive monthly contracts after bootstrapping an input curve.

How it works:

  1. Bootstraps the input curve - Converts the input curve into an arbitrage-free monthly curve using the bootstrapCurve() function
  2. Creates a spread curve - Initializes a new empty curve with the same ondate as the input
  3. Iterates through months - Loops through each month in the bootstrapped curve (starting from the second month)
  4. Calculates spreads - For each month, calculates the difference between the current month's value and the previous month's value
  5. Adds spread contracts - Creates new contracts in the form "M01-M02", "M02-M03", etc., with the calculated spread value
  6. Returns the spread curve - The final output is a curve containing all the calendar spreads

Example Usage:

base_curve = ${curve:"CRUDE_OIL:BASE:2022-01-14"}
spreads = timespread(base_curve)
// Result: A curve with contracts like M01-M02: 0.50, M02-M03: -0.25, etc.

Use Case:

This is commonly used in energy trading to analyze the contango/backwardation of futures markets by looking at the month-on-month price differences.

Would you like me to help you:

  • Modify this function for a different purpose?
  • Create a similar function with different logic?
  • Test this function with sample data?

Create a curve build function

This example shows the code assistant generating a new curve building function.

Can you create an odsl function that does a timespread, but with a spread of 12 months, e.g. M01-M13
Answer from assistant: Code

Perfect! ✅ The script has been validated successfully. Here's the yearSpread function:

yearSpread Function

function yearSpread(input)
bootstrapped = bootstrapCurve(input)
spread = Curve(input.ondate)
 
contracts = bootstrapped.contracts
contractSize = contracts.size()
 
for i = 1 to contractSize - 12
current = contracts[i - 1]
future = contracts[i + 11]
 
spreadValue = future.value - current.value
spreadTenor = current.tenor + "-" + future.tenor
 
spread.add(spreadTenor, spreadValue)
next
 
yearSpread = spread
end

What it does:

Calculates 12-month spreads (year-over-year spreads) from a forward curve. It compares prices that are 12 months apart (e.g., M01 vs M13, M02 vs M14, etc.).

Function Signature:

yearSpread(input)

Parameters:

  • input (Curve) - A forward curve to calculate spreads from

How it works:

  1. Bootstraps the input curve - Creates an arbitrage-free monthly curve
  2. Creates a new curve - Initializes an empty curve for the year spreads
  3. Iterates through contracts - Loops through all contracts, comparing each month with the month 12 periods ahead
  4. Calculates spreads - Subtracts the earlier month value from the later month value (M13 - M01)
  5. Creates spread contracts - Adds contracts labeled "M01-M13", "M02-M14", etc.
  6. Returns the year spread curve - A curve containing all the 12-month spreads

Example Usage:

// Load a crude oil curve
crude = ${curve:"CRUDE_OIL:BASE:2024-01-15"}
 
// Calculate year spreads
year_spreads = yearSpread(crude)
 
// Print results
print year_spreads
// Output: Curve with M01-M13: 2.50, M02-M14: 1.75, M03-M15: 0.50, etc.

Use Case:

This is useful for analyzing contango/backwardation patterns across a full year cycle, helping traders identify structural market conditions and seasonal patterns in commodity futures.


Would you like me to:

  1. Save this function to the platform?
  2. Modify the function (e.g., different spread periods, different calculation)?
  3. Test it with actual market data?
  4. Create other spread variations (e.g., 6-month spreads, quarterly spreads)?

Save script

The code assistant can save the script to the platform.

Can you add the function to the MyFunctions script
Answer from assistant: Code

Perfect! ✅ Successfully saved!

The yearSpread function has been added to your MyFunctions script. The script now contains both functions:

  1. timespread(input) - Calculates month-on-month spreads (original function)
  2. yearSpread(input) - Calculates 12-month year-over-year spreads (new function)

You can now use both functions:

// Get a curve
curve = ${curve:"CRUDE_OIL:BASE:2024-01-15"}
 
// Calculate month spreads
monthly_spreads = timespread(curve)
 
// Calculate year spreads
yearly_spreads = yearSpread(curve)
 
// Print results
print "Monthly spreads: " + monthly_spreads
print "Yearly spreads: " + yearly_spreads

The script has been saved with ID: ai-generated\MyFunctions

Would you like me to:

  1. Create other spread functions (e.g., quarterly, seasonal)?
  2. Test these functions with actual market data?
  3. Modify the existing functions?

Add comments to a script

The code assistant can read a script from the platform, add comments to it and save it back

Can you add comments to the functions in the MyFunctions script and save it back
Answer from assistant: Code

Perfect! ✅ Successfully saved!

The MyFunctions script has been updated with detailed comments for both functions. Here's what was added:

// Function: timespread
// Description: Calculates month-on-month spreads from a bootstrapped forward curve
// Parameters: input (Curve) - A forward curve to calculate spreads from
// Returns: Curve - A curve containing consecutive month spreads (M01-M02, M02-M03, etc.)
// Use Case: Analyze contango/backwardation patterns in commodity futures markets
function timespread(input)
bootstrapped = bootstrapCurve(input)
spread = Curve(input.ondate)
 
contracts = bootstrapped.contracts
contractSize = contracts.size()
 
// Loop through contracts, comparing each month with the next month
for i = 1 to contractSize - 1
current = contracts[i - 1]
next = contracts[i]
 
// Calculate the spread value (difference between consecutive months)
spreadValue = next.value - current.value
spreadTenor = current.tenor + "-" + next.tenor
 
// Add the spread contract to the output curve
spread.add(spreadTenor, spreadValue)
next
 
timespread = spread
end
 
// Function: yearSpread
// Description: Calculates 12-month year-over-year spreads from a forward curve
// Parameters: input (Curve) - A forward curve to calculate spreads from
// Returns: Curve - A curve containing 12-month spreads (M01-M13, M02-M14, etc.)
// Use Case: Analyze seasonal patterns and year-over-year contango/backwardation
function yearSpread(input)
bootstrapped = bootstrapCurve(input)
spread = Curve(input.ondate)
 
contracts = bootstrapped.contracts
contractSize = contracts.size()
 
// Loop through contracts, comparing each month with the month 12 periods ahead
for i = 1 to contractSize - 12
current = contracts[i - 1]
future = contracts[i + 11]
 
// Calculate the spread value (difference between 12-month apart contracts)
spreadValue = future.value - current.value
spreadTenor = current.tenor + "-" + future.tenor
 
// Add the spread contract to the output curve
spread.add(spreadTenor, spreadValue)
next
 
yearSpread = spread
end

The script has been saved with comprehensive comments that document:

  • Function purpose
  • Parameters and return values
  • Use cases
  • Inline code explanations

Would you like me to:

  1. Add even more detailed comments (e.g., mathematical formulas, examples)?
  2. Create documentation for this script?
  3. Test the functions with actual data?