Esempio n. 1
0
        private List <OHLCKandle> GetSmoothData(List <OHLCKandle> kandles, int lookback, string smoothing)
        {
            PineScriptFunction fn = new PineScriptFunction();

            if (smoothing.ToUpper() == "DEMA")
            {
                kandles = fn.dema(kandles, lookback);
            }
            else
            {
                kandles = fn.smma(kandles, lookback);
            }

            return(kandles);
        }
Esempio n. 2
0
        /// <summary>
        /// Computes Bollinger Bands
        /// </summary>
        /// <param name="kandles">The input kandles over which bollinger bands will be calculated</param>
        /// <param name="bollCrossDistance">The maximum distance till which cross with lower and upper sma is valid</param>
        public void Calculate(List<OHLCKandle> kandles, int bollCrossDistance)
        {
            PineScriptFunction fn = new PineScriptFunction();

            var close = kandles.Last().Close;

            var openseries = kandles.Select(x => (decimal)x.Open).ToList();

            var closeseries = kandles.Select(x => (decimal)x.Close).ToList();

            this.BollingerData = fn.bollinger(kandles, 20);

            this.BollingerUpper = BollingerData.Last().High;

            this.BollingerMiddle = BollingerData.Last().Close;

            this.BollingerLower = BollingerData.Last().Low;

            this.BollingerUpperPercentage = Math.Round((100 * (this.BollingerUpper - close) / this.BollingerUpper), 3);

            this.BollingerMiddlePercentage = Math.Round((100 * (close - this.BollingerMiddle) / this.BollingerMiddle), 3);

            this.BollingerLowerPercentage = Math.Round((100 * (close - this.BollingerLower) / close), 3);

            var pricecrosstopband = fn.crossunder(openseries, BollingerData.Select(x => x.High).ToList());

            var pricecrossbottomband = fn.crossover(closeseries, BollingerData.Select(x => x.Low).ToList());

            var pricecrossmiddleband1 = fn.crossunder(openseries, BollingerData.Select(x => x.Close).ToList());

            var pricecrossmiddleband2 = fn.crossover(closeseries, BollingerData.Select(x => x.Close).ToList());

            this.BollTopCrossed = pricecrosstopband.Skip(pricecrosstopband.Count - bollCrossDistance).Take(bollCrossDistance).Contains(true);

            this.BollBottomCrossed = pricecrossbottomband.Skip(pricecrossbottomband.Count - bollCrossDistance).Take(bollCrossDistance).Contains(true);

            this.BollMiddleCrossed = pricecrossmiddleband1.Skip(pricecrossmiddleband1.Count - 2).Take(2).Contains(true)||
                                     pricecrossmiddleband2.Skip(pricecrossmiddleband2.Count - 2).Take(2).Contains(true);
        }
        public void RunStrategy(List <OHLCKandle> inputkandles, RobotInput robotInput, ref StrategyData strategyData, ref SimplePosition currentPosition)
        {
            PineScriptFunction fn = new PineScriptFunction();

            //convert to higher timeframe
            var largekandles = fn.converttohighertimeframe(inputkandles, KandleMultiplier);//3

            if (Smoothing.ToUpper() == "DEMA")
            {
                //higher timeframe candles with dema values
                largekandles = fn.dema(largekandles, 8);

                //lower timeframe candles with dema values
                inputkandles = fn.dema(inputkandles, 8);
            }
            else
            {
                //higher timeframe candles with smma values
                largekandles = fn.smma(largekandles, 8);

                //lower timeframe candles with smma values
                inputkandles = fn.smma(inputkandles, 8);
            }


            var closeseriesmma = inputkandles.Select(x => x.Close).ToList();


            //map higher timeframe values to lower timeframe values
            var altkandles = fn.superimposekandles(largekandles, inputkandles);

            var closeSeriesAlt = altkandles.Select(x => x.Close).ToList();

            var openSeriesAlt = altkandles.Select(x => x.Open).ToList();


            //trend and mood calculation-
            strategyData.trend = closeSeriesAlt.Last() > openSeriesAlt.Last() ? "BULLISH" : "BEARISH";

            strategyData.mood = closeseriesmma.Last() > openSeriesAlt.Last() ? "BULLISH" : "BEARISH";


            //buy sell signal
            var xlong = fn.crossover(closeSeriesAlt, openSeriesAlt);

            var xshort = fn.crossunder(closeSeriesAlt, openSeriesAlt);

            strategyData.isBuy = xlong.Last();

            strategyData.isSell = xshort.Last();


            //historical data
            strategyData.histdata = "";
            for (int i = 0; i < xlong.Count; i++)
            {
                if (xlong[i])
                {
                    strategyData.histdata += " B" + (xlong.Count - i - 1).ToString();
                }
                else if (xshort[i])
                {
                    strategyData.histdata += " S" + (xlong.Count - i - 1).ToString();
                }
                else
                {
                    // meh :\
                }
            }

            MakeBuySellDecision(ref strategyData, ref currentPosition, robotInput);

            if (strategyData.Output != StrategyOutput.None)
            {
                ResetCounters();

                strategyData.profitFactor = 1m;
            }
        }
Esempio n. 4
0
        public void RunStrategy(RobotInput robotInput, SimplePosition currentPosition, ref StrategyData strategyData)
        {
            List <OHLCKandle> inputkandles = strategyData.kandles.Select(x => new OHLCKandle
            {
                Close     = x.Close,
                CloseTime = x.CloseTime,
                High      = x.High,
                Low       = x.Low,
                Open      = x.Open,
                OpenTime  = x.OpenTime
            }).ToList();

            PineScriptFunction fn = new PineScriptFunction();

            //convert to higher timeframe
            var largekandles = fn.converttohighertimeframe(inputkandles, KandleMultiplier);//3

            //higher timeframe candles with smoothened values
            largekandles = GetSmoothData(largekandles, 8, Smoothing);

            //lower timeframe candles with smoothened values
            inputkandles = GetSmoothData(inputkandles, 8, Smoothing);

            var inputcloseseriesmoothed = inputkandles.Select(x => x.Close).ToList();

            //map higher timeframe values to lower timeframe values
            var altkandles = fn.superimposekandles(largekandles, inputkandles);

            var closeSeriesAlt = altkandles.Select(x => x.Close).ToList();

            var openSeriesAlt = altkandles.Select(x => x.Open).ToList();

            //end map higher timeframe values to lower timeframe values

            //trend and mood calculation-
            strategyData.trend = closeSeriesAlt.Last() > openSeriesAlt.Last() ? "BULLISH" : "BEARISH";

            strategyData.mood = inputcloseseriesmoothed.Last() > openSeriesAlt.Last() ? "BULLISH" : "BEARISH";

            //start buy sell signal
            var xlong = fn.crossover(closeSeriesAlt, openSeriesAlt);

            var xshort = fn.crossunder(closeSeriesAlt, openSeriesAlt);

            strategyData.isBuy = xlong.Last();

            strategyData.isSell = xshort.Last();
            //end buy sell signal

            UpdateBollingerData(ref strategyData);

            UpdateSignalData(ref strategyData, xlong, xshort);

            strategyDecision.Decide(ref strategyData, currentPosition, robotInput);

            if (strategyData.Decision != StrategyDecision.None)
            {
                strategyDecision.ResetCounters();

                strategyData.profitFactor = 1m;
            }
        }
Esempio n. 5
0
        public void RunStrategy(List <OHLCKandle> inputkandles, ref bool isBuy, ref bool isSell, ref string trend, ref string mood, ref string histdata)
        {
            PineScriptFunction fn = new PineScriptFunction();

            var closevalues = inputkandles.Select(x => x.Close).ToList();

            var highvalues = inputkandles.Select(x => x.High).ToList();

            var lowvalues = inputkandles.Select(x => x.Low).ToList();

            //start mood and trend code
            var ema0 = fn.ema(closevalues, 13);

            trend = fn.trend(ema0);

            var _out = fn.sma(closevalues, 8);

            if (fn.bearish(closevalues, _out))
            {
                mood = "BEARISH";
            }
            else if (fn.bullish(closevalues, _out))
            {
                mood = "BULLISH";
            }
            else
            {
                //meh :\
            }
            //end mood and trend code


            //start signal code

            var vh1 = fn.ema(fn.highest(fn.avgseries(lowvalues, closevalues), 5), 5);

            var vl1 = fn.ema(fn.lowest(fn.avgseries(highvalues, closevalues), 8), 8);

            var e_ema1 = fn.ema(closevalues, 1);

            var e_ema2 = fn.ema(e_ema1, 1);

            var e_ema3 = fn.ema(e_ema2, 1);

            var tema = fn.tema(e_ema1, e_ema2, e_ema3);

            var e_e1 = fn.ema(closevalues, 8);

            var e_e2 = fn.ema(e_e1, 5);

            var dema = fn.dema(e_e1, e_e2);

            var signal = fn.signal(tema, dema, vh1, vl1);

            var _isBuy = fn.and(fn.and(fn.greaterthan(tema, dema), fn.greaterthan(signal, lowvalues)), fn.signalcomparebuy(signal));

            var _isSell = fn.and(fn.and(fn.lessthan(tema, dema), fn.lessthan(signal, highvalues)), fn.signalcomparesell(signal));

            isBuy = _isBuy.Last();

            isSell = _isSell.Last();

            //end signal code

            for (int i = _isBuy.Count - 10; i <= _isBuy.Count - 2; ++i)
            {
                if (_isBuy[i])
                {
                    histdata += "B\t";
                }
                else if (_isSell[i])
                {
                    histdata += "S\t";
                }
                else
                {
                    histdata += "0\t";
                }
            }
        }