<?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>Divergence &#8211; The Financial Hacker</title>
	<atom:link href="https://financial-hacker.com/tag/divergence/feed/" rel="self" type="application/rss+xml" />
	<link>https://financial-hacker.com</link>
	<description>A new view on algorithmic trading</description>
	<lastBuildDate>Mon, 13 Apr 2020 12:08:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://financial-hacker.com/wp-content/uploads/2017/07/cropped-mask-32x32.jpg</url>
	<title>Divergence &#8211; The Financial Hacker</title>
	<link>https://financial-hacker.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Petra on Programming: The Smoothed OBV</title>
		<link>https://financial-hacker.com/petra-on-programming-the-smoothed-obv/</link>
					<comments>https://financial-hacker.com/petra-on-programming-the-smoothed-obv/#comments</comments>
		
		<dc:creator><![CDATA[Petra Volkova]]></dc:creator>
		<pubDate>Sun, 15 Mar 2020 15:13:50 +0000</pubDate>
				<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Divergence]]></category>
		<category><![CDATA[Indicator]]></category>
		<category><![CDATA[OBV]]></category>
		<guid isPermaLink="false">https://financial-hacker.com/?p=3373</guid>

					<description><![CDATA[In his article in the S&#38;C April 2020 issue, Vitali Apirine proposed a modified On Balance Volume indicator (OBVM). The hope was that OBVM crossovers and divergences make great trade signals, especially for stock indices. I got the job to put that to the test. The original OBV indicator was invented by Joseph Granville in &#8230; <a href="https://financial-hacker.com/petra-on-programming-the-smoothed-obv/" class="more-link">Continue reading<span class="screen-reader-text"> "Petra on Programming: The Smoothed OBV"</span></a>]]></description>
										<content:encoded><![CDATA[
<p><em>In his article in the S&amp;C April 2020 issue, <strong>Vitali Apirine</strong> proposed a modified On Balance Volume indicator (OBVM). The hope was that OBVM crossovers and divergences make great trade signals, especially for stock indices. I got the job to put that to the test.</em></p>
<p><span id="more-3373"></span></p>
<p>The original OBV indicator was invented by Joseph Granville in 1963. It differs from other indicators insofar as it takes trade volume into account. The purpose was to measure the volume &#8216;flowing into and out&#8217; (whatever that means) of an asset. Volume supposedly flows out when the price goes down, and flows in when it goes up. The net sum of all the flowing is the OBV. This is its algorithm in C for the Zorro platform:</p>
<pre class="prettyprint">var OBV(vars Data,var Volume)<br />{<br />  vars OBVData = series(Volume,2);<br />  if(Data[0] &gt; Data[1])<br />    return OBVData[0] = OBVData[1]+Volume;<br />  else if(Data[0] &lt; Data[1])<br />    return OBVData[0] = OBVData[1]-Volume;<br />  else<br />    return OBVData[0] = OBVData[1];<br />}</pre>
<p>Since it follows the sign of price changes, the OBV is correlated to the price curve. By the way, before you read further on, here&#8217;s a trick that can save  money when you&#8217;re using the free Zorro version. Normally, volume and market depth are only available in Zorro S. But even the free version reads volume from online sources and stores it in historical data. So you only need to read it out of the history file and store it in a dataset, and can then access it in the script for any given bar with the <strong>dataFind</strong> function. 4 lines of code save EUR 35 per month. </p>
<p>For using OBV crossovers or price divergences for trade signals, the article author smoothed the OBV with an EMA and named it &#8220;OBVM&#8221;. The algorithm:</p>
<pre class="prettyprint">var OBVM(vars Prices, int Period)
{
   vars OBVData = series(OBV(Prices,marketVol()));
   return EMA(OBVData,Period);
}</pre>
<p>For a crossover, naturally a second curve will be needed. Apirine invented the &#8220;OBVM Signal&#8221; by applying another EMA to the OBVM: </p>
<pre class="prettyprint">var OBVMSignal(vars OBVMData, int Period)
{
   return EMA(OBVMData, Period);
}</pre>
<p>The resulting OBVM (blue) and OBVM Signal (red) curves with the S&amp;P 500:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPX.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3399" src="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPX.png" alt="" width="701" height="362" srcset="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPX.png 701w, https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPX-300x155.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a></p>
<p>We&#8217;re now going to test the OBVM usefulness for trade signals. Following the author&#8217;s suggestions, we&#8217;ll use a crossover of the OBVM and the OBVM signal for entering a short or long SPY position. Of course it&#8217;s no problem to manually tweak the indicator and signal periods (7 and 10 in the chart above) until they produce great results. But that would not work in live trading, so we&#8217;re optimizing them with walk forward analysis. This is the C script for Zorro that trades SPY on SPX OBVM crossovers:</p>
<pre class="prettyprint">function run() 
{
	set(PARAMETERS,LOGFILE,TESTNOW,PLOTNOW);
	BarPeriod = 1440;
	EndDate = 20200215;

	assetAdd("SPX","STOOQ:^SPX"); // load price history from Stooq
	assetAdd("SPY","STOOQ:SPY.US");
	asset("SPY");

	NumWFOCycles = 5; // activate walk forward analysis
	int ShortPeriod = optimize(7,3,20);
	int LongPeriod = ShortPeriod*optimize(1.3,1.1,2.0);
	
	asset("SPX");
	vars Prices = series(priceClose());
	vars OBVMData = series(OBVM(Prices,ShortPeriod));
	vars OBVMSignals = series(OBVMSignal(OBVMData,LongPeriod));

	asset("SPY");
	if(crossOver(OBVMData,OBVMSignals))
		enterLong();
	else if(crossUnder(OBVMData,OBVMSignals))
		enterShort();
}</pre>
<p>For ensuring that the long period is always longer than the short one, not the period itself, but a multiplication factor is optimized. After training, the system produces indeed a positive result. But the equity curve (blue bars) does not look very convincing:</p>
<figure id="attachment_3402" aria-describedby="caption-attachment-3402" style="width: 701px" class="wp-caption alignnone"><a href="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPXcross.png"><img decoding="async" class="wp-image-3402 size-full" src="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPXcross.png" alt="" width="701" height="362" srcset="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPXcross.png 701w, https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPXcross-300x155.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a><figcaption id="caption-attachment-3402" class="wp-caption-text">OBVM crossover result</figcaption></figure>
<p><br />We can see that the smoothed OBV crossover system is not really superior to a buy-and-hold strategy. But the author also recommended trading on divergence between the OBVM and the price curve. Maybe this produces a better result? The divergence trading system:</p>
<pre class="prettyprint">function run() 
{
	set(PARAMETERS,LOGFILE,TESTNOW,PLOTNOW);
	BarPeriod = 1440;
	EndDate = 20200215;

	assetAdd("SPX","STOOQ:^SPX"); // load price history from Stooq
	assetAdd("SPY","STOOQ:SPY.US");
	asset("SPY");

	NumWFOCycles = 5; // activate walk forward analysis
	int ShortPeriod = optimize(7,3,20);
	int LongPeriod = ShortPeriod*optimize(1.3,1.1,2.0);
	
	asset("SPX");
	vars Prices = series(priceClose());
	vars OBVMData = series(OBVM(Prices,ShortPeriod));
	vars Highs = series(priceHigh()), Lows = series(priceLow());
	int Div = Divergence(Highs,Lows,OBVMData,LongPeriod);
<br />	asset("SPY");
	MaxLong = MaxShort = 1;	// don't open several trades
	if(Div&amp;1) // bullish divergence
		enterLong();
	else if(Div&amp;2) // bearish divergence
		enterShort();	
}</pre>
<p>This system uses the <strong>Divergence</strong> indicator. The indicator draws lines between the most prominent recent peaks and valleys of the data series, and returns a result with several flags depending on  which of the lines go in which different directions. The details of the <strong>Divergence</strong> function can be found in the Zorro manual under &#8220;Indicators&#8221;. After training again, we get this result:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPYdiv.png"><img decoding="async" class="alignnone size-full wp-image-3408" src="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPYdiv.png" alt="" width="701" height="362" srcset="https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPYdiv.png 701w, https://financial-hacker.com/wp-content/uploads/2020/03/OBV_SPYdiv-300x155.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a></p>
<p>Not much better than the crossover. But at least in the first part of the price curve, both tests produced a positive result. Maybe the indicator can be improved with a different signal generation method, a better smoothing function than EMA, and/or adding an anti-whipsaw or trend detection filter.</p>
<p>So when not directly for a trade system, how can you otherwise use the OBVM? The author knows the answer: <em>&#8220;It is not a standalone indicator. As with all indicators, it is important to use OBVM in conjunction with other aspects of technical analysis&#8221;</em>. Those other aspects are, of course, other indicators. Thats the beauty of technical analysis: Combine any two indicators and get a new one. And if in doubt, use it only in conjunction with other indicators. So just have many, many indicators. If possible in many, many different colors.</p>
<figure id="attachment_3376" aria-describedby="caption-attachment-3376" style="width: 760px" class="wp-caption alignnone"><a href="https://financial-hacker.com/wp-content/uploads/2020/02/indicators.png"><img loading="lazy" decoding="async" class="wp-image-3376 size-full" src="https://financial-hacker.com/wp-content/uploads/2020/02/indicators.png" alt="" width="760" height="360" srcset="https://financial-hacker.com/wp-content/uploads/2020/02/indicators.png 760w, https://financial-hacker.com/wp-content/uploads/2020/02/indicators-300x142.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a><figcaption id="caption-attachment-3376" class="wp-caption-text">Indicators. Use all of them.</figcaption></figure>
<h3>Reference</h3>
<p>Vitali Apirine, On-Balance Volume Modified, Stocks&amp;Commodities 4/2020</p>
<p>The indicator and test script is available in the Scripts 2020 repository.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://financial-hacker.com/petra-on-programming-the-smoothed-obv/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
