Exemple #1
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            List <decimal?> rsi  = candles.Rsi(5);
            StochItem       fast = candles.StochFast();
            BbandItem       bb   = candles.Bbands(20);

            List <decimal?> adx     = candles.Adx(14);
            List <decimal?> plusDi  = candles.PlusDi(14);
            List <decimal?> minusDi = candles.MinusDi(14);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (rsi[i] < 22 && fast.K[i] < 25 && fast.D[i - 1] > fast.K[i - 1] && fast.D[i] - fast.K[i] < 0.3m)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else if (rsi[i] > 70 && fast.K[i] > 50)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
            }

            return(result);
        }
Exemple #2
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();
            StochItem stoch = candles.Stoch(10);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i < 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else
                {
                    if (stoch.K[i] > 20 && stoch.K[i - 1] < 20 || stoch.D[i] > 20 && stoch.D[i - 1] < 20)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else if (stoch.K[i] < 80 && stoch.K[i - 1] > 80 || stoch.D[i] < 80 && stoch.D[i - 1] > 80)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
            }

            return(result);
        }
Exemple #3
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            List <decimal?> rsi      = candles.Rsi(14);
            BbandItem       bbands   = candles.Bbands(20);
            StochItem       stoch    = candles.Stoch();
            StochItem       stochRsi = candles.StochRsi(fastKPeriod: 3);
            List <decimal>  close    = candles.Close();
            List <decimal>  open     = candles.Open();

            for (int i = 0; i < candles.Count(); i++)
            {
                if (rsi[i] > 70 && stoch.K[i] > 80 && close[i] > open[i] && stochRsi.K[i] > 80 && stoch.K[i] >= stoch.D[i] && stochRsi.K[i] >= stochRsi.D[i] && close[i] > bbands.UpperBand[i] + (bbands.UpperBand[i] - bbands.MiddleBand[i]) * 0.05m)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else if (rsi[i] < 30 && stoch.K[i] < 20 && close[i] < open[i] && stochRsi.K[i] < 20 && stoch.K[i] <= stoch.D[i] && stochRsi.K[i] <= stochRsi.D[i] && close[i] < bbands.LowerBand[i] - (bbands.MiddleBand[i] - bbands.LowerBand[i]) * 0.05m)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }
Exemple #4
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            MacdItem  macd  = candles.Macd(7, 12, 9);
            StochItem stoch = candles.StochRsi(9, CandleVariableCode.CLOSE, 3, 3, TicTacTec.TA.Library.Core.MAType.Sma);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (macd.Macd[i] - macd.Signal[i] < 0 && stoch.K[i] > 70 && stoch.K[i] < stoch.D[i])
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else if (macd.Macd[i] - macd.Signal[i] > 0 && stoch.K[i] < 30 && stoch.K[i] > stoch.D[i])
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }
Exemple #5
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            StochItem       stoch    = candles.Stoch(13);
            List <decimal?> adx      = candles.Adx(14);
            List <decimal?> bearBull = candles.BearBull();

            for (int i = 0; i < candles.Count(); i++)
            {
                if (adx[i] > 50 && (stoch.K[i] > 90 || stoch.D[i] > 90) && bearBull[i] == -1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else if (adx[i] < 20 && (stoch.K[i] < 10 || stoch.D[i] < 10) && bearBull[i] == 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            StochItem       stoch  = candles.Stoch();
            List <decimal?> sma200 = candles.Sma(200);

            List <decimal> closes = candles.Select(x => x.Close).ToList();

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i < 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else
                {
                    if (sma200[i] < closes[i] &&
                        stoch.K[i - 1] <= stoch.D[i - 1] &&
                        stoch.K[i] > stoch.D[i] &&
                        stoch.D[i - 1] < 20 &&
                        stoch.K[i - 1] < 20)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else if (stoch.K[i - 1] <= stoch.D[i - 1] &&
                             stoch.K[i] > stoch.D[i] &&
                             stoch.D[i - 1] > 80 &&
                             stoch.K[i - 1] > 80)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
            }

            return(result);
        }
Exemple #7
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            SmaStochRsiPreset preset = null;

            if (!string.IsNullOrWhiteSpace(Preset))
            {
                preset = JsonConvert.DeserializeObject <SmaStochRsiPreset>(Preset);
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();
            List <decimal> price = candles.Select(x => x.Close).ToList();

            StochItem       stoch = candles.Stoch(preset?.Stoch ?? 8);
            List <decimal?> sma   = candles.Sma(preset?.Sma ?? 150);
            List <decimal?> rsi   = candles.Rsi(preset?.Rsi ?? 3);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i < 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else
                {
                    if (price[i] > sma[i] && stoch.K[i] > 70 && rsi[i] < 20 && stoch.K[i] > stoch.D[i])
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else if (price[i] < sma[i] && stoch.K[i] > 70 && rsi[i] > 80 && stoch.K[i] < stoch.D[i])
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
            }

            return(result);
        }
Exemple #8
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            List <decimal?> sma    = candles.Sma(100);
            List <decimal>  closes = candles.Close();
            List <decimal?> adx    = candles.Adx();
            List <decimal?> tema   = candles.Tema(4);
            List <decimal?> mfi    = candles.Mfi(14);

            List <decimal?> cci         = candles.Cci(5);
            StochItem       stoch       = candles.StochFast();
            List <decimal?> middleBands = candles.Bbands().MiddleBand;

            List <decimal?> fishers = candles.Fisher();

            for (int i = 0; i < candles.Count(); i++)
            {
                if (closes[i] < sma[i] && cci[i] < -100 && fishers[i] < 0 && adx[i] > 20 && mfi[i] < 30 && tema[i] <= middleBands[i])
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else if (fishers[i] == 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
            }

            return(result);
        }
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            WvfExtendedPreset preset = null;

            if (!string.IsNullOrWhiteSpace(Preset))
            {
                preset = JsonConvert.DeserializeObject <WvfExtendedPreset>(Preset);
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();
            //List<ICandle> candles = candleItems.ToList();

            List <decimal> close = candles.Select(x => x.Close).ToList();
            List <decimal> high  = candles.Select(x => x.High).ToList();
            List <decimal> low   = candles.Select(x => x.Low).ToList();
            List <decimal> open  = candles.Select(x => x.Open).ToList();

            List <decimal?> rsi      = candles.Rsi(preset?.Rsi ?? 20);
            List <decimal?> ema      = rsi.Ema(preset?.Ema ?? 10);
            StochItem       stochRsi = candles.StochRsi(preset?.StochRsi ?? 14);

            List <decimal>  wvfs         = new List <decimal>();
            List <decimal>  standardDevs = new List <decimal>();
            List <decimal>  rangeHighs   = new List <decimal>();
            List <decimal?> upperRanges  = new List <decimal?>();

            int     pd   = 22;       // LookBack Period Standard Deviation High
            int     bbl  = 20;       // Bollinger Band Length
            decimal mult = 2.0m;     // Bollinger Band Standard Deviation Up
            int     lb   = 50;       // Look Back Period Percentile High
            decimal ph   = .85m;     // Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%
            decimal pl   = 1.01m;    // Lowest Percentile - 1.10=90%, 1.05=95%, 1.01=99%
            int     ltLb = 40;       // Long-Term Look Back Current Bar Has To Close Below This Value OR Medium Term")
            int     mtLb = 14;       // Medium-Term Look Back Current Bar Has To Close Below This Value OR Long Term")
            int     str  = 3;        // Entry Price Action Strength--Close > X Bars Back")

            for (int i = 0; i < candles.Count(); i++)
            {
                int itemsToPick      = i < pd - 1 ? i + 1 : pd;
                int indexToStartFrom = i < pd - 1 ? 0 : i - pd;

                decimal highestClose = candles.Skip(indexToStartFrom).Take(itemsToPick).Select(x => x.Close).Max();
                decimal wvf          = (highestClose - candles.ElementAt(i).Low) / highestClose * 100;

                // Calculate the WVF
                wvfs.Add(wvf);

                decimal standardDev = 0;

                if (wvfs.Count > 1)
                {
                    if (wvfs.Count < bbl)
                    {
                        standardDev = mult * GetStandardDeviation(wvfs.Take(bbl).ToList());
                    }
                    else
                    {
                        standardDev = mult * GetStandardDeviation(wvfs.Skip(wvfs.Count - bbl).Take(bbl).ToList());
                    }
                }

                // Also calculate the standard deviation.
                standardDevs.Add(standardDev);
            }

            List <decimal?> midLines = wvfs.Sma(bbl);

            for (int i = 0; i < candles.Count(); i++)
            {
                decimal?upperBand = midLines[i] + standardDevs[i];

                if (upperBand.HasValue)
                {
                    upperRanges.Add(upperBand.Value);
                }
                else
                {
                    upperRanges.Add(null);
                }

                int itemsToPickRange      = i < lb - 1 ? i + 1 : lb;
                int indexToStartFromRange = i < lb - 1 ? 0 : i - lb;

                decimal rangeHigh = wvfs.Skip(indexToStartFromRange).Take(itemsToPickRange).Max() * ph;
                rangeHighs.Add(rangeHigh);
            }

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i < ltLb)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else
                {
                    //Filtered Bar Criteria
                    bool upRange     = low[i] > low[i - 1] && close[i] > high[i - 1];
                    bool upRangeAggr = close[i] > close[i - 1] && close[i] > open[i - 1];

                    //Filtered Criteria
                    bool filtered     = (wvfs[i - 1] >= upperRanges[i - 1] || wvfs[i - 1] >= rangeHighs[i - 1]) && wvfs[i] < upperRanges[i] && wvfs[i] < rangeHighs[i];
                    bool filteredAggr = (wvfs[i - 1] >= upperRanges[i - 1] || wvfs[i - 1] >= rangeHighs[i - 1]) && !(wvfs[i] < upperRanges[i] && wvfs[i] < rangeHighs[i]);

                    bool filteredAlert   = upRange && close[i] > close[i - str] && (close[i] < close[i - ltLb] || close[i] < close[i - mtLb]) && filtered;
                    bool aggressiveAlert = upRangeAggr && close[i] > close[i - str] && (close[i] < close[i - ltLb] || close[i] < close[i - mtLb]) && filteredAggr;

                    if ((filteredAlert || aggressiveAlert) && rsi[i] > ema[i] && rsi[i - 1] < ema[i - 1])
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else if (stochRsi.K[i] > 80 && stochRsi.K[i] > stochRsi.D[i] && stochRsi.K[i - 1] < stochRsi.D[i - 1])
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
            }

            return(result);
        }
Exemple #10
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            WvfEmaCrossoverPreset preset = null;

            if (!string.IsNullOrWhiteSpace(Preset))
            {
                preset = JsonConvert.DeserializeObject <WvfEmaCrossoverPreset>(Preset);
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            List <decimal> close = candles.Select(x => x.Close).ToList();
            List <decimal> high  = candles.Select(x => x.High).ToList();
            List <decimal> low   = candles.Select(x => x.Low).ToList();
            List <decimal> open  = candles.Select(x => x.Open).ToList();

            List <decimal?> rsi      = candles.Rsi(preset?.Rsi ?? 20);
            List <decimal?> ema      = rsi.Ema(preset?.Ema ?? 10);
            StochItem       stochRsi = candles.StochRsi(preset?.StochRsi ?? 14);

            List <decimal>  wvfs         = new List <decimal>();
            List <decimal>  standardDevs = new List <decimal>();
            List <decimal>  rangeHighs   = new List <decimal>();
            List <decimal?> upperRanges  = new List <decimal?>();

            int     pd   = 22;       // LookBack Period Standard Deviation High
            int     bbl  = 20;       // Bollinger Band Length
            decimal mult = 2.0m;     // Bollinger Band Standard Deviation Up
            int     lb   = 50;       // Look Back Period Percentile High
            decimal ph   = .85m;     // Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%

            List <decimal?> shorts = candles.Ema(6);
            List <decimal?> longs  = candles.Ema(3);

            for (int i = 0; i < candles.Count(); i++)
            {
                int itemsToPick      = i < pd - 1 ? i + 1 : pd;
                int indexToStartFrom = i < pd - 1 ? 0 : i - pd;

                decimal highestClose = candles.Skip(indexToStartFrom).Take(itemsToPick).Select(x => x.Close).Max();
                decimal wvf          = (highestClose - candles.ElementAt(i).Low) / highestClose * 100;

                // Calculate the WVF
                wvfs.Add(wvf);

                decimal standardDev = 0;

                if (wvfs.Count > 1)
                {
                    if (wvfs.Count < bbl)
                    {
                        standardDev = mult * GetStandardDeviation(wvfs.Take(bbl).ToList());
                    }
                    else
                    {
                        standardDev = mult * GetStandardDeviation(wvfs.Skip(wvfs.Count - bbl).Take(bbl).ToList());
                    }
                }

                // Also calculate the standard deviation.
                standardDevs.Add(standardDev);
            }

            List <decimal?> midLines = wvfs.Sma(bbl);

            for (int i = 0; i < candles.Count(); i++)
            {
                decimal?upperBand = midLines[i] + standardDevs[i];

                if (upperBand.HasValue)
                {
                    upperRanges.Add(upperBand.Value);
                }
                else
                {
                    upperRanges.Add(null);
                }

                int itemsToPickRange      = i < lb - 1 ? i + 1 : lb;
                int indexToStartFromRange = i < lb - 1 ? 0 : i - lb;

                decimal rangeHigh = wvfs.Skip(indexToStartFromRange).Take(itemsToPickRange).Max() * ph;
                rangeHighs.Add(rangeHigh);
            }

            for (int i = 0; i < candles.Count(); i++)
            {
                double diff = Convert.ToDouble(100 * (shorts[i] - longs[i]) / ((shorts[i] + longs[i]) / 2));

                if (diff > 1.5 && (wvfs[i] >= upperRanges[i] || wvfs[i] >= rangeHighs[i] && rsi[i] > ema[i] && rsi[i - 1] < ema[i - 1]))
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else if (diff <= -0.1 && stochRsi.K[i] > 80 && stochRsi.K[i] > stochRsi.D[i] && stochRsi.K[i - 1] < stochRsi.D[i - 1])
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            StochItem       stoch    = candles.Stoch(14);
            List <decimal?> emaShort = candles.Ema(5);
            List <decimal?> emaLong  = candles.Ema(10);
            List <decimal?> rsi      = candles.Rsi(14);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i < 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else
                {
                    decimal?slowk1 = stoch.K[i];
                    decimal?slowkp = stoch.K[i - 1];
                    decimal?slowd1 = stoch.D[i];
                    decimal?slowdp = stoch.D[i - 1];

                    bool pointedUp = false, pointedDown = false, kUp = false, dUp = false;

                    if (slowkp < slowk1)
                    {
                        kUp = true;
                    }

                    if (slowdp < slowd1)
                    {
                        dUp = true;
                    }

                    if (slowkp < 80 && slowdp < 80 && kUp && dUp)
                    {
                        pointedUp = true;
                    }

                    if (slowkp > 20 && slowdp > 20 && !kUp && !dUp)
                    {
                        pointedDown = true;
                    }

                    if (emaShort[i] >= emaLong[i] && emaShort[i - 1] < emaLong[i] && rsi[i] > 50 && pointedUp)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else if (emaShort[i] <= emaLong[i] && emaShort[i - 1] > emaLong[i] && rsi[i] < 50 && pointedDown)
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
            }

            return(result);
        }
Exemple #12
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            SarStochPreset preset = null;

            if (!string.IsNullOrWhiteSpace(Preset))
            {
                preset = JsonConvert.DeserializeObject <SarStochPreset>(Preset);
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            StochItem       stoch     = candles.Stoch(preset?.Stoch ?? 13);
            StochItem       stochFast = candles.StochFast(preset?.StochFast ?? 13);
            List <decimal?> sar       = candles.Sar(preset?.SarAccelerationFactor ?? 3);

            List <decimal> highs  = candles.Select(x => x.High).ToList();
            List <decimal> lows   = candles.Select(x => x.Low).ToList();
            List <decimal> closes = candles.Select(x => x.Close).ToList();
            List <decimal> opens  = candles.Select(x => x.Open).ToList();

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i > 2)
                {
                    decimal?currentSar = sar[i];
                    decimal?priorSar   = sar[i - 1];
                    decimal lastHigh   = highs[i];
                    decimal lastLow    = lows[i];
                    decimal lastOpen   = opens[i];
                    decimal lastClose  = closes[i];
                    decimal priorHigh  = highs[i - 1];
                    decimal priorLow   = lows[i - 1];
                    decimal priorOpen  = opens[i - 1];
                    decimal priorClose = closes[i - 1];
                    decimal prevOpen   = opens[i - 2];
                    decimal prevClose  = closes[i - 2];

                    bool below            = currentSar < lastLow;
                    bool above            = currentSar > lastHigh;
                    bool redCandle        = lastOpen < lastClose;
                    bool greenCandle      = lastOpen > lastClose;
                    bool priorBelow       = priorSar < priorLow;
                    bool priorAbove       = priorSar > priorHigh;
                    bool priorRedCandle   = priorOpen < priorClose;
                    bool priorGreenCandle = priorOpen > priorClose;
                    bool prevRedCandle    = prevOpen < prevClose;
                    bool prevGreenCandle  = prevOpen > prevClose;

                    priorRedCandle   = prevRedCandle || priorRedCandle;
                    priorGreenCandle = prevGreenCandle || priorGreenCandle;

                    int fsar = 0;

                    if (priorAbove && priorRedCandle && below && greenCandle)
                    {
                        fsar = 1;
                    }
                    else if (priorBelow && priorGreenCandle && above && redCandle)
                    {
                        fsar = -1;
                    }

                    if (fsar == -1 && (stoch.K[i] > 90 || stoch.D[i] > 90 || stochFast.K[i] > 90 || stochFast.D[i] > 90))
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                    }
                    else if (fsar == 1 && (stoch.K[i] < 10 || stoch.D[i] < 10 || stochFast.K[i] < 10 || stochFast.D[i] < 10))
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                    }
                    else
                    {
                        result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                    }
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }
Exemple #13
0
        public override IEnumerable <(ICandle, ITradingAdviceCode)> AllForecasts(IEnumerable <ICandle> candles)
        {
            if (candles.Count() < MinNumberOfCandles)
            {
                throw new Exception("Number of candles less then expected");
            }

            RedWeddingPreset preset = null;

            if (!string.IsNullOrWhiteSpace(Preset))
            {
                preset = JsonConvert.DeserializeObject <RedWeddingPreset>(Preset);
            }

            List <(ICandle, ITradingAdviceCode)> result = new List <(ICandle, ITradingAdviceCode)>();

            decimal fastEnd   = 0.666m;
            int     smaPeriod = preset?.Sma ?? 6;

            List <decimal?> open  = candles.Open().Sma(smaPeriod);
            List <decimal?> close = candles.Close().Sma(smaPeriod);
            List <decimal?> high  = candles.High().Sma(smaPeriod);
            List <decimal?> low   = candles.Low().Sma(smaPeriod);

            List <decimal> closes = candles.Close();

            // Calculate the vClose
            List <decimal?> vClose = new List <decimal?>();

            for (int i = 0; i < open.Count; i++)
            {
                if (open[i].HasValue && close[i].HasValue && low[i].HasValue && high[i].HasValue)
                {
                    vClose.Add((open[i].Value + close[i].Value + low[i].Value + high[i].Value) / 4);
                }
                else
                {
                    vClose.Add(null);
                }
            }

            // Calculate the vOpen
            decimal         smooth        = fastEnd * fastEnd;
            List <decimal?> vOpen         = new List <decimal?>();
            List <decimal?> shiftedvClose = new List <decimal?> {
                null
            };

            foreach (decimal?item in vClose)
            {
                if (item != vClose.Last())
                {
                    shiftedvClose.Add(item);
                }
            }

            for (int i = 0; i < vClose.Count; i++)
            {
                if (shiftedvClose[i] != null)
                {
                    if (vClose[i] == null)
                    {
                        vOpen.Add(shiftedvClose[i]);
                    }
                    else if (vOpen[i - 1] == null)
                    {
                        vOpen.Add(shiftedvClose[i]);
                    }
                    else
                    {
                        vOpen.Add(vOpen[i - 1] + smooth * (shiftedvClose[i] - vOpen[i - 1]));
                    }
                }
            }

            List <decimal?> snow_high = new List <decimal?>();

            for (int i = 0; i < vClose.Count; i++)
            {
                if (high[i].HasValue && vClose[i].HasValue && vOpen[i].HasValue)
                {
                    snow_high.Add(Math.Max(high[i].Value, Math.Max(vClose[i].Value, vOpen[i].Value)));
                }
                else
                {
                    snow_high.Add(null);
                }
            }

            List <decimal?> snow_low = new List <decimal?>();

            for (int i = 0; i < vClose.Count; i++)
            {
                if (low[i].HasValue && vClose[i].HasValue && vOpen[i].HasValue)
                {
                    snow_low.Add(Math.Min(low[i].Value, Math.Min(vClose[i].Value, vOpen[i].Value)));
                }
                else
                {
                    snow_low.Add(null);
                }
            }

            List <decimal?> long_sma  = vClose.Sma(10);
            List <decimal?> short_sma = vClose.Sma(3);
            StochItem       stoch     = candles.Stoch();
            List <decimal?> fish      = candles.Fisher();

            List <bool> sma_crossover  = short_sma.Crossover(long_sma);
            List <bool> sma_crossunder = short_sma.Crossunder(long_sma);
            List <bool> snow_cross     = vClose.Crossunder(vOpen);

            List <bool> stoch_cross  = stoch.K.Crossunder(80);
            List <bool> stoch_cross2 = stoch.K.Crossunder(stoch.D);

            for (int i = 0; i < candles.Count(); i++)
            {
                if (i <= 1)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
                else if (fish[i] >= fish[i - 1] && closes[i] < snow_high[i] && sma_crossover[i])
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.BUY));
                }
                else if (fish[i] < fish[i - 1] && fish[i - 1] >= fish[i - 2] || sma_crossunder[i] || snow_cross[i] || stoch_cross[i] || stoch_cross2[i] && stoch.K[i - 1] > 80)
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.SELL));
                }
                else
                {
                    result.Add((candles.ElementAt(i), TradingAdviceCode.HOLD));
                }
            }

            return(result);
        }