Ejemplo n.º 1
0
			public decimal Calculate(Candle candle)
			{
				if (Candles.Count == 0)
					Candles.Add(candle);

				if (candle.OpenTime != Candles[Candles.Count - 1].OpenTime)
					Candles.Add(candle);
				else
					Candles[Candles.Count - 1] = candle;

				_prevValue = _ind.GetCurrentValue();

				if (Candles.Count < 3)
					return _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 = _ind.Acceleration;
					return _xp + (_longPosition ? -1 : 1) * (max - min) * _af;
				}

				if (_afIncreased && _prevBar != Candles.Count)
					_afIncreased = false;

				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 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 value;
			}