/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (saveCurrentBar != CurrentBar) { swingHighSwings.Set(0); // initializing important internal swingLowSwings.Set(0); // initializing important internal swingHighSeries.Set(0); // initializing important internal swingLowSeries.Set(0); // initializing important internal lastHighCache.Add(High[0]); if (lastHighCache.Count > (2 * strength) + 1) { lastHighCache.RemoveAt(0); } lastLowCache.Add(Low[0]); if (lastLowCache.Count > (2 * strength) + 1) { lastLowCache.RemoveAt(0); } if (lastHighCache.Count == (2 * strength) + 1) { bool isSwingHigh = true; double swingHighCandidateValue = (double)lastHighCache[strength]; for (int i = 0; i < strength; i++) { if ((double)lastHighCache[i] >= swingHighCandidateValue - double.Epsilon) { isSwingHigh = false; } } for (int i = strength + 1; i < lastHighCache.Count; i++) { if ((double)lastHighCache[i] > swingHighCandidateValue - double.Epsilon) { isSwingHigh = false; } } swingHighSwings.Set(strength, isSwingHigh ? swingHighCandidateValue : 0.0); if (isSwingHigh) { lastSwingHighValue = swingHighCandidateValue; } if (isSwingHigh) { currentSwingHigh = swingHighCandidateValue; for (int i = 0; i <= strength; i++) { SwingHighPlot.Set(i, currentSwingHigh); } } else if (High[0] > currentSwingHigh) { currentSwingHigh = 0.0; SwingHighPlot.Reset(); } else { SwingHighPlot.Set(currentSwingHigh); } if (isSwingHigh) { for (int i = 0; i <= strength; i++) { swingHighSeries.Set(i, lastSwingHighValue); } } else { swingHighSeries.Set(lastSwingHighValue); } } if (lastLowCache.Count == (2 * strength) + 1) { bool isSwingLow = true; double swingLowCandidateValue = (double)lastLowCache[strength]; for (int i = 0; i < strength; i++) { if ((double)lastLowCache[i] <= swingLowCandidateValue + double.Epsilon) { isSwingLow = false; } } for (int i = strength + 1; i < lastLowCache.Count; i++) { if ((double)lastLowCache[i] < swingLowCandidateValue + double.Epsilon) { isSwingLow = false; } } swingLowSwings.Set(strength, isSwingLow ? swingLowCandidateValue : 0.0); if (isSwingLow) { lastSwingLowValue = swingLowCandidateValue; } if (isSwingLow) { currentSwingLow = swingLowCandidateValue; for (int i = 0; i <= strength; i++) { SwingLowPlot.Set(i, currentSwingLow); } } else if (Low[0] < currentSwingLow) { currentSwingLow = double.MaxValue; SwingLowPlot.Reset(); } else { SwingLowPlot.Set(currentSwingLow); } if (isSwingLow) { for (int i = 0; i <= strength; i++) { swingLowSeries.Set(i, lastSwingLowValue); } } else { swingLowSeries.Set(lastSwingLowValue); } } saveCurrentBar = CurrentBar; } else { if (High[0] > High[strength] && swingHighSwings[strength] > 0.0) { swingHighSwings.Set(strength, 0.0); for (int i = 0; i <= strength; i++) { SwingHighPlot.Reset(i); } currentSwingHigh = 0.0; } else if (High[0] > High[strength] && currentSwingHigh != 0.0) { SwingHighPlot.Reset(); currentSwingHigh = 0.0; } else if (High[0] <= currentSwingHigh) { SwingHighPlot.Set(currentSwingHigh); } if (Low[0] < Low[strength] && swingLowSwings[strength] > 0.0) { swingLowSwings.Set(strength, 0.0); for (int i = 0; i <= strength; i++) { SwingLowPlot.Reset(i); } currentSwingLow = double.MaxValue; } else if (Low[0] < Low[strength] && currentSwingLow != double.MaxValue) { SwingLowPlot.Reset(); currentSwingLow = double.MaxValue; } else if (Low[0] >= currentSwingLow) { SwingLowPlot.Set(currentSwingLow); } } swingSizeSeries.Set(SwingHighPlot[0] - SwingLowPlot[0]); // Average the swing size swingAvg = SMA(swingSizeSeries, 200)[0]; swingAvgShort = SMA(swingSizeSeries, 4)[0]; DrawTextFixed("swingsize", "SwingAvg(200) = " + String.Format("{0:0.00}", swingAvg) + "\nSwingAvg(5) = " + String.Format("{0:0.00}", swingAvgShort), TextPosition.BottomRight); //Print( "Time = " + Time[0].Hour + " " + Time[0].Minute + " Value = " + SwingLowPlot[0] + " " + SwingHighPlot[0]); //Print( "swingSizeSeries " + swingAvgShort ); }
protected override void OnBarUpdate() { double high0 = (Input is Indicator || Input is Series <double>) ? Input[0] : High[0]; double low0 = (Input is Indicator || Input is Series <double>) ? Input[0] : Low[0]; double close0 = (Input is Indicator || Input is Series <double>) ? Input[0] : Close[0]; double highStrength = High[Math.Min(Strength, CurrentBar)]; double lowStrength = Low[Math.Min(Strength, CurrentBar)]; if (BarsArray[0].BarsType.IsRemoveLastBarSupported && CurrentBar < saveCurrentBar) { currentSwingHigh = SwingHighPlot.IsValidDataPoint(0) ? SwingHighPlot[0] : 0; currentSwingLow = SwingLowPlot.IsValidDataPoint(0) ? SwingLowPlot[0] : 0; lastSwingHighValue = swingHighSeries[0]; lastSwingLowValue = swingLowSeries[0]; swingHighSeries[Strength] = 0; swingLowSeries[Strength] = 0; lastHighCache.Clear(); lastLowCache.Clear(); for (int barsBack = Math.Min(CurrentBar, constant) - 1; barsBack >= 0; barsBack--) { lastHighCache.Add((Input is Indicator || Input is Series <double>) ? Input[barsBack] : High[barsBack]); lastLowCache.Add((Input is Indicator || Input is Series <double>) ? Input[barsBack] : Low[barsBack]); } saveCurrentBar = CurrentBar; return; } if (saveCurrentBar != CurrentBar) { swingHighSwings[0] = 0; // initializing important internal swingLowSwings[0] = 0; // initializing important internal swingHighSeries[0] = 0; // initializing important internal swingLowSeries[0] = 0; // initializing important internal lastHighCache.Add(high0); if (lastHighCache.Count > constant) { lastHighCache.RemoveAt(0); } lastLowCache.Add(low0); if (lastLowCache.Count > constant) { lastLowCache.RemoveAt(0); } if (lastHighCache.Count == constant) { bool isSwingHigh = true; double swingHighCandidateValue = (double)lastHighCache[Strength]; for (int i = 0; i < Strength; i++) { if (((double)lastHighCache[i]).ApproxCompare(swingHighCandidateValue) >= 0) { isSwingHigh = false; } } for (int i = Strength + 1; i < lastHighCache.Count; i++) { if (((double)lastHighCache[i]).ApproxCompare(swingHighCandidateValue) > 0) { isSwingHigh = false; } } swingHighSwings[Strength] = isSwingHigh ? swingHighCandidateValue : 0.0; if (isSwingHigh) { lastSwingHighValue = swingHighCandidateValue; } if (isSwingHigh) { currentSwingHigh = swingHighCandidateValue; for (int i = 0; i <= Strength; i++) { SwingHighPlot[i] = currentSwingHigh; } } else if (high0 > currentSwingHigh || currentSwingHigh.ApproxCompare(0.0) == 0) { currentSwingHigh = 0.0; SwingHighPlot[0] = close0; SwingHighPlot.Reset(); } else { SwingHighPlot[0] = currentSwingHigh; } if (isSwingHigh) { for (int i = 0; i <= Strength; i++) { swingHighSeries[i] = lastSwingHighValue; } } else { swingHighSeries[0] = lastSwingHighValue; } } if (lastLowCache.Count == constant) { bool isSwingLow = true; double swingLowCandidateValue = (double)lastLowCache[Strength]; for (int i = 0; i < Strength; i++) { if (((double)lastLowCache[i]).ApproxCompare(swingLowCandidateValue) <= 0) { isSwingLow = false; } } for (int i = Strength + 1; i < lastLowCache.Count; i++) { if (((double)lastLowCache[i]).ApproxCompare(swingLowCandidateValue) < 0) { isSwingLow = false; } } swingLowSwings[Strength] = isSwingLow ? swingLowCandidateValue : 0.0; if (isSwingLow) { lastSwingLowValue = swingLowCandidateValue; } if (isSwingLow) { currentSwingLow = swingLowCandidateValue; for (int i = 0; i <= Strength; i++) { SwingLowPlot[i] = currentSwingLow; } } else if (low0 < currentSwingLow || currentSwingLow.ApproxCompare(0.0) == 0) { currentSwingLow = double.MaxValue; SwingLowPlot[0] = close0; SwingLowPlot.Reset(); } else { SwingLowPlot[0] = currentSwingLow; } if (isSwingLow) { for (int i = 0; i <= Strength; i++) { swingLowSeries[i] = lastSwingLowValue; } } else { swingLowSeries[0] = lastSwingLowValue; } } saveCurrentBar = CurrentBar; } else if (CurrentBar >= constant - 1) { if (high0 > highStrength && swingHighSwings[Strength] > 0.0) { swingHighSwings[Strength] = 0.0; for (int i = 0; i <= Strength; i++) { SwingHighPlot[i] = close0; SwingHighPlot.Reset(i); currentSwingHigh = 0.0; } } else if (high0 > highStrength && currentSwingHigh.ApproxCompare(0.0) != 0) { SwingHighPlot[0] = close0; SwingHighPlot.Reset(); currentSwingHigh = 0.0; } else if (high0 <= currentSwingHigh) { SwingHighPlot[0] = currentSwingHigh; } if (low0 < lowStrength && swingLowSwings[Strength] > 0.0) { swingLowSwings[Strength] = 0.0; for (int i = 0; i <= Strength; i++) { SwingLowPlot[i] = close0; SwingLowPlot.Reset(i); currentSwingLow = double.MaxValue; } } else if (low0 < lowStrength && currentSwingLow.ApproxCompare(double.MaxValue) != 0) { SwingLowPlot.Reset(); currentSwingLow = double.MaxValue; } else if (low0 >= currentSwingLow) { SwingLowPlot[0] = currentSwingLow; } } }
/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (saveCurrentBar != CurrentBar) { swingHighSwings.Set(0); // initializing important internal swingLowSwings.Set(0); // initializing important internal swingHighSeries.Set(0); // initializing important internal swingLowSeries.Set(0); // initializing important internal lastHighCache.Add(High[0]); if (lastHighCache.Count > (2 * strength) + 1) { lastHighCache.RemoveAt(0); } lastLowCache.Add(Low[0]); if (lastLowCache.Count > (2 * strength) + 1) { lastLowCache.RemoveAt(0); } if (lastHighCache.Count == (2 * strength) + 1) { bool isSwingHigh = true; double swingHighCandidateValue = (double)lastHighCache[strength]; for (int i = 0; i < strength; i++) { if ((double)lastHighCache[i] >= swingHighCandidateValue - double.Epsilon) { isSwingHigh = false; } } for (int i = strength + 1; i < lastHighCache.Count; i++) { if ((double)lastHighCache[i] > swingHighCandidateValue - double.Epsilon) { isSwingHigh = false; } } swingHighSwings.Set(strength, isSwingHigh ? swingHighCandidateValue : 0.0); if (isSwingHigh) { lastSwingHighValue = swingHighCandidateValue; } if (isSwingHigh) { currentSwingHigh = swingHighCandidateValue; for (int i = 0; i <= strength; i++) { SwingHighPlot.Set(i, currentSwingHigh); } } else if (High[0] > currentSwingHigh) { currentSwingHigh = 0.0; SwingHighPlot.Reset(); } else { SwingHighPlot.Set(currentSwingHigh); } if (isSwingHigh) { for (int i = 0; i <= strength; i++) { swingHighSeries.Set(i, lastSwingHighValue); } } else { swingHighSeries.Set(lastSwingHighValue); } } if (lastLowCache.Count == (2 * strength) + 1) { bool isSwingLow = true; double swingLowCandidateValue = (double)lastLowCache[strength]; for (int i = 0; i < strength; i++) { if ((double)lastLowCache[i] <= swingLowCandidateValue + double.Epsilon) { isSwingLow = false; } } for (int i = strength + 1; i < lastLowCache.Count; i++) { if ((double)lastLowCache[i] < swingLowCandidateValue + double.Epsilon) { isSwingLow = false; } } swingLowSwings.Set(strength, isSwingLow ? swingLowCandidateValue : 0.0); if (isSwingLow) { lastSwingLowValue = swingLowCandidateValue; } if (isSwingLow) { currentSwingLow = swingLowCandidateValue; for (int i = 0; i <= strength; i++) { SwingLowPlot.Set(i, currentSwingLow); } } else if (Low[0] < currentSwingLow) { currentSwingLow = double.MaxValue; SwingLowPlot.Reset(); } else { SwingLowPlot.Set(currentSwingLow); } if (isSwingLow) { for (int i = 0; i <= strength; i++) { swingLowSeries.Set(i, lastSwingLowValue); } } else { swingLowSeries.Set(lastSwingLowValue); } } saveCurrentBar = CurrentBar; } else { if (High[0] > High[strength] && swingHighSwings[strength] > 0.0) { swingHighSwings.Set(strength, 0.0); for (int i = 0; i <= strength; i++) { SwingHighPlot.Reset(i); } currentSwingHigh = 0.0; } else if (High[0] > High[strength] && currentSwingHigh != 0.0) { SwingHighPlot.Reset(); currentSwingHigh = 0.0; } else if (High[0] <= currentSwingHigh) { SwingHighPlot.Set(currentSwingHigh); } if (Low[0] < Low[strength] && swingLowSwings[strength] > 0.0) { swingLowSwings.Set(strength, 0.0); for (int i = 0; i <= strength; i++) { SwingLowPlot.Reset(i); } currentSwingLow = double.MaxValue; } else if (Low[0] < Low[strength] && currentSwingLow != double.MaxValue) { SwingLowPlot.Reset(); currentSwingLow = double.MaxValue; } else if (Low[0] >= currentSwingLow) { SwingLowPlot.Set(currentSwingLow); } } }