added more functions
This commit is contained in:
parent
a1c82ae0b8
commit
cca9ac03dd
8 changed files with 1563 additions and 2 deletions
|
|
@ -86,6 +86,58 @@ export function blackScholes(params: OptionParameters): OptionPricing {
|
|||
};
|
||||
}
|
||||
|
||||
export function impliedVolatility(
|
||||
price: number, S: number, K: number, T: number, r: number, isCall = true
|
||||
): number {
|
||||
// …Newton–Raphson on σ to match blackScholesPrice
|
||||
let sigma = 0.2; // Initial guess for volatility
|
||||
const tolerance = 1e-6;
|
||||
const maxIterations = 100;
|
||||
let iteration = 0;
|
||||
let priceDiff = 1; // Initialize to a non-zero value
|
||||
while (Math.abs(priceDiff) > tolerance && iteration < maxIterations) {
|
||||
const params: OptionParameters = {
|
||||
spotPrice: S,
|
||||
strikePrice: K,
|
||||
timeToExpiry: T,
|
||||
riskFreeRate: r,
|
||||
volatility: sigma
|
||||
};
|
||||
|
||||
const calculatedPrice = isCall ? blackScholes(params).callPrice : blackScholes(params).putPrice;
|
||||
priceDiff = calculatedPrice - price;
|
||||
|
||||
// Calculate Vega
|
||||
const greeks = calculateGreeks(params, isCall ? 'call' : 'put');
|
||||
const vega = greeks.vega * 100; // Convert from percentage to absolute
|
||||
|
||||
if (vega === 0) {
|
||||
break; // Avoid division by zero
|
||||
}
|
||||
|
||||
sigma -= priceDiff / vega; // Update volatility estimate
|
||||
iteration++;
|
||||
}
|
||||
if (iteration === maxIterations) {
|
||||
console.warn('Implied volatility calculation did not converge');
|
||||
}
|
||||
|
||||
if (sigma < 0) {
|
||||
console.warn('Calculated implied volatility is negative, returning 0');
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sigma > 10) {
|
||||
console.warn('Calculated implied volatility is too high, returning 10');
|
||||
return 10; // Cap at a reasonable maximum
|
||||
}
|
||||
if (isNaN(sigma)) {
|
||||
console.warn('Calculated implied volatility is NaN, returning 0');
|
||||
return 0;
|
||||
}
|
||||
return sigma
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate option Greeks using Black-Scholes model
|
||||
*/
|
||||
|
|
@ -502,3 +554,165 @@ function boxMullerTransform(): number {
|
|||
|
||||
return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prices a straddle option strategy
|
||||
*/
|
||||
export function straddle(params: OptionParameters): { callPrice: number; putPrice: number; strategyCost: number } {
|
||||
const callOption = blackScholes(params);
|
||||
const putOption = blackScholes(params);
|
||||
const strategyCost = callOption.callPrice + putOption.putPrice;
|
||||
|
||||
return {
|
||||
callPrice: callOption.callPrice,
|
||||
putPrice: putOption.putPrice,
|
||||
strategyCost: strategyCost
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Prices a strangle option strategy
|
||||
*/
|
||||
export function strangle(callParams: OptionParameters, putParams: OptionParameters): { callPrice: number; putPrice: number; strategyCost: number } {
|
||||
const callOption = blackScholes(callParams);
|
||||
const putOption = blackScholes(putParams);
|
||||
const strategyCost = callOption.callPrice + putOption.putPrice;
|
||||
|
||||
return {
|
||||
callPrice: callOption.callPrice,
|
||||
putPrice: putOption.putPrice,
|
||||
strategyCost: strategyCost
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Prices a butterfly option strategy
|
||||
*/
|
||||
export function butterfly(
|
||||
lowerStrikeParams: OptionParameters,
|
||||
middleStrikeParams: OptionParameters,
|
||||
upperStrikeParams: OptionParameters
|
||||
): {
|
||||
lowerCallPrice: number;
|
||||
middleCallPrice: number;
|
||||
upperCallPrice: number;
|
||||
strategyCost: number;
|
||||
} {
|
||||
const lowerCall = blackScholes(lowerStrikeParams);
|
||||
const middleCall = blackScholes(middleStrikeParams);
|
||||
const upperCall = blackScholes(upperStrikeParams);
|
||||
|
||||
const strategyCost = lowerCall.callPrice - 2 * middleCall.callPrice + upperCall.callPrice;
|
||||
|
||||
return {
|
||||
lowerCallPrice: lowerCall.callPrice,
|
||||
middleCallPrice: middleCall.callPrice,
|
||||
upperCallPrice: upperCall.callPrice,
|
||||
strategyCost: strategyCost
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Prices a condor option strategy
|
||||
*/
|
||||
export function condor(
|
||||
lowerStrikeParams: OptionParameters,
|
||||
middleLowerStrikeParams: OptionParameters,
|
||||
middleUpperStrikeParams: OptionParameters,
|
||||
upperStrikeParams: OptionParameters
|
||||
): {
|
||||
lowerCallPrice: number;
|
||||
middleLowerCallPrice: number;
|
||||
middleUpperCallPrice: number;
|
||||
upperCallPrice: number;
|
||||
strategyCost: number;
|
||||
} {
|
||||
const lowerCall = blackScholes(lowerStrikeParams);
|
||||
const middleLowerCall = blackScholes(middleLowerStrikeParams);
|
||||
const middleUpperCall = blackScholes(middleUpperStrikeParams);
|
||||
const upperCall = blackScholes(upperStrikeParams);
|
||||
|
||||
const strategyCost = lowerCall.callPrice - middleLowerCall.callPrice - middleUpperCall.callPrice + upperCall.callPrice;
|
||||
|
||||
return {
|
||||
lowerCallPrice: lowerCall.callPrice,
|
||||
middleLowerCallPrice: middleLowerCall.callPrice,
|
||||
middleUpperCallPrice: middleUpperCall.callPrice,
|
||||
upperCallPrice: upperCall.callPrice,
|
||||
strategyCost: strategyCost
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates combined Greeks for an option strategy
|
||||
*/
|
||||
export function calculateStrategyGreeks(
|
||||
positions: Array<{
|
||||
optionType: 'call' | 'put';
|
||||
quantity: number;
|
||||
params: OptionParameters;
|
||||
}>
|
||||
): GreeksCalculation {
|
||||
let totalDelta = 0;
|
||||
let totalGamma = 0;
|
||||
let totalTheta = 0;
|
||||
let totalVega = 0;
|
||||
let totalRho = 0;
|
||||
|
||||
for (const position of positions) {
|
||||
const greeks = calculateGreeks(position.params, position.optionType);
|
||||
|
||||
totalDelta += greeks.delta * position.quantity;
|
||||
totalGamma += greeks.gamma * position.quantity;
|
||||
totalTheta += greeks.theta * position.quantity;
|
||||
totalVega += greeks.vega * position.quantity;
|
||||
totalRho += greeks.rho * position.quantity;
|
||||
}
|
||||
|
||||
return {
|
||||
delta: totalDelta,
|
||||
gamma: totalGamma,
|
||||
theta: totalTheta,
|
||||
vega: totalVega,
|
||||
rho: totalRho
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Black-Scholes option pricing model with greeks
|
||||
*/
|
||||
export function blackScholesWithGreeks(params: OptionParameters, optionType: 'call' | 'put' = 'call'): { pricing: OptionPricing; greeks: GreeksCalculation } {
|
||||
const pricing = blackScholes(params);
|
||||
const greeks = calculateGreeks(params, optionType);
|
||||
return { pricing, greeks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the breakeven point for a call option at expiration
|
||||
*/
|
||||
export function callBreakeven(strikePrice: number, callPrice: number): number {
|
||||
return strikePrice + callPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the breakeven point for a put option at expiration
|
||||
*/
|
||||
export function putBreakeven(strikePrice: number, putPrice: number): number {
|
||||
return strikePrice - putPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimates the probability of profit for a call option at expiration
|
||||
*/
|
||||
export function callProbabilityOfProfit(spotPrice: number, strikePrice: number, timeToExpiry: number, riskFreeRate: number, volatility: number): number {
|
||||
const d1 = (Math.log(spotPrice / strikePrice) + (riskFreeRate + 0.5 * volatility * volatility) * timeToExpiry) / (volatility * Math.sqrt(timeToExpiry));
|
||||
return normalCDF(d1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimates the probability of profit for a put option at expiration
|
||||
*/
|
||||
export function putProbabilityOfProfit(spotPrice: number, strikePrice: number, timeToExpiry: number, riskFreeRate: number, volatility: number): number {
|
||||
const d1 = (Math.log(spotPrice / strikePrice) + (riskFreeRate + 0.5 * volatility * volatility) * timeToExpiry) / (volatility * Math.sqrt(timeToExpiry));
|
||||
return 1 - normalCDF(d1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue