Coding towards CFA (1) – Forward Contract Pricing & Valuation
Linxiao Ma
Financial Data Architect | Hands-on Engineer | PhD | CFA Candidate | Distributed Database Expert | DolphinDB UK Rep. | Tech Blogger | Insane Coder
*More articles can be found from my blog site - https://dataninjago.com
This blog post focuses on pricing and valuing forward contracts for underlying assets with or without carry costs and benefits, as well as pricing stock index forward contracts.
In this blog series, I will aim to code the formulas and model algorithms covered in the CFA Level 2 program using Python and DolphinDB. Each topic will begin with a brief description of the formulas or algorithms, followed by their implementations in Python and DolphinDB.
Forward Contract
Forward Contract Pricing
The forward price is the future value of the underlying asset, assuming it grows at the risk-free rate over a specified period. The future value is the amount of money equivalent to the spot price, invested at the compounded risk-free interest rate over the given time period.
Code – Python
def price_forward_contract(spot_price, risk_free_rate, time_to_maturity):
forward_price = spot_price * (1 + risk_free_rate)**time_to_maturity
return forward_price
spot_price = 63.31
risk_free_rate = 0.0275
time_to_maturity = 0.25
forward_price = price_forward_contract(spot_price, risk_free_rate, time_to_maturity)
print(f"The forward price is: {forward_price:.2f}")
Code – DolphinDB
def price_forward_contract(spot_price, risk_free_rate, time_to_maturity){
forward_price = spot_price * pow((1 + risk_free_rate), time_to_maturity)
return forward_price
}
spot_price = 63.31
risk_free_rate = 0.0275
time_to_maturity = 0.25
forward_price = price_forward_contract(spot_price, risk_free_rate, time_to_maturity)
print("The forward price is: "+forward_price)
Forward Contract Valuation
The value of an existing forward contract at a point in time before expiration is determined by calculating the present value of the difference between the forward price set at the initiation of the contract and the forward price at the current moment.
Since the spot price at the current moment is directly observable, a simpler method to calculate the value is to find the difference between the current spot price and the present value of the forward price set at the initiation of the contract.
Code – Python
def value_forward_contract(current_spot_price, forward_price, risk_free_rate, time_to_maturity):
forward_value = current_spot_price - forward_price / ((1 + risk_free_rate) ** time_to_maturity)
return forward_value
current_spot_price = 110
risk_free_rate = 0.05
forward_price = 105
time_to_maturity_in_years = 0.25
forward_value = value_forward_contract(current_spot_price, forward_price, risk_free_rate, time_to_maturity_in_years)
print(f"The forward value is: {forward_value:.2f}")
Code – DolphinDB
def value_forward_contract(current_spot_price, forward_price, risk_free_rate, time_to_maturity){
forward_value = current_spot_price - forward_price / (pow((1 + risk_free_rate), time_to_maturity))
return forward_value
}
current_spot_price = 110
risk_free_rate = 0.05
forward_price = 105
time_to_maturity_in_years = 0.25
forward_value = value_forward_contract(current_spot_price, forward_price, risk_free_rate, time_to_maturity_in_years)
print("The forward value is: "+forward_value)
Forward Contract with Carry Costs and Benefits
While carry costs or benefits apply to the underlying asset of a forward contract, the pricing and valuation of the contract must account for these factors.
Forward Contract Pricing
Since carry costs increase the burden of holding the underlying asset, they must be added to the forward price. Conversely, carry benefits reduce the burden, so they should be subtracted from the forward price.
Code – Python
def price_forward_with_CF(spot_price, risk_free_rate, time_to_maturity, carry_benefits=0,time_to_benefits=0, carry_costs=0, time_to_costs=0):
current_benefits_value = carry_benefits / ((1 + risk_free_rate) ** time_to_benefits)
current_costs_value = carry_costs / ((1 + risk_free_rate) ** time_to_costs)
forward_price = (spot_price + current_costs_value - current_benefits_value) * ((1 + risk_free_rate) ** time_to_maturity)
return forward_price
spot_price = 1000
risk_free_rate = 0.05
time_to_maturity = 3/12
carry_benefits = 10
time_to_benefits = 2/12
forward_price = price_forward_with_CF(spot_price, risk_free_rate, time_to_maturity, carry_benefits, time_to_benefits)
print(f"The forward price is: {forward_price:.2f}")
Code – DolphinDB
def price_forward_with_CF(spot_price, risk_free_rate, time_to_maturity, carry_benefits=0,
time_to_benefits=0, carry_costs=0, time_to_costs=0) {
current_benefits_value = carry_benefits / pow((1 + risk_free_rate), time_to_benefits)
current_costs_value = carry_costs / pow((1 + risk_free_rate), time_to_costs)
forward_price = (spot_price + current_costs_value - current_benefits_value) * pow((1 + risk_free_rate), time_to_maturity)
return forward_price
}
spot_price = 1000
risk_free_rate = 0.05
time_to_maturity = 3.0/12.0
carry_benefits = 10
time_to_benefits = 2.0/12.0
forward_price = price_forward_with_CF(spot_price, risk_free_rate, time_to_maturity, carry_benefits, time_to_benefits)
print("The forward price is: "+forward_price)
Forward Contract Valuation
The value of a forward contract is calculated as the present value of the difference between the forward price at the current time and the forward price at initiation.
Code – Python
def value_forward_with_CF(current_spot_price, forward_price, risk_free_rate, time_to_maturity, current_carry_benefits=0,current_carry_costs=0):
forward_value = ((current_spot_price + current_carry_costs - current_carry_benefits) * ((1 + risk_free_rate)**time_to_maturity) - forward_price) / ((1 + risk_free_rate)**time_to_maturity)
return forward_value
current_spot_price = 1050
forward_price = 1000
risk_free_rate = 0.02
time_to_maturity = 5/12
current_carry_benefits = 28
current_carry_costs = 4
forward_value = value_forward_with_CF(current_spot_price, forward_price, risk_free_rate, time_to_maturity, current_carry_benefits, current_carry_costs)
print(f"The forward value is: {forward_value:.2f}")
Code – DolphinDB
def value_forward_with_CF(current_spot_price, forward_price, risk_free_rate, time_to_maturity, current_carry_benefits=0, current_carry_costs=0){
forward_price_0 = forward_price
forward_price_t = (current_spot_price + current_carry_costs - current_carry_benefits) * pow((1 + risk_free_rate), time_to_maturity)
forward_value = (forward_price_t - forward_price_0) / pow((1 + risk_free_rate), time_to_maturity)
return forward_value
}
current_spot_price = 1050
forward_price = 1000
risk_free_rate = 0.02
time_to_maturity = 5.0/12.0
current_carry_benefits = 28
current_carry_costs = 4
forward_value = value_forward_with_CF(current_spot_price, forward_price, risk_free_rate, time_to_maturity, current_carry_benefits, current_carry_costs)
print("The forward value is: " + forward_value )
Stock Index Forward Pricing
For pricing stock index forward contracts with a pool of underlying assets , continuous compounding interest rates and dividend yield rates are assumed. The forward price needs to be adjusted for carry costs or benefits, such as the dividends from the underlying stocks.
Code – Python
def price_forward_with_CCR(spot_price, continuous_compound_interest_rate,
time_to_maturity, carry_benefits_rate=0, carry_costs_rate=0):
forward_price = spot_price * np.exp((continuous_compound_interest_rate - carry_benefits_rate + carry_costs_rate)*time_to_maturity)
return forward_price
spot_price = 3500
continuous_compound_interest_rate = 0.0015
time_to_maturity = 3/12
carry_benefits_rate = 0.03
forward_price = price_forward_with_CCR(spot_price, continuous_compound_interest_rate, time_to_maturity, carry_benefits_rate=carry_benefits_rate)
print(f"The forward price is: {forward_price:.2f}")
Code – DolphinDB
def price_forward_with_CCR(spot_price, continuous_compound_interest_rate, time_to_maturity, carry_benefits_rate=0, carry_costs_rate=0){
forward_price = spot_price * exp((continuous_compound_interest_rate - carry_benefits_rate + carry_costs_rate)*time_to_maturity)
return forward_price
}
spot_price = 3500
continuous_compound_interest_rate = 0.0015
time_to_maturity = 3.0/12.0
carry_benefits_rate = 0.03
forward_price = price_forward_with_CCR(spot_price, continuous_compound_interest_rate, time_to_maturity, carry_benefits_rate=carry_benefits_rate)
print("The forward price is:" + forward_price)