ZigZag Class

Modified on 2014/09/29 13:47 by Eugene — Categorized as: Community Components

Syntax

public ZigZag(WealthScript ws, double reversalAmount, bool initializeAsTrough, PeakTroughMode ptmode)
public ZigZag(WealthScript ws, DataSeries singleSeries, double reversalAmount, bool initializeAsTrough, PeakTroughMode ptmode)
public DataSeries PeakSeries()
public DataSeries PeakBarSeries()
public DataSeries TroughSeries()
public DataSeries TroughBarSeries()
public DataSeries PeakTroughSeries()
public void Draw(Color upColor, Color downColor, LineStyle style, int width)
public void PlotPeakTrough(Color plotColor, int width)
public Dictionary<int, ZZ> ZZs

wsA WealthScript reference. Pass this
reversalAmountThe amount of price reversal in percentage or points per the ptmode parameter that determines when a peak or trough as formed.
initializeAsTroughPass true or false to initialize the data series as a peak or a trough. Generally it does not matter how you initialize the method, but for short series in which the first assumption is important, you can control that.
PeakTroughMode Enumeration that determines if the reversalAmount parameter is measured in percentage or points.
singleSeriesThe second overload for ZigZag() allows you to specify a single series on which the ZigZag is calculated. In the other method, the ZigZag is formed between ws.Bars.High and ws.Bars.Low.
reversalAmountThe amount of price reversal in percentage or points per the ptmode parameter that determines when a peak or trough as formed.

ZZ Object

The Dictionary ZigZag.ZZs contains a reference to a ZZ object for each bar. Information from the ZZ object should be used when backtesting strategies involving peaks/troughs as it allows you to know when a peak/trough had been detected on a particular without peeking. Although you can manipulate the values of a ZZ object, you should not do it. Access a ZZ object at a particular bar from the Dictionary and use the information from its properties to identify the most recent peak/trough bars/prices or if a peak/trough has been detected.

ZZ PropertiesDescription:
int lastPeakBar Returns the bar number of the most recently detected peak.
int lastTroughBarReturns the bar number of the most recently detected trough.
int lastPeakOrTroughBarReturns the bar number of the most recently detected peak or trough.
double lastPeakPriceReturns the price of the most recently detected peak.
double lastTroughPriceReturns the price of the most recently detected trough.
double lastPeakOrTroughPriceReturns the price of the most recently detected peak or trough.
bool peakDetectedReturns true when a peak of the ZigZag reversal percentage has been detected. The value remains true until a new trough is detected.
bool troughDetectedReturns true when a trough of the ZigZag reversal percentage has been detected. The value remains true until a new peak is detected.



Description

This ZigZag class simplifies creating, plotting, and indicator creation for strategies that require a zigzag method.

Example

The example demonstrates the simplicity of creating and plotting zigzags as well as shows the difference between a zigzag based on a High/Low and one that uses the Close series only.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components;

namespace WealthLab.Strategies { public class ZZdemo : WealthScript { protected override void Execute() { // Zigzag Bars.High/Low default ZigZag zg = new ZigZag(this, 8, true, PeakTroughMode.Percent); zg.Draw(Color.Gray, Color.Gray, LineStyle.Solid, 2); // ZigZag for a single series ZigZag zz = new ZigZag(this, Close, 8, true, PeakTroughMode.Percent); zz.Draw( Color.Blue, Color.Red, LineStyle.Solid, 2); zz.PlotPeakTrough(Color.Blue, 3); // Zigzags aren't just for the standard price series. You can apply them to any DataSeries like an indicator DataSeries rsi = RSI.Series(Close, 14); ChartPane rsiPane = CreatePane(50, true, true); PlotSeries(rsiPane, rsi, Color.Black, LineStyle.Solid, 2); ZigZag zzrsi = new ZigZag(this, rsi, 20, true, PeakTroughMode.Value); zzrsi.Draw(rsiPane, Color.Blue, Color.Red, LineStyle.Solid, 2); // Highlight the bars on which the most-recent peak or trough was detected for the High/Low ZigZag object, zg for (int bar = 1; bar < Bars.Count; bar++) { ZZ zzo = zg.ZZs[bar]; if (zzo.peakDetected) SetBackgroundColor(bar, Color.FromArgb(40, Color.Blue)); if (zzo.troughDetected) SetBackgroundColor(bar, Color.FromArgb(40, Color.Red)); } } } }

The example demonstrates the simplicity of creation a trading system based on divergences between price and StochK. Its rules are:

  1. System goes Long, if the StochK is in the oversold area (below 20) and StochK is higher than the last low(peak) of the StochK value (e.g. now the value 14, last time it was 12)
  2. System goes Short, if the StochK is in the overbought area (above 75) and StochK is lower than the last high(peak) of the StochK value (e.g. now the value 80, last time it was 82)
  3. The reversal level is 20% or 30 as absolute value

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components;

namespace WealthLab.Strategies { public class StochK_Divergence_for_07nioe: WealthScript { private StrategyParameter paramRev; private StrategyParameter paramOv; private StrategyParameter paramOb; private StrategyParameter paramExit; public StochK_Divergence_for_07nioe() { paramRev = CreateParameter("Rev.%", 20, 5, 50, 5); paramOv = CreateParameter("Oversold", 20, 65, 95, 5); paramOb = CreateParameter("Overbot", 75, 10, 30, 5); paramExit = CreateParameter("Exit after", 5, 1, 10, 1); } protected override void Execute() { StochK stochk = StochK.Series(Bars, 10); ChartPane paneStoch = CreatePane(75,true,true); PlotSeries(paneStoch, stochk, Color.Purple, LineStyle.Solid, 2); DrawHorzLine(paneStoch, 20, Color.Green, LineStyle.Solid, 1); DrawHorzLine(paneStoch, 75, Color.Red, LineStyle.Solid, 1); //3. The reversal level is 20% or 30 as absolute value.

ZigZag zzsto = new ZigZag(this, stochk, paramRev.Value, true, PeakTroughMode.Value); zzsto.Draw(paneStoch, Color.Blue, Color.Red, LineStyle.Solid, 2); for (int bar = GetTradingLoopStartBar(10); bar < Bars.Count; bar++) { if (IsLastPositionActive) { /* Exit after N days */ Position p = LastPosition; if ( bar+1 - p.EntryBar >= paramExit.ValueInt ) ExitAtMarket( bar+1, p, "Timed" ); } else { ZZ zzLast = zzsto.ZZs[bar]; ZZ zzPrev = zzsto.ZZs[bar-1];

//1. System goes Long, if the StochasticK is in the oversold area ( for example <20) AND //stochasticK HIGHER than the last low(peak) of the stochasticK value (for example: now the value 14, last time it was 12) if (zzLast.troughDetected && zzLast.lastTroughPrice > zzPrev.lastTroughPrice && stochk[zzLast.lastTroughBar] < paramOv.ValueInt) { BuyAtMarket(bar+1); SetBackgroundColor(bar, Color.FromArgb(40, Color.Red)); DrawLine(paneStoch,zzLast.lastTroughBar,stochk[zzLast.lastTroughBar], zzPrev.lastTroughBar,stochk[zzPrev.lastTroughBar],Color.Blue,LineStyle.Dashed,2); } //2. System goes Short, if the StochasticK is in the overbought area (for example >75) AND //stochasticK is LOWER than the last high(peak) of the stochasticK value. (for example: now the value 80, last time it was 82)

if (zzLast.peakDetected && zzLast.lastPeakPrice < zzPrev.lastPeakPrice && stochk[zzLast.lastPeakBar] > paramOb.ValueInt) { ShortAtMarket(bar+1); SetBackgroundColor(bar, Color.FromArgb(40, Color.Blue)); DrawLine(paneStoch,zzLast.lastPeakBar,stochk[zzLast.lastPeakBar], zzPrev.lastPeakBar,stochk[zzPrev.lastPeakBar],Color.Red,LineStyle.Dashed,2); } } } } } }