多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 【C#】 25. 根據Option組成的投資組合的payoff,識別組成成分

【C#】 25. 根據Option組成的投資組合的payoff,識別組成成分

來源:程序員人生   發布時間:2015-06-01 08:29:27 閱讀次數:3718次

今天終究做了1個很久之前就想完成的option利用!

假定現在有1個全部由Option構成的投資組合,這些option的underlying都是同1個股票,有相同的maturity。

在Excel中寫個函數,它可以根據portfolio在不同underlying price時的Payoff來辨認投資組合中各個option組成。


上圖左側的表格就是Input,Payoff圖在其下方(strangle);而右側的表格就是計算得到的結果,我寫的這個函數名字為 OpTypeStrikeContractNum


using System; using System.Collections.Generic; using System.Linq; using System.Text; using ExcelDna.Integration; using FinMktReverseEngineering; using Excel = Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using Microsoft.SolverFoundation.Services; using Microsoft.SolverFoundation.Solvers; namespace OptionFunctions {     //Solver Class for calculate the contract number for options     public class CSolver     {         private double[] interpolatedStockPrice;         private double[] interpolatedTargetPayoffs;         private Option[] options;         private int optionNum;         //Constructor         public CSolver(Option[] Options,double[] InterpolatedTargetPayoffs, double[] InterpolatedStockPrice)         {             this.options = Options;             this.interpolatedTargetPayoffs = InterpolatedTargetPayoffs;             this.interpolatedStockPrice = InterpolatedStockPrice;             this.optionNum = options.Length;         }         //Target Function         public double TargetFunction(double[] ContractNum)         {             double sum2OfDiff=0.0;             double sum2OfContractNum=0.0;             double totalPayoff;             for (int i = 0; i < interpolatedStockPrice.Length; i++)             {                 totalPayoff = 0.0;                 for (int j = 0;  j < options.Length;  j++)                 {                     totalPayoff+= options[j].Payoff(interpolatedStockPrice[i]) * ContractNum[j];                     sum2OfContractNum += Math.Pow(ContractNum[j]-Math.Round(ContractNum[j],0), 2);  //Very important! To avoid trivial answers!!!                 }                 sum2OfDiff += Math.Pow(interpolatedTargetPayoffs[i] - totalPayoff, 2);   //Residuals^2             }             return sum2OfDiff+sum2OfContractNum;         }         //Solver         internal double[] IndidualSolve()         {             var hls = new HybridLocalSearchSolver();             int[] contractNumID = new int[optionNum];             double[] results=new double[optionNum];             //Add variable in the solver             for (int i = 0; i < contractNumID.Length; i++)             {                 hls.AddVariable(out contractNumID[i],                     Microsoft.SolverFoundation.Common.Rational.NegativeInfinity,                     Microsoft.SolverFoundation.Common.Rational.PositiveInfinity,                      false);               }             //Solving             hls.AddGoal(hls.CreateNaryFunction(TargetFunction, contractNumID));//目標函數最小化             var hlsr = hls.Solve(new HybridLocalSearchParameters());             //Set results             for (int i = 0; i < results.Length; i++)             {                 results[i] = hlsr.GetValue(contractNumID[i]);             }             return results;        }     }         public class OptionFunctions         {             //Function OptionPrice             [ExcelFunction(Description = "Exact solution for European option", Category = "Option Functions")]             public static double OptionPrice([ExcelArgument(Description = @"Call (""C"") or a put (""P"")")]string optionType,                 [ExcelArgument(Description = @"Stock")]double underlying, [ExcelArgument(Description = @"Risk-free rate")]double interestRate,                  [ExcelArgument(Description = @"Volatility")]double volatility, [ExcelArgument(Description = @"Strike price")]double strikePrice,                   [ExcelArgument(Description = @"Time to maturity(in years)")]double timeToMaturity, [ExcelArgument(Description = @"Cost of carry")]double costOfCarry)             {                 // Basic validation -                 if (underlying <= 0.0 || volatility <= 0.0 || timeToMaturity <= 0.0 || strikePrice <= 0.0)                 {                     // Exception will be returned to Excel as #VALUE.                     throw new ArgumentException();                 }                 Option o = new Option();                 o.otyp = optionType;                 o.r = interestRate;                 o.sigma = volatility;                 o.K = strikePrice;                 o.T = timeToMaturity;                 o.b = costOfCarry;                 return o.Price(underlying);             }             //Function OptionPriceGreeks             [ExcelFunction(Description = "Compute exact solution for a European option, and returns price and greeks as a two-column, "                 + "six-row array with names and values", Category = "Option Functions")]             public static object[,] OptionPriceGreeks([ExcelArgument(Description = @"Call (""C"") or a put (""P"")")]string optionType,                 [ExcelArgument(Description = @"Value of the underlying stock")]double underlying, [ExcelArgument(Description = @"Risk-free rate")]double interestRate,                  [ExcelArgument(Description = @"Volatility")]double volatility, [ExcelArgument(Description = @"Strike price")]double strikePrice,                   [ExcelArgument(Description = @"Time to maturity(years)")]double timeToMaturity, [ExcelArgument(Description = @"Cost of carry")]double costOfCarry)             {                 // Basic validation                 if (underlying <= 0.0 || volatility <= 0.0 || timeToMaturity <= 0.0 || strikePrice <= 0.0)                 {                     // Exception will be returned to Excel as #VALUE.                     throw new ArgumentException();                 }                 Option o = new Option();                 o.otyp = optionType;                 o.r = interestRate;                 o.sigma = volatility;                 o.K = strikePrice;                 o.T = timeToMaturity;                 o.b = costOfCarry;                 return new object[7, 2]{             {"Price", o.Price(underlying)},             {"Delta", o.Delta(underlying)},             {"Gamma", o.Gamma(underlying)},             {"Vega", o.Vega(underlying)},             {"Theta", o.Theta(underlying)},             {"Rho", o.Rho(underlying)},             {"Coc", o.Coc(underlying)}             };             }             //Function Payoff             [ExcelFunction(Description = "Payoff for European option", Category = "Option Functions")]             public static object[,] Payoff(                 [ExcelArgument(Description = @"Call (""C"") or a put (""P"")")]object[] optionType,                 [ExcelArgument(Description = @"Strike price")]object[] strikes)             {                 int len=optionType.Length;                 //Option and Stock Price at T array <span style="white-space:pre"> </span>        Option[] options;                 double[] st;                  double[] payoffs;                    options= new Option[len];                 st = new double[len + 2];                 payoffs=new double[len+2];                 //Initiate the first element st=0.0                 st[0] = 0.0;                 st[len + 1] = (double)strikes[len⑴]+10.0;    //The last ST is last strike +10.0                 for (int i = 0; i < len; i++) <span style="white-space:pre"> </span>    { <span style="white-space:pre"> </span>        options[i]=new Option();                     options[i].otyp=(string)optionType[i];                     options[i].K=(double)strikes[i];                     st[i + 1] = (double)strikes[i]; //Set st as strike price                 }                 //Payoffs at different strockPrice                 payoffs = COptionPayoff.OptionPayoff(st, options);                 //Return result: St and corresponding Payoff                     object[,] result;                 result = new object[len+2,2];                 for (int i = 0; i < len+2; i++) <span style="white-space:pre"> </span>    { <span style="white-space:pre"> </span>        result[i,0]=(double)st[i];                     result[i,1]=(double)payoffs[i]; <span style="white-space:pre"> </span>    }                 return result;             }             //Return corresponding option contract number by minimizing square of sum of the payoff difference              //ST:       0,  100,    110,    120,    140,    150             //Payoff:  0,   10,     20,     20,     10,     10                [ExcelFunction(Description = "Return corresponding option contract number by minimizing square of sum of the payoff difference ", Category = "Option Functions")]             public static object[,] OpTypeStrikeContractNum(                 [ExcelArgument(Description = @"Stock price at maturity")]object[] StockPrices,                 [ExcelArgument(Description = @"Target payoffs")]object[] TargetPayoffs)             {                 int obsNum = StockPrices.Length;    //input number of stock price and target payoffs                 int optionNumNeeded = 2*(obsNum - 2);   //option number needed                 int numAfterInterpolation = obsNum * 2 - 2;                 double[] ContractNum;     //1st col: Type; 2nd col: Strike; 3rd col: ContractNum                 double[] interpolatedStockPrice;                     double[] interpolatedTargetPayoffs;                 Option[] options;                                  //Initiate the needed options                 options = new Option[optionNumNeeded];                 ContractNum = new double[optionNumNeeded];                 interpolatedStockPrice = new double[numAfterInterpolation]; //10                 interpolatedTargetPayoffs = new double[numAfterInterpolation];  //10                                  //Set options strike prices                 for (int i = 0; i < obsNum⑵; i++) //0,1,2,3                 {                     //Set the Option Instances                     options[i] = new Option();                     options[i].otyp = "C";                     options[i].K = (double)StockPrices[i + 1];                     options[i + optionNumNeeded / 2] = new Option();                     options[i + optionNumNeeded / 2].otyp = "P";                     options[i + optionNumNeeded / 2].K = (double)StockPrices[i + 1];                     //Set certain interpolated stock prices                     interpolatedStockPrice[i*2]=(double)StockPrices[i]; //0,2,4,6                     interpolatedTargetPayoffs[i*2]=(double)TargetPayoffs[i];                 }                 //Set the last two elements in the interpolation                 interpolatedStockPrice[numAfterInterpolation - 2] = (double)StockPrices[obsNum - 2];                 interpolatedStockPrice[numAfterInterpolation - 1] = (double)StockPrices[obsNum - 1];                 interpolatedTargetPayoffs[numAfterInterpolation - 2] = (double)TargetPayoffs[obsNum - 2];                 interpolatedTargetPayoffs[numAfterInterpolation - 1] = (double)TargetPayoffs[obsNum - 1];                 //Interpolation                 for (int i = 1; i < optionNumNeeded; i += 2)                 {                     interpolatedStockPrice[i] = 0.5 * (interpolatedStockPrice[i - 1] + interpolatedStockPrice[i + 1]);                     interpolatedTargetPayoffs[i] = 0.5 * (interpolatedTargetPayoffs[i - 1] + interpolatedTargetPayoffs[i + 1]);                 }                 //Solver                 CSolver solver = new CSolver(options, interpolatedTargetPayoffs, interpolatedStockPrice);                 ContractNum = solver.IndidualSolve();                 //result => 1st column is option type; 2nd column is strike; 3rd column is contract number                 object[,] result = new object[optionNumNeeded,3];                 for (int i = 0; i < optionNumNeeded; i++)                 {                     result[i, 0] = (string)options[i].otyp;                     result[i, 1] = (double)options[i].K;                     result[i,2] =(double) ContractNum[i];                 }                 return result;             }         }     }


之前1直困惑我的問題是,當用許多option(不同的K)去求解時,會出現很多小數contract number的問題,最后終究想到了1個辦法,那就是把它作為目標函數的1部份寫進去!結果成功!!!好爽!!!

例如,1個Strangle+Put:



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 成人欧美一区二区三区视频xxx | 女人毛片a毛片久久人人 | 日韩欧美亚洲国产精品字幕久久久 | a欧美在线| 亚洲天堂视频在线观看 | 欧美一区二区三区日韩免费播 | freefromvideos性欧美破 | 亚洲五月七月丁香缴情 | 欧美伊人 | 中文字幕组 | 国产精品亚洲综合一区 | 欧美一区二区三区高清视频 | 久久久日韩精品国产成人 | 精品久久一区二区 | jiucao在线观看精品 | 一本大道香蕉久在线不卡视频 | 午夜爽爽视频 | 亚洲日本在线观看视频 | 欧美在线精品一区二区三区 | 波多野结衣在线观看视频 | 亚洲欧美片 | 亚洲aⅴ在线 | 校园春色亚洲色图 | 亚洲精品综合一区二区三区 | 337p粉嫩日本大胆艺术 | 国模一区二区三区视频一 | asianjapanese日本护士 | 羞羞网站在线播放 | 爱爱客影院在线影院gf发现 | 女人l8毛片a一级毛片 | 美国一级毛片oo | 黑人videovideosex大 | 色综合久久综合欧美综合图片 | 欧美人与禽x0x0牲伦交 | 天天天做天天天天爱天天想 | 一级做a爰片性色毛片男 | 成人a毛片手机免费播放 | 韩日精品 | 亚洲视频中文字幕在线观看 | 久久精品国产精品2020 | 成人精品区|