Ejemplo n.º 1
0
        static void MimicStreamingData(BinanceClient client)
        {
            foreach (var symbol in AllCurrencies.Select(p => p.Name).ToList())
            {
                Currency currency = AllCurrencies.Where(p => p.Name == symbol).FirstOrDefault();

                var sticks = client.GetCandleSticks(currency.Name, Interval, null, DateTime.Now, 500).Result;

                //Cycle through each "close" like they were real time data.
                foreach (var stick in sticks)
                {
                    KlineMessage           klineMessage = new KlineMessage();
                    KlineMessage.KlineData data         = new KlineMessage.KlineData
                    {
                        Open      = stick.Open,
                        Symbol    = currency.Name,
                        EndTime   = stick.CloseTime,
                        Close     = stick.Close,
                        StartTime = stick.OpenTime
                    };

                    currency.CalculateTradeAlgorithm(data, client);
                }
            }
        }
Ejemplo n.º 2
0
        public static void CalculateTradeAlgorithm(this Currency currency, KlineData data, BinanceClient client)
        {
            DateTime currentTime = UnixTimeStampToDateTime(data.StartTime);
            DateTime endTime     = UnixTimeStampToDateTime(data.EndTime);

            try
            {
                if (currency.CloseTime == data.EndTime)
                {
                    return;
                }

                currency.CloseTime = data.EndTime;
                //Get the last 99 klines to calc
                var sticks = client.GetCandleSticks(currency.Name, BinanceAPIMain.Interval, null, currentTime, 99).Result;

                var sticks1hr = client.GetCandleSticks(currency.Name, TimeInterval.Hours_1, null, currentTime, 200).Result;

                //Perhaps we calculate the EMA with the latest price, but the previous EMA is the close of the last kline?
                if (currency.PreviousRSI != -1)
                {
                    currency.PreviousRSI = currency.RSI.Last();
                }
                currency.EMA12.Clear();
                currency.EMA7.Clear();
                currency.EMA26.Clear();
                currency.EMA99.Clear();
                currency.MACD.Clear();
                currency.Signal.Clear();
                currency.MACDHistogram.Clear();
                currency.RSI.Clear();
                currency.EMA12                 = CalculateEMA(sticks, 12);
                currency.EMA26                 = CalculateEMA(sticks, 26);
                currency.EMA99                 = CalculateEMA(sticks, 99);
                currency.EMA7                  = CalculateEMA(sticks, 7);
                currency.MACD                  = SubtractValues(currency.EMA12, currency.EMA26);
                currency.Signal                = CalculateTrendLine(currency.MACD, 9);
                currency.MACDHistogram         = SubtractValues(currency.MACD, currency.Signal);
                currency.MACDHistPosPercentage = CalcLowestPercentPos(currency.MACDHistogram, 10);
                currency.RSI = CalculateRSI(sticks, 21);

                if (currency.PreviousRSI == -1)
                {
                    currency.PreviousRSI = currency.RSI.Last();
                }

                ConsoleColor color = currency.MACDHistogram.Last() < 0 ? ConsoleColor.Magenta : ConsoleColor.Cyan;
                BinanceAPIMain.WriteToLog("Symbol " + currency.Name + " Price: " + data.Close + " Close Time:" + endTime + Environment.NewLine +
                                          "MACD Histogram: " + currency.MACDHistogram.Last() + Environment.NewLine +
                                          "MACD Histogram Lowest 10%: " + currency.MACDHistPosPercentage + Environment.NewLine +
                                          "RSI: " + currency.RSI.Last() + Environment.NewLine +
                                          "RSI-1: " + currency.PreviousRSI + Environment.NewLine +
                                          "Signal: " + currency.Signal.Last() + Environment.NewLine +
                                          "EMA99(n): " + Decimal.Round(currency.EMA99.Last(), 7) + " EMA99(n-1): " + Decimal.Round(currency.EMA99[currency.EMA7.Count - 2], 7) + Environment.NewLine +
                                          "EMA7(n): " + Decimal.Round(currency.EMA7.Last(), 7) + " EMA7(n-1): " + Decimal.Round(currency.EMA7[currency.EMA7.Count - 2], 7) +
                                          " EMA7(n-2): " + Decimal.Round(currency.EMA7[currency.EMA7.Count - 3], 7) + Environment.NewLine, color, createFile: false);


                if (!currency.InTrade && currency.MACDHistogram != null && currency.CycleSeen &&
                    currency.MACDHistogram.Last() > currency.MACDHistPosPercentage &&
                    currency.EMA7.Last() > (currency.EMA7[currency.EMA7.Count - 2] * (decimal)1.0005) &&
                    currency.EMA7[currency.EMA7.Count - 2] > currency.EMA7[currency.EMA7.Count - 3] &&
                    currency.EMA99.Last() > currency.EMA99[currency.EMA99.Count() - 2] &&
                    currency.RSI.Last() < 60)
                {
                    if (BinanceAPIMain.Debug == "false")
                    {
                        Trading.ExecuteTrade(client, data, currency, OrderSide.BUY);
                    }
                    else
                    {
                        Trading.ExecuteTradeDebug(client, data, currency, OrderSide.BUY);
                    }
                }
                else if (currency.InTrade &&
                         (currency.RSI.Last() >= 70 || currency.RSI[currency.RSI.Count() - 2] >= 70) && currency.RSI.Last() < currency.RSI[currency.RSI.Count() - 2] &&
                         data.Close > currency.CoinsBought.Min(p => p.Item2))
                {
                    if (BinanceAPIMain.Debug == "false")
                    {
                        Trading.ExecuteTrade(client, data, currency, OrderSide.SELL);
                    }
                    else
                    {
                        Trading.ExecuteTradeDebug(client, data, currency, OrderSide.SELL);
                    }
                    currency.InTrade   = false;
                    currency.CycleSeen = false;
                }
                else if (!currency.CycleSeen && currency.MACDHistogram.Last() < 0 && currency.MACDHistogram != null)
                {
                    currency.CycleSeen           = true;
                    currency.LastTransactionTime = DateTime.Now.AddMinutes(-2);
                    BinanceAPIMain.WriteToLog("Symbol: " + currency.Name + " Cycle Seen: " + currency.CycleSeen.ToString() + Environment.NewLine, ConsoleColor.Cyan);
                }
            }
            catch (Exception ex)
            {
                BinanceAPIMain.WriteToLog(currency.Name + ": " + ex.Message + ex.StackTrace + " from currency.CalcAlgorithm" + " at " + DateTime.Now, ConsoleColor.DarkRed);
            }
        }