/// <summary> /// Called on every new bar of data. /// </summary> /// <param name="currentBar">The current bar of the simulation</param> public override void OnBarUpdate(int currentBar) { base.OnBarUpdate(currentBar); if (currentBar < 2) { return; } ZigZagWaves zigzag = (ZigZagWaves)_dependents[0]; // Did we get all the points needed to make price and time projections. if (zigzag.Waves[currentBar] != null) { List <ZigZagWaves.WavePoint> points = zigzag.Waves[currentBar].Points; // External retracements are last point minus the one before projected from the last point. //int externalDiff = points[0].Bar - points[1].Bar; //AddValueToHistogram(points[0].Bar + Convert.ToInt32(externalDiff * 1.272)); //AddValueToHistogram(points[0].Bar + Convert.ToInt32(externalDiff * 1.618)); // Alternate retracements are the 2nd to last point minus the one right before it projected from the last point. //int alternateDiff = points[1].Bar - points[2].Bar; //AddValueToHistogram(points[0].Bar + Convert.ToInt32(alternateDiff * 0.618)); //AddValueToHistogram(points[0].Bar + alternateDiff); //AddValueToHistogram(points[0].Bar + Convert.ToInt32(alternateDiff * 1.618)); // Internal retracements are the 2nd to last point minus the 3rd to last point projected from the last point. //int interalDiff = points[2].Bar - points[3].Bar; //AddValueToHistogram(points[0].Bar + Convert.ToInt32(interalDiff * 0.382)); //AddValueToHistogram(points[0].Bar + Convert.ToInt32(interalDiff * 0.500)); //AddValueToHistogram(points[0].Bar + Convert.ToInt32(interalDiff * 0.618)); //AddValueToHistogram(points[0].Bar + Convert.ToInt32(interalDiff * 0.786)); // Add the last cycle averages for the opposite cycles since the last point // is opposite the way we want to buy. List <int> cycleDiffs = new List <int>(); for (int i = 1; i < points.Count - 2 && i <= 9; i += 2) { cycleDiffs.Add(points[i].Bar - points[i + 2].Bar); } cycleDiffs.FilterOutliers(); for (int i = cycleDiffs.Min(); i < cycleDiffs.Max(); i++) { AddValueToHistogram(points[1].Bar + i); } // Add the low to high or high to low cycle and then project that from the last point. cycleDiffs.Clear(); for (int i = 0; i < points.Count - 1 && i <= 8; i += 2) { cycleDiffs.Add(points[i].Bar - points[i + 1].Bar); } cycleDiffs.FilterOutliers(); for (int i = cycleDiffs.Min(); i < cycleDiffs.Max(); i++) { AddValueToHistogram(points[0].Bar + i); } } }
/// <summary> /// Calculates lines of best fit for the past highs and lows. /// </summary> /// <param name="points">List of points to calculate from</param> /// <param name="currentBar">Current bar of the simulation</param> private void CalculateLinesOfBestFit(List <ZigZagWaves.WavePoint> points, int currentBar) { int highMod = BuyDirection[currentBar] > 0.0 ? 0 : 1; int lowMod = highMod ^ 1; ZigZagWaves zigzag = (ZigZagWaves)_dependents[0]; int maxPoints = zigzag.MinRequiredPoints; // Get the highs and lows as a list of points to use for the generation. List <ZigZagWaves.WavePoint> highs = points.Where((item, index) => index % 2 == highMod && index < maxPoints).ToList(); List <ZigZagWaves.WavePoint> lows = points.Where((item, index) => index % 2 == lowMod && index < maxPoints).ToList(); // Also do a line for all the points. List <ZigZagWaves.WavePoint> all = points.Where((item, index) => index < maxPoints).ToList(); // Reverse the order of the points since the lowest index is the last point which will // mess up the slope calculations. highs.Reverse(); lows.Reverse(); all.Reverse(); // Save the lines to the values to be plotted. SaveLineOfBestFit(HighBestFitLine, highs, HighBestFitLineSlope, currentBar); SaveLineOfBestFit(LowBestFitLine, lows, LowBestFitLineSlope, currentBar); SaveLineOfBestFit(AllBestFitLine, all, AllBestFitLineSlope, currentBar); }
/// <summary> /// Sets the zigzag deviation value. /// </summary> /// <param name="deviation">New deviation value</param> public void SetZigZagDeviation(double deviation) { ZigZagWaves zigzag = (ZigZagWaves)_dependents[0]; zigzag.SetZigZagDeviation(deviation); }
/// <summary> /// Returns the wave data for the current zigzag waves. /// </summary> /// <param name="currentBar">Current bar of the simulation</param> /// <returns>See summary</returns> public ZigZagWaves.WaveData GetWaveData(int currentBar) { ZigZagWaves zigzag = (ZigZagWaves)_dependents[0]; return(zigzag.Waves[currentBar]); }
/// <summary> /// Called on every new bar of data. /// </summary> /// <param name="currentBar">The current bar of the simulation</param> public override void OnBarUpdate(int currentBar) { base.OnBarUpdate(currentBar); if (currentBar < 2) { return; } // Zero out all the values for the plot. for (int i = currentBar; i >= currentBar - MaxPlotBars && i >= 0; i--) { HitZone[i] = null; External[(int)ExternalType._127][i] = 0.0; External[(int)ExternalType._162][i] = 0.0; Alternate[(int)AlternateType._62][i] = 0.0; Alternate[(int)AlternateType._100][i] = 0.0; Alternate[(int)AlternateType._162][i] = 0.0; Internal[(int)InternalType._38][i] = 0.0; Internal[(int)InternalType._50][i] = 0.0; Internal[(int)InternalType._62][i] = 0.0; Internal[(int)InternalType._79][i] = 0.0; BuyDirection[i] = 0.0; HighBestFitLine[i] = 0.0; LowBestFitLine[i] = 0.0; AllBestFitLine[i] = 0.0; HighBestFitLineSlope[i] = 0.0; LowBestFitLineSlope[i] = 0.0; AllBestFitLineSlope[i] = 0.0; } ZigZagWaves zigzag = (ZigZagWaves)_dependents[0]; // Did we get all the points needed to make price and time projections. if (zigzag.Waves[currentBar] != null) { List <ZigZagWaves.WavePoint> points = zigzag.Waves[currentBar].Points; if (DoWavesPassFilters(points)) { // Points indicies are ordered in the first point is the closest point // to the current bar. // Loop twice purely to display the line as a flat line and we need at least // two points for the line to show up on the chart. for (int i = currentBar; i >= currentBar - 1; i--) { // External retracements are last point minus the one before projected from the last point. double externalDiff = points[0].Price - points[1].Price; External[(int)ExternalType._127][i] = points[0].Price - externalDiff * 1.272; External[(int)ExternalType._162][i] = points[0].Price - externalDiff * 1.618; // Alternate retracements are the 2nd to last point minus the one right before it projected from the last point. double alternateDiff = points[1].Price - points[2].Price; Alternate[(int)AlternateType._62][i] = points[0].Price + alternateDiff * 0.618; Alternate[(int)AlternateType._100][i] = points[0].Price + alternateDiff; Alternate[(int)AlternateType._162][i] = points[0].Price + alternateDiff * 1.618; // Internal retracements are the 2nd to last point minus the 3rd to last point projected from the last point. double interalDiff = points[2].Price - points[3].Price; Internal[(int)InternalType._38][i] = points[0].Price - interalDiff * 0.382; Internal[(int)InternalType._50][i] = points[0].Price - interalDiff * 0.500; Internal[(int)InternalType._62][i] = points[0].Price - interalDiff * 0.618; Internal[(int)InternalType._79][i] = points[0].Price - interalDiff * 0.786; } // The last cycle sets us up for the opposite cycle. BuyDirection[currentBar] = zigzag.Waves[currentBar].TrendDirection * -1.0; // Save the best fit lines for this set of waves. CalculateLinesOfBestFit(points, currentBar); } } }