<?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>Corona drop &#8211; The Financial Hacker</title>
	<atom:link href="https://financial-hacker.com/tag/corona-drop/feed/" rel="self" type="application/rss+xml" />
	<link>https://financial-hacker.com</link>
	<description>A new view on algorithmic trading</description>
	<lastBuildDate>Thu, 04 Jun 2020 10:25:09 +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>Corona drop &#8211; The Financial Hacker</title>
	<link>https://financial-hacker.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Petra on Programming: The Correlation Cycle Indicator</title>
		<link>https://financial-hacker.com/petra-on-programming-the-correlation-cycle-indicator/</link>
					<comments>https://financial-hacker.com/petra-on-programming-the-correlation-cycle-indicator/#comments</comments>
		
		<dc:creator><![CDATA[Petra Volkova]]></dc:creator>
		<pubDate>Tue, 02 Jun 2020 12:32:32 +0000</pubDate>
				<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Corona drop]]></category>
		<category><![CDATA[Correlation cycle]]></category>
		<guid isPermaLink="false">https://financial-hacker.com/?p=3447</guid>

					<description><![CDATA[The previous article dealt with indicators based on correlation with a trend line. This time we&#8217;ll look into another correlation-based indicator by John Ehlers. The new Correlation Cycle indicator (CCY) measures the price curve correlation with a sine wave. This works surprisingly well &#8211; not for generating trade signals, but for a different purpose. Ehlers &#8230; <a href="https://financial-hacker.com/petra-on-programming-the-correlation-cycle-indicator/" class="more-link">Continue reading<span class="screen-reader-text"> "Petra on Programming: The Correlation Cycle Indicator"</span></a>]]></description>
										<content:encoded><![CDATA[
<p><em>The previous article dealt with indicators based on correlation with a trend line. This time we&#8217;ll look into another correlation-based indicator by <strong>John Ehlers</strong>. The new Correlation Cycle indicator (CCY) measures the price curve correlation with a sine wave. This works surprisingly well &#8211; not for generating trade signals, but for a different purpose.</em></p>
<p><span id="more-3447"></span></p>
<p>Ehlers published the indicator together with TradeStation code in the recent S&amp;C magazine. Since the C language supports function pointers, we can code it in a shorter and more elegant way:</p>
<pre class="prettyprint">var correlY(var Phase); // function pointer
var cosFunc(var Phase) { return cos(2*PI*Phase); }
var sinFunc(var Phase) { return -sin(2*PI*Phase); }

var correl(vars Data, int Length, function Func)
{
   correlY = Func; 
   var Sx = 0, Sy = 0, Sxx = 0, Sxy = 0, Syy = 0;
   int count;
   for(count = 0; count &lt; Length; count++) {
      var X = Data[count];
      var Y = correlY((var)count/Length);
      Sx += X; Sy += Y;
      Sxx += X*X; Sxy += X*Y; Syy += Y*Y;
   }
   if(Length*Sxx-Sx*Sx &gt; 0 &amp;&amp; Length*Syy-Sy*Sy &gt; 0)
      return (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy));
   else return 0;
}

var CCY(vars Data, int Length) { return correl(Data,Length,cosFunc); }<br />var CCYROC(vars Data, int Length) { return correl(Data,Length,sinFunc); }</pre>
<p>The <strong>correl</strong> function measures the correlation of the Data series with an arbitrary curve given by the <strong>Func</strong> function. This allows us to create all sorts of correlation indicators by just using a different <strong>Func</strong>. For example, it reduces Ehlers&#8217; <a href="https://financial-hacker.com/petra-on-programming-a-unique-trend-indicator/">Correlation Trend Indicator</a> from the previous article to 2 lines:</p>
<pre class="prettyprint">var trendFunc(var Phase) { return -Phase; }

var CTI(vars Data,int Length) { return correl(Data,Length,trendFunc); }
 </pre>
<p>The empty <strong>correlY</strong> function pointer in the code above serves as a template for <strong>Func</strong>, and is used for calling it inside the <strong>correl</strong> function. For the CTI it&#8217;s simply a rising slope (negative because series are in reverse order), for the CCY it&#8217;s the standard cosine function.</p>
<p>At first let&#8217;s see how the CCY indicator behaves when applied to a sine wave. We&#8217;re using Zorro&#8217;s wave generator to produce a sine chirp with a rising cycle length from 15 up to 30 bars, which is 25% below and 50% above the used CCY period of 20 bars. The code:</p>
<pre class="prettyprint">function run()<br />{<br />   MaxBars = 300;<br />   LookBack = 40;<br />   asset(""); // dummy asset<br />   ColorUp = ColorDn = 0; // don't plot a price curve<br />   <br />   vars Chirp = series(genSine(15,30));<br />   plot("Chirp",2*Chirp[0]-1,LINE,BLUE);<br />   plot("CCY",CCY(Chirp,20),LINE,RED);<br />}
 </pre>
<p>And the result:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_Chirp.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3467" src="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_Chirp.png" alt="" width="701" height="362" srcset="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_Chirp.png 701w, https://financial-hacker.com/wp-content/uploads/2020/06/CCY_Chirp-300x155.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a>This confirms Ehlers&#8217; stress test. A shorter period results in a phase lag, a longer period in a phase lead. We&#8217;re now going to apply the indicator to real-world price curves. This code displays the CCY and its rate of change (CCYROC) in a SPY chart:</p>
<pre class="prettyprint">function run()<br />{<br />   BarPeriod = 1440;
   LookBack = 40;
   StartDate = 20190101;

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

   vars Prices = series(priceClose());
   plot("CCY(14)",CCY(Prices,14),NEW|LINE,RED);
   plot("ROC(14)",CCYROC(Prices,14),LINE,GREEN);
 }</pre>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_ROC.png"><img decoding="async" class="alignnone size-full wp-image-3468" src="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_ROC.png" alt="" width="701" height="365" srcset="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_ROC.png 701w, https://financial-hacker.com/wp-content/uploads/2020/06/CCY_ROC-300x156.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a></p>
<p>What&#8217;s the use of the Correlation Cycle indicator in a trading system? The chart might hint that its peaks or valleys could be used for trade signals, but you can save the time of testing it: I did already. The CCY is no good for trade signals. But Ehlers had another idea. The phase angle of the CCY and CCYROC reflects the market state. It returns 1 for a rising trend, -1 for a falling trend, and 0 for cycle regime. Here&#8217;s the code of Ehlers CCY market state indicator:</p>
<pre class="prettyprint">var CCYState(vars Data,int Length,var Threshold)<br />{<br />   vars Angles = series(0,2);<br />   var Real = correl(Data,Length,cosFunc);<br />   var Imag = correl(Data,Length,sinFunc);<br />// compute the angle as an arctangent function and resolve ambiguity<br />   if(Imag != 0) Angles[0] = 90 + 180/PI*atan(Real/Imag);<br />   if(Imag &gt; 0) Angles[0] -= 180;<br />// do not allow the rate change of angle to go negative<br />   if(Angles[1]-Angles[0] &lt; 270 &amp;&amp; Angles[0] &lt; Angles[1])<br />      Angles[0] = Angles[1];<br />   //return Angles[0];<br />// compute market state<br />   if(abs(Angles[0]-Angles[1]) &lt; Threshold)<br />      return ifelse(Angles[0] &lt; 0,-1,1);<br />   else return 0;<br />}</pre>
<p>Applied to SPY:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_State.png"><img decoding="async" class="alignnone size-full wp-image-3469" src="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_State.png" alt="" width="701" height="365" srcset="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_State.png 701w, https://financial-hacker.com/wp-content/uploads/2020/06/CCY_State-300x156.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a></p>
<p>At first glance, trends and cycles seem to be rather well and timely detected. But how useful is the indicator in a real trading system?</p>
<p>For finding out, we&#8217;ll compare the performance with and without market state detection of a simple trend follower, as in the first Zorro workshop. It uses a lowpass filter for detecting trend reversals. The only parameter is the cutoff period of the low pass filter. This parameter is walk forward optimized, so the system does depend on any choosen parameter value. The trend follower without market state detection:</p>
<pre class="prettyprint">function run() 
{
   set(PARAMETERS);
   BarPeriod = 1440;
   LookBack = 40;
   NumYears = 8;

   assetAdd("SPY","STOOQ:SPY.US"); // load price history from Stooq
   asset("SPY");
   
   NumWFOCycles = 4;
   int Cutoff = optimize(10,5,30,5);

   vars Prices = series(priceClose());
   vars Signals = series(LowPass(Prices,Cutoff));
   if(valley(Signals))
      enterLong();
   else if(peak(Signals))
      enterShort();
}</pre>
<p>The system enters a long position on any valley of the lowpass filtered price curve, and a short position on any peak. The resulting equity curve:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWithout.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3470" src="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWithout.png" alt="" width="701" height="362" srcset="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWithout.png 701w, https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWithout-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 can see that the simple SPY trend follower is not very good. Yes, it&#8217;s profitable, but the main profit came from some lucky trades at the corona drop. In the years before the system had long flat periods. Let&#8217;s see if the CCYState indicator can help. Its two parameters, period and threshold, are also walk forward optimized. The new script:</p>
<pre class="prettyprint">function run() 
{
   set(PARAMETERS);
   BarPeriod = 1440;
   LookBack = 40;
   NumYears = 8;

   assetAdd("SPY","STOOQ:SPY.US"); // load price history from Stooq
   asset("SPY");
   
   NumWFOCycles = 4;
   int Cutoff = optimize(10,5,30,5);
   int Period = optimize(14,10,25,1);
   var Threshold = optimize(9,5,15,1);

   vars Prices = series(priceClose());
   var State = CCYState(Prices,Period,Threshold);
   plot("State",State*0.9,NEW|LINE,BLUE);
   vars Signals = series(LowPass(Prices,Cutoff));
   
   if(State != 0) {
      if(valley(Signals))
         enterLong();
      else if(peak(Signals))
         enterShort();
   }
   else {
      exitLong(); 
      exitShort();
   }
}</pre>
<p>The new system trades only when the market state is 1 or -1, indicating trend regime. It goes out of the market when the market state is 0. We can see that this improves the equity curve remarkably:</p>
<p><a href="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWith.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3471" src="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWith.png" alt="" width="701" height="365" srcset="https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWith.png 701w, https://financial-hacker.com/wp-content/uploads/2020/06/CCY_TradeWith-300x156.png 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></a></p>
<p>I think most people would prefer this system to the previous one, even though it stayed out of the market at the corona drop. Ehlers&#8217; market state indicator did a good job.</p>
<h3>Reference</h3>
<p>John Ehlers, Correlation Cycle Indicator, Stocks&amp;Commodities 6/2020</p>
<p>The indicators and trade systems are available in the Scripts 2020 repository.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://financial-hacker.com/petra-on-programming-the-correlation-cycle-indicator/feed/</wfw:commentRss>
			<slash:comments>25</slash:comments>
		
		
			</item>
	</channel>
</rss>
