Example #1
0
        public Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var mfiValue = _mfiIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            Console.WriteLine($"Mfi indicator value: {mfiValue}");

            if (mfiValue < 0)
            {
                return(Task.FromResult(TrendDirection.None));
            }
            if (mfiValue > 0 && mfiValue <= 20)
            {
                if (_lastTrend == TrendDirection.Short)
                {
                    _lastTrend = TrendDirection.Long;
                    return(Task.FromResult(_lastTrend));
                }
            }
            if (mfiValue >= 80)
            {
                if (_lastTrend == TrendDirection.Long)
                {
                    _lastTrend = TrendDirection.Short;
                    return(Task.FromResult(_lastTrend));
                }
            }

            return(Task.FromResult(TrendDirection.None));
        }
Example #2
0
        public Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var rsiValue = _rsiIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            Console.WriteLine($"Rsi value: {rsiValue}");

            if (rsiValue < 30 && rsiValue > 0)
            {
                if (_lastTrend == TrendDirection.Long)
                {
                    return(Task.FromResult(TrendDirection.None));
                }
                _lastTrend = TrendDirection.Long;
                return(Task.FromResult(_lastTrend));
            }

            if (rsiValue > 70)
            {
                if (_lastTrend == TrendDirection.Short)
                {
                    return(Task.FromResult(TrendDirection.None));
                }

                _lastTrend = TrendDirection.Short;
                return(Task.FromResult(_lastTrend));
            }

            return(Task.FromResult(TrendDirection.None));
        }
        public Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var candleSticksValue = _candleSticksIndicator.GetIndicatorValue(currentCandle);

            if (_lastTrend == TrendDirection.Short)
            {
                if (candleSticksValue.CandleFormat == CandleFormat.BullishMarubozu)
                {
                    _lastTrend = TrendDirection.Long;
                    return(Task.FromResult(_lastTrend));
                }
            }
            if (_lastTrend == TrendDirection.Long)
            {
                if (currentCandle.ClosePrice >= _lastBuyPrice * (decimal)1.01 ||
                    candleSticksValue.CandleFormat == CandleFormat.BearishMarubozu)
                {
                    _lastTrend = TrendDirection.Short;
                }
                else
                {
                    return(Task.FromResult(TrendDirection.None));
                }
            }

            return(Task.FromResult(TrendDirection.None));
        }
Example #4
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue  = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            if (_lastTrend == TrendDirection.Short)
            {
                if (shortEmaValue > longEmaValue)
                {
                    if (_persistenceBuyCount > 2)
                    {
                        _lastTrend = TrendDirection.Long;
                    }
                    else
                    {
                        _persistenceBuyCount++;
                        return(await Task.FromResult(TrendDirection.None));
                    }
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (shortEmaValue < longEmaValue)
                {
                    if (_persistenceSellCount > 5)
                    {
                        _lastTrend = TrendDirection.Short;
                    }
                    else
                    {
                        _persistenceSellCount++;
                        return(await Task.FromResult(TrendDirection.None));
                    }
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #5
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var price         = currentCandle.ClosePrice;
            var shortEmaValue = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue  = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            var emaTrend = shortEmaValue > longEmaValue ? TrendDirection.Long : TrendDirection.Short;

            //Console.WriteLine($"Short EMA value: {shortEmaValue}; Long EMA value: {longEmaValue}; EMA Trend: {emaTrend}; Candlesticks: {candleSticksValue}");
            if (_lastTrend == TrendDirection.Short)
            {
                if (emaTrend == TrendDirection.Long)
                {
                    _lastTrend    = TrendDirection.Long;
                    _lastBuyPrice = price;
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (price >= _lastBuyPrice * (decimal)1.01 ||
                    price < _lastBuyPrice * (decimal)0.9975)
                {
                    _lastTrend = TrendDirection.Short;
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #6
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue  = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue   = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var emaDiffValue   = shortEmaValue - longEmaValue;
            var signalEmaValue = Math.Round(_signalEmaIndicator.GetIndicatorValue(emaDiffValue).IndicatorValue, 4);
            var macdValue      = Math.Round(emaDiffValue - signalEmaValue, 4);

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"MACD: {macdValue};\t" +
                              $"PeekMACD: {_maxOrMinMacd};\t" +
                              $"Close price: {currentCandle.ClosePrice};");

            if (!_lastMacd.HasValue)
            {
                _lastMacd       = macdValue;
                _lastClosePrice = currentCandle.ClosePrice;
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastMacd < 0 && macdValue >= 0)
            {
                if (_macdSwitch)
                {
                    _maxMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (_lastMacd > 0 && macdValue <= 0)
            {
                if (_macdSwitch)
                {
                    _minMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (macdValue < 0 && macdValue < _minMacd)
            {
                _minMacd           = macdValue;
                _minMacdClosePrice = currentCandle.ClosePrice;
            }

            if (macdValue > 0 && macdValue > _maxMacd)
            {
                _maxMacd           = macdValue;
                _maxMacdClosePrice = currentCandle.ClosePrice;
            }

            // wait 1 hour
            if (_candleCount <= 60)
            {
                _candleCount++;
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue > 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_stopTrading == false &&
                    macdValue < _options.BuyThreshold &&
                    diffPreviousMacd < -(decimal)1.0 &&
                    macdValue > _lastMacd &&
                    currentCandle.ClosePrice > _lastClosePrice)
                {
                    _macdRate = (1 - Math.Round(_minMacdClosePrice / _maxMacdClosePrice, 4)) * 100;
                    Console.WriteLine($"MaxMacd: {_maxMacdClosePrice}; MinMacd: {_minMacdClosePrice}; Rate: {_macdRate}%");

                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    if (_macdRate < (decimal)2.75 || _macdRate > (decimal)5.5)
                    {
                        _stopTrading = true;
                        return(await Task.FromResult(TrendDirection.None));
                    }

                    _lastTrend    = TrendDirection.Long;
                    _maxOrMinMacd = 0;
                    _lastBuyPrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage   = 1 - (_macdRate - (decimal)0.4) / 100;
                var profitPercentage = 1 + (_macdRate + (decimal)0.4) / 100; //(decimal) 1.038;
                var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_lastMacd > macdValue &&
                    diffPreviousMacd > (decimal)1.0 &&
                    (currentCandle.ClosePrice > _lastBuyPrice * profitPercentage ||
                     currentCandle.ClosePrice < _lastBuyPrice * stopPercentage))
                {
                    Console.WriteLine($"Stop percentage: {stopPercentage}; Profit percentage: {profitPercentage}");
                    _lastTrend      = TrendDirection.Short;
                    _maxOrMinMacd   = 0;
                    _stopTrading    = true;
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue  = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var macdValue     = Math.Round(shortEmaValue - longEmaValue, 4);

            var ichimokuCloudValue = _ichimokuCloudIndicator.GetIndicatorValue(currentCandle);
            var ema200Value        = _ema200Indicator.GetIndicatorValue(currentCandle).IndicatorValue;

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"SSA: {ichimokuCloudValue.IchimokuCloud?.SenkouSpanAValue}; " +
                              $"SSB: {ichimokuCloudValue.IchimokuCloud?.SenkouSpanBValue}; " +
                              $"TS: {ichimokuCloudValue.IchimokuCloud?.TenkanSenValue}; " +
                              $"KS: {ichimokuCloudValue.IchimokuCloud?.KijunSenValue}; " +
                              $"EMA200: {ema200Value}; " +
                              $"MACD: {macdValue}; " +
                              $"MinMaxMacd: {_maxOrMinMacd}; " +
                              $"Close price: {currentCandle.ClosePrice};");

            if (!_lastMacd.HasValue || _lastMacd == 0)
            {
                _lastMacd = macdValue;
                return(await Task.FromResult(TrendDirection.None));
            }

            if (ichimokuCloudValue.IchimokuCloud == null)
            {
                _lastMacd = macdValue;
                return(await Task.FromResult(TrendDirection.None));
            }

            var ssa = ichimokuCloudValue.IchimokuCloud.SenkouSpanAValue;
            var ssb = ichimokuCloudValue.IchimokuCloud.SenkouSpanBValue;
            //var ts = ichimokuCloudValue.IchimokuCloud.TenkanSenValue;
            //var ks = ichimokuCloudValue.IchimokuCloud.KijunSenValue;

            //var ichimokuCloudMin = new List<decimal> { ssa, ssb, ts, ks}.Min();
            var ichimokuCloudSsList = new List <decimal> {
                ssa, ssb
            };

            _last10Ema200ClosePriceRate.Enqueue(Math.Round(currentCandle.ClosePrice / ema200Value, 4));

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue > 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_stopTrading == false &&
                    macdValue < 0 &&
                    diffPreviousMacd < -(decimal)0.2 &&
                    macdValue > _lastMacd &&
                    currentCandle.ClosePrice > _lastClosePrice &&
                    currentCandle.ClosePrice > ema200Value &&
                    currentCandle.ClosePrice <= ichimokuCloudSsList.Max() &&
                    currentCandle.ClosePrice >= ichimokuCloudSsList.Min() &&
                    _last10Ema200ClosePriceRate.GetItems().Count(a => a > (decimal)1.0) > 5)
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    _lastTrend      = TrendDirection.Long;
                    _maxOrMinMacd   = 0;
                    _lastBuyPrice   = currentCandle.ClosePrice;
                    _stopTrading    = true;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage   = (decimal)0.98;
                var profitPercentage = (decimal)1.03;
                if (currentCandle.ClosePrice <= _lastBuyPrice * stopPercentage)
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    _lastTrend      = TrendDirection.Short;
                    _maxOrMinMacd   = 0;
                    _stopTrading    = true;
                    return(await Task.FromResult(_lastTrend));
                }

                var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_lastMacd > macdValue &&
                    diffPreviousMacd > (decimal)0.4
                    //&& currentCandle.ClosePrice < ema200Value
                    //&& currentCandle.ClosePrice > _lastBuyPrice * profitPercentage
                    )
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;

                    if (currentCandle.ClosePrice <= _lastBuyPrice * profitPercentage)
                    {
                        return(await Task.FromResult(TrendDirection.None));
                    }

                    Console.WriteLine($"Stop percentage: {stopPercentage}; Profit percentage: {profitPercentage}");
                    _lastTrend    = TrendDirection.Short;
                    _maxOrMinMacd = 0;
                    _stopTrading  = true;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #8
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue  = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var macdValue     = Math.Round(shortEmaValue - longEmaValue, 4);

            var rsiValue = _rsiIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            var ichimokuCloudValue = _ichimokuCloudIndicator.GetIndicatorValue(currentCandle);

            var ssa = ichimokuCloudValue.IchimokuCloud?.SenkouSpanAValue;
            var ssb = ichimokuCloudValue.IchimokuCloud?.SenkouSpanBValue;
            var ts  = ichimokuCloudValue.IchimokuCloud?.TenkanSenValue;
            var ks  = ichimokuCloudValue.IchimokuCloud?.KijunSenValue;

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"SSA: {ssa}; " +
                              $"SSB: {ssb}; " +
                              $"TS: {ts}; " +
                              $"KS: {ks}; " +
                              $"MACD: {macdValue}; " +
                              $"MinMaxMacd: {_maxOrMinMacd}; " +
                              $"RSI: {rsiValue}; " +
                              $"Close price: {Math.Round(currentCandle.ClosePrice, 4)};");

            // wait 1 hour
            if (_candleCount <= 60)
            {
                _candleCount++;
                _last5Macd.Enqueue(macdValue);
                _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if ((currentCandle.ClosePrice < ssa &&
                     currentCandle.ClosePrice < ssb) &&
                    _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (currentCandle.ClosePrice > ssa &&
                    currentCandle.ClosePrice > ssb &&
                    currentCandle.CandleType == CandleType.Green &&
                    _stopTrading == false &&
                    macdValue < 15
                    )
                {
                    _lastMacd = macdValue;
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastTrend      = TrendDirection.Long;
                    _lastBuyPrice   = currentCandle.ClosePrice;
                    _lastClosePrice = currentCandle.ClosePrice;
                }
                else
                {
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (_delayCount < 10)
                {
                    Console.WriteLine($"DelayCount: {_delayCount}");
                    _delayCount++;
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }

                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage               = (decimal)0.95;
                var profitPercentage             = (decimal)1.015;
                var diffPreviousMacd             = _maxOrMinMacd < 0 ? 0 : Math.Abs(_maxOrMinMacd - macdValue);
                var diffLastClosePricePercentage = (currentCandle.ClosePrice / _lastClosePrice) * (decimal)100.0 - 100;
                if (diffLastClosePricePercentage <= (decimal) - 1.5)
                {
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastMacd       = macdValue;
                    _delayCount     = 1;
                    _lastTrend      = TrendDirection.Short;
                    _stopTrading    = true;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(_lastTrend));
                }

                if (currentCandle.ClosePrice < _lastBuyPrice * stopPercentage)
                {
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastMacd       = macdValue;
                    _delayCount     = 1;
                    _lastTrend      = TrendDirection.Short;
                    _stopTrading    = true;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(_lastTrend));
                }

                if (
                    macdValue > 0 &&
                    _lastMacd > 0 &&
                    _lastMacd > macdValue &&
                    diffPreviousMacd > (decimal)1.0 &&
                    currentCandle.ClosePrice > _lastBuyPrice * profitPercentage)
                {
                    _last5Macd.Enqueue(macdValue);
                    _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                    _lastMacd       = macdValue;
                    _delayCount     = 1;
                    _lastTrend      = TrendDirection.Short;
                    _stopTrading    = true;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(_lastTrend));
                }

                _last5Macd.Enqueue(macdValue);
                _prevClosePrices.Enqueue(currentCandle.ClosePrice);
                _lastMacd       = macdValue;
                _lastClosePrice = currentCandle.ClosePrice;
                return(await Task.FromResult(TrendDirection.None));
            }

            return(await Task.FromResult(_lastTrend));
        }
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue  = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var macdValue     = Math.Round(shortEmaValue - longEmaValue, 4);

            var rsiValue = _rsiIndicator.GetIndicatorValue(currentCandle).IndicatorValue;

            var ichimokuCloudValue = _ichimokuCloudIndicator.GetIndicatorValue(currentCandle);

            var ssa = ichimokuCloudValue.IchimokuCloud?.SenkouSpanAValue;
            var ssb = ichimokuCloudValue.IchimokuCloud?.SenkouSpanBValue;
            var ts  = ichimokuCloudValue.IchimokuCloud?.TenkanSenValue;
            var ks  = ichimokuCloudValue.IchimokuCloud?.KijunSenValue;

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"SSA: {ssa}; " +
                              $"SSB: {ssb}; " +
                              $"TS: {ts}; " +
                              $"KS: {ks}; " +
                              //$"LFSsa: {ichimokuCloudValue.IchimokuCloud?.SsaFuture.Last()}; " +
                              //$"LFSsb: {ichimokuCloudValue.IchimokuCloud?.SsbFuture.Last()}; " +
                              $"MACD: {macdValue}; " +
                              $"MinMaxMacd: {_maxOrMinMacd}; " +
                              $"RSI: {rsiValue}; " +
                              $"Close price: {Math.Round(currentCandle.ClosePrice, 4)};");

            // wait 1 hour
            if (_candleCount <= 60)
            {
                _candleCount++;
                _last5Macd.Enqueue(macdValue);
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue < 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (currentCandle.ClosePrice > ssa &&
                    currentCandle.ClosePrice > ssb &&
                    currentCandle.CandleType == CandleType.Green
                    //&& ts > ks
                    && currentCandle.ClosePrice > ts &&
                    currentCandle.ClosePrice > ks &&
                    macdValue > 0 &&
                    macdValue < 1 &&
                    _stopTrading == false &&
                    rsiValue >= 70 &&
                    _lastMacd < macdValue &&
                    _last5Macd.GetItems().All(a => a < macdValue)
                    )
                {
                    _lastMacd = macdValue;
                    _last5Macd.Enqueue(macdValue);
                    _lastTrend      = TrendDirection.Long;
                    _lastBuyPrice   = currentCandle.ClosePrice;
                    _lastClosePrice = currentCandle.ClosePrice;
                }
                else
                {
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (_delayCount < 10)
                {
                    Console.WriteLine($"DelayCount: {_delayCount}");
                    _delayCount++;
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }

                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage   = (decimal)0.985;
                var profitPercentage = (decimal)1.018;
                var diffPreviousMacd = Math.Abs(_maxOrMinMacd - macdValue);
                if (currentCandle.ClosePrice < _lastBuyPrice * stopPercentage)
                {
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _delayCount     = 1;
                    _lastTrend      = TrendDirection.Short;
                    _stopTrading    = true;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(_lastTrend));
                }

                if (
                    macdValue > 0 &&
                    //_lastMacd > 0 &&
                    _lastMacd < macdValue &&
                    //diffPreviousMacd > (decimal)0.2 &&
                    //rsiValue > 80 &&
                    (ssa * (decimal)1.005 >= currentCandle.ClosePrice ||
                     ssb * (decimal)1.005 >= currentCandle.ClosePrice) &&
                    currentCandle.ClosePrice > _lastBuyPrice * profitPercentage)
                {
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _delayCount     = 1;
                    _lastTrend      = TrendDirection.Short;
                    _stopTrading    = true;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(_lastTrend));
                }

                //if (
                //    macdValue > 0 &&
                //    _lastMacd > 0 &&
                //    _lastMacd < macdValue &&
                //    _lastClosePrice > currentCandle.ClosePrice &&
                //    //_lastClosePrice - currentCandle.ClosePrice > 2 &&
                //    rsiValue > 80 &&
                //    currentCandle.ClosePrice > _lastBuyPrice * profitPercentage)
                //{
                //    _last5Macd.Enqueue(macdValue);
                //    _lastMacd = macdValue;
                //    _delayCount = 1;
                //    _lastTrend = TrendDirection.Short;
                //    _stopTrading = true;
                //    _lastClosePrice = currentCandle.ClosePrice;
                //    return await Task.FromResult(_lastTrend);
                //}

                if (currentCandle.ClosePrice < ts &&
                    currentCandle.ClosePrice < ks &&
                    currentCandle.ClosePrice < ssa ||
                    currentCandle.ClosePrice < ssb)
                {
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    if (currentCandle.ClosePrice >= _lastBuyPrice * stopPercentage)
                    {
                        return(await Task.FromResult(TrendDirection.None));
                    }
                    _delayCount  = 1;
                    _lastTrend   = TrendDirection.Short;
                    _stopTrading = true;
                }
                else
                {
                    _last5Macd.Enqueue(macdValue);
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #10
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue  = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue   = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var macdValue      = Math.Round(shortEmaValue - longEmaValue, 4);
            var signalEmaValue = Math.Round(_signalEmaIndicator.GetIndicatorValue(macdValue).IndicatorValue, 4);
            var histogramValue = Math.Round(macdValue - signalEmaValue, 4);

            var ichimokuCloudValue = _ichimokuCloudIndicator.GetIndicatorValue(currentCandle);

            var ssa = ichimokuCloudValue.IchimokuCloud?.SenkouSpanAValue;
            var ssb = ichimokuCloudValue.IchimokuCloud?.SenkouSpanBValue;

            DepthModel depth = null;

            if (ichimokuCloudValue.IchimokuCloud != null)
            {
                depth = await _exchangeProvider.GetDepth(tradingPair);
            }

            var bid      = depth?.Bids.First(f => f.Quantity == depth.Bids.Max(b => b.Quantity));
            var ask      = depth?.Asks.First(f => f.Quantity == depth.Asks.Max(b => b.Quantity));
            var bidPrice = bid?.Price ?? Math.Round(currentCandle.ClosePrice, 4);
            var askPrice = ask?.Price ?? Math.Round(currentCandle.ClosePrice, 4);

            var bidPercentage = Math.Round((currentCandle.ClosePrice / bidPrice) * (decimal)100.0 - (decimal)100.0, 2);
            var askPercentage = Math.Round((currentCandle.ClosePrice / askPrice) * (decimal)100.0 - (decimal)100.0, 2);

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"MACD Value: {macdValue}; " +
                              $"SSA/SSB: {ssa}/{ssb}; " +
                              $"Bids price/qty/%: {bid}/{bidPercentage}%; " +
                              $"Asks price/qty/%: {ask}/{askPercentage}%; " +
                              $"Close price: {Math.Round(currentCandle.ClosePrice, 4)};");

            _volumenQueue.Enqueue(currentCandle.Volume);

            if (!_lastMacd.HasValue || _lastMacd == 0)
            {
                _lastMacd       = macdValue;
                _lastClosePrice = currentCandle.ClosePrice;
                _macdDirection  = macdValue < 0 ? MacdDirection.LessThanZero : MacdDirection.GreaterThanZero;
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastMacd < 0 && macdValue >= 0)
            {
                if (_macdSwitch)
                {
                    _maxMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (_lastMacd > 0 && macdValue <= 0)
            {
                if (_macdSwitch)
                {
                    _minMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (macdValue < 0 && macdValue < _minMacd)
            {
                _minMacd           = macdValue;
                _minMacdClosePrice = currentCandle.ClosePrice;
            }

            if (macdValue > 0 && macdValue > _maxMacd)
            {
                _maxMacd           = macdValue;
                _maxMacdClosePrice = currentCandle.ClosePrice;
            }

            // wait 0.5 hour
            if (_candleCount <= 30)
            {
                _candleCount++;
                if (macdValue < 0 && macdValue < _minWarmupMacd)
                {
                    _minWarmupMacd = macdValue;
                }
                Console.WriteLine($"Min warmup Macd: {_minWarmupMacd}");
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue > 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                //var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_stopTrading == false &&
                    macdValue < _options.BuyThreshold &&
                    currentCandle.ClosePrice > ssa &&
                    currentCandle.ClosePrice > ssb &&
                    currentCandle.CandleType == CandleType.Green
                    //&& diffPreviousMacd < -(decimal)0.2
                    && macdValue > _lastMacd &&
                    currentCandle.ClosePrice > _lastClosePrice)
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;

                    _profitEstimationRate = CheckProfitPrecentageAsync(depth, currentCandle.ClosePrice);
                    if (_profitEstimationRate < 0)
                    {
                        return(await Task.FromResult(TrendDirection.None));
                    }

                    _lastTrend    = TrendDirection.Long;
                    _maxOrMinMacd = 0;
                    _lastBuyPrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage   = 1 - _profitEstimationRate / (decimal)100.0;                  //1 - (_macdRate - (decimal)0.4) / 100; //(decimal) 0.97;
                var profitPercentage = 1 + (_profitEstimationRate + (decimal)0.4) / (decimal)100.0; //1 + (_macdRate + (decimal)0.4) / 100; //(decimal) 1.038;
                //var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_lastMacd > macdValue
                    //&& diffPreviousMacd > (decimal)1.0
                    && currentCandle.ClosePrice > _lastBuyPrice * profitPercentage ||
                    currentCandle.ClosePrice < _lastBuyPrice * stopPercentage)
                {
                    Console.WriteLine($"Stop percentage: {stopPercentage}; Profit percentage: {profitPercentage}");
                    _lastTrend      = TrendDirection.Short;
                    _maxOrMinMacd   = 0;
                    _stopTrading    = true;
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #11
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue  = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue   = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var emaDiffValue   = shortEmaValue - longEmaValue;
            var signalEmaValue = Math.Round(_signalEmaIndicator.GetIndicatorValue(emaDiffValue).IndicatorValue, 4);
            var macdValue      = Math.Round(emaDiffValue - signalEmaValue, 4);

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"MACD: {macdValue}; " +
                              $"PeekMACD: {_maxOrMinMacd}; " +
                              $"Close price: {currentCandle.ClosePrice}; ");

            if (!_lastMacd.HasValue)
            {
                _lastMacd = macdValue;
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue > 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                _lastMacd = macdValue;
                if (_stopTrading == false && macdValue < _options.BuyThreshold)
                {
                    _lastTrend    = TrendDirection.Long;
                    _maxOrMinMacd = 0;
                    _lastBuyPrice = currentCandle.ClosePrice;
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                _lastMacd = macdValue;
                if (_maxOrMinMacd > _options.SellThreshold &&
                    currentCandle.ClosePrice > _lastBuyPrice * (decimal)1.01)
                {
                    _lastTrend    = TrendDirection.Short;
                    _maxOrMinMacd = 0;
                    _stopTrading  = true;
                }
                else
                {
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }
Example #12
0
        public async Task <TrendDirection> CheckTrendAsync(string tradingPair, CandleModel currentCandle)
        {
            var shortEmaValue  = _shortEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var longEmaValue   = _longEmaIndicator.GetIndicatorValue(currentCandle).IndicatorValue;
            var macdValue      = Math.Round(shortEmaValue - longEmaValue, 4);
            var signalEmaValue = Math.Round(_signalEmaIndicator.GetIndicatorValue(macdValue).IndicatorValue, 4);
            var histogramValue = Math.Round(macdValue - signalEmaValue, 4);

            var depth = await _exchangeProvider.GetDepth(tradingPair);

            var bidsSum = depth.Bids.Sum(s => s.Quantity);
            var asksSum = depth.Asks.Sum(s => s.Quantity);

            Console.WriteLine($"DateTs: {currentCandle.StartDateTime:s}; " +
                              $"MACD Value/Hist.: {macdValue}/{histogramValue};\t" +
                              $"Bids price/qty/sum qty: {depth.Bids.First(f => f.Quantity == depth.Bids.Max(b => b.Quantity))}/{Math.Round(bidsSum,4)};\t" +
                              $"Asks price/qty/sum qty: {depth.Asks.First(f => f.Quantity == depth.Asks.Max(b => b.Quantity))}/{Math.Round(asksSum, 4)};\t" +
                              $"Close price: {currentCandle.ClosePrice};");

            _volumenQueue.Enqueue(currentCandle.Volume);

            if (!_lastMacd.HasValue || _lastMacd == 0)
            {
                _lastMacd       = macdValue;
                _lastClosePrice = currentCandle.ClosePrice;
                _macdDirection  = macdValue < 0 ? MacdDirection.LessThanZero : MacdDirection.GreaterThanZero;
                return(await Task.FromResult(TrendDirection.None));
            }

            //CreateMacdStatistics(macdValue, currentCandle);

            if (_lastMacd < 0 && macdValue >= 0)
            {
                if (_macdSwitch)
                {
                    _maxMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (_lastMacd > 0 && macdValue <= 0)
            {
                if (_macdSwitch)
                {
                    _minMacd = 0;
                }
                else
                {
                    _macdSwitch = true;
                }
            }

            if (macdValue < 0 && macdValue < _minMacd)
            {
                _minMacd           = macdValue;
                _minMacdClosePrice = currentCandle.ClosePrice;
            }

            if (macdValue > 0 && macdValue > _maxMacd)
            {
                _maxMacd           = macdValue;
                _maxMacdClosePrice = currentCandle.ClosePrice;
            }

            // wait 0.5 hour
            if (_candleCount <= 30)
            {
                _candleCount++;
                if (macdValue < 0 && macdValue < _minWarmupMacd)
                {
                    _minWarmupMacd = macdValue;
                }
                Console.WriteLine($"Min warmup Macd: {_minWarmupMacd}");
                return(await Task.FromResult(TrendDirection.None));
            }

            if (_lastTrend == TrendDirection.Short)
            {
                if (macdValue > 0 && _stopTrading)
                {
                    _stopTrading = false;
                }

                if (macdValue < 0 && macdValue < _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                //var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_stopTrading == false &&
                    macdValue < _options.BuyThreshold
                    //&& diffPreviousMacd < -(decimal)0.2
                    && macdValue > _lastMacd &&
                    currentCandle.ClosePrice > _lastClosePrice)
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;

                    _profitEstimationRate = CheckProfitPrecentageAsync(depth, currentCandle.ClosePrice);
                    if (_profitEstimationRate < 0)
                    {
                        return(await Task.FromResult(TrendDirection.None));
                    }

                    _lastTrend    = TrendDirection.Long;
                    _maxOrMinMacd = 0;
                    _lastBuyPrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }
            else if (_lastTrend == TrendDirection.Long)
            {
                if (macdValue > 0 && macdValue > _lastMacd)
                {
                    _maxOrMinMacd = macdValue;
                }

                if (macdValue < 0)
                {
                    _maxOrMinMacd = 0;
                }

                var stopPercentage   = 1 - _profitEstimationRate / (decimal)100.0;                  //1 - (_macdRate - (decimal)0.4) / 100; //(decimal) 0.97;
                var profitPercentage = 1 + (_profitEstimationRate + (decimal)0.4) / (decimal)100.0; //1 + (_macdRate + (decimal)0.4) / 100; //(decimal) 1.038;
                //var diffPreviousMacd = _maxOrMinMacd - macdValue;
                if (_lastMacd > macdValue
                    //&& diffPreviousMacd > (decimal)1.0
                    && currentCandle.ClosePrice > _lastBuyPrice * profitPercentage ||
                    currentCandle.ClosePrice < _lastBuyPrice * stopPercentage)
                {
                    Console.WriteLine($"Stop percentage: {stopPercentage}; Profit percentage: {profitPercentage}");
                    _lastTrend      = TrendDirection.Short;
                    _maxOrMinMacd   = 0;
                    _stopTrading    = true;
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                }
                else
                {
                    _lastMacd       = macdValue;
                    _lastClosePrice = currentCandle.ClosePrice;
                    return(await Task.FromResult(TrendDirection.None));
                }
            }

            return(await Task.FromResult(_lastTrend));
        }