示例#1
0
        public static Pine IIF(PineBool pineBool, double pineTrue, double pineFalse)
        {
            var bl = pineBool;
            var tr = pineTrue;
            var fl = pineFalse;

            return(bl.Select(x => x ? tr : fl).ToPine());
        }
示例#2
0
        public static IEnumerable <T> ZipEnd <T>(PineBool first, PineBool second, Func <bool, bool, T> result)
        {
            var fR = ((IEnumerable <bool>)first).Reverse();
            var sR = ((IEnumerable <bool>)second).Reverse();

            var fs = fR.Zip(sR, (f, s) => (f, s));

            return(fs.Reverse().Select(x => result(x.f, x.s)));
        }
示例#3
0
        public static Pine IIF(PineBool pineBool, double pineTrue, Pine pineFalse)
        {
            var bl = ((IEnumerable <bool>)pineBool).Reverse();
            var tr = pineTrue;
            var fl = ((IEnumerable <double>)pineFalse).Reverse();

            var bf = bl.Zip(fl, (b, f) => (b, f));

            return(bf.Reverse().Select(x => x.b ? tr : x.f).ToPine());
        }
示例#4
0
        public static Pine IIF(PineBool pineBool, Pine pineTrue, double pineFalse)
        {
            var bl = ((IEnumerable <bool>)pineBool).Reverse();
            var tr = ((IEnumerable <double>)pineTrue).Reverse();
            var fl = pineFalse;

            var bt = bl.Zip(tr, (b, t) => (b, t));

            return(bt.Reverse().Select(x => x.b ? x.t : fl).ToPine());
        }
示例#5
0
        public static PineBool ToPineBool(this IEnumerable <double?> source)
        {
            PineBool ret = new PineBool();

            foreach (double?num in source)
            {
                ret.Add(num != null);
            }
            return(ret);
        }
示例#6
0
        /// <summary>Выбирает возвращаемые значения из элементов двух последовательностей по условию в первой последовательности</summary>
        /// <param name="pineBool">Последовательность задающая условие выбора</param>
        /// <param name="pineTrue">Последовательность для True</param>
        /// <param name="pineFalse">Последовательность для False</param>
        /// <returns></returns>
        public static Pine IIF(PineBool pineBool, Pine pineTrue, Pine pineFalse)
        {
            var bl = ((IEnumerable <bool>)pineBool).Reverse();
            var tr = ((IEnumerable <double>)pineTrue).Reverse();
            var fl = ((IEnumerable <double>)pineFalse).Reverse();

            var btf = bl.Zip(tr, (b, t) => (b, t)).Zip(fl, (bt, f) => (bt.b, bt.t, f));

            return(btf.Reverse().Select(x => x.b ? x.t : x.f).ToPine());
        }
示例#7
0
        public static PineBool ToPineBool(this IEnumerable <bool> source)
        {
            PineBool ret = new PineBool();

            foreach (bool num in source)
            {
                ret.Add(num);
            }
            return(ret);
        }
示例#8
0
        public static PineBool CrossOver(Pine xSource, Pine ySource)
        {
            IEnumerable <(double x, double y)> tuples = ZipEnd(xSource, ySource, (x, y) => (x, y));
            PineBool ret = new PineBool();

            (double x, double y)prev = tuples.First();
            ret.Add(prev.x == prev.y);
            tuples = tuples.Skip(1);
            foreach ((double x, double y)tupl in tuples)
            {
                ret.Add(tupl.x == tupl.y || (tupl.x > tupl.y && prev.x < prev.y));
                prev = tupl;
            }
            return(ret);
        }
示例#9
0
        public static Pine BarsSince(PineBool source)
        {
            Pine   ret   = new Pine();
            double count = 0;

            foreach (bool x in source)
            {
                if (x)
                {
                    count++;
                }
                else
                {
                    count = 0;
                }
                ret.Add(count);
            }
            return(ret);
        }
示例#10
0
        //public static Pine BarsSince(PineBool source)
        //{
        //    Pine ret = new Pine();

        //        foreach (bool x in source)
        //    {
        //        if (x)
        //            count++;
        //         else if()

        //        else
        //            count = 0;
        //        ret.Add(count);
        //    }
        //    return ret;

        //}


        public static Pine BarsSince(PineBool source)
        {
            Pine   buf   = new Pine();
            double count = 0;

            for (int i = 0; i < source.Count; i++)
            {
                if (source[i])
                {
                    count = 1;
                }
                else if (i > 0 && !source[i] && buf.ElementAt(i - 1) > 0)
                {
                    count++;
                }
                else
                {
                    count = 0;
                }
                buf.Add(count);
            }
            buf.Reverse();
            return(buf);
        }
示例#11
0
 Pine BarsSince(PineBool source) => TA.BarsSince(source);
示例#12
0
        private List <CalculationResult> BestStrategy(List <TradeBucketedDto> src)
        {
            //Передаем src - массив свечей, в порядке возрастания от 01.01.2014 - 12.12.2014
            //на выходе талб с результатом
            List <CalculationResult> result = new List <CalculationResult>();

            try
            {
                Pine close  = src.CloseToPine();//src.Close().ToPine();
                Pine volume = src.VolumeToPine();
                Pine high   = src.HighToPine();
                Pine low    = src.LowToPine();
                //Pine hlc3 = (high + low + close) / 3.0;
                IEnumerable <DateTime> timeStamp = src.TimeStampDateTime();
                IEnumerable <DateTime> timeOpen  = src.TimeOpen();

                #region strategy for 3 min



                Pine low_1 = low.Drop(1);

                Pine lowlow1 = low - low_1;


                Pine fast_ma = Ema(low, 496);
                Pine slow_ma = Ema(low, 14);
                Pine macd    = fast_ma - slow_ma;
                Pine signal  = Sma(macd, 7);

                Pine wt0  = Tradition(low, volume) + 40;
                Pine swt0 = Sma(wt0, 300);
                Pine wt1  = Ema(wt0, 6);

                Pine csi = TSIfor3min(lowlow1);

                Pine     csi_1        = csi.Drop(1);
                Pine     wt1_1        = wt1.Drop(1);
                Pine     high_1       = high.Drop(1);
                PineBool shortsignals = CrossUnder(macd, signal);
                PineBool longcond1    = (csi - csi_1 > 12 & csi < 112 & low_1 <low& high> high_1);
                PineBool longcond2    = (csi < 65 & low_1 <low& high> high_1);
                PineBool longcond3    = (csi < 112 & (wt1 - wt1_1) > 3);
                PineBool longcond4    = (csi < 65 & (wt1 - wt1_1) > 3);



                PineBool longCond  = ((longcond1 | longcond2 | longcond3 | longcond4) & macd < -150);
                PineBool shortCond = shortsignals;

                Pine longShortCond = PineBool.ZipEnd(longCond, shortCond, (ln, sh) => ln ? 1.0 : sh ? -1.0 : ((double?)null)).ToPineNA().ToPine(Approximation.Step);

                var handCond = Pine.ZipEnd(longShortCond, longShortCond.Drop(1), (lsc, lscd) => (lsc > 0.5 && lscd < -0.5) ? "Long" : (lsc <0.5 && lscd> -0.5) ? "Short" : "");

                #endregion

                timeStamp = timeStamp.Reverse();
                close.Reverse();
                low.Reverse();
                high.Reverse();
                csi.Reverse();
                wt1.Reverse();

                longcond1.Reverse();
                longcond2.Reverse();
                longcond3.Reverse();
                longcond4.Reverse();


                longCond.Reverse();
                shortCond.Reverse();
                handCond = handCond.Reverse();

                /*longShortCond[longShortCond.Count-1] = 1;
                *  longShortCond[longShortCond.Count-2] = -1;
                *  longShortCond[longShortCond.Count-3] = 0;*/

                longShortCond.Reverse();
                for (int i = 0; i < longCond.Count; i++)
                {
                    result.Add(
                        new CalculationResult(
                            i < timeStamp.Count() ? timeStamp.ElementAt(i) : DateTimeOffset.UtcNow,
                            i < close.Count() ? close.ElementAt(i).ToString() : "",
                            i < low.Count() ? low.ElementAt(i).ToString() : "",
                            i < high.Count() ? high.ElementAt(i).ToString() : "",
                            i < csi.Count() ? csi.ElementAt(i).ToString() : "",
                            i < wt1.Count() ? wt1.ElementAt(i).ToString() : "",
                            i < lowlow1.Count() ? lowlow1.ElementAt(i).ToString() : "",
                            i < longCond.Count() ? longCond.ElementAt(i) ? "Yes" : "" : "",
                            i < shortCond.Count() ? shortCond.ElementAt(i) ? "Yes" : "" : "",
                            i < longShortCond.Count() ? longShortCond.ElementAt(i) : 0,
                            i < handCond.Count() ? handCond.ElementAt(i).ToString() : "",
                            i < longcond1.Count() ? longcond1.ElementAt(i).ToString() : "",
                            i < longcond2.Count() ? longcond2.ElementAt(i).ToString() : "",
                            i < longcond3.Count() ? longcond3.ElementAt(i).ToString() : "",
                            i < longcond4.Count() ? longcond4.ElementAt(i).ToString() : ""
                            ));
                }

                //string lastSignalPumpOrSliv = "";
                for (int i = result.Count - 1; i > 0; i--)
                {
                    if (result[i - 1].LongShortCond == 1.0 && result[i].LongShortCond == -1.0)/* ||
                                                                                               * result[i].Pump == "1" && result[i+1].Pump == "0")*/
                    {
                        result[i - 1].Signal = "Buy";
                    }

                    if (result[i - 1].LongShortCond == -1.0 && result[i].LongShortCond == 1.0)/* ||
                                                                                               * result[i].Sliv == "1" && result[i+1].Sliv == "0")*/
                    {
                        result[i - 1].Signal = "Sell";
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Debug("BestStrategy error" + ex.Message);
            }

            return(result);
        }
        public void BestStrategy(int countCandles = 720)
        {
            DateTime?lastTimeStamp = LastCandle?.TimeStamp;


            if (lastTimeStamp != null)
            {
                int countNotCalc = allCandles.Count(cand => cand.TimeStamp > lastTimeStamp);
                if (countNotCalc + 150 < countCandles)
                {
                    countCandles = countNotCalc + 150;
                }
            }

            List <Candle> candlesCalc;
            int           begInd = allCandles.Count - countCandles;

            if (begInd < 0)
            {
                begInd = 0;
            }
            if (begInd < allCandles.Count)
            {
                candlesCalc = allCandles.GetRange(begInd, allCandles.Count - begInd);
            }
            else
            {
                candlesCalc = new List <Candle>();
            }

            Pine close  = candlesCalc.Close().ToPine();
            Pine volume = candlesCalc.Volume().ToPine();
            Pine high   = candlesCalc.High().ToPine();
            Pine low    = candlesCalc.Low().ToPine();
            Pine hlc3   = (high + low + close) / 3.0;
            IEnumerable <DateTime> timeStamp = candlesCalc.TimeStamp();
            IEnumerable <DateTime> timeOpen  = candlesCalc.TimeOpen();

            OutValues.AddRowHeaders(timeOpen);
            OutValues.Add("Close", close);

            Pine change = Change(close);

            Pine fast_ma = Sma(low, 16);
            Pine slow_ma = Sma(low, 29);
            Pine macd    = fast_ma - slow_ma;
            Pine signal  = Ema(macd, 9);
            Pine hist    = macd - signal;

            Pine hist_2  = hist.Drop(2);
            Pine hist_3  = hist.Drop(3);
            Pine close_2 = close.Drop(2);

            PineBool LOL       = hist_3 <3 & hist_3> -3 & hist_2 <3 & hist_2> -3;
            PineBool longCond  = hist > 0 & (close - close_2) < 66 & !LOL;
            PineBool shortCond = hist < 0 & (close_2 - close) < 66 & !LOL;


            OutValues.Add("hist_3", hist_3);
            OutValues.Add("hist", hist);
            OutValues.Add("LOL", LOL);

            OutValues.Add("longCond", longCond);
            OutValues.Add("shortCond", shortCond);

            Pine longShortCond = PineBool.ZipEnd(longCond, shortCond, (ln, sh) => ln ? 1.0 : sh ? -1.0 : na).ToPineNA().ToPine(Approximation.Step);

            var handCond = Pine.ZipEnd(longShortCond, longShortCond.Drop(1), (lsc, lscd) => (lsc > 0.5 && lscd < -0.5) ? "Long" : (lsc <0.5 && lscd> -0.5) ? "Short" : "");

            OutValues.Add("Сигнал", handCond);


            if (longShortCond.Last() == 1.0 && longShortCond.Drop(1).Last() == -1.0)
            {
                OnSignal(SignalEnum.Long);
            }
            if (longShortCond.Last() == -1.0 && longShortCond.Drop(1).Last() == 1.0)
            {
                OnSignal(SignalEnum.Short);
            }
        }