Option Theoretical Price Chart

Author: Copyright 2019, Serj_Che
Indicators Used
Indicator of the average true range
0 Views
0 Downloads
0 Favorites
Option Theoretical Price Chart
ÿþ//+---------------------------------------------------------------------------+

//|                                        Option Theoretical Price Chart.mq5 |

//|                                                  Copyright 2019, Serj_Che |

//|                                    https://www.mql5.com/ru/users/serj_che |

//+---------------------------------------------------------------------------+

#property copyright "Copyright 2019, Serj_Che"

#property link      "https://www.mql5.com/ru/users/serj_che"

#property version   "1.00"

#property description "=48:0B>@ AB@>8B 3@0D8: B5>@5B8G5A:>9 F5=K ?F8>=0," 

#property description "=0 >A=>20=88 F5=K 107>2>3> 0:B820 8 8AB>@8G5A:>9 2>;0B8;L=>AB8,"

#property description "@0AAG8B0==>9 A ?><>ILN AB0=40@B=>3> 8=48:0B>@0 ATR." 

#property indicator_separate_window



#property indicator_buffers 5 

#property indicator_plots   1 

#property indicator_type1   DRAW_CANDLES

#property indicator_color1  clrDimGray,clrOrangeRed,clrSteelBlue 

#property indicator_style1  STYLE_SOLID 

#property indicator_width1  3 



enum eotyp{Call,Put};

enum edatetyp{Futures_Expiration,Custom_Date};

enum evoltyp{Historical,Custom};



//--- indicator settings

input eotyp       Option_Typ=Call;

input double      Strike=66000;

input evoltyp     Select_Volatility=Historical;

input double      Custom_Volatility=10.0;

input int         AtrPeriod=14;  // ATR period

input edatetyp    Select_Expiration=Futures_Expiration;

input datetime    Expiration_Date=D'21.03.2019 18:45';



//--- indicator buffers

double         BufferOpen[];

double         BufferHigh[];

double         BufferLow[];

double         BufferClose[];

double         BufferColor[];



//--- global variable

int hatr;

double atr[];

datetime exptime;

//+---------------------------------------------------------------------------+

//| Custom indicator initialization function                                  |

//+---------------------------------------------------------------------------+

int OnInit()

  {

//--- create ATR indicator

   hatr=iATR(_Symbol,PERIOD_D1,AtrPeriod);

   if(hatr==INVALID_HANDLE)

     { Print("! ERROR ATR INVALID_HANDLE: ",GetLastError()); return(INIT_FAILED); }

   exptime=(datetime)SymbolInfoInteger(_Symbol,SYMBOL_EXPIRATION_TIME);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferOpen,INDICATOR_DATA);

   SetIndexBuffer(1,BufferHigh,INDICATOR_DATA);

   SetIndexBuffer(2,BufferLow,INDICATOR_DATA);

   SetIndexBuffer(3,BufferClose,INDICATOR_DATA);

   SetIndexBuffer(4,BufferColor,INDICATOR_COLOR_INDEX);

   string name="OTPC "+EnumToString(Option_Typ);

   PlotIndexSetString(0,PLOT_LABEL,name+" Open;"+name+" High;"+name+" Low;"+name+" Close"); 

   IndicatorSetString(INDICATOR_SHORTNAME,name+": ");

   IndicatorSetInteger(INDICATOR_DIGITS,2);

   IndicatorSetInteger(INDICATOR_LEVELS,1);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,STYLE_SOLID);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

//---

   return(INIT_SUCCEEDED);

  }

//+---------------------------------------------------------------------------+

//| Custom indicator iteration function                                       |

//+---------------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   int i,limit;

   double days=0.0,hv=Custom_Volatility/100;

//--- preliminary calculations

   limit=(int)MathMax(prev_calculated-1,0);

   for(i=limit; i<rates_total && !_StopFlag; i++)

     {

      if(Select_Expiration==Custom_Date) exptime=Expiration_Date;

      datetime currtime=time[i];

      if(i==rates_total-1) currtime=TimeTradeServer();

      days=(double)(exptime-currtime)/(1440*60); // days before expiration



      if(CopyBuffer(hatr,0,time[i],1,atr)<1) return(i);

      if(atr[0]==0 || atr[0]==EMPTY_VALUE)

        {

         BufferOpen[i]=0; BufferHigh[i]=0; BufferLow[i]=0; BufferClose[i]=0; continue;

        }



      if(Select_Volatility==Historical) hv=(atr[0]/close[i])*15.8745; //sqrt(252);

      BufferOpen[i]=OptionTheorPrice(Option_Typ,open[i],Strike,days/366.0,hv);

      BufferHigh[i]=OptionTheorPrice(Option_Typ,high[i],Strike,days/366.0,hv);

      BufferLow[i]=OptionTheorPrice(Option_Typ,low[i],Strike,days/366.0,hv);

      BufferClose[i]=OptionTheorPrice(Option_Typ,close[i],Strike,days/366.0,hv);

     }

   IndicatorSetDouble(INDICATOR_LEVELVALUE,BufferClose[rates_total-1]);

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+---------------------------------------------------------------------------+

//-- typ: Type of option                                                      |

//-- F: Current Futures Price                                                 |

//-- S: Strike option                                                         |

//-- V: Volatility                                                            |

//-- T: Time in fractions of a year before expiration (30 / 365)              |

//-- return: option price                                                     |

//+---------------------------------------------------------------------------+

double OptionTheorPrice(eotyp typ,double F,double S,double T,double V)

  {

   double price_Call=0.0;

   double price_Put=0.0;

   double a,b,d0,d1,d2;

   if(T<=0) price_Call=MathMax(F-S,0);

   else

     {

      a = V * V * T / 2;

      b = V * MathSqrt(T);

      d0 = MathLog(F / S);

      d1 = (d0 + a) / b;

      d2 = (d0 - a) / b;

      price_Call=F*N(d1)-S*N(d2);

     }

   price_Put=price_Call+S-F;

   if(typ==Call) return(price_Call);

   if(typ==Put) return(price_Put);

   return(0);

  }

//+---------------------------------------------------------------------------+

//-- Standard Normal Distribution Function (Probability Density)              |

//+---------------------------------------------------------------------------+

double N(double x)

  {

   if(x > 10) return(1);

   if(x < -10) return(0);

   double ax= MathAbs(x);

   double t = 1 / (1 + 0.2316419 * ax);

   double d = 1 / MathSqrt(2 * M_PI) * MathExp(-x * x / 2);

   double p = d * t * ((((1.330274429 * t - 1.821255978) * t + 1.781477937) * t - 0.356563782) * t + 0.31938153);

   if(x > 0) return(1 - p);

   else return(p);

  }

//+---------------------------------------------------------------------------+

//+---------------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---