Cone8
 ( 6.17% )
- ago
Dr. John Ehlers has a related article on deck for the TASC Jan 2025 edition, so I thought this would be an impressive throwback to those who haven't seen it before. Below you'll find a WL8 C# code translation of our Trader Tips' for the Corona Charts TASC article from November 2008.




Note!
Build 105 (min) required to run this code.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using WealthLab.TASC; using System.Collections; namespace WealthScript2 { public class CoronaCharts : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {                   // Declare the DataSeries to pass to the method          TimeSeries dc, snr, swing, tv;          // Get the dominant cycle, SNR, Swing Position, and Trend Vigor for use in a strategy          SuperIndicators(bars, bars.AveragePriceHL, out dc, out snr, out swing, out tv); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { }       public const double twoPi = 2 * Math.PI;       public const double fourPi = 4 * Math.PI;       public class ArrayHolder       { // current, old, older          internal double I, I2, I3;          internal double Q, Q2, Q3;          internal double R, R2, R3;          internal double Im, Im2, Im3;          internal double A;          internal double dB, dB2;       }       // Keep cntMax fifo samples and find the Highest and Lowest lead for samples in the list       private void PhaseList(ref ArrayList fifo, int cntMax, double lead, out double H, out double L)       {          H = lead; L = lead;          if ( fifo.Count < cntMax)             fifo.Add(lead);          else          {             fifo.RemoveAt(0);             fifo.Add(lead);          }          for (int n = 0; n < fifo.Count - 1; n++)          {             double val = (double)fifo[n];             if ( val > H) H = val;             if ( val < L) L = val;          }       }       public void SuperIndicators(BarHistory bars, TimeSeries ds, out TimeSeries domCycMdn, out TimeSeries snrSer, out TimeSeries psnSer, out TimeSeries tvSer)       {          // Initialize arrays          ArrayHolder[] ah = new ArrayHolder[61];          for (int n = 12; n < 61; n++)             ah[n] = new ArrayHolder();          double domCycle = 0d;          string s = ds.Description + ")";          TimeSeries[] DB = new TimeSeries[61];          TimeSeries domCyc = new TimeSeries(ds.DateTimes, 0); domCyc.Description = "DC(" + s;          domCycMdn = new TimeSeries(ds.DateTimes, 0); domCycMdn.Description = "DomCyc(" + s;          snrSer = new TimeSeries(ds.DateTimes, 0);   snrSer.Description = "SNR(" + s;          psnSer = new TimeSeries(ds.DateTimes, 0);   psnSer.Description = "Swing Position(" + s;          tvSer = new TimeSeries(ds.DateTimes, 0);    tvSer.Description = "Trend Vigor(" + s;          // Create and plot the decibel series - change the colors later                      for ( int n = 12; n < 61; n++)          {             double d = n / 2.0;             DB[n] = domCyc + d;             DB[n].Description = "Cycle." + d.ToString();             PlotTimeSeriesLine(DB[n], "", "dbPane", WLColor.Black, 4, LineStyle.Solid, true);          }                    // SNR Corona chart                   TimeSeries[] Raster = new TimeSeries[51];          for ( int n = 1; n < 51; n++)          {             double d = 1d + n / 5.0;             Raster[n] = domCyc + d;             Raster[n].Description = "SNR." + d.ToString();                   PlotTimeSeriesLine(Raster[n], "", "snrPane", WLColor.Black, 4, LineStyle.Solid, true);          }          // Swing Corona chart                   TimeSeries[] RasterSwg = new TimeSeries[51];          for ( int n = 1; n < 51; n++)          {             double d = 0.2 * n - 5.0;             RasterSwg[n] = domCyc + d;             RasterSwg[n].Description = "Swing." + d.ToString();             PlotTimeSeriesLine(RasterSwg[n], "", "swgPane", WLColor.Black, 4, LineStyle.Solid, true);          }          // Trend Vigor Corona chart                   TimeSeries[] RasterTV = new TimeSeries[51];          for (int n = 1; n < 51; n++)          {             double d = 0.4 * n - 10.0;             RasterTV[n] = domCyc + d;             RasterTV[n].Description = "TV." + d.ToString();             PlotTimeSeriesLine(RasterTV[n], "", "tvPane", WLColor.Black, 4, LineStyle.Solid, true);          }          // Convert decibels to RGB color for display          WLColor[] color = new WLColor[21];          for (int n = 0; n <= 10; n++) // yellow to red: 0 to 10 dB             color[n] = WLColor.FromRgb(255, (byte)(255 - (255 * n / 10)), 0);          for (int n = 11; n <= 20; n++) // red to black: 11 to 20 db             color[n] = WLColor.FromRgb((byte)(255 * (20 - n) / 10), 0, 0);          // Corona chart SNR colors          WLColor[] colorSNR = new WLColor[21];          for (int n = 0; n <= 10; n++)          {             byte c1 = (byte)(220 - (22 * n));             byte c2 =(byte)(255 - (7 * n));             colorSNR[n] = WLColor.FromRgb(c1, c2, c2);          }          for (int n = 11; n <= 20; n++)          {             byte c2 = (byte)(190 * (2 - n / 10d));             colorSNR[n] = WLColor.FromRgb(0, c2, c2);          }          // Corona chart Swing colors          WLColor[] colorSwg = new WLColor[21];          for (int n = 0; n <= 10; n++)          {             byte c1 = (byte)(180 - (18 * n));             byte c2 = (byte)(255 - (8 * n));             byte c3 = (byte)(210 - (15 * n));             colorSwg[n] = WLColor.FromRgb(c1, c2, c3);          }          for (int n = 11; n <= 20; n++)          {             byte c2 = (byte)(172 * (2 - n / 10d));             byte c3 = (byte)(64 * (2 - n / 10d));             colorSwg[n] = WLColor.FromRgb(0, c2, c3);          }          // Trend Vigor chart colors          WLColor[] colorTV = new WLColor[21];          for (int n = 0; n <= 10; n++)          {             colorTV[n] = WLColor.FromRgb((Byte)(60 - (6 * n)), (byte)(120 - (12 * n)), 255);          }          for (int n = 11; n <= 20; n++)             colorTV[n] = WLColor.FromRgb(0, 0, (byte)(255 * (2 - n / 10d)));          // Detrend data by High Pass Filtering with a 40 Period cutoff                TimeSeries HP = domCyc;          double alpha = (1 - Math.Sin(twoPi / 30)) / Math.Cos(twoPi / 30);          for (int bar = 1; bar < ds.Count; bar++)             HP[bar] = 0.5 * (1 + alpha) * Momentum.Series(ds, 1)[bar] + alpha * HP[bar - 1];          FIRSmoother smoothHP = new FIRSmoother(HP); //FIR.Series(HP, "1,2,3,3,2,1");          TimeSeries hmlSer = Median.Series(bars.High - bars.Low, 5);          double avg, signal, noise, snr; // Variables SNR                double avg1 = 0d;          double signal1 = 0d;          double noise1 = 0d;          const double delta2 = 0.1; // Variables Swing, TV          double BP2_1 = 0d;          double BP2_2 = 0d;          double beta2, BP2, g2, alpha2, Q2, Lead60, HL, LL, widthSwg;          ArrayList fifoList = new ArrayList(51);          ArrayList fifoPsn = new ArrayList(21);          double ratio = 0d;          double ratio1 = 0d;          double widthTV = 0d;          // arrays to hold raster values from previous bar          int[] raster2 = new int[51];          int[] raster2Swg = new int[51];          int[] raster2TV = new int[51];          for (int bar = 51; bar < ds.Count; bar++)          {             double maxAmpl = 0d;             double delta = -0.015 * bar + 0.5;             delta = delta < 0.1 ? 0.1 : delta;             for (int n = 12; n < 61; n++)             {                double beta = Math.Cos(fourPi / n);                double g = 1 / Math.Cos(2 * fourPi * delta / n);                double a = g - Math.Sqrt(g * g - 1);                ah[n].Q = Momentum.Series(smoothHP, 1)[bar] * n / fourPi;                ah[n].I = smoothHP[bar];                ah[n].R = 0.5 * (1 - a) * (ah[n].I - ah[n].I3) + beta * (1 + a) * ah[n].R2 - a * ah[n].R3;                ah[n].Im = 0.5 * (1 - a) * (ah[n].Q - ah[n].Q3) + beta * (1 + a) * ah[n].Im2 - a * ah[n].Im3;                ah[n].A = ah[n].R * ah[n].R + ah[n].Im * ah[n].Im;                maxAmpl = ah[n].A > maxAmpl ? ah[n].A : maxAmpl;             }             double num = 0; double den = 0;             for (int n = 12; n < 61; n++)             {                ah[n].I3 = ah[n].I2;                ah[n].I2 = ah[n].I;                ah[n].Q3 = ah[n].Q2;                ah[n].Q2 = ah[n].Q;                ah[n].R3 = ah[n].R2;                ah[n].R2 = ah[n].R;                ah[n].Im3 = ah[n].Im2;                ah[n].Im2 = ah[n].Im;                ah[n].dB2 = ah[n].dB;                if (maxAmpl != 0 && ah[n].A / maxAmpl > 0)                   ah[n].dB = 10 * Math.Log10((1 - 0.99 * ah[n].A / maxAmpl) / 0.01);                ah[n].dB = 0.33 * ah[n].dB + 0.67 * ah[n].dB2;                ah[n].dB = ah[n].dB > 20 ? 20 : ah[n].dB;                               SetSeriesBarColor(DB[n], bar, color[(int)Math.Round(ah[n].dB)]);                if (ah[n].dB <= 6)                {                   num += n * (20 - ah[n].dB);                   den += (20 - ah[n].dB);                }                if (den != 0) domCycle = 0.5 * num / den;             }             domCycMdn[bar] = Median.Value(bar, domCyc, 5);             domCycMdn[bar] = domCycle < 6 ? 6 : domCycle;             // Calculate SNR this bar             signal = 0d;             noise = 0d;             snr = 0d;             avg = 0.1 * ds[bar] + 0.9 * avg1;             if (avg != 0d || maxAmpl > 0)                signal = 0.2 * Math.Sqrt(maxAmpl) + 0.9 * signal1;             if (avg != 0d)                noise = 0.1 * hmlSer[bar] + 0.9 * noise1;             if (signal != 0d || noise != 0d)                snr = 20 * Math.Log10(signal / noise) + 3.5;             snr = snr < 1d ? 0d : snr;             snr = snr > 10d ? 10d : snr;             snr = snr * 0.1;             snrSer[bar] = snr * 10 + 1;             double Width = snr > 0.5 ? 0d : -0.4 * snr + 0.2;             // Calculate the Swing this bar             beta2 = Math.Cos(twoPi / domCycMdn[bar]);             g2 = 1 / Math.Cos(fourPi * delta2 / domCycMdn[bar]);             alpha2 = g2 - Math.Sqrt(g2 * g2 - 1);             BP2 = 0.5 * (1 - alpha2) * (ds[bar] - ds[bar - 2])                + beta2 * (1 + alpha2) * BP2_1 - alpha2 * BP2_2;             //Quadrature component is derivative of InPhase component divided by omega             Q2 = (domCycMdn[bar] / twoPi) * (BP2 - BP2_1);             Lead60 = 0.5 * BP2 + 0.866 * Q2;             PhaseList(ref fifoList, 50, Lead60, out HL, out LL);             double Psn = (Lead60 - LL) / (HL - LL);             PhaseList(ref fifoPsn, 20, Psn, out HL, out LL);             if (HL - LL > 0.85) widthSwg = 0.01;             else widthSwg = 0.15 * (HL - LL);             psnSer[bar] = 10 * Psn - 5d;             // Calculate Trend Vigor this bar             // (Filter Bandpass component - same as Swing above; substitute variable names)             //Pythagorean theorem to establish cycle amplitude             double Ampl2 = Math.Sqrt(BP2 * BP2 + Q2 * Q2);             //Trend amplitude taken over the cycle period             int cycPeriod = (int)(domCycMdn[bar] - 1);             if (cycPeriod < 12) cycPeriod = 12;             double Trend = ds[bar] - ds[bar - cycPeriod];             if (Trend != 0 && Ampl2 != 0)                ratio = 0.33 * Trend / Ampl2 + 0.67 * ratio1;             if (ratio > 10) ratio = 10d;             if (ratio < -10) ratio = -10d;             double tv = 0.05 * (ratio + 10d);             if (tv < 0.3 || tv > 0.7) widthTV = 0.01;             if (tv >= 0.3 && tv < 0.5) widthTV = tv - 0.3;             if (tv >= 0.5 && tv <= 0.7) widthTV = 0.7 - tv;             tvSer[bar] = 20d * tv - 10d;             /* Plot the rasters... */             int snr50 = (int)Math.Round(50 * snr);             int psn50 = (int)Math.Round(50 * Psn);             int tv50 = (int)Math.Round(50 * tv);             for (int n = 1; n < 51; n++)             { // 0.4 used below comes from 20/50 to normalize the color from 1 to 20                double raster = 20d;                if (n < snr50) // bottom of corona                   raster = 0.5 * (Math.Pow((20 * snr - 0.4 * n) / Width, 0.8) + raster2[n]);                else if (n > snr50 && (0.4 * n - 20 * snr) / Width > 1) // top of corona                   raster = 0.5 * (Math.Pow((-20 * snr + 0.4 * n) / Width, 0.8) + raster2[n]);                else if (n == snr50)                   raster = 0.5 * raster2[n];                if (raster > 20) raster = 20;                else if (raster < 0) raster = 0;                if (snr > 0.5) raster = 20;                SetSeriesBarColor(Raster[n], bar, colorSNR[(int)(raster)]);                raster2[n] = (int)raster;                // Plot the Swing raster                double rasterSwg = 20d;                if (n < psn50) // bottom of corona                   rasterSwg = 0.5 * (Math.Pow((20 * Psn - 0.4 * n) / widthSwg, 0.95) + 0.5 * raster2Swg[n]);                else if (n > psn50 && (0.4 * n - 20 * Psn) / widthSwg > 1) // top of corona                   rasterSwg = 0.5 * (Math.Pow((-20 * Psn + 0.4 * n) / widthSwg, 0.95) + 0.5 * raster2Swg[n]);                else if (n == psn50)                   rasterSwg = 0.5 * raster2Swg[n];                if (rasterSwg > 20) rasterSwg = 20;                else if (rasterSwg < 0) rasterSwg = 0;                if (HL - LL > 0.8) rasterSwg = 20;                SetSeriesBarColor(RasterSwg[n], bar, colorSwg[(int)(rasterSwg)]);                raster2Swg[n] = (int)rasterSwg;                // Plot the Trend Vigor raster                double rasterTV = 20d;                if (n < tv50)                   rasterTV = 0.8 * (Math.Pow((20 * tv - 0.4 * n) / widthTV, 0.85) + 0.2 * raster2TV[n]);                else if (n > tv50) // top of corona                   rasterTV = 0.8 * (Math.Pow((-20 * tv + 0.4 * n) / widthTV, 0.85) + 0.2 * raster2TV[n]);                else if (n == tv50)                   rasterTV = 0.5 * raster2TV[n];                if (rasterTV < 0) rasterTV = 0;                if (rasterTV > 20 || tv < 0.3 || tv > 0.7) rasterTV = 20;                SetSeriesBarColor(RasterTV[n], bar, colorTV[(int)(rasterTV)]);                raster2TV[n] = (int)rasterTV;             }             avg1 = avg;             signal1 = signal;             noise1 = noise;             BP2_2 = BP2_1;             BP2_1 = BP2;             ratio1 = ratio;          }          for (int n = 1; n < 51; n++)          {             PlotTimeSeriesLine(RasterTV[n], "", "tvPane", WLColor.Transparent, 4, LineStyle.Solid, true);          }          bool suppressLabel = false;          PlotTimeSeriesLine(domCycMdn, "Dominant Cycle", "dbPane", WLColor.Yellow, 3, LineStyle.Solid, suppressLabel);          PlotTimeSeriesLine(snrSer, "SNR", "snrPane", WLColor.FromRgb(220, 255, 255), 3, LineStyle.Solid, suppressLabel);          PlotTimeSeriesLine(psnSer, "Swing Position", "swgPane", WLColor.FromRgb(180, 255, 210), 3, LineStyle.Solid, suppressLabel);          PlotTimeSeriesLine(tvSer, "Trend Vigor", "tvPane", WLColor.FromRgb(64, 128, 255), 3, LineStyle.Solid, suppressLabel);                } } }
0
57
2 Replies

Reply

Bookmark

Sort
Glitch8
 ( 12.53% )
- ago
#1
OK, for a second I thought I took more than my microdose :o
1
- ago
#2
It would be helpful if some trading code was posted as well so I can gauge how well this all works.

The "internal" declaration is included in some places. Is there an external library that's missing here that the ArrayHolder class depends on? Why not just call it "protected" (or even "public")?

It's interesting how ArrayHolder employs "real" and "imaginary" components rather than the "complex" number data type instead. It makes the arithmetic messier, but if the reader isn't good at complex arithmetic . . . (I guess we're not publishing in an engineering journal.)

It's also interesting how the Dominant Cycle can forecast major potential trading entry and exit points in the time domain for a waveform that's not that cyclic. I don't see how he takes so little signal energy to get such a definitive waveform.
0

Reply

Bookmark

Sort