Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
// Check if form data is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form inputs
$underlyingPrice = floatval($_POST['underlyingPrice']);
$optionType = $_POST['optionType'];
$strikePrice = floatval($_POST['strikePrice']);
$dte = intval($_POST['dte']);
$position = $_POST['position'];
$contracts = intval($_POST['contracts']);
$premium = floatval($_POST['premium']) * 100; // Convert to per-share value
// Regulation T Margin
$regTMargin = 0;
if ($position === 'long') {
$regTMargin = $premium * $contracts; // Full premium for long options
} else {
// Short uncovered option: 20% of underlying + premium - out-of-money amount
$otmAmount = $optionType === 'call'
? max(0, $strikePrice - $underlyingPrice)
: max(0, $underlyingPrice - $strikePrice);
$regTMargin = (0.20 * $underlyingPrice * 100 + $premium - $otmAmount * 100) * $contracts;
$regTMargin = max($regTMargin, $premium * $contracts); // Minimum is premium
}
// Portfolio Margin (TIMS) - Simplified ±15% stress test
$priceRange = 0.15; // ±15%
$points = 10;
$step = (2 * $priceRange * $underlyingPrice) / ($points - 1);
$maxLoss = 0;
for ($i = 0; $i < $points; $i++) {
$testPrice = $underlyingPrice * (1 - $priceRange) + $step * $i;
// Simplified intrinsic value for option at test price
$testValue = $optionType === 'call'
? max(0, $testPrice - $strikePrice) * 100
: max(0, $strikePrice - $testPrice) * 100;
$positionValue = $position === 'long'
? ($testValue - $premium)
: ($premium - $testValue);
$positionValue *= $contracts;
if ($positionValue < $maxLoss) $maxLoss = $positionValue;
}
$portfolioMargin = abs($maxLoss);
// Output results as an array (you can modify this to suit your needs)
$results = [
'premium' => number_format($premium, 2),
'regT_margin' => number_format($regTMargin, 2),
'portfolio_margin' => number_format($portfolioMargin, 2)
];
// For simplicity, print results (replace with your preferred output method)
echo "Option Premium: $" . $results['premium'] . "\n";
echo "Regulation T Margin: $" . $results['regT_margin'] . "\n";
echo "Portfolio Margin (TIMS): $" . $results['portfolio_margin'] . "\n";
}
?>