Example #1
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);
            }
        }