<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MQLmagazine.com &#187; Financial modelling</title>
	<atom:link href="http://mqlmagazine.com/category/financial-modelling/feed/" rel="self" type="application/rss+xml" />
	<link>http://mqlmagazine.com</link>
	<description>All things MetaTrader</description>
	<lastBuildDate>Sat, 10 Jul 2010 14:57:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Statistical Method for Self-Tuning Trading Systems</title>
		<link>http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/</link>
		<comments>http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/#comments</comments>
		<pubDate>Mon, 24 May 2010 21:28:30 +0000</pubDate>
		<dc:creator>Bogdan Caramalac, MQLmagazine sr.editor</dc:creator>
				<category><![CDATA[Financial modelling]]></category>

		<guid isPermaLink="false">http://mqlmagazine.com/?p=1910</guid>
		<description><![CDATA[[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]
I am starting to write this article in the idea that it might become useful in a not too far time horizon. I have been thinking at this for some time. The autotuning is an idea that is out there for some while, however the extreme complexity of retail [...]]]></description>
			<content:encoded><![CDATA[<p><a title="[Versiunea romaneasca]" href="http://mqlmagazine.com/ro/modelare-financiara/o-metoda-statistica-pentru-autoreglarea-sistemelor-de-trading" target="_top">[Versiunea romaneasca]</a> <a title="[MQLmagazine.com in romana]" href="http://mqlmagazine.com/ro" target="_top">[MQLmagazine.com in romana]</a> <a title="[English edition]" href="http://mqlmagazine.com" target="_top">[English edition]</a></p>
<p>I am starting to write this article in the idea that it might become useful in a not too far time horizon. I have been thinking at this for some time. The autotuning is an idea that is out there for some while, however the extreme complexity of retail trading systems approach made it quite inapplicable. However, HFT trading systems are generally simple, and quite similar rules are applied to hundreds of equities, in a parametrized fashion. Thus, if rules are simple and can be written in &#8220;<strong>when</strong> this, <strong>do</strong> that , <strong>resulting</strong> this&#8221; instead of maze-like logics of retail systems, then a set of optimal statistics on the entry data (spanning over an array of quotes, orderbook data and so on) is the premise for simple operations like quoting, getting filled, taking profit or stop loss in just a few ticks away. As seen in the previous Progress Apama article, these algorithms are highly unstable, therefore there is a constant run for newer algorithms. </p>
<p>Now you may ask, why would we attempt autotuning of trading systems if the institutionals do not (at least on the HFT level). Thing is, some of their algos are so fast that <strong>they might not even have time enough to record the data in a database</strong>, so automatic tuning, that requires extra analysis, might be a complete waste of time, with a too small improvement brought to the algos compared to the extra latency that is forming up. <strong>What will be coming in these articles has therefore an experimental nature and doesn&#8217;t have to be taken as a solution, rather an attempt &#8211; I cannot tell for sure if there is any point in attempting to find, with regular statistics, recurring parameters of a stochastic process.</strong> </p>
<p>So, let&#8217;s presume that an array of parameters (such as individual volatilities, moving averages, executions volume, etc.), is to be fed to a database or to an inner storing array. We have to take into account the fact that a trading signal may exist permanently, at every tick. Supposing the volume to be constant, then same signal can be wired into two actions: either buy or sell. Each signal will therefore have two results, one for buying, one for selling. We could however create separate rules for selling.</p>
<p>For instance, a signal comprised of a volatility and the distance between mid quote and mid quotes moving average comes with: 0.03 stddev &#038; 2 ticks. To twist it a bit, we introduce quoting, and we note 0 for no fill. Wired into a buy with a 3 tick take profit and 2 tick stop loss, at a given time, makes 2 ticks profit, wired into a sell it is not filled. So the buys database will be added a (0.03 ; 2 ; 2) and the sells (0.03 ; 2 ; 0). </p>
<p>And now the things become interesting. At the end, after the data is gathered for a mass of signals that make up the &#8220;big number&#8221; for the statistics, we have to look thru the database. First, a simple watch will group the database by action and result. In this case, we have six final states:</p>
<p>1. BUY: quote, get filled, take profit (BQFT)<br />
2. BUY: quote, get filled, stop loss (BQFS)<br />
3. BUY: quote, don&#8217;t get filled, cancel order (BQC)<br />
4. SELL: quote, get filled, take profit (SQFT)<br />
5. SELL: quote, get filled, stop loss (SQFS)<br />
6. SELL: quote, don&#8217;t get filled, cancel order (SQC)</p>
<p>So we have six cases. Analysing BUY with quote, get filled, take profit, is just one of the six states and will comprise a large part of data, the same as the other states. The purpose of the statistical analysis is to have a statistic prepaired per each case. For instance, our case should look, in the end, like:</p>
<p>0.03, 2 :  45% BQFT, 35% BQSL, 20% BQC ; 30% SQFT, 30% SQSL, 40% SQC</p>
<p>After the intial mass of signals along with results is collected, statistics have to be deployed for each combination of the parameters. Having real numbers as parameters, with lots of digits , will lead to the fact that you may have actually a number of cases equal to the number of signals that were the input, rendering statistics impossible. So, there has to be applied some data aggregation. For instance, for each parameter set, can be calculated the quartiles then the number of bins with the Freedman-Diaconis formula (this is an example, other methods can be applied). This will yield up a different number of bins for each of the parameters. Then a new array will be created, where each case will have a sticker denoting its number of bins. For instance the 0.03 stddev is labeled as &#8220;Bin 1&#8243; on the stddev scale and the 2 tick distance is labeled as &#8220;Bin 2&#8243; on the tick distance scale. The total number of cases may be smaller or equal than the product of bins count per each criteria. If you have 3 criterias and 10 bins each, that may be even 1000 number of cases. More likely, a core number of cases will have a lot of situations recorded, while others will barely have one situation recorded. However, the more platykurtic the repartition of situations per cases, the more enforced the need for a &#8220;bigger number&#8221; of situations. After applying the statistics, our database statistics will look like:</p>
<p>Bin 1, Bin 2 :  45% BQFT, 35% BQSL, 20% BQC ; 30% SQFT, 30% SQSL, 40% SQC</p>
<p>What is important is that <strong>relevant statistics</strong> is to be extracted. If this is the current case, what would be the trading decision? Go for a buy with a chance of take profit in 45% of the situations this case appears? Only that it has to stand to 35% stop losses&#8230; Because on the sell side it&#8217;s even worse, with stop losses situations roughly equal to take profit ones. This case has to be decided as a <strong>buy</strong> case, although <strong>with reserves</strong>. Unless the profits are relevant enough, there is no decision to be taken. Nevertheless, <strong>it is mandatory for the algo to have a decision module that picks only relevant cases for trading</strong>. But <strong>how fast can the machine be</strong>, because after the initial data is gathered, each new signal, (that can be even a new tick, in a HFT-like algo), will trigger a massive recalculation of bin sizes on the entire database, <strong>to update the statistics, and at the same time to allow the delivering and execution of the trading decision in real time?</strong></p>
<p><em>Remember my article about the CEP engine. The CEP is visioned there as a recognition &#8211; trigger module, but what if the CEP is first used as a recording device ? What if the CEP is wired to the statistics engine? How much latency would this add to the calculus? Can we still talk about real time?</em></p>
<div class='dd_after'><table><tr><td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/&amp;t=A+Statistical+Method+for+Self-Tuning+Trading+Systems&amp;s=compact' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/&amp;title=A+Statistical+Method+for+Self-Tuning+Trading+Systems&amp;t=2' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'><!--yahooBuzzArticleHeadline=A+Statistical+Method+for+Self-Tuning+Trading+Systems;//--></script><script type='text/javascript' src='http://d.yimg.com/ds/badge2.js' badgetype='small-votes'></script></td><td><iframe src='http://api.tweetmeme.com/button.js?url=http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/&amp;source=&amp;style=compact' height='20' width='90' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'> var fbShare = {url: 'http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/',size:'small'}</script> <script type='text/javascript' src='http://widgets.fbshare.me/files/fbshare.js'></script></td></tr></table></div><!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/ -->]]></content:encoded>
			<wfw:commentRss>http://mqlmagazine.com/financial-modelling/a-statistical-method-for-self-tuning-trading-systems/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Anatomy of a simple CEP engine</title>
		<link>http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/</link>
		<comments>http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 19:34:10 +0000</pubDate>
		<dc:creator>Bogdan Caramalac, MQLmagazine sr.editor</dc:creator>
				<category><![CDATA[Financial modelling]]></category>

		<guid isPermaLink="false">http://mqlmagazine.com/?p=1603</guid>
		<description><![CDATA[[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]
If there wouldn&#8217;t be that CEP abbreviation there you&#8217;d probably ask how that something inherently complex could become simple. Thing is, everything starts by being simple at first then evolving to something more complicated. 
Even if you might have read my Progress Apama article, I remind you here the [...]]]></description>
			<content:encoded><![CDATA[<p><a title="[Versiunea romaneasca]" href="http://mqlmagazine.com/ro/modelare-financiara/anatomia-unui-motor-cep-simplu/" target="_top">[Versiunea romaneasca]</a> <a title="[MQLmagazine.com in romana]" href="http://mqlmagazine.com/ro" target="_top">[MQLmagazine.com in romana]</a> <a title="[English edition]" href="http://mqlmagazine.com" target="_top">[English edition]</a></p>
<p>If there wouldn&#8217;t be that CEP abbreviation there you&#8217;d probably ask how that something inherently complex could become simple. Thing is, everything starts by being simple at first then evolving to something more complicated. </p>
<p>Even if you might have read my <a title="Progress Apama" href="http://mqlmagazine.com/other-platforms/progress-apama-how-deep-does-hft-rabbit-hole-go/" target="_top">Progress Apama</a> article, I remind you here the definition given by Dan Hubscher to CEP : <strong>&#8220;the ability to relate two or more events, even if these events occur in a span of time, and then determining meaning from that relationship immediately, such as detecting a trading signal, or an alpha-generating profit opportunity&#8221;.</strong></p>
<p>So, given that events have to be related, within a time constraint, <strong>there must be an event queue, which stores individual events </strong>, as they are being received. Sure, <strong>storing is not made ad infinitum</strong>, in an ever increasing queue ; the queue size has to be dynamic, but it&#8217;s length in time should not exceed a predetermined amount of time, say 5 minutes. This is why a CEP engine, that is mainly targetted for HFT and Level II trading, doesn&#8217;t have to mixed with Level I trading signals, because these span on a way higher amount of time, lots of minutes to hours or even more, making the event stack not only to grow to an unmanageable size, but also to slow down the engine speed.</p>
<p><strong>The covered amount of time</strong> is the first limitation of the engine. </p>
<p>Second, is the link between simple events. <strong>Simple events are not fully independent</strong>, as the Progress Apama webinar slide might show. For instance, you are considering a cross of higher Bollinger Band as an event, but also the cross of the lower Bollinger Band. These events are completely opposite. Should both happen, at the time the second happens, the first one must elliminated from the queue, as you see in the scheme below that event 4 is deleted. There are two paths that may be chosen, either opposite events are removed from the event queue after some time &#8211; <em>which is diffcult to determine</em>, or before final validation of a complex event there are searched opposite simple events coming after the ones that make up the complex event.</p>
<p>Third, <strong>avoiding event redundancy</strong>. Bad implementations can cause redundancy, meaningly same event to be reported over and over again. For instance, &#8220;MSFT is below 5-minute moving average&#8221;. A parenthesis here &#8211; <em>I don&#8217;t mean the 5 minute chart, rather the moving average of all ticks occurred in last 5 minutes, that&#8217;s why I said MetaTrader should have tick data and tick period enabled.</em>When it happens, it is an event. But the next second, at the next tick, it will be reported again, and the engine has to reject it, because it already is in the queue, as there is the case with the orange event from the scheme, which doesn&#8217;t even gets a number, because it is rejected from the beginning. Such situations should be avoided from start &#8211; the engine mustn&#8217;t lose time for redundancy check, otherwise the engine should be able to filter for these events , but it&#8217;s better that the event stream is cleaned before.</p>
<p><a href="http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/attachment/simple-cep-engine-schematics/" rel="attachment wp-att-1682"><img src="http://mqlmagazine.com/wp-content/uploads/2010/03/Simple-CEP-Engine-Schematics.jpg" alt="" title="Simple CEP Engine Schematics" width="899" height="532" class="alignnone size-full wp-image-1682" /></a></p>
<p>This is the schematics of the engine. The tick events, or the more complex calculus events originate from an OnChart() event that is triggered by more Tick Event EAs, as they are described in the article <a title="Tick events in MQL5: complicated for now, but functional" href="http://mqlmagazine.com/mql-programming/tick-events-in-mql5-complicated-for-now-but-functional/" target="_top">Tick events in MQL5: complicated for now, but functional</a> .</p>
<p>The first column is event number in the queue, from the most recent to the oldest. Each simple event, that is linked to the data, has a number allocated to it. The same event, happening on a different instrument, has to be given a different number, so same event sequence making up a complex event will be made up of same simple events but with different identfiers. The engine must know <strong>which events are completely opposite</strong>, for instance 1 and 3, also which event is allowed to appear multiple times in the queue, because some may be allowed (for instance execution events (the ones beginning with E in the scheme)). All other events will be forbidden from appearing multiple times &#8211; but however this should not be done by the engine per se, the one that moves the event queue, rather these must be forbidden from the pre-processing phase.</p>
<p>At the same time, the CEP engine must recognize each complex event knowing all simple event arrangements.<br />
For instance, if a complex event for Microsoft (MSFT) is made of  &#8220;10 followed by (11 or (12 and 13))&#8221; within 3 minutes, then the binder will look for, within 3 minutes: 10, 11 ; 10, 12, 13 ; 10, 13, 12 meaning for all alternate paths. If the same event would mapped for SP500 (^GSPC), given that its sub events will have different numbers, the condition might look like &#8220;5 followed by (15 or (16 and 17))&#8221;, having the arrangements: 5, 15 ; 5, 16, 17 ; 5, 17, 16. Such arrangements can be given directly to the engine, written directly inside, or generated inside with an algorithm, transforming the binary expression, with the notable difference that <strong>&#8220;FOLLOWED BY&#8221; is a non commutative AND</strong>. Because the rules are defined as series of events appearing in a certain order, the negation can&#8217;t exist as in a binary expression. It can be implemented only a a interdiction for some events to appear over the entire time constraint or within some sequences, as simple forbidden entries between required events.</p>
<p>You can see that I included in the scheme two bind columns. That is , <strong>one simple event can be binded to more than one complex event</strong>, because otherwise some rules would never trigger. Complex events would have a separate ID set, that is not to intersect with the simple events ID sets. The engine core will send the complex events to be processed by user in the EventsCallback(), but at the same time, the user could <strong>pipeline</strong> these events to another CEP engine.</p>
<p>The matching algorithm begins with the construction of the simple events matrix per each complex event. The simple event matrix will contain on each row an arrangement. Because checking is done backward, from the later events to older events, <strong>arrangements have to be written in the matrix in a reversed manner</strong>. Each complex event will need passing thru the events queue only for as long as the time constraint spans, in a vertical manner : first pass will check events on the first column, and simple events found this way will be pre-binded ; second pass will check the second column only for arrangements that had a pre-bind on the first. When no new pre-binds take place, matching is aborted for that specific complex event. However, to minimize the time needed, each pass will regard all events, before jumping to the next pass. When searches are cancelled for all complex events, algorithm ends.<br />
When an arrangement has all simple-events pre-binded, it&#8217;s time to check for negations. First, there will be an entire period check for global negations &#8211; events forbidden during entire complex event, then for events forbidden between pre-binded events. When all negations are checked,a final check has to be done to ensure they still abide the time constraint, and only in this case, simple events are binded to the complex event, <em>provided that in the bind array of each simple event this bind is not previously done &#8211; to avoid re-binding to same complex event</em>. The resulting complex event is then put in a bind queue and passed to EventsCallback(). <em>Sending events to EventsCallback() must take place after the algo is done, otherwise there might appear recursive engine calls that will not end their execution.</em>For the simple events that are being binded, the current bind array position will be filled with the complex event, the position is then incremented by one.</p>
<p>Here is an example of different complex events in different stages of pre-binding:</p>
<p><a href="http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/attachment/prebinding-of-simple-events/" rel="attachment wp-att-1700"><img src="http://mqlmagazine.com/wp-content/uploads/2010/03/Prebinding-of-Simple-Events.jpg" alt="" title="Prebinding of Simple Events" width="486" height="469" class="alignnone size-full wp-image-1700" /></a></p>
<p>For the MSFT event, the engine finds event 12 on the first pass, and pre-binds it on the third arrangement. On the second pass, it finds event 13, and pre-binds it to the second arrangement. It finds 12 too, but it can&#8217;t pre-bind it to the second arrangement because it occurred before 13. If on the next pass finds the older 10 event, third arrangement is completed, being no negations there, and if time still abides constraint, provided that complex event 1 was not binded yet, then simple events on the third arrangement are binded to complex event 1, bind counter incremented, event placed in the bind queue. For the ^GSPC event (complex event 2), the engine has completed already the first arrangement. Event 5 does not appear checked for second and third arrangement because events on the previous columns are not checked.</p>
<p>As for the execution events, these are sent by the EnumCallbacks() event of the DealHandler class from the <a title="Distinguishing quasi simultaneous fills - class to report last deals" href="http://mqlmagazine.com/mql-programming/distinguishing-quasi-simultaneous-fills-class-to-report-last-deals/" target="_top">Distinguishing quasi simultaneous fills &#8211; class to report last deals</a> article. Simple execution events can also be mapped in more complex execution events because deals have magic numbers and comments, and if you buy two times in row same 1000 stocks, you will know which buy was part of which complex execution event. This is why such events could be treated either seperately or in combination with market events.</p>
<p>We will come with a coding example in a future article.</p>
<div class='dd_after'><table><tr><td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/&amp;t=Anatomy+of+a+simple+CEP+engine&amp;s=compact' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/&amp;title=Anatomy+of+a+simple+CEP+engine&amp;t=2' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'><!--yahooBuzzArticleHeadline=Anatomy+of+a+simple+CEP+engine;//--></script><script type='text/javascript' src='http://d.yimg.com/ds/badge2.js' badgetype='small-votes'></script></td><td><iframe src='http://api.tweetmeme.com/button.js?url=http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/&amp;source=&amp;style=compact' height='20' width='90' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'> var fbShare = {url: 'http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/',size:'small'}</script> <script type='text/javascript' src='http://widgets.fbshare.me/files/fbshare.js'></script></td></tr></table></div><!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/ -->]]></content:encoded>
			<wfw:commentRss>http://mqlmagazine.com/financial-modelling/anatomy-of-a-simple-cep-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtual methods in MQL5 &#8211; an application on options</title>
		<link>http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/</link>
		<comments>http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 15:25:53 +0000</pubDate>
		<dc:creator>Bogdan Caramalac, MQLmagazine sr.editor</dc:creator>
				<category><![CDATA[Financial modelling]]></category>
		<category><![CDATA[MQL programming]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[mql5]]></category>
		<category><![CDATA[virtual]]></category>

		<guid isPermaLink="false">http://mqlmagazine.com/?p=523</guid>
		<description><![CDATA[[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]
This is a continuation of our article Intro to Options. Part II : Option valuation and greeks . However, it can be viewed and understood seperately, because it focuses on the programming side.
The option models are exceedingly complicated from the math point of view. The purpose of this article [...]]]></description>
			<content:encoded><![CDATA[<p><a title="[Versiunea romaneasca]" href="http://mqlmagazine.com/ro/programarea-in-mql/metode-virtuale-in-mql5-aplicatie-asupra-optiunilor/" target="_top">[Versiunea romaneasca]</a> <a title="[MQLmagazine.com in romana]" href="http://mqlmagazine.com/ro" target="_top">[MQLmagazine.com in romana]</a> <a title="[English edition]" href="http://mqlmagazine.com" target="_top">[English edition]</a></p>
<p>This is a continuation of our article <a title="Intro to Options. Part II : Option valuation and greeks" href="http://mqlmagazine.com/option-trading/intro-to-options-part-ii-option-valuation-and-greeks/" target="_top">Intro to Options. Part II : Option valuation and greeks</a> . However, it can be viewed and understood seperately, because it focuses on the programming side.</p>
<p>The option models are exceedingly complicated from the math point of view. The purpose of this article is to present an implementation of the Black-Scholes valuation model, also by introducing <strong>virtual methods</strong>.</p>
<p>One of the functions that appears over and over again in financial math, also in the Black-Scholes model, is the <strong>cummulative distribution function</strong>.</p>
<p>The function that describes the bell-shaped form of the distribution is called <strong>probability density function</strong>. The area that is below it, meaningly the definite integral , is the <strong>cummulative distribution function</strong>. And for the case with 0 mean and 1 standard deviation, (assuming also no skewness , mezokurtic) this distribution is called <strong>normal</strong>.</p>
<p>The definite integral, also known as Riemann sum, calculates the area below a function&#8217;s graphics by adding infinitezimal rectangles that span below the function and x axis. Obviously, the Riemann calculus procedure is always the same, while the function is something user defined. Both Pascal and C++ allowed function types. A C++ declaration would have looked like this:</p>
<pre lang="c++">
typedef  double (*SingleVarFunction) (double) ;
double Riemann(SingleVarFunction pFunc,double a, double b, int divisions)
</pre>
<p>In this C++ case, the Riemann could have received as parameter any function that received one double as parameter and returned double as result. However, this mechanism does not exist in MQL5.</p>
<p>This is where <strong>virtual methods</strong> come into play. Objects have properties. Properties that are designed to be public can be accessed from outside the objects, and then methods called again. For instance the user can change the radius of a circle and call the redraw method. But in the case of a Riemann sum what has to be passed as a property value is itself a method. <strong>User-defined methods that are called by inherited methods are called <em>virtual methods</em></strong>.</p>
<p>This is the MQL5 implementation for Riemann sum:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5238"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p523code8"><pre class="mql5" style="font-family:monospace;"><span style="color: #0000ff;">class</span> RiemannSumCalculus
    <span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
     <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">double</span> IntegratableFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>0.0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>     
     <span style="color: #0000ff;">double</span> Riemann<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> a,<span style="color: #0000ff;">double</span> b,<span style="color: #0000ff;">uint</span> divisions<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">double</span> sum,swap,stepsize,x<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">long</span> istep<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000080;">&gt;</span>b<span style="color: #008000;">&#41;</span> 
          <span style="color: #008000;">&#123;</span>
           swap<span style="color: #000080;">=</span>a<span style="color: #008080;">;</span>
           a<span style="color: #000080;">=</span>b<span style="color: #008080;">;</span>
           b<span style="color: #000080;">=</span>swap<span style="color: #008080;">;</span>
          <span style="color: #008000;">&#125;</span>
        sum<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>divisions<span style="color: #000080;">==</span>0<span style="color: #008000;">&#41;</span>
          divisions<span style="color: #000080;">=</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>
        stepsize<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>b<span style="color: #000040;">-</span>a<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>divisions<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>istep<span style="color: #000080;">=</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>istep<span style="color: #000080;">&lt;=</span>divisions<span style="color: #008080;">;</span>istep<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#123;</span>
            x<span style="color: #000080;">=</span>a<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>istep<span style="color: #000040;">-</span>0.5<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>stepsize<span style="color: #008080;">;</span>
            sum<span style="color: #000080;">=</span>sum<span style="color: #000040;">+</span>IntegratableFunction<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>stepsize<span style="color: #008080;">;</span>
           <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
     <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>As you see, the IntegratableFunction method is designed as <strong>virtual</strong>. The function itself does nothing, returns always a zero. Because the function is not made to be used directly. It is just a place holder for user-defined IntegratableFunction(s) that are to be used by the Riemann method.</p>
<p>Next step is to have the Gauss cummulative distribution.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5239"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p523code9"><pre class="mql5" style="font-family:monospace;"><span style="color: #0000ff;">class</span> GaussCummulativeFunction <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> RiemannSumCalculus          
  <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span> <span style="color: #0000ff;">double</span> sigma<span style="color: #008080;">;</span>  
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>                                 
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">double</span> IntegratableFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span> 
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> <span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>x<span style="color: #000040;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>2<span style="color: #000040;">*</span>sigma<span style="color: #000040;">*</span>sigma<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">double</span> CummulativeDistribution<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> stddev,<span style="color: #0000ff;">double</span> l1,<span style="color: #0000ff;">double</span> l2,<span style="color: #0000ff;">uint</span> divisions<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
        sigma<span style="color: #000080;">=</span>stddev<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>1<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>stddev<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>2<span style="color: #000040;">*</span><span style="color: #333399;">M_PI</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> Riemann<span style="color: #008000;">&#40;</span>l1,l2,divisions<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>As you can see the class is inherited from RiemannSumCalculus. The IntegratableFunction is redefined, and the CummulativeDistribution function calls the inherited Riemann, which, in turn, will call the local IntegratableFunction, instead of the inherited null-value place holder function. And since this is a class, we can create a more easy way to access this, by calling this method from a function:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p52310"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p523code10"><pre class="mql5" style="font-family:monospace;"><span style="color: #0000ff;">double</span> cum_norm<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   GaussCummulativeFunction normaldistribution<span style="color: #008080;">;</span>      
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>normaldistribution.<span style="color: #007788;">CummulativeDistribution</span><span style="color: #008000;">&#40;</span>1,<span style="color: #000040;">-</span>7.0,x,1000000<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>   
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Perhaps 1,000,000 divisions seem a bit too much, but it&#8217;s necesary to keep results accurate.</p>
<p>What follows is the MQL5 version of the Black-Scholes procedure written by George Levy in his book, &#8220;Computational finance using C and C++&#8221;, page 76 (with some small modifications):</p>

<div class="wp_codebox"><table width="100%" ><tr id="p52311"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
</pre></td><td class="code" id="p523code11"><pre class="mql5" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Greeks
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> delta<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> gamma<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> vega<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> theta<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> rho<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>  
&nbsp;
<span style="color: #0000ff;">void</span> black_scholes<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> value, Greeks <span style="color: #000040;">&amp;</span>greeks, <span style="color: #0000ff;">double</span> s0, <span style="color: #0000ff;">double</span> x, <span style="color: #0000ff;">double</span> sigma, <span style="color: #0000ff;">double</span> t, <span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> q, <span style="color: #0000ff;">bool</span> iscall, <span style="color: #0000ff;">long</span> <span style="color: #000040;">&amp;</span>iflag<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
     <span style="color: #ff0000; font-style: italic;">/* 
     Input parameters:
     =================
     s0 - the current price of the underlying asset
     x - the strike price
     sigma - the volatility
     t - the time to maturity
     r - the interest rate
     q - the continuous dividend yield
     iscall - true if option is a call
&nbsp;
     Output parameters:
     ==================
     value - the value of the option
     greeks[] - the hedge statistics output;
     iflag - an error indicator
     */</span>
&nbsp;
     greeks.<span style="color: #007788;">delta</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     greeks.<span style="color: #007788;">gamma</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     greeks.<span style="color: #007788;">vega</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     greeks.<span style="color: #007788;">theta</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     greeks.<span style="color: #007788;">rho</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">double</span> one<span style="color: #000080;">=</span>1.0,two<span style="color: #000080;">=</span>2.0,zero<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">double</span> eps,d1,d2,temp,temp1,temp2,np<span style="color: #008080;">;</span>
     <span style="color: #ff0000; font-style: italic;">/* Check if any of the the input arguments are too small */</span>
     eps<span style="color: #000080;">=</span><span style="color:#800080;">1.0e-16</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&lt;</span> eps<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>sigma <span style="color: #000080;">&lt;</span> eps<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>t <span style="color: #000080;">&lt;</span> eps<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> 
       <span style="color: #008000;">&#123;</span> 
        iflag <span style="color: #000080;">=</span> <span style="color: #008000;">2</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
     temp <span style="color: #000080;">=</span> <span style="color: #8a2be2;">log</span><span style="color: #008000;">&#40;</span>s0<span style="color: #000040;">/</span>x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     d1 <span style="color: #000080;">=</span> temp<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>r<span style="color: #000040;">-</span>q<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>sigma<span style="color: #000040;">*</span>sigma<span style="color: #000040;">/</span>two<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>t<span style="color: #008080;">;</span>
     d1 <span style="color: #000080;">=</span> d1<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>sigma<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     d2 <span style="color: #000080;">=</span> d1<span style="color: #000040;">-</span>sigma<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #ff0000; font-style: italic;">/* evaluate the option price */</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>iscall<span style="color: #000080;">==</span><span style="color: #333399;">true</span><span style="color: #008000;">&#41;</span>
       value <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span>d1<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span> x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     else
       value <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>d1<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #ff0000; font-style: italic;">/* then calculate the Greeks */</span>
     temp1 <span style="color: #000080;">=</span> <span style="color: #000040;">-</span>d1<span style="color: #000040;">*</span>d1<span style="color: #000040;">/</span>two<span style="color: #008080;">;</span>
     d2 <span style="color: #000080;">=</span> d1<span style="color: #000040;">-</span>sigma<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     np <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>one<span style="color: #000040;">/</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>two<span style="color: #000040;">*</span><span style="color: #333399;">M_PI</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span>temp1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>iscall<span style="color: #000080;">==</span><span style="color: #333399;">true</span><span style="color: #008000;">&#41;</span> 
       <span style="color: #008000;">&#123;</span> 
        <span style="color: #ff0000; font-style: italic;">/* a call option */</span>
        greeks.<span style="color: #007788;">delta</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>cum_norm<span style="color: #008000;">&#40;</span>d1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
        greeks.<span style="color: #007788;">theta</span> <span style="color: #000080;">=</span> <span style="color: #000040;">-</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>np<span style="color: #000040;">*</span>sigma<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>two<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> q<span style="color: #000040;">*</span>s0<span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span>d1<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span> r<span style="color: #000040;">*</span>x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
        greeks.<span style="color: #007788;">rho</span> <span style="color: #000080;">=</span> x<span style="color: #000040;">*</span>t<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
     else 
       <span style="color: #008000;">&#123;</span> 
        <span style="color: #ff0000; font-style: italic;">/* a put option */</span>
        greeks.<span style="color: #007788;">delta</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>cum_norm<span style="color: #008000;">&#40;</span>d1<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> one<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        greeks.<span style="color: #007788;">theta</span> <span style="color: #000080;">=</span> <span style="color: #000040;">-</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>np<span style="color: #000040;">*</span>sigma<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>two<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> q<span style="color: #000040;">*</span>s0<span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>d1<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> r<span style="color: #000040;">*</span>x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
        greeks.<span style="color: #007788;">rho</span> <span style="color: #000080;">=</span> <span style="color: #000040;">-</span>x<span style="color: #000040;">*</span>t<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>cum_norm<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>d2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
     greeks.<span style="color: #007788;">gamma</span> <span style="color: #000080;">=</span> np<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>s0<span style="color: #000040;">*</span>sigma<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
     greeks.<span style="color: #007788;">vega</span> <span style="color: #000080;">=</span> s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>np<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And the following , translated from the same book, to get the implied volatility:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p52312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p523code12"><pre class="mql5" style="font-family:monospace;"><span style="color: #0000ff;">void</span> implied_volatility<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> value, <span style="color: #0000ff;">double</span> s0, <span style="color: #0000ff;">double</span> x, <span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> sigma<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,<span style="color: #0000ff;">double</span> t, <span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> q, <span style="color: #0000ff;">bool</span> iscall, <span style="color: #0000ff;">long</span><span style="color: #000040;">&amp;</span> iflag<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #ff0000; font-style: italic;">/* Input parameters:
    =================
    value - the current value of the option
    s0 - the current price of the underlying asset
    x - the strike price
    sigma[] - the input bounds on the volatility: sigma[0], the lower bound and, sigma[1],the upper bound
    t - the time to maturity
    r - the interest rate
    q - the continuous dividend yield
    iscall - true if option is a call;
&nbsp;
    Output parameters:
    ==================
    sigma[] - the element sigma[0] contains the estimated implied volatility
    iflag - an error indicator
    */</span>
    <span style="color: #0000ff;">double</span> zero<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> fx, sig1, sig2<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> val,tolx<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> temp,eps,epsqrt,temp1,v1<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> max_iters, i, ind, ir<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> c<span style="color: #008000;">&#91;</span>20<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> sig,vega<span style="color: #008080;">;</span>
    Greeks greeks<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> done<span style="color: #008080;">;</span>
    tolx <span style="color: #000080;">=</span> eps<span style="color: #008080;">;</span>
    eps<span style="color: #000080;">=</span><span style="color:#800080;">1.0e-16</span><span style="color: #008080;">;</span>
    epsqrt <span style="color: #000080;">=</span> <span style="color: #8a2be2;">sqrt</span><span style="color: #008000;">&#40;</span>eps<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>iscall <span style="color: #000080;">==</span> <span style="color: #333399;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #ff0000; font-style: italic;">/* a call option */</span>
      temp1 <span style="color: #000080;">=</span> <span style="color: #8a2be2;">MathMax</span><span style="color: #008000;">&#40;</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span>x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span>,zero<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    else <span style="color: #ff0000; font-style: italic;">/* a put option */</span>
      temp1 <span style="color: #000080;">=</span> <span style="color: #8a2be2;">MathMax</span><span style="color: #008000;">&#40;</span>x<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>r<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span>s0<span style="color: #000040;">*</span><span style="color: #8a2be2;">exp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>q<span style="color: #000040;">*</span>t<span style="color: #008000;">&#41;</span>,zero<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    v1 <span style="color: #000080;">=</span> <span style="color: #8a2be2;">fabs</span><span style="color: #008000;">&#40;</span>value<span style="color: #000040;">-</span>temp1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>v1 <span style="color: #000080;">&lt;=</span> epsqrt<span style="color: #008000;">&#41;</span> 
      <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* the volatility is too small */</span>
       iflag <span style="color: #000080;">=</span> <span style="color: #008000;">3</span><span style="color: #008080;">;</span>
       <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   iflag <span style="color: #000080;">=</span> <span style="color: #008000;">0</span><span style="color: #008080;">;</span>
   i <span style="color: #000080;">=</span> <span style="color: #008000;">0</span><span style="color: #008080;">;</span>
   max_iters <span style="color: #000080;">=</span> <span style="color: #008000;">50</span><span style="color: #008080;">;</span>
   done <span style="color: #000080;">=</span> <span style="color: #008000;">0</span><span style="color: #008080;">;</span>
   sig <span style="color: #000080;">=</span> sigma<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* initial estimate */</span>
   val <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> max_iters<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>done<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* Newton iteration */</span>
         black_scholes<span style="color: #008000;">&#40;</span>val,greeks,s0,x,sig,t,r,q,iscall,iflag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* compute the Black-Scholes option value, val */</span>
         vega <span style="color: #000080;">=</span> greeks.<span style="color: #007788;">vega</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* and vega. */</span>
         sig1 <span style="color: #000080;">=</span> sig <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>val <span style="color: #000040;">-</span> value<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>vega<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* compute the new estimate of sigma using Newton’s method */</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>tolx <span style="color: #000080;">&gt;</span> <span style="color: #8a2be2;">fabs</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>sig1 <span style="color: #000040;">-</span> sig<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>sig1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
           <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* check whether the specified accuracy has been reached */</span>
             done <span style="color: #000080;">=</span> <span style="color: #008000;">1</span><span style="color: #008080;">;</span>
           <span style="color: #008000;">&#125;</span>
         sig <span style="color: #000080;">=</span> sig1<span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* up date sigma */</span>
         <span style="color: #000040;">++</span>i<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
   sigma<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> sig1<span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* return the estimate for sigma */</span>
   <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now , keeping these parts as the beginning of our script, we will complete our script to test the Black-Scholes model.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p52313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p523code13"><pre class="mql5" style="font-family:monospace;"><span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #808080;">//| Script program start function                                    |</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">void</span> OnStart<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> StockPrice<span style="color: #000080;">=</span><span style="color:#800080;">100.00</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> SelectedStrike<span style="color: #000080;">=</span><span style="color:#800080;">100.00</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> r<span style="color: #000080;">=</span><span style="color:#800080;">0.1</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> sigma<span style="color: #000080;">=</span><span style="color:#800080;">0.3</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> q<span style="color: #000080;">=</span><span style="color:#800080;">0.06</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">ObjectsDeleteAll</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">ChartID</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #000040;">-</span>1,<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">int</span> s0<span style="color: #008080;">;</span>
   Greeks greeks<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> value<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> t<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">int</span> T<span style="color: #008080;">;</span>   
   <span style="color: #0000ff;">long</span> iflag<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>T<span style="color: #000080;">=</span><span style="color: #008000;">10</span><span style="color: #008080;">;</span>T<span style="color: #000080;">&gt;=</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>T<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
       t<span style="color: #000080;">=</span>0.1<span style="color: #000040;">*</span>T<span style="color: #008080;">;</span>
       black_scholes<span style="color: #008000;">&#40;</span>value, greeks, StockPrice, SelectedStrike, sigma, t, r, q, <span style="color: #333399;">true</span>, iflag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>       
       <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>t,<span style="color: #008000;">1</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; :: &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>value,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">delta</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">gamma</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">vega</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">theta</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">rho</span>,3<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Time    Value     Delta    Gamma    Vega    Theta      Rho&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Calls&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>T<span style="color: #000080;">=</span><span style="color: #008000;">10</span><span style="color: #008080;">;</span>T<span style="color: #000080;">&gt;=</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>T<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
       t<span style="color: #000080;">=</span>0.1<span style="color: #000040;">*</span>T<span style="color: #008080;">;</span>
       black_scholes<span style="color: #008000;">&#40;</span>value, greeks, StockPrice, SelectedStrike, sigma, t, r, q, <span style="color: #333399;">false</span>, iflag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>       
       <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>t,<span style="color: #008000;">1</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; :: &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>value,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">delta</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">gamma</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">vega</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">theta</span>,<span style="color: #008000;">3</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot; &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>greeks.<span style="color: #007788;">rho</span>,3<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>      
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Time    Value     Delta    Gamma    Vega    Theta      Rho&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>   
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Puts&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Continuous dividend yield: &quot;</span>,q<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Implied volatility: &quot;</span>,sigma<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Riskless rate: &quot;</span>,r<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Selected strike: &quot;</span>,SelectedStrike<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Stock price: &quot;</span>,StockPrice<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;ASSUMPTIONS: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>         
  <span style="color: #008000;">&#125;</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span></pre></td></tr></table></div>

<p>And the output should look like this:</p>
<p><img src="http://mqlmagazine.com/wp-content/uploads/2009/12/optionsdemo.jpg" alt="Black &amp; Scholes Demo" title="Black &amp; Scholes Demo" width="289" height="664" class="alignnone size-full wp-image-526" /></p>
<p>Now delete the previous added lines that completed the script, and add the following ones, to test implied volatility function:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p52314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p523code14"><pre class="mql5" style="font-family:monospace;"><span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #808080;">//| Script program start function                                    |</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">void</span> OnStart<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> S <span style="color: #000080;">=</span> <span style="color:#800080;">10.0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> X <span style="color: #000080;">=</span> <span style="color:#800080;">10.5</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> r <span style="color: #000080;">=</span> <span style="color:#800080;">0.1</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> sigmat <span style="color: #000080;">=</span> <span style="color:#800080;">0.5</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> q <span style="color: #000080;">=</span> <span style="color:#800080;">0.04</span><span style="color: #008080;">;</span>  
  Greeks greeks<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> sigma<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> value, T<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> i, ifail, put<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> origsigma<span style="color: #000080;">=</span>sigmat<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> flag<span style="color: #008080;">;</span>
  ifail <span style="color: #000080;">=</span> <span style="color: #008000;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #008000;">5</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&gt;=</span> <span style="color: #008000;">1</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
     T <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>i<span style="color: #000040;">*</span><span style="color:#800080;">0.5</span><span style="color: #008080;">;</span>
     black_scholes<span style="color: #008000;">&#40;</span>value,greeks,S,X,sigmat,T,r,q,<span style="color: #333399;">true</span>,flag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     sigma<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color:#800080;">0.05</span><span style="color: #008080;">;</span>
     sigma<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color:#800080;">1.0</span><span style="color: #008080;">;</span>
     implied_volatility<span style="color: #008000;">&#40;</span>value,S,X,sigma,T,r,q,<span style="color: #333399;">true</span>,flag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #8a2be2;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;%8.4f %15.4f    %15.4f     (%8.4e) &quot;</span>,T,value,sigma<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span>,<span style="color: #8a2be2;">fabs</span><span style="color: #008000;">&#40;</span>sigmat<span style="color: #000040;">-</span>sigma<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
     sigmat <span style="color: #000080;">=</span> sigmat <span style="color: #000040;">-</span> <span style="color:#800080;">0.1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #8a2be2;">printf</span> <span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot; Time(years)  Option value  True sigma   Error&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Underlying= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>S,2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Strike= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>X,2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Continuous dividend yield= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>q<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008000;">2</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Implied volatility= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>origsigma<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008000;">2</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Riskless rate= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>r<span style="color: #000040;">*</span><span style="color: #008000;">100</span>,<span style="color: #008000;">2</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot;%&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;x= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>X,2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;S= &quot;</span>,<span style="color: #8a2be2;">DoubleToString</span><span style="color: #008000;">&#40;</span>S,2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;ASSUMPTIONS&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
  <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The script plugs some values into the Black-Scholes model, gets option values, then sets a guess range and calls the implied volatility function, that gives pretty accurate results:</p>
<p><img src="http://mqlmagazine.com/wp-content/uploads/2009/12/Ivdemo.jpg" alt="Implied Volatility Demo" title="Implied Volatility Demo" width="275" height="319" class="alignnone size-full wp-image-525" /></p>
<div class='dd_after'><table><tr><td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/&amp;t=Virtual+methods+in+MQL5+-+an+application+on+options&amp;s=compact' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/&amp;title=Virtual+methods+in+MQL5+-+an+application+on+options&amp;t=2' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'><!--yahooBuzzArticleHeadline=Virtual+methods+in+MQL5+-+an+application+on+options;//--></script><script type='text/javascript' src='http://d.yimg.com/ds/badge2.js' badgetype='small-votes'></script></td><td><iframe src='http://api.tweetmeme.com/button.js?url=http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/&amp;source=&amp;style=compact' height='20' width='90' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'> var fbShare = {url: 'http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/',size:'small'}</script> <script type='text/javascript' src='http://widgets.fbshare.me/files/fbshare.js'></script></td></tr></table></div><!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/ -->]]></content:encoded>
			<wfw:commentRss>http://mqlmagazine.com/mql-programming/virtual-methods-in-mql5-an-application-on-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Volatility analysis : bridging the gap from volatility forecasting to price forecasting</title>
		<link>http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/</link>
		<comments>http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 19:00:53 +0000</pubDate>
		<dc:creator>Bogdan Caramalac, MQLmagazine sr.editor</dc:creator>
				<category><![CDATA[Financial modelling]]></category>
		<category><![CDATA[MQL programming]]></category>
		<category><![CDATA[Bollinger Bands]]></category>
		<category><![CDATA[mql4]]></category>
		<category><![CDATA[mql5]]></category>
		<category><![CDATA[standard deviation]]></category>
		<category><![CDATA[volatility]]></category>

		<guid isPermaLink="false">http://mqlmagazine.com/?p=55</guid>
		<description><![CDATA[[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]
Volatility. A word that generates panic for investors as well as faster heartbeats for traders. How many times haven&#8217;t you tried before to trade only trending markets and catch the momentum ?
According to InvestorWords.com, volatility is the relative rate at which the price of a security moves up and [...]]]></description>
			<content:encoded><![CDATA[<p><a title="[Versiunea romaneasca]" href="http://mqlmagazine.com/ro/programarea-in-mql/analiza-volatilitatii-de-la-previziunea-volatilitatii-la-previziunea-pretului/" target="_top">[Versiunea romaneasca]</a> <a title="[MQLmagazine.com in romana]" href="http://mqlmagazine.com/ro" target="_top">[MQLmagazine.com in romana]</a> <a title="[English edition]" href="http://mqlmagazine.com" target="_top">[English edition]</a></p>
<p>Volatility. A word that generates panic for investors as well as faster heartbeats for traders. How many times haven&#8217;t you tried before to trade only trending markets and catch the momentum ?</p>
<p>According to InvestorWords.com, volatility is <strong><em>the relative rate at which the price of a security moves up and down. Volatility is found by calculating the annualized standard deviation of daily change in price. If the price of a stock moves up and down rapidly over short time periods, it has high volatility. If the price almost never changes, it has low volatility.</em></strong> So volatility is measured by standard deviation (noted with greek lowercase sigma letter). And what&#8217;s the most popular indicator based on standard deviation ? You guessed, the famous Bollinger Bands.  Bollinger Bands (BB) is one of the first indicators that you have learned to use in trading.<strong><em> </em></strong>You have been told that, there is a signal to buy or sell whenever the market touches one of the bands, followed by a possible walk along the band, and afterwards when price turns away, it will go like that until it touches the moving average.  This rule actually doesn&#8217;t tell too much. There is nothing saying how much it will walk along the band until it turns back. Which you may find very frustrating on your own &#8230; equity.  If you graph the basis of this indicator, the standard deviation, you will see a sinusoidal chart. Volatility goes up and down in <em>quite a continuous pattern, without swift moves. </em>Since it&#8217;s not making swift moves, it means the volatility itself is more predictible than the price.  Since it&#8217;s forecastable, why not using BBs over volatility ?  But wait. Standard deviation is relative. It measures the dispersion over the analyzed period. If we add the BBs over the standard deviation, it means we will use the standard deviation of the standard deviation to catch moves in volatility!  <img class="alignleft size-full wp-image-60" title="example1_bboverstd" src="http://mqlmagazine.com/wp-content/uploads/2009/11/example1_bboverstd.jpg" alt="example1_bboverstd" width="995" height="513" /> This time there is no fear of false positives, because the analysed data set, standard deviation of the price, is a continuous function. Second, even if the signal is wrong and the volatility turns back and trends down towards its moving average, there&#8217;s not too much damage done, because the volatility is already down when signal is taken, and a move down in volatility will mean less movement in price.  The bluish line is the standard deviation of last 20 bars opening price. Signals we are interested in happen when volatility crosses its own upper BB. Of course, we are not interested in crosses with lower BB, because they mean volatility is going down faster than normal. During a trend, a powerful countermovement will appear like the volatility would be going down. Best signals are when crosses with the upper BB happen at a very reduced historical volatility &#8211; below 20% of the on-screen range. If you compare with the other signals on chart, BB over price intersections with the price, you can see that this one may cross even when the volatility is within its own BB bands, so this is a way to filter lots of error-prone BB signals that are given by price BB intersections. A good entry-exit criteria would be that the distance between the upper volatility BB band and the volatility to be below 2% of volatility range.  Below is the <strong>MQL4 transcript of the </strong><strong>Bollinger Bands over Standard Deviation </strong>indicator.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5519"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
</pre></td><td class="code" id="p55code19"><pre class="mql4" style="font-family:monospace;"><span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #666666;">//|                                                    BBoverSTD.mq4 |</span>
<span style="color: #666666;">//|                                                 Bogdan Caramalac |</span>
<span style="color: #666666;">//|                                     mailto:fxeconomist@yahoo.com |</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #339900;">#property copyright &quot;Bogdan Caramalac&quot;</span>
<span style="color: #339900;">#property link      &quot;mailto:fxeconomist@yahoo.com&quot;</span>
&nbsp;
<span style="color: #339900;">#property indicator_separate_window</span>
<span style="color: #339900;">#property indicator_buffers 4</span>
<span style="color: #339900;">#property indicator_color1 LightSeaGreen //STDDEV</span>
<span style="color: #339900;">#property indicator_color2 Blue          //MA</span>
<span style="color: #339900;">#property indicator_color3 Yellow        //Upper BB</span>
<span style="color: #339900;">#property indicator_color4 Yellow        //Lower BB</span>
&nbsp;
<span style="color: #666666;">//---- input parameters</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">int</span>       STDLEN<span style="color: #000080;">=</span><span style="color: #0000dd;">20</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">int</span>       BBLEN<span style="color: #000080;">=</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">double</span>    BBMULT<span style="color: #000080;">=</span><span style="color:#800080;">2.0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">string</span>    MATYPE<span style="color: #000080;">=</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">int</span>       MALEN<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">extern</span> <span style="color: #0000ff;">int</span>       Logarithm<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">double</span> ExtMapBuffer1<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer2<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer3<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer4<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #666666;">//| Custom indicator initialization function                         |</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">int</span> init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
<span style="color: #666666;">//---- indicators</span>
   <span style="color: #8a2be2;">SetIndexStyle</span><span style="color: #008000;">&#40;</span>0,<span style="color: #333399;">DRAW_LINE</span>, 0, 1, <span style="color: #0000ff;">indicator_color1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>0, ExtMapBuffer1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexStyle</span><span style="color: #008000;">&#40;</span>1,<span style="color: #333399;">DRAW_LINE</span>, 0, 1, <span style="color: #0000ff;">indicator_color2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>1, ExtMapBuffer2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexStyle</span><span style="color: #008000;">&#40;</span>2,<span style="color: #333399;">DRAW_LINE</span>, 0, 1, <span style="color: #0000ff;">indicator_color3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>2, ExtMapBuffer3<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexStyle</span><span style="color: #008000;">&#40;</span>3,<span style="color: #333399;">DRAW_LINE</span>, 0, 1, <span style="color: #0000ff;">indicator_color4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>3, ExtMapBuffer4<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">//----</span>
   <span style="color: #8a2be2;">SetIndexDrawBegin</span><span style="color: #008000;">&#40;</span>0,STDLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #8a2be2;">SetIndexDrawBegin</span><span style="color: #008000;">&#40;</span>1,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexDrawBegin</span><span style="color: #008000;">&#40;</span>2,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexDrawBegin</span><span style="color: #008000;">&#40;</span>3,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #666666;">//| Custom indicator iteration function                              |</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">int</span> start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #ff00ff;">Bars</span><span style="color: #000080;">&lt;=</span><span style="color: #8a2be2;">MathMax</span><span style="color: #008000;">&#40;</span>STDLEN<span style="color: #000040;">+</span>BBLEN,MALEN<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>      
      <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>    
   <span style="color: #0000ff;">int</span> pos<span style="color: #000080;">=</span>Bars<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>    
   <span style="color: #8a2be2;">ArraySetAsSeries</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,<span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>    
   <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>pos<span style="color: #000080;">&gt;=</span>0<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_SMA</span>,<span style="color: #333399;">MODE_OPEN</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>1<span style="color: #000040;">&amp;&amp;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>0.00<span style="color: #008000;">&#41;</span>
           ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;EMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_EMA</span>,<span style="color: #333399;">MODE_OPEN</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>1<span style="color: #000040;">&amp;&amp;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>0.00<span style="color: #008000;">&#41;</span>
           ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_SMMA</span>,<span style="color: #333399;">MODE_OPEN</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>1<span style="color: #000040;">&amp;&amp;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>0.00<span style="color: #008000;">&#41;</span>
           ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;LWMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_LWMA</span>,<span style="color: #333399;">MODE_OPEN</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>1<span style="color: #000040;">&amp;&amp;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>0.00<span style="color: #008000;">&#41;</span>
           ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
       pos<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
   pos<span style="color: #000080;">=</span>Bars<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>pos<span style="color: #000080;">&gt;=</span>0<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MALEN<span style="color: #000040;">!</span><span style="color: #000080;">=</span>0<span style="color: #008000;">&#41;</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iMAOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,MALEN,0,<span style="color: #333399;">MODE_SMA</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">else</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;EMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MALEN<span style="color: #000040;">!</span><span style="color: #000080;">=</span>0<span style="color: #008000;">&#41;</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iMAOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,MALEN,0,<span style="color: #333399;">MODE_EMA</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">else</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MALEN<span style="color: #000040;">!</span><span style="color: #000080;">=</span>0<span style="color: #008000;">&#41;</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iMAOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,MALEN,0,<span style="color: #333399;">MODE_SMMA</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">else</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;LWMA&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MALEN<span style="color: #000040;">!</span><span style="color: #000080;">=</span>0<span style="color: #008000;">&#41;</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iMAOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,MALEN,0,<span style="color: #333399;">MODE_LWMA</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">else</span>
           ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
       pos<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
   pos<span style="color: #000080;">=</span>Bars<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>pos<span style="color: #000080;">&gt;=</span>0<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
      ExtMapBuffer3<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
      ExtMapBuffer4<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #333399;">EMPTY_VALUE</span><span style="color: #008080;">;</span>
      ExtMapBuffer3<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iBandsOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,BBLEN,BBMULT,0,<span style="color: #333399;">MODE_HIGH</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      ExtMapBuffer4<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">iBandsOnArray</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1,0,BBLEN,BBMULT,0,<span style="color: #333399;">MODE_LOW</span>,pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      pos<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Parameters:</p>
<p>STDLEN &#8211; length of the period whose standard deviation is calculated;</p>
<p>BBLEN &#8211; length of the period whose BB bands are calculated for;</p>
<p>BBMULT &#8211; standard deviation multiplicator for BB bands;</p>
<p>MATYPE &#8211; a helper Moving Average over the volatility is created, of this type ; same type is applied to the construction of the standard deviation in first place;</p>
<p>MALEN &#8211; period of the helper Moving Average (turned off with 0)</p>
<p>Logarithm &#8211; used for logarithming the scale ; for 0, disabled, for 1, natural logarithm;</p>
<p>Credits to Irtron and Ruptor from the mql4.com forum for sorting out this indicator.</p>
<p>The iMAOnArray function is buggy; an MQL4 indicator file can contain more than one loop used for calculation.</p>
<p><strong><em>Let&#8217;s take a peek at the anatomy of this MQL4 indicator.</em></strong></p>
<p>Lines 9 to 15 are the intro of the indicator ; it is specified that the indicator is in a <strong>separate window</strong> , number of buffers and coloring for each;</p>
<p>Lines 17 to 22 are the definition of parameters; what makes them parameters, as opposed to what is known as global variables in other programming languages , (for instance the Public declarations in Visual Basic) is the keyword <strong>extern </strong>in the front of the type ; when the indicator is installed (dragged over the chart) , these parameters can be modified . Also these parameters can be modified any time by the user ; but no expert advisor or script can touch the parameters of an indicator running in a window.</p>
<p>Lines 24 to 27 are the buffers ; since we specified that are 4 of them, 4 are defined. Indicator buffers are numbers that denominate arrays inside indicators that contain data. For instance, if an indicator would copy OHLC bar data to indicator buffers, 0 would be for open prices, 1 for high , 2 for low, 3 for close. In our case, as you will see later, 0 is for standard deviation, 1 for helper moving average, 2 for higher BB band and 3 for lower BB band.</p>
<p>Lines 31 to 49 make up the <strong>init() </strong>function. This function is called every time the chart parameters are changed (symbol , periodicity) or connection coming back up. The function defines the drawing style for each of the buffers and assigns the arrays to indicator buffers. Also specifies where drawing point begins for each buffer. Drawing point should be carefully calculated, so that data that is the base for buffer to be there previously. For instance, if you calculate a 10 element moving average, drawing point should not be larger than 10 elements below the count of bars.</p>
<p>Lines 54 to 131 make up the <strong>start() </strong>function. <strong>start()</strong> is triggered  at every incoming quote. It starts with a condition checking that there are enough <strong>Bars</strong> to draw the indicator. Then, sets the position to start drawing. Position is a bar index. Indicator is calculated from a positive position to zero (current bar).  The <strong>ArraySetAsSeries() </strong>function alters the working mode of an array, setting it work like time series. This does not affect the array per se. Rather, it tells the special functions that work with arrays to treat arrays reversed. The functions that calculate indicators over arrays (like <strong>iMAOnArray()</strong> , <strong>iStdDevOnArray</strong>() and so on) pass the array from left to right, from lower indexes to higher indexes. The <strong>ArraySetAsSeries</strong>() applied to an array will make these functions pass the array from right to left, from higher indexes to lower indexes, exactly how a custom indicator loop does. Then what follows are the loops. In a simple indicator quite everything can be done in a single loop, but here three loops do the trick. The first loop will calculate the standard deviation ; the second loop will calculate the helper moving average, and finally the third loop will calculate the upper and lower BBs.</p>
<p><strong>Same indicator, MQL5 version</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="p5520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
</pre></td><td class="code" id="p55code20"><pre class="mql5" style="font-family:monospace;"><span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #808080;">//|                                                    BBoverSTD.mq5 |</span>
<span style="color: #808080;">//|                                       Copyright Bogdan Caramalac |</span>
<span style="color: #808080;">//|                                                  mqlmagazine.com |</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #339900;">#property copyright &quot;Bogdan Caramalac&quot;</span>
<span style="color: #339900;">#property link      &quot;mqlmagazine.com&quot;</span>
<span style="color: #339900;">#property version   &quot;1.02&quot;</span>
&nbsp;
<span style="color: #339900;">#property indicator_separate_window</span>
<span style="color: #339900;">#property indicator_buffers 4</span>
<span style="color: #339900;">#property indicator_applied_price PRICE_OPEN</span>
&nbsp;
<span style="color: #339900;">#property indicator_plots   4</span>
<span style="color: #339900;">#property indicator_type1 DRAW_LINE</span>
<span style="color: #339900;">#property indicator_label1 &quot;Stddev&quot;</span>
<span style="color: #339900;">#property indicator_color1 LightSeaGreen //STDDEV</span>
<span style="color: #339900;">#property indicator_type2 DRAW_LINE</span>
<span style="color: #339900;">#property indicator_label2 &quot;Helper MA&quot;</span>
<span style="color: #339900;">#property indicator_color2 Blue          //MA</span>
<span style="color: #339900;">#property indicator_type3 DRAW_LINE</span>
<span style="color: #339900;">#property indicator_label3 &quot;Upper BB&quot;</span>
<span style="color: #339900;">#property indicator_color3 Yellow        //Upper BB</span>
<span style="color: #339900;">#property indicator_type4 DRAW_LINE</span>
<span style="color: #339900;">#property indicator_label4 &quot;Lower BB&quot;</span>
<span style="color: #339900;">#property indicator_color4 Yellow        //Lower BB</span>
&nbsp;
<span style="color: #808080;">//---- input parameters</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">int</span>       STDLEN<span style="color: #000080;">=</span><span style="color: #008000;">20</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">int</span>       BBLEN<span style="color: #000080;">=</span><span style="color: #008000;">10</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">double</span>    BBMULT<span style="color: #000080;">=</span><span style="color:#800080;">2.0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">string</span>    MATYPE<span style="color: #000080;">=</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">int</span>       MALEN<span style="color: #000080;">=</span><span style="color: #008000;">2</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">input</span> <span style="color: #0000ff;">int</span>       Logarithm<span style="color: #000080;">=</span><span style="color: #008000;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">double</span> ExtMapBuffer1<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer2<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer3<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> ExtMapBuffer4<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#include &lt;MovingAverages.mqh&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> StdDevHandle<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #808080;">//| Custom indicator initialization function                         |</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">int</span> OnInit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
<span style="color: #808080;">//--- indicator buffers mapping</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>0, ExtMapBuffer1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>1, ExtMapBuffer2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>2, ExtMapBuffer3<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">SetIndexBuffer</span><span style="color: #008000;">&#40;</span>3, ExtMapBuffer4<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #808080;">//----</span>
   <span style="color: #8a2be2;">PlotIndexSetInteger</span><span style="color: #008000;">&#40;</span>0,<span style="color: #333399;">PLOT_DRAW_BEGIN</span>,STDLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">PlotIndexSetInteger</span><span style="color: #008000;">&#40;</span>1,<span style="color: #333399;">PLOT_DRAW_BEGIN</span>,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">PlotIndexSetInteger</span><span style="color: #008000;">&#40;</span>2,<span style="color: #333399;">PLOT_DRAW_BEGIN</span>,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">PlotIndexSetInteger</span><span style="color: #008000;">&#40;</span>3,<span style="color: #333399;">PLOT_DRAW_BEGIN</span>,STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #808080;">//---</span>
   <span style="color: #808080;">//we create iStdDev handler for use in main cycle;</span>
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008000;">&#41;</span>
     StdDevHandle<span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_SMA</span>,<span style="color: #333399;">PRICE_OPEN</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;EMA&quot;</span><span style="color: #008000;">&#41;</span>
     StdDevHandle<span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_EMA</span>,<span style="color: #333399;">PRICE_OPEN</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;LWMA&quot;</span><span style="color: #008000;">&#41;</span>
     StdDevHandle<span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_LWMA</span>,<span style="color: #333399;">PRICE_OPEN</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMMA&quot;</span><span style="color: #008000;">&#41;</span>
     StdDevHandle<span style="color: #000080;">=</span><span style="color: #008000;">iStdDev</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">Symbol</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #8a2be2;">Period</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,STDLEN,0,<span style="color: #333399;">MODE_SMMA</span>,<span style="color: #333399;">PRICE_OPEN</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #808080;">//| Custom indicator iteration function                              |</span>
<span style="color: #808080;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">int</span> OnCalculate<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> rates_total,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> prev_calculated,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">datetime</span><span style="color: #000040;">&amp;</span> time<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> open<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> high<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> low<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span> close<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">long</span><span style="color: #000040;">&amp;</span> tick_volume<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">long</span><span style="color: #000040;">&amp;</span> volume<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,
                <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span> spread<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> res<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #808080;">//array to hold current iStdDev value;</span>
    <span style="color: #0000ff;">double</span> buffer_helper<span style="color: #008000;">&#91;</span>500<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> stddevonstddev<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>    <span style="color: #808080;">//current stddev on stddev</span>
    <span style="color: #0000ff;">double</span> squaredsum<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>        <span style="color: #808080;">//current squared sum</span>
    <span style="color: #0000ff;">double</span> bbma<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>              <span style="color: #808080;">//current ma for bollinger bands calculus</span>
    <span style="color: #0000ff;">double</span> bbsum<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>             <span style="color: #808080;">//current sum for calculus of bbma</span>
    <span style="color: #0000ff;">double</span> prevma<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>            <span style="color: #808080;">//previous helper moving average value, for use with SMMA or LWMA   </span>
    <span style="color: #0000ff;">int</span> buff_stddevs<span style="color: #000080;">=</span><span style="color: #008000;">0</span><span style="color: #008080;">;</span>           <span style="color: #808080;">//used length of stddevs buffer</span>
    <span style="color: #0000ff;">int</span> buff_helper_len<span style="color: #000080;">=</span><span style="color: #008000;">0</span><span style="color: #008080;">;</span>        <span style="color: #808080;">//used length of helper buffer   </span>
    <span style="color: #0000ff;">int</span> startpos<span style="color: #000080;">=</span><span style="color: #8a2be2;">MathRound</span><span style="color: #008000;">&#40;</span><span style="color: #8a2be2;">MathMin</span><span style="color: #008000;">&#40;</span>prev_calculated,rates_total<span style="color: #000040;">-</span>1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #808080;">//this is the start position ; it goes UPWARD, we are on an array, not on series</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>rates_total<span style="color: #000080;">&lt;</span>STDLEN<span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>rates_total<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>startpos<span style="color: #000080;">&lt;</span>STDLEN<span style="color: #008000;">&#41;</span>
      startpos<span style="color: #000080;">=</span>STDLEN<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> pos<span style="color: #000080;">=</span>startpos<span style="color: #008080;">;</span>pos<span style="color: #000080;">&lt;</span>rates_total<span style="color: #008080;">;</span>pos<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #808080;">//main cycle, going from startpos to rates_total</span>
       <span style="color: #008000;">&#123;</span>
        <span style="color: #808080;">//Reads 1 element from position rates_total-pos, buffer 0 of StdDevIndicator calculus designated by StdDevHandle</span>
        <span style="color: #808080;">//rates_total-pos to convert position ; buffers are time series (curent value has index 0)</span>
        <span style="color: #8a2be2;">CopyBuffer</span><span style="color: #008000;">&#40;</span>StdDevHandle,0,rates_total<span style="color: #000040;">-</span>pos,1,res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>res<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><span style="color: #808080;">//copy value from temporary res[0] array index to final ExtMapBuffer1        </span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>1<span style="color: #008000;">&#41;</span>
          ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Logarithm<span style="color: #000080;">==</span>2<span style="color: #008000;">&#41;</span>
          ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #8a2be2;">MathLog10</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pos<span style="color: #000080;">&gt;</span>STDLEN<span style="color: #000040;">+</span>BBLEN<span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
           <span style="color: #808080;">//enough values to calculate Bollies over volatility</span>
           squaredsum<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
           bbsum<span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
           <span style="color: #808080;">//calculating the moving average for bollinger bands</span>
           <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span>pos<span style="color: #000040;">-</span>BBLEN<span style="color: #000040;">+</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;=</span>pos<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
              bbsum<span style="color: #000080;">=</span>bbsum<span style="color: #000040;">+</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
           bbma<span style="color: #000080;">=</span>bbsum<span style="color: #000040;">/</span>BBLEN<span style="color: #008080;">;</span>
           <span style="color: #808080;">//calculating the squared sum for the stddev over stddev needed for bollinger bands</span>
           <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span>pos<span style="color: #000040;">-</span>BBLEN<span style="color: #000040;">+</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;=</span>pos<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
              squaredsum<span style="color: #000080;">=</span>squaredsum<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>bbma<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>bbma<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
           <span style="color: #808080;">//stddev on stddev and bollinger bands</span>
           stddevonstddev<span style="color: #000080;">=</span><span style="color: #8a2be2;">MathSqrt</span><span style="color: #008000;">&#40;</span>squaredsum<span style="color: #000040;">/</span>BBLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
           ExtMapBuffer3<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>bbma<span style="color: #000040;">+</span>BBMULT<span style="color: #000040;">*</span>stddevonstddev<span style="color: #008080;">;</span>
           ExtMapBuffer4<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>bbma<span style="color: #000040;">-</span>BBMULT<span style="color: #000040;">*</span>stddevonstddev<span style="color: #008080;">;</span>
           <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pos<span style="color: #000080;">&gt;</span>MALEN<span style="color: #000040;">+</span>BBLEN<span style="color: #000040;">+</span>STDLEN<span style="color: #008000;">&#41;</span>
             <span style="color: #008000;">&#123;</span>
              <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MALEN<span style="color: #000040;">!</span><span style="color: #000080;">=</span><span style="color: #008000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #808080;">//goes in only if helper moving average is indeed enabled</span>
                <span style="color: #008000;">&#123;</span>
                 <span style="color: #808080;">//making buffer for helper MA</span>
                 <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span><span style="color: #008000;">1</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;=</span>MALEN<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>    
                    buffer_helper<span style="color: #008000;">&#91;</span>MALEN<span style="color: #000040;">-</span>j<span style="color: #000040;">+</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>ExtMapBuffer1<span style="color: #008000;">&#91;</span>pos<span style="color: #000040;">-</span>j<span style="color: #000040;">+</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>            
                 <span style="color: #808080;">//calculating different kinds of moving averages, by calling moving average methods from MovingAverages.mqh</span>
                 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMA&quot;</span><span style="color: #008000;">&#41;</span>
                   ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>SimpleMA<span style="color: #008000;">&#40;</span>MALEN,MALEN,buffer_helper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;EMA&quot;</span><span style="color: #008000;">&#41;</span>
                   <span style="color: #008000;">&#123;</span>
                    ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>ExponentialMA<span style="color: #008000;">&#40;</span>MALEN,MALEN,prevma,buffer_helper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                    prevma<span style="color: #000080;">=</span>ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                   <span style="color: #008000;">&#125;</span>
                 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;SMMA&quot;</span><span style="color: #008000;">&#41;</span>
                   <span style="color: #008000;">&#123;</span>
                    ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>SmoothedMA<span style="color: #008000;">&#40;</span>MALEN,MALEN,prevma,buffer_helper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                    prevma<span style="color: #000080;">=</span>ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                   <span style="color: #008000;">&#125;</span>
                 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>MATYPE<span style="color: #000080;">==</span><span style="color: #008080;">&quot;LWMA&quot;</span><span style="color: #008000;">&#41;</span>
                   ExtMapBuffer2<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>LinearWeightedMA<span style="color: #008000;">&#40;</span>MALEN,MALEN,buffer_helper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>      
                <span style="color: #008000;">&#125;</span><span style="color: #808080;">//if (MALEN!=0)</span>
             <span style="color: #008000;">&#125;</span><span style="color: #808080;">//if (pos&gt;MALEN+STDLEN)                           </span>
          <span style="color: #008000;">&#125;</span><span style="color: #808080;">//else if if (buff_stddevs&lt;BBLEN)</span>
       <span style="color: #008000;">&#125;</span><span style="color: #808080;">//for (pos=startpos;pos&lt;rates_total;pos++)</span>
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>rates_total<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Well, programming in MQL5 is signifiantly harder. MQL5 has a whole different way of approaching indicators, so different that comparisons to MQL4 are practically pointless. The indicator begins in a similar manner, with a larger intro, lines 10-26 . The intro has a much wider number of settings compared to MQL4. Then follow the parameters, lines 29-34, and finally the buffers, lines 36-39. Note in lines 29-34 the replacement of <strong>extern</strong> by <strong>input</strong> , which takes its place in MQL5. <strong>extern</strong> still remains valid in MQL5, but it has a different role.</p>
<p>Until this moment it doesn&#8217;t seem too complicated, because code is similar to MQL4. But now it takes a complete different turn&#8230;<br />
First, an include of the MovingAverages.mqh on line 41. This will be needed below, because the different moving averages calculations will be done by calling moving average routines residing in this lib. Then, on line 43, public declaration of <strong>Standard Deviation indicator handle (only one, because we pick by moving average type)</strong>. In MQL5, calls to indicators don&#8217;t return indicator values, as they were doing in MQL4. Rather, they return a handle. The handle is needed later to get indicator values, in a calls to <strong>CopyBuffer()</strong>. The <strong>OnInit()</strong>, spanning on lines 48-71, is similar again to MQL4 <strong>init()</strong> , at least in the 50-59 lines , where there are buffer assignations and draw begin setups. The <strong>OnInit()</strong> ends with calls to <strong>iStdDev()</strong> ; the indicator will be used inside the main loop, but the handle is generated once! Four calls to <strong>iStdDev</strong>, generating the unique handle for the chosen type of moving average required by MATYPE parameter. </p>
<p>The <strong>OnCalculate()</strong> event is practically the new master loop. This function has a kind of a mysterious prototype. Its implementation details are not fully known, so the function allows a pretty wide range of prototypes. The one I used in the indicator is the default one that you get when you create a new indicator. As opposed to MQL4, when index bars are descendant from <em>Bars</em> to 0, current bar, in MQL5, index bars are ascendant from <em>prev_calculated</em>-1 to <em>rates_total</em>-1. </p>
<p>Following the declarations of the variables, the loop begins in line 100. First thing to do, we get the current value of the standard deviation. Since we have the handler, we call the <strong>CopyBuffer()</strong> to retrive the value for the current position. The current position is the cycle variable <em>pos</em>. However, <strong>CopyBuffer()</strong> understands position as a position in a series, not as a position in an array. Therefore, we will copy 1 value from <em>rates-total-pos</em>, from the first series, 0. If the indicator would have returned more than one value, say the indicator would have returned more value, like a Heiken Ashi, for instance, that returns bar informations, we would have had more than one series, adressing series 0, 1, 2 and 3 to get values of Heiken Ashi bar prices. The values got with <strong>CopyBuffer()</strong> are put in an array. But we need one number, so the array where we put the data , <em>res</em> will have only one value, res[0], which is copied to our first buffer, ExtIndexBuffer1, and then logarithming is applied, according to Logarithm parameter, lines 106-109 (now the decimal logarithm is included too, besides the natural).</p>
<p>What follows is pretty easy to follow. We count if we have enough values to start calculating the Bollinger Bands over the standard deviation. When there is enough data , (&#8220;if&#8221; branch line 110), we go back BBLEN values and calculate the average, then we do again same loop and calculate the standard deviation over the standard deviation, then finally Bollinger Bands , lines 124-125.<br />
Same as before, but with the condition of having at least one standard deviation over standard deviation calculated, we check to see if we have MALEN values of these, to compute the first helper moving average. But unlike previous time, we copy values of initial standard deviation in a buffer. Buffer will span from MALEN down to 1, with larger indexes for older values. The reason to do it is that we will pass this buffer as a parameter to moving average methods taken from MovingAverages.mqh, and their inner implementation is passing this way thru the arrays. These are simple functions. Unlike indicators, their results are answers of inner calculations, not handlers. And their results are written in the buffer for the helper moving average, ExtMapBuffer2. With the calculation of the helper moving average, all calculations are done and the loop closes.</p>
<p><strong>Volatility forecasting</strong></p>
<p>This is why I included in the indicator a helper moving average: to help forecast volatility. I would stress, however, that some key requirements to be met before attempting to do so:<br />
<strong>1. The BB band channel should be below  2% from historical standard deviation range &#8211; </strong>this will insure that the signal does not happen accidentally when BB bands are already at distance.<br />
<strong>2. Current standard deviation should be below 5% from historical standard deviation high &#8211; </strong>this will insure that there is a high chance of an explosive volatility growth after the signal.</p>
<p>The normal signal should be the cross of the upper BB band. The exit signal should be determining a weakening in volatility speed. And that happens when volatility tops while BB upper band still going up. To determine this quick, use a quick helper moving average &#8211; with a period of 2 . Once that crosses the volatility, you can already consider the exit signal. But it can be determined even quicker, when there is a too large distance between the higher BB band and the standard deviation. One of the best interpretations of this chart would be by converting data &#8211; BB bands, standard deviation, moving average etc. to their graphics counterparts &#8211; point coordinates on chart. This might tell you for instance when the standard deviation <em>touches</em> the upper BB band.</p>
<p>Provided that you calculate the upper and lower BB for a fixed number of bars maybe <strong>WindowBarsPerChart() </strong>that gives the number of bars of the chart where your expert advisor or script runs, or a given number of bars &#8211; and you extract the highest upper BB and lowest lower BB, let&#8217;s name them <em>bbmax </em>and <em>bbmin</em> &#8211; you can calculate a virtual graphics position of any value (be it a BB, standard deviation or helper moving average), using a given screen <em>height </em>(say 300 virtual &#8220;pixels&#8221;). Let&#8217;s call this one <em>testvalue</em>. So the <em>position </em>of the testvalue would be:</p>
<pre>
double position=MathRound( height*(testvalue-bbmin)/bbmax );
</pre>
<p>This way, a composite condition would be, after conditions 1. and 2. are fulfilled, that there is either a cross <em>or </em>a touch. Exit criteria would be : standard deviation is below upper BB and there is no touch anymore. However, at the first sign of weaking volatility, the exit signal will be triggered, and that might happen too early. It would be good to backcheck the touch status using a lower resolution.</p>
<p>There are two possibly situations that contribute to standard deviation growth<strong>: either prices are going either up or down in an accelerated fashion, </strong>or<strong> prices change direction swiftly from up to down and viceversa with a growing amplitude. </strong><strong> </strong></p>
<p>If the volatility kept going up for the last bars , we might presume first condition : that prices are trending. In this case, we can just check the last 2-3 opening prices to see the direction and trade it, if it&#8217;s established. <strong>But, no matter if direction is established or not, </strong>the volatility can still go up. Because volatility is at historically low when we take the signal, and tiny increases mean just the aggitation is growing up, not that the direction is established. Price fluctuations may follow an up-down pattern with <strong>growing amplitude</strong>, until direction becomes established. However, volatility is still low. Being low, it means that <strong>prices don&#8217;t move too much &#8211; we can afford to lose</strong>. No matter which direction we trade, we check at every new bar if we trade it in the good direction. If not, cut loss and trade again. Because if the signal is correct and volatility is going to traverse 30% to 70% of its historical range, we&#8217;re in for big bucks!</p>
<p>Volatility forecast is pretty straightforward, and is more manual than automated<strong>. </strong>Since we expect volatility to be going thru the roof, we can do a linear or parabolic forecast.  The <strong>linear model</strong> is simple. If we have the current standard deviation on bar 0 (<em>s0</em>) and the expected standard deviation for bar m (<em>sm</em>)<em>, </em>we have forecasted standard deviation per bar f(bar)=so+(sm-so)*bar/m.</p>
<p>The <strong>parabolic model</strong> is harder, because it needs an extra point to define the curvature. This is solvable with an equation system. Given three points (b1,stddev1) , (b2,stddev2) , (b3,stddev3) where b1, b2, b3 are bar indexes and stddev1,stddev2,stddev3 are estimated standard deviations :<br />
<img class="alignleft size-full wp-image-87" title="three points quadratic function" src="http://mqlmagazine.com/wp-content/uploads/2009/11/three-points-quadratic-function.jpg" alt="three points quadratic function" width="370" height="299" /></p>
<p>Once we have the parameters of the quadratic function that describes the standard deviation according to estimated points, we can generate the standard deviation per each bar, see MQL4 script:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5521"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
</pre></td><td class="code" id="p55code21"><pre class="mql4" style="font-family:monospace;"><span style="color: #0000ff;">double</span> Determinant<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>origa<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,<span style="color: #0000ff;">int</span> l1,<span style="color: #0000ff;">int</span> c1,<span style="color: #0000ff;">int</span> l2,<span style="color: #0000ff;">int</span> c2,<span style="color: #0000ff;">int</span> jumpline,<span style="color: #0000ff;">int</span> jumpcol<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> a<span style="color: #008000;">&#91;</span>50,50<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> degree,i,j,ip,jp,dets<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> result<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> reshere<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> r<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">string</span> image<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> ipm,jpm<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">50</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
       <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">50</span><span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
          a<span style="color: #008000;">&#91;</span>i,j<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
       <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>l1<span style="color: #000080;">==</span>0<span style="color: #008000;">&#41;</span>
       l1<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>c1<span style="color: #000080;">==</span>0<span style="color: #008000;">&#41;</span>
       c1<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    degree<span style="color: #000080;">=</span>c2<span style="color: #000040;">-</span>c1<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>jumpcol<span style="color: #000040;">!</span><span style="color: #000080;">=</span>0<span style="color: #008000;">&#41;</span>
       degree<span style="color: #000080;">=</span>degree<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    ip<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span>l1<span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>l2<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
      ipm<span style="color: #000080;">=</span>ip<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>       
      jp<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000040;">!</span><span style="color: #000080;">=</span>jumpline<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>j<span style="color: #000080;">=</span>c1<span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;=</span>c2<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
          jpm<span style="color: #000080;">=</span>jp<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
          <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>j<span style="color: #000040;">!</span><span style="color: #000080;">=</span>jumpcol<span style="color: #008000;">&#41;</span>
             <span style="color: #008000;">&#123;</span>
             r<span style="color: #000080;">=</span>origa<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span>1,j<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008080;">;</span>                          
             a<span style="color: #008000;">&#91;</span>ipm,jpm<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>r<span style="color: #008080;">;</span>             
             jp<span style="color: #000080;">=</span>jp<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
             <span style="color: #008000;">&#125;</span>
          <span style="color: #008000;">&#125;</span><span style="color: #666666;">//for (j=c1;j&lt;=c2;j++c2)              </span>
        ip<span style="color: #000080;">=</span>ip<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span><span style="color: #666666;">//if (i!=jumpline)</span>
      <span style="color: #008000;">&#125;</span><span style="color: #666666;">//for (i=l1;i&lt;=l2;i++)</span>
    result<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>degree<span style="color: #000080;">==</span>1<span style="color: #008000;">&#41;</span>
       result<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>  
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>degree<span style="color: #000080;">==</span>2<span style="color: #008000;">&#41;</span>
       result<span style="color: #000080;">=</span>a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>degree<span style="color: #000080;">==</span>3<span style="color: #008000;">&#41;</span>
       result<span style="color: #000080;">=</span>a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> 
              a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> 
              a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> 
              a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> 
              a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> 
              a<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,1<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>2<span style="color: #000040;">-</span>1,3<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> a<span style="color: #008000;">&#91;</span>3<span style="color: #000040;">-</span>1,2<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span> <span style="color: #008080;">;</span>    
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>degree<span style="color: #000080;">&gt;</span>3<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
       <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>dets<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>dets<span style="color: #000080;">&lt;=</span>degree<span style="color: #008080;">;</span>dets<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#123;</span>
           reshere<span style="color: #000080;">=</span>Determinant<span style="color: #008000;">&#40;</span>origa,1,1,degree,degree,1,dets<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
           result<span style="color: #000080;">=</span>result <span style="color: #000040;">+</span> <span style="color: #8a2be2;">MathPow</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>1,1<span style="color: #000040;">+</span>dets<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>origa<span style="color: #008000;">&#91;</span>1<span style="color: #000040;">-</span>1,dets<span style="color: #000040;">-</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>reshere<span style="color: #008080;">;</span>
           <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>result<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> EstablishStddevFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> b1,<span style="color: #0000ff;">double</span> stddev1,<span style="color: #0000ff;">int</span> b2,<span style="color: #0000ff;">double</span> stddev2,<span style="color: #0000ff;">int</span> b3,<span style="color: #0000ff;">double</span> stddev3,<span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>a, <span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>b, <span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>c<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> d,da,db,dc<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> barray<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> stddev<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> det_d<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> det_da<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> det_db<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">double</span> det_dc<span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>3<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>b1<span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008080;">;</span>
   barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>b2<span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008080;">;</span>
   barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>b3<span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008080;">;</span>
   stddev<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev1<span style="color: #008080;">;</span>
   stddev<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev2<span style="color: #008080;">;</span>
   stddev<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev3<span style="color: #008080;">;</span>
   det_d<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_d<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_d<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>   
   det_d<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_d<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_d<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   det_d<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_d<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_d<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
   det_da<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_da<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_da<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>   
   det_da<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_da<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_da<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   det_da<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_da<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_da<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
   det_db<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_db<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_db<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>   
   det_db<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_db<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_db<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   det_db<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_db<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_db<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
   det_dc<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_dc<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_dc<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   
   det_dc<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_dc<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_dc<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
   det_dc<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>  det_dc<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>1<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>barray<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>   det_dc<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>stddev<span style="color: #008000;">&#91;</span>2<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
   d<span style="color: #000080;">=</span>Determinant<span style="color: #008000;">&#40;</span>det_d,1,1,3,3,0,0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   da<span style="color: #000080;">=</span>Determinant<span style="color: #008000;">&#40;</span>det_da,1,1,3,3,0,0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   db<span style="color: #000080;">=</span>Determinant<span style="color: #008000;">&#40;</span>det_db,1,1,3,3,0,0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   dc<span style="color: #000080;">=</span>Determinant<span style="color: #008000;">&#40;</span>det_dc,1,1,3,3,0,0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>   
   a<span style="color: #000080;">=</span>da<span style="color: #000040;">/</span>d<span style="color: #008080;">;</span>
   b<span style="color: #000080;">=</span>db<span style="color: #000040;">/</span>d<span style="color: #008080;">;</span>
   c<span style="color: #000080;">=</span>dc<span style="color: #000040;">/</span>d<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #666666;">//| script program start function                                    |</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span>
<span style="color: #0000ff;">int</span> start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> aa,bb,cc<span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Standard Deviation parabolic forecasting example&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>        
   EstablishStddevFunction<span style="color: #008000;">&#40;</span>0,0.003,3,0.006,9,0.1,aa,bb,cc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;Forecasted standard deviations&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> bar<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>bar<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>bar<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
       <span style="color: #8a2be2;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">&quot;bar &quot;</span>,<span style="color: #8a2be2;">DoubleToStr</span><span style="color: #008000;">&#40;</span>bar,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>,<span style="color: #008080;">&quot;  stddev=&quot;</span>,<span style="color: #8a2be2;">DoubleToStr</span><span style="color: #008000;">&#40;</span>aa<span style="color: #000040;">*</span>bar<span style="color: #000040;">*</span>bar<span style="color: #000040;">+</span>bb<span style="color: #000040;">*</span>bar<span style="color: #000040;">+</span>cc,4<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span>0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #666666;">//+------------------------------------------------------------------+</span></pre></td></tr></table></div>

<p>Note that the Determinant function has a recursive application, up to the degree 3, where Sarrus rule is applied. Thus you can use this function to calculate n-degree determinants &#8211; but be careful on time and memory allocation requirements. An MQL5 prototype of this function will look like this:</p>
<pre>double Determinant(double &amp;origa[][50],ushort l1,ushort c1,ushort l2,ushort c2,ushort jumpline,ushort jumpcol)</pre>
<p>Note that the second dimension is fixed at 50. MQL5 does not support unspecified dimensions larger than one.</p>
<p>All of these might sound good, but don&#8217;t forget that <em>we don&#8217;t trade volatility.</em> We don&#8217;t have a price direction signal.</p>
<p>But the <strong>price direction can be extracted from volatility evolution.</strong></p>
<p><strong>Price forecasting from volatility forecast</strong></p>
<p>We have to consider that at every new bar, a price goes out of analysis and another price comes in. If we have <em>oldest </em>for oldest price and <em>newest </em>for the incoming price, we have:</p>
<p>New Average = Old Average &#8211; Oldest member/N + Newest member/N ;</p>
<p>Now, given that we plotted already the following standard deviations, the question is, which is the <em>newest </em>price that will give the new standard deviation in the queue?</p>
<p>So we take the estimated new standard deviation, according to the model, and we square it : thus we have the variance. Multiplying the variance by N (number of prices in the analysis), we start shaping up the equation for the newest member.  Taking into account <em>n </em>elements, from <em>P1 (</em>the element thrown out<em>) </em>to <em>Pn</em> (the last element), <em>Pn+1 </em>(the new price that has to be determined) and the current average, we form the standard deviation equation, by squaring the requested standard deviation and multiplying it by n, which yields the sum of squares <em>Pi</em> &#8211; new average<em> , </em>which is computed as above, cutting out the <em>P1</em> influence and adding <em>Pn+1</em> influence. We use as helper a variable called <em>avgc</em> , which is the average less the influence of P1: (<em>Needless to say, equation is written for the case standard deviation is calculated using a regular simple moving average)</em></p>
<p><img class="alignleft size-full wp-image-83" title="stddev equation" src="http://mqlmagazine.com/wp-content/uploads/2009/11/stddev-equation.jpg" alt="stddev equation" width="816" height="472" /></p>
<p>So what you can see is what it was expected. Every standard deviation appears as a result of a new price, which can be either up, or down, which are, of course, roots a quadratic equation. I grouped the elements in order to see the coefficients for free Pn+1, squared Pn+1 and free member. Given <em>m</em> forecasted standard deviations, we have 2^m forecasted prices final after <em>m</em> bars. Of course, these are not to be generated straight ; they form a tree ; so first calculation will yield two prices ; every one of them will enter the price based for a new calculation and so on. So we will have 2, 4, 8, 16&#8230;2^m prices until the end. Most likely, the price will not have an amplified ranging movement for all the m bars. Rather, we can expect a few bars to be like that, after that the direction will be established. It is good to keep the high and low prices per each calculated bar. We can be whipsawed at the beginning, during a few swings &#8211; and here the estimations will be useful to calculate possible losses &#8211; but after these swings price movements will be way larger than in the swing period. My advice is that this method to be used on longer timeframes &#8211; larger than four hours. It can work on shorter timeframes, but big volatility catches on longer timeframes can yield spectacular results.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5522"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p55code22"><pre class="mql4" style="font-family:monospace;"><span style="color: #0000ff;">void</span> CalculateStats<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>data<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,<span style="color: #0000ff;">int</span> lastindex,<span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>avg, <span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>stddev<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> sum<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>lastindex<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
       sum<span style="color: #000080;">=</span>sum<span style="color: #000040;">+</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   avg<span style="color: #000080;">=</span>sum<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>lastindex<span style="color: #000040;">+</span>1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   sum<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>lastindex<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
       sum<span style="color: #000080;">=</span>sum<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>avg<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>avg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   stddev<span style="color: #000080;">=</span><span style="color: #8a2be2;">MathSqrt</span><span style="color: #008000;">&#40;</span>sum<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>lastindex<span style="color: #000040;">+</span>1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CalculateNextPrices<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>p<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>,<span style="color: #0000ff;">int</span> lastindex,<span style="color: #0000ff;">double</span> newstddev,<span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>newprice1,<span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span>newprice2<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">double</span> avg,avgc,stddev,suma2pi,sumapi2,sumapimavgc,sumapimavgc2,delta,a,b,c<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">int</span> n,i<span style="color: #008080;">;</span>
   CalculateStats<span style="color: #008000;">&#40;</span>p,lastindex,avg,stddev<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   n<span style="color: #000080;">=</span>lastindex<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
   avgc<span style="color: #000080;">=</span>avg<span style="color: #000040;">-</span>p<span style="color: #008000;">&#91;</span>0<span style="color: #008000;">&#93;</span><span style="color: #000040;">/</span>n<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>lastindex<span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
      suma2pi<span style="color: #000080;">=</span>suma2pi<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>2<span style="color: #000040;">*</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      sumapi2<span style="color: #000080;">=</span>sumapi2<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">*</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      sumapimavgc<span style="color: #000080;">=</span>sumapimavgc<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>avgc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      sumapimavgc2<span style="color: #000080;">=</span>sumapimavgc2<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>avgc<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span>avgc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
   a<span style="color: #000080;">=</span>1<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span>1.0<span style="color: #000040;">/</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   b<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>2.0<span style="color: #000040;">*</span>avgc<span style="color: #000040;">/</span>n<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span>2.0<span style="color: #000040;">/</span>n<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>sumapimavgc<span style="color: #000040;">-</span>2.0<span style="color: #000040;">*</span>avgc<span style="color: #008080;">;</span>
   c<span style="color: #000080;">=</span>sumapimavgc2<span style="color: #000040;">+</span>avgc<span style="color: #000040;">*</span>avgc<span style="color: #000040;">-</span>newstddev<span style="color: #000040;">*</span>newstddev<span style="color: #000040;">*</span>n<span style="color: #008080;">;</span>
&nbsp;
   delta<span style="color: #000080;">=</span>b<span style="color: #000040;">*</span>b<span style="color: #000040;">-</span>4.0<span style="color: #000040;">*</span>a<span style="color: #000040;">*</span>c<span style="color: #008080;">;</span>
   newprice1<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>b<span style="color: #000040;">-</span><span style="color: #8a2be2;">MathSqrt</span><span style="color: #008000;">&#40;</span>delta<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>2.0<span style="color: #000040;">*</span>a<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   newprice2<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span>b<span style="color: #000040;">+</span><span style="color: #8a2be2;">MathSqrt</span><span style="color: #008000;">&#40;</span>delta<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>2.0<span style="color: #000040;">*</span>a<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The first function calculates the statistical parameters : average and standard deviation. The second function calculates the new prices that match up the new standard deviation. Both arrays are zero-based. The <em>lastindex </em>parameter is the last subscript that contains data. Note the use of integer constants in formulas that calculate double variables. These constants must be written in a double fashion : 1.0 instead of 1, 2.0 instead of 2, and so on , otherwise results are error-prone.</p>
<p>Be careful when using parabollic standard deviation calculus. If the first chosen standard deviation is before parabola peak, the standard deviation got from the function may get to zero or negative, where real roots of the price quadratic function will be one or none &#8211; complex roots don&#8217;t have any meaning here!</p>
<p><img class="alignleft size-full wp-image-102" title="Volatility test" src="http://mqlmagazine.com/wp-content/uploads/2009/11/Volatility-test.jpg" alt="Volatility test" width="993" height="566" /></p>
<div class='dd_after'><table><tr><td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/&amp;t=Volatility+analysis+%3A+bridging+the+gap+from+volatility+forecasting+to+price+forecasting&amp;s=compact' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/&amp;title=Volatility+analysis+%3A+bridging+the+gap+from+volatility+forecasting+to+price+forecasting&amp;t=2' height='18' width='120' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'><!--yahooBuzzArticleHeadline=Volatility+analysis+%3A+bridging+the+gap+from+volatility+forecasting+to+price+forecasting;//--></script><script type='text/javascript' src='http://d.yimg.com/ds/badge2.js' badgetype='small-votes'></script></td><td><iframe src='http://api.tweetmeme.com/button.js?url=http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/&amp;source=&amp;style=compact' height='20' width='90' frameborder='0' scrolling='no'></iframe></td><td><script type='text/javascript'> var fbShare = {url: 'http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/',size:'small'}</script> <script type='text/javascript' src='http://widgets.fbshare.me/files/fbshare.js'></script></td></tr></table></div><!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/ -->]]></content:encoded>
			<wfw:commentRss>http://mqlmagazine.com/mql-programming/volatility-analysis-bridging-the-gap-from-volatility-forecasting-to-price-forecasting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

