Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        public static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            var message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.TextMessage)
            {
                return;
            }

            if (message.From.FirstName.ToLower().Contains("name") && ConfigurationManager.AppSettings["EMail"].Contains("name"))
            {
                switch (message.Text.Split(' ').First())
                {
                case "/hello":
                    if (ConfigurationManager.AppSettings["EMail"].Contains("name"))
                    {
                        await BinanceAPIMain.Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                        await BinanceAPIMain.Bot.SendTextMessageAsync(
                            message.Chat.Id,
                            ConfigurationManager.AppSettings["EMail"] + " " + Console.Title + Environment.NewLine + "ChatID: " + message.Chat.Id.ToString() + " ACK",
                            replyMarkup : new ReplyKeyboardRemove());
                    }
                    break;

                case "/help":
                    if (ConfigurationManager.AppSettings["EMail"].Contains("name"))
                    {
                        StringBuilder builder = new StringBuilder();
                        builder.AppendLine("Available Commands:");
                        builder.AppendLine(@"/hello: get chat window number of bot/client connection");
                        builder.AppendLine(@"/statusXXXX: get current kline close status for each user (name or name)");
                        builder.AppendLine(@"/pricesXXXX: get current prices for each coin purchased by BinanceAPI application and calc return.");

                        await BinanceAPIMain.Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                        await BinanceAPIMain.Bot.SendTextMessageAsync(
                            message.Chat.Id,
                            builder.ToString(),
                            replyMarkup : new ReplyKeyboardRemove());
                    }
                    break;

                case "/status":
                    if (ConfigurationManager.AppSettings["EMail"].Contains("name"))
                    {
                        await BinanceAPIMain.Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                        StringBuilder b = new StringBuilder();
                        b.AppendLine(ConfigurationManager.AppSettings["EMail"] + " " + Console.Title);
                        foreach (Currency c in BinanceAPIMain.AllCurrencies)
                        {
                            b.AppendLine(c.Name + ": close kline at: " + UnixTimeStampToDateTime((double)c.CloseTime));
                        }

                        await BinanceAPIMain.Bot.SendTextMessageAsync(
                            message.Chat.Id,
                            b.ToString(),
                            replyMarkup : new ReplyKeyboardRemove());
                    }
                    break;

                case "/prices":
                    if (ConfigurationManager.AppSettings["EMail"].Contains("name"))
                    {
                        await BinanceAPIMain.Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                        await BinanceAPIMain.Bot.SendTextMessageAsync(
                            message.Chat.Id,
                            Trading.GetCurrentPurchases(BinanceAPIMain.BinanceClient),
                            replyMarkup : new ReplyKeyboardRemove());
                    }
                    break;
                }
            }
        }