/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var result = new VolumeProfileIndicatorValue(this); if (!input.IsFinal) { return(result); } IsFormed = true; var candle = input.GetValue <Candle>(); if (!UseTotalVolume) { if (candle.PriceLevels != null) { foreach (var priceLevel in candle.PriceLevels) { AddVolume(priceLevel.Price, priceLevel.BuyVolume + priceLevel.SellVolume); } } } else { AddVolume(candle.ClosePrice, candle.TotalVolume); } foreach (var level in _levels) { result.Levels.Add(level.Key, level.Value); } return(result); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); try { if (candle.LowPrice < _currentMinimum) { _currentMinimum = candle.LowPrice; _valueBarCount = _currentBarCount; } else if (candle.HighPrice >= _currentMinimum + ReversalAmount.Value) { if (input.IsFinal) { IsFormed = true; } return(new DecimalIndicatorValue(this, _valueBarCount)); } return(new DecimalIndicatorValue(this, this.GetCurrentValue())); } finally { if (input.IsFinal) { _currentBarCount++; } } }
private static void FillValues(IIndicatorValue value, ICollection <decimal?> values) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (value is DecimalIndicatorValue || value is CandleIndicatorValue || value is ShiftedIndicatorValue) { values.Add(value.IsEmpty ? (decimal?)null : value.GetValue <decimal>()); } else if (value is ComplexIndicatorValue complexValue) { foreach (var innerIndicator in ((IComplexIndicator)value.Indicator).InnerIndicators) { var innerValue = complexValue.InnerValues.TryGetValue(innerIndicator); if (innerValue == null) { values.Add(null); } else { FillValues(innerValue, values); } } } else { throw new ArgumentOutOfRangeException(nameof(value), value.GetType(), LocalizedStrings.Str1655); } }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); try { if (candle.LowPrice < _currentMinimum) { _currentMinimum = candle.LowPrice; _valueBarCount = _currentBarCount; } else if (candle.HighPrice >= _currentMinimum + ReversalAmount.Value) { if (input.IsFinal) IsFormed = true; return new DecimalIndicatorValue(this, _valueBarCount); } return new DecimalIndicatorValue(this, this.GetCurrentValue()); } finally { if(input.IsFinal) _currentBarCount++; } }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); // Вычисляем СMO var cmoValue = _cmo.Process(input).GetValue <decimal>(); // Вычисляем Vidya if (!IsFormed) { if (!input.IsFinal) { return(new DecimalIndicatorValue(this, ((Buffer.Skip(1).Sum() + newValue) / Length))); } Buffer.Add(newValue); _prevFinalValue = Buffer.Sum() / Length; return(new DecimalIndicatorValue(this, _prevFinalValue)); } var curValue = (newValue - _prevFinalValue) * _multiplier * Math.Abs(cmoValue / 100m) + _prevFinalValue; if (input.IsFinal) { _prevFinalValue = curValue; } return(new DecimalIndicatorValue(this, curValue)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { if (input.IsFinal) IsFormed = true; return new CandleIndicatorValue(this, input.GetValue<Candle>(), c => c.TotalVolume); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); var smaValue = _sma.Process(input).GetValue <decimal>(); if (input.IsFinal) { Buffer.Add(newValue); if (Buffer.Count > Length) { Buffer.RemoveAt(0); } } var buff = Buffer; if (!input.IsFinal) { buff = new List <decimal>(); buff.AddRange(Buffer.Skip(1)); buff.Add(newValue); } //считаем значение отклонения в последней точке var std = buff.Select(t1 => t1 - smaValue).Select(t => t * t).Sum(); return(new DecimalIndicatorValue(this, (decimal)Math.Sqrt((double)(std / Length)))); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { if (input.IsFinal) { IsFormed = true; } return(new CandleIndicatorValue(this, input.GetValue <Candle>(), c => c.TotalVolume)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var value = input.GetValue <Tuple <decimal, decimal> >(); Buffer.Add(value); Tuple <decimal, decimal> first = null; if (input.IsFinal) { if (Buffer.Count > Length) { Buffer.RemoveAt(0); } } else { if (Buffer.Count > Length) { first = Buffer[0]; Buffer.RemoveAt(0); } } decimal avgSource = 0; decimal avgOther = 0; foreach (var tuple in Buffer) { avgSource += tuple.Item1; avgOther += tuple.Item2; } var len = Buffer.Count; avgSource /= len; avgOther /= len; var covariance = 0m; foreach (var tuple in Buffer) { covariance += (tuple.Item1 - avgSource) * (tuple.Item2 - avgOther); } if (!input.IsFinal) { if (first != null) { Buffer.Insert(0, first); } Buffer.RemoveAt(len - 1); } return(new DecimalIndicatorValue(this, covariance / len)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); if (input.IsFinal) IsFormed = true; return new DecimalIndicatorValue(this, (candle.HighPrice + candle.LowPrice) / 2); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); if (input.IsFinal) { Buffer.Add(newValue); if (Buffer.Count > Length) { Buffer.RemoveAt(0); } } var buff = Buffer; if (!input.IsFinal) { buff = new List <decimal>(); buff.AddRange(Buffer.Skip(1)); buff.Add(newValue); } //x - независимая переменная, номер значения в буфере //y - зависимая переменная - значения из буфера var sumX = 0m; //сумма x var sumY = 0m; //сумма y var sumXy = 0m; //сумма x*y var sumX2 = 0m; //сумма x^2 for (var i = 0; i < buff.Count; i++) { sumX += i; sumY += buff.ElementAt(i); sumXy += i * buff.ElementAt(i); sumX2 += i * i; } //коэффициент при независимой переменной var divisor = (Length * sumX2 - sumX * sumX); if (divisor == 0) { _slope = 0; } else { _slope = (Length * sumXy - sumX * sumY) / divisor; } //свободный член var b = (sumY - _slope * sumX) / Length; //прогноз последнего значения (его номер Length - 1) return(new DecimalIndicatorValue(this, _slope * (Length - 1) + b)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var cov = base.OnProcess(input); var value = input.GetValue<Tuple<decimal, decimal>>(); var sourceDev = _source.Process(value.Item1); var otherDev = _other.Process(value.Item2); return new DecimalIndicatorValue(this, cov.GetValue<decimal>() / (sourceDev.GetValue<decimal>() * otherDev.GetValue<decimal>())); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); if (input.IsFinal) { IsFormed = true; } return(new DecimalIndicatorValue(this, (candle.HighPrice + candle.LowPrice) / 2)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var message = input.GetValue<Level1ChangeMessage>(); var retVal = message.Changes.TryGetValue(Field); if (!IsFormed && retVal != null && input.IsFinal) IsFormed = true; return retVal == null ? new DecimalIndicatorValue(this) : new DecimalIndicatorValue(this, (decimal)retVal); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var shValue = _nominator.Process(input.SetValue(this, candle.ClosePrice * candle.TotalVolume)).GetValue <decimal>(); var znValue = _denominator.Process(input.SetValue(this, candle.TotalVolume)).GetValue <decimal>(); return(znValue != 0 ? new DecimalIndicatorValue(this, (shValue / znValue)) : new DecimalIndicatorValue(this)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var cov = base.OnProcess(input); var value = input.GetValue <Tuple <decimal, decimal> >(); var sourceDev = _source.Process(value.Item1); var otherDev = _other.Process(value.Item2); return(new DecimalIndicatorValue(this, cov.GetValue <decimal>() / (sourceDev.GetValue <decimal>() * otherDev.GetValue <decimal>()))); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); var emaValue = Ema.Process(input.SetValue(this, candle.HighPrice - candle.LowPrice)); if (Ema.IsFormed) { return Roc.Process(emaValue); } return input; }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var emaValue = Ema.Process(input.SetValue(this, candle.HighPrice - candle.LowPrice)); if (Ema.IsFormed) { return(Roc.Process(emaValue)); } return(input); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { IsFormed = true; var candle = input.GetValue <Candle>(); if (candle.TotalVolume == 0) { return(new DecimalIndicatorValue(this)); } return(new DecimalIndicatorValue(this, (candle.HighPrice - candle.LowPrice) / candle.TotalVolume)); }
/// <inheritdoc /> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var emaValue = Ema.Process(input.SetValue(this, candle.HighPrice - candle.LowPrice)); if (Ema.IsFormed) { var val = Roc.Process(emaValue); return(new DecimalIndicatorValue(this, val.GetValue <decimal>())); } return(new DecimalIndicatorValue(this)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <Candle>(); if (input.IsFinal) { _buffer.Add(newValue); if (_buffer.Count > Length) { _buffer.RemoveAt(0); } } if (IsFormed) { decimal valueUp, valueDn; if (input.IsFinal) { valueUp = ((_buffer[0].ClosePrice - _buffer[0].OpenPrice) + 2 * (_buffer[1].ClosePrice - _buffer[1].OpenPrice) + 2 * (_buffer[2].ClosePrice - _buffer[2].OpenPrice) + (_buffer[3].ClosePrice - _buffer[3].OpenPrice)) / 6m; valueDn = ((_buffer[0].HighPrice - _buffer[0].LowPrice) + 2 * (_buffer[1].HighPrice - _buffer[1].LowPrice) + 2 * (_buffer[2].HighPrice - _buffer[2].LowPrice) + (_buffer[3].HighPrice - _buffer[3].LowPrice)) / 6m; } else { valueUp = ((_buffer[1].ClosePrice - _buffer[1].OpenPrice) + 2 * (_buffer[2].ClosePrice - _buffer[2].OpenPrice) + 2 * (_buffer[3].ClosePrice - _buffer[3].OpenPrice) + (newValue.ClosePrice - newValue.OpenPrice)) / 6m; valueDn = ((_buffer[1].HighPrice - _buffer[1].LowPrice) + 2 * (_buffer[2].HighPrice - _buffer[2].LowPrice) + 2 * (_buffer[3].HighPrice - _buffer[3].LowPrice) + (newValue.HighPrice - newValue.LowPrice)) / 6m; } return(new DecimalIndicatorValue(this, valueDn == decimal.Zero ? valueUp : valueUp / valueDn)); } return(new DecimalIndicatorValue(this)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); // Находим минимум и максимум для заданного периода var lowValue = _low.Process(input.SetValue(this, candle.LowPrice)).GetValue <decimal>(); var highValue = _high.Process(input.SetValue(this, candle.HighPrice)).GetValue <decimal>(); if ((highValue - lowValue) != 0) { return(new DecimalIndicatorValue(this, -100m * (highValue - candle.ClosePrice) / (highValue - lowValue))); } return(new DecimalIndicatorValue(this)); }
/// <inheritdoc /> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); if (input.IsFinal) { Buffer.Add(newValue); if (Buffer.Count > Length) { Buffer.RemoveAt(0); } } var buff = Buffer; if (!input.IsFinal) { buff = new List <decimal>(); buff.AddRange(Buffer.Skip(1)); buff.Add(newValue); } //x - независимая переменная, номер значения в буфере //y - зависимая переменная - значения из буфера var sumX = 0m; //сумма x var sumY = 0m; //сумма y var sumXy = 0m; //сумма x*y var sumX2 = 0m; //сумма x^2 for (var i = 0; i < buff.Count; i++) { sumX += i; sumY += buff.ElementAt(i); sumXy += i * buff.ElementAt(i); sumX2 += i * i; } //коэффициент при независимой переменной var divisor = Length * sumX2 - sumX * sumX; if (divisor == 0) { return(new DecimalIndicatorValue(this)); } return(new DecimalIndicatorValue(this, (Length * sumXy - sumX * sumY) / divisor)); }
/// <inheritdoc /> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var average = (candle.HighPrice + candle.LowPrice) / 2; var halfRange = (candle.HighPrice - candle.LowPrice) / 2; Buffer.Add(average); //var Chec1 = Buffer[Buffer.Count - 1]; if (IsFormed) { if (Buffer.Count > Length) { Buffer.RemoveAt(0); } //Сглаженное приращение **************************************************************************** var avgDiff = Buffer[Buffer.Count - 1] - Buffer[Buffer.Count - 2]; var smoothDiff = _smoothConstant * avgDiff + _smoothConstant1 * _value1Old; _value1Old = smoothDiff; //Сглаженный Half Range ********************************************************************************* var smoothRng = _smoothConstant * halfRange + _smoothConstant1 * _value2Old; _value2Old = smoothRng; //Tracking index *********************************************************************************** if (smoothRng != 0) { _lambda = Math.Abs(smoothDiff / smoothRng); } //Alfa для альфа фильтра *************************************************************************** _alpha = (-_lambda * _lambda + (decimal)Math.Sqrt((double)(_lambda * _lambda * _lambda * _lambda + 16 * _lambda * _lambda))) / 8; //Smoothed result ********************************************************************************** var check2 = _alpha * average; var check3 = (1 - _alpha) * _resultOld; var result = check2 + check3; _resultOld = result; return(new DecimalIndicatorValue(this, result)); } _value2Old = halfRange; _resultOld = average; return(new DecimalIndicatorValue(this, _resultOld)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var price = input.GetValue <Candle>().ClosePrice; if (Buffer.Count > Length) { Buffer.RemoveAt(0); } if (input.IsFinal) { Buffer.Add(price); } return(new DecimalIndicatorValue(this, price)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var highValue = _high.Process(input.SetValue(this, candle.HighPrice)).GetValue <decimal>(); var lowValue = _low.Process(input.SetValue(this, candle.LowPrice)).GetValue <decimal>(); var diff = highValue - lowValue; if (diff == 0) { return(new DecimalIndicatorValue(this, 0)); } return(new DecimalIndicatorValue(this, 100 * (candle.ClosePrice - lowValue) / diff)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); decimal?result = null; var buff = _buffer; if (input.IsFinal) { _buffer.Add(candle); // если буффер стал достаточно большим (стал больше длины) if (_buffer.Count > Length) { _buffer.RemoveAt(0); } } else { buff = _buffer.Skip(1).Concat(candle).ToList(); } if (buff.Count >= Length) { // рассчитываем значение var max = buff.Max(t => t.HighPrice); var min = buff.Min(t => t.LowPrice); if (Kijun.IsFormed && input.IsFinal) { Buffer.Add((max + min) / 2); } if (Buffer.Count >= Kijun.Length) { result = Buffer[0]; } if (Buffer.Count > Kijun.Length) { Buffer.RemoveAt(0); } } return(result == null ? new DecimalIndicatorValue(this) : new DecimalIndicatorValue(this, result.Value)); }
/// <summary> /// Обработать новые значения. /// </summary> /// <param name="time">Временная отметка формирования новых данных.</param> /// <param name="value">Значения индикатора.</param> /// <param name="draw">Метод отрисовки значения на графике.</param> /// <returns>Новые значения для отображения на графике.</returns> public override IEnumerable <decimal> ProcessValues(DateTimeOffset time, IIndicatorValue value, DrawHandler draw) { var newYValues = new List <decimal>(); if (!value.IsFormed) { draw(_pnl, 0, double.NaN, double.NaN); } else { var pnl = value.GetValue <decimal>(); draw(_pnl, 0, (double)pnl, (double)0); newYValues.Add(pnl); } return(newYValues); }
/// <inheritdoc /> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var aveP = (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m; var meanValue = _mean.Process(new DecimalIndicatorValue(this, aveP) { IsFinal = input.IsFinal }); if (IsFormed && meanValue.GetValue <decimal>() != 0) { return(new DecimalIndicatorValue(this, ((aveP - _mean.Sma.GetCurrentValue()) / (0.015m * meanValue.GetValue <decimal>())))); } return(new DecimalIndicatorValue(this)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); if (_previouseClosePrice != null) { var min = _previouseClosePrice.Value < candle.LowPrice ? _previouseClosePrice.Value : candle.LowPrice; var max = _previouseClosePrice.Value > candle.HighPrice ? _previouseClosePrice.Value : candle.HighPrice; input = input.SetValue(this, candle.ClosePrice - min); var p7BpValue = _period7BpSum.Process(input).GetValue <decimal>(); var p14BpValue = _period14BpSum.Process(input).GetValue <decimal>(); var p28BpValue = _period28BpSum.Process(input).GetValue <decimal>(); input = input.SetValue(this, max - min); var p7TrValue = _period7TrSum.Process(input).GetValue <decimal>(); var p14TrValue = _period14TrSum.Process(input).GetValue <decimal>(); var p28TrValue = _period28TrSum.Process(input).GetValue <decimal>(); if (input.IsFinal) { _previouseClosePrice = candle.ClosePrice; } if (p7TrValue != 0 && p14TrValue != 0 && p28TrValue != 0) { var average7 = p7BpValue / p7TrValue; var average14 = p14BpValue / p14TrValue; var average28 = p28BpValue / p28TrValue; return(new DecimalIndicatorValue(this, _stoProcentov * (_weight4 * average7 + _weight2 * average14 + _weight1 * average28) / (_weight4 + _weight2 + _weight1))); } return(new DecimalIndicatorValue(this)); } if (input.IsFinal) { _previouseClosePrice = candle.ClosePrice; } return(new DecimalIndicatorValue(this)); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); if (input.IsFinal) { Buffer.Add(newValue); if (Buffer.Count > Length) { Buffer.RemoveAt(0); } } if (input.IsFinal) { return(new DecimalIndicatorValue(this, Buffer.Sum() / Length)); } return(new DecimalIndicatorValue(this, (Buffer.Skip(1).Sum() + newValue) / Length)); }
/// <inheritdoc /> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.GetValue <decimal>(); if (input.IsFinal) { Buffer.Add(newValue); if ((Buffer.Count - 1) > Length) { Buffer.RemoveAt(0); } } if (Buffer.Count == 0) { return(new DecimalIndicatorValue(this)); } return(new DecimalIndicatorValue(this, newValue - Buffer[0])); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var smaValue = _sma.Process(input); if (_sma.IsFormed && input.IsFinal) { Buffer.Add(smaValue.GetValue <decimal>()); } if (!IsFormed) { return(new DecimalIndicatorValue(this)); } if (Buffer.Count > Length) { Buffer.RemoveAt(0); } return(new DecimalIndicatorValue(this, input.GetValue <decimal>() - Buffer[0])); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); if (_previouseClosePrice != null) { var min = _previouseClosePrice.Value < candle.LowPrice ? _previouseClosePrice.Value : candle.LowPrice; var max = _previouseClosePrice.Value > candle.HighPrice ? _previouseClosePrice.Value : candle.HighPrice; input = input.SetValue(this, candle.ClosePrice - min); var p7BpValue = _period7BpSum.Process(input).GetValue<decimal>(); var p14BpValue = _period14BpSum.Process(input).GetValue<decimal>(); var p28BpValue = _period28BpSum.Process(input).GetValue<decimal>(); input = input.SetValue(this, max - min); var p7TrValue = _period7TrSum.Process(input).GetValue<decimal>(); var p14TrValue = _period14TrSum.Process(input).GetValue<decimal>(); var p28TrValue = _period28TrSum.Process(input).GetValue<decimal>(); if (input.IsFinal) _previouseClosePrice = candle.ClosePrice; if (p7TrValue != 0 && p14TrValue != 0 && p28TrValue != 0) { var average7 = p7BpValue / p7TrValue; var average14 = p14BpValue / p14TrValue; var average28 = p28BpValue / p28TrValue; return new DecimalIndicatorValue(this, _stoProcentov * (_weight4 * average7 + _weight2 * average14 + _weight1 * average28) / (_weight4 + _weight2 + _weight1)); } return new DecimalIndicatorValue(this); } if (input.IsFinal) _previouseClosePrice = candle.ClosePrice; return new DecimalIndicatorValue(this); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); if (_prevCandle != null) { if (input.IsFinal) IsFormed = true; var priceMovements = GetPriceMovements(candle, _prevCandle); if (input.IsFinal) _prevCandle = candle; return new DecimalIndicatorValue(this, priceMovements.Max()); } if (input.IsFinal) _prevCandle = candle; return new DecimalIndicatorValue(this, candle.HighPrice - candle.LowPrice); }
protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue <Candle>(); var medianPrice = (candle.ClosePrice + candle.OpenPrice + candle.HighPrice + candle.LowPrice) / 4m; Buffer.Add(medianPrice); Vector <double> val = DenseVector.Create(1, (i) => 0); val[0] = (double)medianPrice; RecalculateFilterParameters(val); if (Buffer.Count < 5) { return(new DecimalIndicatorValue(this, medianPrice)); } IsFormed = true; return(new DecimalIndicatorValue(this, (decimal)xplus[0])); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var newValue = input.IsSupport(typeof(Candle)) ? input.GetValue <Candle>().HighPrice : input.GetValue <decimal>(); var lastValue = Buffer.Count == 0 ? newValue : this.GetCurrentValue(); // добавляем новое начало if (input.IsFinal) { Buffer.Add(newValue); } if (newValue > lastValue) { // Новое значение и есть экстремум lastValue = newValue; } if (Buffer.Count > Length) { var first = Buffer[0]; // удаляем хвостовое значение if (input.IsFinal) { Buffer.RemoveAt(0); } // удаляется экстремум, для поиска нового значения необходим проход по всему буфферу if (first == lastValue && lastValue != newValue) { // ищем новый экстремум lastValue = Buffer.Aggregate(newValue, (current, t) => Math.Max(t, current)); } } return(new DecimalIndicatorValue(this, lastValue)); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); var buffer = input.IsFinal ? _buffer : _buffer.ToList(); buffer.Add(candle); // если буфер стал достаточно большим (стал больше длины) if (buffer.Count > Length) { // Запоминаем интересующие изменения свечек до буфера if (buffer[0].HighPrice > buffer[1].HighPrice) _wasHighDown = true; else if (buffer[0].HighPrice < buffer[1].HighPrice) _wasHighDown = false; if (buffer[0].LowPrice < buffer[1].LowPrice) _wasLowUp = true; else if (buffer[0].LowPrice > buffer[1].LowPrice) _wasLowUp = false; buffer.RemoveAt(0); } // Логика расчета: последующие/предыдущие значения должны быть меньше/больше центрального значения if (buffer.Count >= Length) { // флаги для расчета фракталов (если флаг равен false, то на центральной свече нет фрактала) var isMax = true; var isMin = true; var centerHighPrice = buffer[_numCenter].HighPrice; var centerLowPrice = buffer[_numCenter].LowPrice; for (var i = 0; i < buffer.Count; i++) { if (i == _numCenter) continue; // Все значения до и после центральной свечи должны быть // больше для фрактала вверх и меньше для фрактала вниз if (buffer[i].HighPrice > centerHighPrice) { isMax = false; } if (buffer[i].LowPrice < centerLowPrice) { isMin = false; } } // Фильтр для ситуаций где цена у экстремума одинакова за период больше длины буфера if (isMax && _wasHighDown && buffer.GetRange(0, _numCenter).Min(i => i.HighPrice) == centerHighPrice) { isMax = false; } if (isMin && _wasLowUp && buffer.GetRange(0, _numCenter).Max(i => i.LowPrice) == centerLowPrice) { isMin = false; } var shift = buffer.Count - _numCenter - 1; var upValue = isMax ? new ShiftedIndicatorValue(this, shift, new DecimalIndicatorValue(this, centerHighPrice)) : new ShiftedIndicatorValue(this); var downValue = isMin ? new ShiftedIndicatorValue(this, shift, new DecimalIndicatorValue(this, centerLowPrice)) : new ShiftedIndicatorValue(this); upValue.IsFinal = input.IsFinal; downValue.IsFinal = input.IsFinal; var complexValue = new ComplexIndicatorValue(this); complexValue.InnerValues.Add(Up, Up.Process(upValue)); complexValue.InnerValues.Add(Down, Down.Process(downValue)); return complexValue; } return base.OnProcess(new ShiftedIndicatorValue(this, 0, input.SetValue(this, 0))); }
/// <summary> /// Обработать входное значение. /// </summary> /// <param name="input">Входное значение.</param> /// <returns>Результирующее значение.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); if (_candles.Count == 0) _candles.Add(candle); _prevValue = this.GetCurrentValue(); if (candle.OpenTime != _candles[_candles.Count - 1].OpenTime) { _candles.Add(candle); //_prevValue = this.GetCurrentValue(); } else _candles[_candles.Count - 1] = candle; if (_candles.Count < 3) return new DecimalIndicatorValue(this, _prevValue); if (_candles.Count == 3) { _longPosition = _candles[_candles.Count - 1].HighPrice > _candles[_candles.Count - 2].HighPrice; var max = _candles.Max(t => t.HighPrice); var min = _candles.Min(t => t.LowPrice); _xp = _longPosition ? max : min; _af = Acceleration; return new DecimalIndicatorValue(this, _xp + (_longPosition ? -1 : 1) * (max - min) * _af); } if (_afIncreased && _prevBar != _candles.Count) _afIncreased = false; if (input.IsFinal) IsFormed = true; var value = _prevValue; if (_reverseBar != _candles.Count) { _todaySar = TodaySar(_prevValue + _af * (_xp - _prevValue)); for (var x = 1; x <= 2; x++) { if (_longPosition) { if (_todaySar > _candles[_candles.Count - 1 - x].LowPrice) _todaySar = _candles[_candles.Count - 1 - x].LowPrice; } else { if (_todaySar < _candles[_candles.Count - 1 - x].HighPrice) _todaySar = _candles[_candles.Count - 1 - x].HighPrice; } } if ((_longPosition && (_candles[_candles.Count - 1].LowPrice < _todaySar || _candles[_candles.Count - 2].LowPrice < _todaySar)) || (!_longPosition && (_candles[_candles.Count - 1].HighPrice > _todaySar || _candles[_candles.Count - 2].HighPrice > _todaySar))) { return new DecimalIndicatorValue(this, Reverse()); } if (_longPosition) { if (_prevBar != _candles.Count || _candles[_candles.Count - 1].LowPrice < _prevSar) { value = _todaySar; _prevSar = _todaySar; } else value = _prevSar; if (_candles[_candles.Count - 1].HighPrice > _xp) { _xp = _candles[_candles.Count - 1].HighPrice; AfIncrease(); } } else if (!_longPosition) { if (_prevBar != _candles.Count || _candles[_candles.Count - 1].HighPrice > _prevSar) { value = _todaySar; _prevSar = _todaySar; } else value = _prevSar; if (_candles[_candles.Count - 1].LowPrice < _xp) { _xp = _candles[_candles.Count - 1].LowPrice; AfIncrease(); } } } else { if (_longPosition && _candles[_candles.Count - 1].HighPrice > _xp) _xp = _candles[_candles.Count - 1].HighPrice; else if (!_longPosition && _candles[_candles.Count - 1].LowPrice < _xp) _xp = _candles[_candles.Count - 1].LowPrice; value = _prevSar; _todaySar = TodaySar(_longPosition ? Math.Min(_reverseValue, _candles[_candles.Count - 1].LowPrice) : Math.Max(_reverseValue, _candles[_candles.Count - 1].HighPrice)); } _prevBar = _candles.Count; return new DecimalIndicatorValue(this, value); }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); if (_needAdd) { _buffer.Insert(0, candle); _lowBuffer.Insert(0, 0); _highBuffer.Insert(0, 0); _zigZagBuffer.Insert(0, 0); } else { _buffer[0] = candle; _lowBuffer[0] = 0; _highBuffer[0] = 0; _zigZagBuffer[0] = 0; } const int level = 3; int limit; decimal lastHigh = 0; decimal lastLow = 0; if (_buffer.Count - 1 == 0) { limit = _buffer.Count - Depth; } else { int i = 0, count = 0; while (count < level && i < _buffer.Count - Depth) { var res = _zigZagBuffer[i]; if (res != 0) { count++; } i++; } limit = --i; } for (var shift = limit; shift >= 0; shift--) { //--- low var val = _buffer.Skip(shift).Take(Depth).Min(v => _lowValue(v)); if (val == lastLow) { val = 0.0m; } else { lastLow = val; if (_lowValue(_buffer[shift]) - val > 0.0m * val / 100) { val = 0.0m; } else { for (var back = 1; back <= BackStep; back++) { var res = _lowBuffer[shift + back]; if (res != 0 && res > val) { _lowBuffer[shift + back] = 0.0m; } } } } if (_lowValue(_buffer[shift]) == val) _lowBuffer[shift] = val; else _lowBuffer[shift] = 0m; //--- high val = _buffer.Skip(shift).Take(Depth).Max(v => _highValue(v)); if (val == lastHigh) { val = 0.0m; } else { lastHigh = val; if (val - _highValue(_buffer[shift]) > 0.0m * val / 100) { val = 0.0m; } else { for (var back = 1; back <= BackStep; back++) { var res = _highBuffer[shift + back]; if (res != 0 && res < val) { _highBuffer[shift + back] = 0.0m; } } } } if (_highValue(_buffer[shift]) == val) _highBuffer[shift] = val; else _highBuffer[shift] = 0m; } // final cutting lastHigh = -1; lastLow = -1; var lastHighPos = -1; var lastLowPos = -1; for (var shift = limit; shift >= 0; shift--) { var curLow = _lowBuffer[shift]; var curHigh = _highBuffer[shift]; if ((curLow == 0) && (curHigh == 0)) continue; //--- if (curHigh != 0) { if (lastHigh > 0) { if (lastHigh < curHigh) { _highBuffer[lastHighPos] = 0; } else { _highBuffer[shift] = 0; } } //--- if (lastHigh < curHigh || lastHigh < 0) { lastHigh = curHigh; lastHighPos = shift; } lastLow = -1; } //---- if (curLow != 0) { if (lastLow > 0) { if (lastLow > curLow) { _lowBuffer[lastLowPos] = 0; } else { _lowBuffer[shift] = 0; } } //--- if ((curLow < lastLow) || (lastLow < 0)) { lastLow = curLow; lastLowPos = shift; } lastHigh = -1; } } for (var shift = limit; shift >= 0; shift--) { if (shift >= _buffer.Count - Depth) { _zigZagBuffer[shift] = 0.0m; } else { var res = _highBuffer[shift]; if (res != 0.0m) { _zigZagBuffer[shift] = res; } else { _zigZagBuffer[shift] = _lowBuffer[shift]; } } } int valuesCount = 0, valueId = 0; for (; valueId < _zigZagBuffer.Count && valuesCount < 2; valueId++) { if (_zigZagBuffer[valueId] != 0) valuesCount++; } _needAdd = input.IsFinal; if (valuesCount != 2) return new DecimalIndicatorValue(this); if (input.IsFinal) IsFormed = true; LastValueShift = valueId - 1; CurrentValue = _currentValue(_buffer[0]); return new DecimalIndicatorValue(this, _zigZagBuffer[LastValueShift]); }
public override IEnumerable<decimal> ProcessValues(DateTimeOffset time, IIndicatorValue value, DrawHandler draw) { var newYValues = new List<decimal>(); if (!value.IsFormed) { draw(_pnl, 0, double.NaN, double.NaN); } else { var pnl = value.GetValue<decimal>(); draw(_pnl, 0, (double)pnl, (double)0); newYValues.Add(pnl); } return newYValues; }
/// <summary> /// To handle the input value. /// </summary> /// <param name="input">The input value.</param> /// <returns>The resulting value.</returns> protected override IIndicatorValue OnProcess(IIndicatorValue input) { var value = _byPrice(input.GetValue<Candle>()); if (_needAdd) { _buffer.Add(value); _zigZagBuffer.Add(0); } else { _buffer[_buffer.Count - 1] = value; _zigZagBuffer[_zigZagBuffer.Count - 1] = 0; } const int level = 3; int limit = 0, count = 0; while (count < level && limit >= 0) { var res = _zigZagBuffer[limit]; if (res != 0) { count++; } limit--; } limit++; var min = _buffer[limit]; var max = min; int action = 0, j = 0; for (var i = limit + 1; i < _buffer.Count; i++) { if (_buffer[i] > max) { max = _buffer[i]; if (action != 2) //action=1:building the down-point (min) of ZigZag { if (max - min >= _deviation * min) //min (action!=2) end,max (action=2) begin { action = 2; _zigZagBuffer[i] = max; j = i; min = max; } else _zigZagBuffer[i] = 0.0m; //max-min=miser,(action!=2) continue } else //max (action=2) continue { _zigZagBuffer[j] = 0.0m; _zigZagBuffer[i] = max; j = i; min = max; } } else if (_buffer[i] < min) { min = _buffer[i]; if (action != 1) //action=2:building the up-point (max) of ZigZag { if (max - min >= _deviation * max) //max (action!=1) end,min (action=1) begin { action = 1; _zigZagBuffer[i] = min; j = i; max = min; } else _zigZagBuffer[i] = 0.0m; //max-min=miser,(action!=1) continue } else //min (action=1) continue { _zigZagBuffer[j] = 0.0m; _zigZagBuffer[i] = min; j = i; max = min; } } else _zigZagBuffer[i] = 0.0m; } int valuesCount = 0, valueId = 0; decimal last = 0, lastButOne = 0; for (var i = _zigZagBuffer.Count - 1; i > 0 && valuesCount < 2; i--, valueId++) { if (_zigZagBuffer[i] == 0) continue; valuesCount++; if (valuesCount == 1) last = _zigZagBuffer[i]; else lastButOne = _zigZagBuffer[i]; } _needAdd = input.IsFinal; if (valuesCount != 2) return Container.Count > 1 ? this.GetCurrentValue<ShiftedIndicatorValue>() : new ShiftedIndicatorValue(this); if (input.IsFinal) IsFormed = true; CurrentValue = last; return new ShiftedIndicatorValue(this, valueId - 1, input.SetValue(this, lastButOne)); }
protected override IIndicatorValue OnProcess(IIndicatorValue input) { var candle = input.GetValue<Candle>(); IsFormed = _security == null || candle.Security == _security; return new CandleIndicatorValue(this, candle, c => c.ClosePrice); }