Description
This is a very simple example of using the powerful ‘Math.Numerics’ package inside cTrader to calculate Option pricing using the BlackScholes model.
Future version will include more sophisticated implementations.
This should only be used for indices for now, thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using MathNet.Numerics;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class E7BlackScholesModel : Indicator
{
[Parameter("Strike Price", DefaultValue = 1.0)]
public double StrikePrice { get; set; }
[Parameter("Time to Expiration (Days)", DefaultValue = 5)]
public int TimeToExpirationDays { get; set; }
[Parameter("Risk-Free Interest Rate", DefaultValue = 1.81)]
public double RiskFreeInterestRate { get; set; }
[Parameter("Volatility", DefaultValue = 12)]
public double Volatility { get; set; }
[Output("Call Option Value", LineColor = "Lime")]
public IndicatorDataSeries CallOptionValue { get; set; }
[Output("Put Option Value", LineColor = "Red")]
public IndicatorDataSeries PutOptionValue { get; set; }
private double T;
private const VerticalAlignment vAlign = VerticalAlignment.Top;
private const HorizontalAlignment hAlign = HorizontalAlignment.Left;
public override void Calculate(int index)
{
T = TimeToExpirationDays / 365.0;
RunOptions(index);
}
private void RunOptions(int index)
{
double SpotPrice = Bars.ClosePrices[index];
double sigma = Volatility / 100;
double r = RiskFreeInterestRate / 100;
double d1 = (Math.Log(SpotPrice / StrikePrice) + (r + 0.5 * Math.Pow(sigma, 2)) * T) / (sigma * Math.Sqrt(T));
double d2 = d1 - sigma * Math.Sqrt(T);
double callOptionValue = SpotPrice * Normal.CDF(0, 1, d1) - StrikePrice * Math.Exp(-r * T) * Normal.CDF(0, 1, d2);
double putOptionValue = StrikePrice * Math.Exp(-r * T) * Normal.CDF(0, 1, -d2) - SpotPrice * Normal.CDF(0, 1, -d1);
CallOptionValue[index] = callOptionValue;
PutOptionValue[index] = putOptionValue;
double callDelta = Normal.CDF(0, 1, d1);
double putDelta = -Normal.CDF(0, 1, -d1);
double gamma = Normal.PDF(0, 1, d1) / (SpotPrice * sigma * Math.Sqrt(T));
double vega = SpotPrice * Normal.PDF(0, 1, d1) * Math.Sqrt(T) * 0.01;
double callRho = StrikePrice * T * Math.Exp(-r * T) * Normal.CDF(0, 1, d2) * 0.01;
double putRho = -StrikePrice * T * Math.Exp(-r * T) * Normal.CDF(0, 1, -d2) * 0.01;
string callOptionValueText = string.Format("Call Option Value : {0}", callOptionValue.ToString("F" + Symbol.Digits));
string callDeltaText = string.Format("Call Delta : {0}", callDelta.ToString("F" + Symbol.Digits));
string callGammaText = string.Format("Call Gamma : {0}", gamma.ToString("F" + Symbol.Digits));
string callVegaText = string.Format("Call Vega : {0}", vega.ToString("F" + Symbol.Digits));
string callRhoText = string.Format("Call Rho : {0}", callRho.ToString("F" + Symbol.Digits));
string putOptionValueText = string.Format("Put Option Value : {0}", putOptionValue.ToString("F" + Symbol.Digits));
string putDeltaText = string.Format("Put Delta : {0}", putDelta.ToString("F" + Symbol.Digits));
string putGammaText = string.Format("Put Gamma : {0}", gamma.ToString("F" + Symbol.Digits));
string putVegaText = string.Format("Put Vega : {0}", vega.ToString("F" + Symbol.Digits));
string putRhoText = string.Format("Put Rho : {0}", putRho.ToString("F" + Symbol.Digits));
ChartObjects.DrawText("Call Option Value", callOptionValueText, StaticPosition.TopLeft, Colors.Lime);
ChartObjects.DrawText("Call Delta", "\n" + callDeltaText, StaticPosition.TopLeft, Colors.Lime);
ChartObjects.DrawText("Call Gamma", "\n\n" + callGammaText, StaticPosition.TopLeft, Colors.Lime);
ChartObjects.DrawText("Call Vega", "\n\n\n" + callVegaText, StaticPosition.TopLeft, Colors.Lime);
ChartObjects.DrawText("Call Rho", "\n\n\n\n" + callRhoText, StaticPosition.TopLeft, Colors.Lime);
string structuretext = string.Format("E7 Trading");
Chart.DrawStaticText("E7 Trading", "\t" + structuretext, vAlign, hAlign, Color.Lime);
ChartObjects.DrawText("Put Option Value", putOptionValueText, StaticPosition.TopRight, Colors.Red);
ChartObjects.DrawText("Put Delta", "\n" + putDeltaText, StaticPosition.TopRight, Colors.Red);
ChartObjects.DrawText("Put Gamma", "\n\n" + putGammaText, StaticPosition.TopRight, Colors.Red);
ChartObjects.DrawText("Put Vega", "\n\n\n" + putVegaText, StaticPosition.TopRight, Colors.Red);
ChartObjects.DrawText("Put Rho", "\n\n\n\n" + putRhoText, StaticPosition.TopRight, Colors.Red);
}
}
}
Gwave
Joined on 26.10.2014
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: E7 BlackScholes Model.algo
- Rating: 0
- Installs: 404
- Modified: 13/08/2024 15:31
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
YE
hi, what is the package ? I juste take MathNet 5.0.0 and he make an error :
Package “MathNet.Numerics” is not supported
What specific limitations make this current implementation of the Black-Scholes model in cTrader only suitable for indices, and will future versions support other asset types like stocks or commodities? [size=1][url=https://planet-clicker.com][color=#232323]planet clicker[/color][/url][/size]