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.
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:
- Bootstraps the input curve - Converts the input curve into an arbitrage-free monthly curve using the
bootstrapCurve()function - Creates a spread curve - Initializes a new empty curve with the same ondate as the input
- Iterates through months - Loops through each month in the bootstrapped curve (starting from the second month)
- Calculates spreads - For each month, calculates the difference between the current month's value and the previous month's value
- Adds spread contracts - Creates new contracts in the form "M01-M02", "M02-M03", etc., with the calculated spread value
- 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.
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
endWhat 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:
- Bootstraps the input curve - Creates an arbitrage-free monthly curve
- Creates a new curve - Initializes an empty curve for the year spreads
- Iterates through contracts - Loops through all contracts, comparing each month with the month 12 periods ahead
- Calculates spreads - Subtracts the earlier month value from the later month value (M13 - M01)
- Creates spread contracts - Adds contracts labeled "M01-M13", "M02-M14", etc.
- 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:
- Save this function to the platform?
- Modify the function (e.g., different spread periods, different calculation)?
- Test it with actual market data?
- Create other spread variations (e.g., 6-month spreads, quarterly spreads)?
Save script
The code assistant can save the script to the platform.
Perfect! ✅ Successfully saved!
The yearSpread function has been added to your MyFunctions script. The script now contains both functions:
- timespread(input) - Calculates month-on-month spreads (original function)
- 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_spreadsThe script has been saved with ID: ai-generated\MyFunctions
Would you like me to:
- Create other spread functions (e.g., quarterly, seasonal)?
- Test these functions with actual market data?
- 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
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
endThe 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:
- Add even more detailed comments (e.g., mathematical formulas, examples)?
- Create documentation for this script?
- Test the functions with actual data?