public static void ExecuteTradeDebug(BinanceClient client, KlineData data, Currency currency, OrderSide side) { var allPrices = client.GetAllPrices().Result; var price = allPrices.Where(p => p.Symbol.Contains(currency.Name)).Select(a => a.Price).FirstOrDefault(); var finalPrice = BinanceAPIMain.Debug == "true" ? data.Close : price; DateTime currentTime = BinanceAPIMain.Debug == "true" ? CurrencyCalc.UnixTimeStampToDateTime(data.EndTime) : DateTime.Now; if (side == OrderSide.BUY) { //Orders will break if not successful, so check for try/catch block and add into currency ledger or move on. try { currency.CoinsBought.Add(new Tuple <decimal, decimal>(currency.AllottedCoins, data.Close)); currency.InTrade = true; BinanceAPIMain.WriteToLog(currency.AllottedCoins + ":" + currency.Name + ": go in on trade at: " + data.Close + " Time: " + currentTime, ConsoleColor.Yellow); BinanceAPIMain.WriteToLogTrades(currency.Name + ": Buy " + currency.AllottedCoins + " coins at " + data.Close + " for the kline ending at " + currentTime); BinanceAPIMain.WriteToCurrencyFile(currency.AllottedCoins + "|" + data.Close, currency.Name); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.Message + " while buying " + currency.Name, ConsoleColor.DarkRed); } } else if (side == OrderSide.SELL) { try { foreach (Tuple <decimal, decimal> tuplePair in currency.CoinsBought) { decimal percentage = Math.Round(((data.Close - tuplePair.Item2) / tuplePair.Item2) * 100, 2); ConsoleColor color = percentage > 0 ? ConsoleColor.White : ConsoleColor.Red; BinanceAPIMain.WriteToLogTrades(currency.Name + ": Sell " + currency.AllottedCoins + " coins at " + data.Close + " for the kline ending at " + currentTime + " for a " + percentage + " percent change"); BinanceAPIMain.WriteToLog(currency.Name + ": sold " + tuplePair.Item1 + " coins at " + data.Close + " on " + currentTime + Environment.NewLine + currency.Name + ": Made: " + (data.Close - tuplePair.Item2) + " Percentage: " + percentage, color); } currency.CoinsBought.Clear(); currency.LastSalePrice = data.Close; currency.LastTransactionTime = DateTime.Now; //Remove the file which indicated we had an open position on the trade. if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt")) { File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt"); } } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.Message + " while selling " + currency.Name, ConsoleColor.DarkRed); } } }
public static void ExecuteTrade(BinanceClient client, KlineData data, Currency currency, OrderSide side) { var allPrices = client.GetAllPrices().Result; var price = allPrices.Where(p => p.Symbol.Contains(currency.Name)).Select(a => a.Price).FirstOrDefault(); DateTime currentTime = DateTime.Now; if (side == OrderSide.BUY && BinanceAPIMain.TotalTrades < 4) { //Before we execute, update the allotted coins BinanceAPIMain.CalculateCoinsToTrade(client, currency, allPrices); //Orders will break if not successful, so check for try/catch block and add into currency ledger or move on. try { var order = client.PostNewOrder(currency.Name.ToLower(), currency.AllottedCoins, 0.1m, side, OrderType.MARKET, TimeInForce.IOC); Task.WaitAll(order); if (order.Result.OrderId != -1) { BinanceAPIMain.TotalTrades++; currency.InTrade = true; var finalCoinsBought = GetHeldCoins(client, currency.Name); currency.CoinsBought.Add(new Tuple <decimal, decimal>(finalCoinsBought, price)); BinanceAPIMain.WriteToLog(finalCoinsBought + ":" + currency.Name + ": go in on trade at: " + price + " Time: " + currentTime, ConsoleColor.Yellow); BinanceAPIMain.WriteToLogTrades(currency.Name + ": Buy " + finalCoinsBought + " coins at " + price + " Time: " + currentTime); BinanceAPIMain.WriteToCurrencyFile(finalCoinsBought + "|" + price, currency.Name); } } catch (OperationCanceledException oce) { BinanceAPIMain.WriteToLog(oce.Message + " while buying " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.InnerException + " while buying " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } } else if (side == OrderSide.SELL) { try { foreach (Tuple <decimal, decimal> tuplePair in currency.CoinsBought) { var order = client.PostNewOrder(currency.Name.ToLower(), tuplePair.Item1, 0.1m, side, OrderType.MARKET, TimeInForce.IOC); Task.WaitAll(order); if (order.Result.OrderId != -1) { decimal percentage = Math.Round(((price - tuplePair.Item2) / tuplePair.Item2) * 100, 2); ConsoleColor color = percentage > 0 ? ConsoleColor.White : ConsoleColor.Red; BinanceAPIMain.WriteToLogTrades(currency.Name + ": Sell " + tuplePair.Item1 + " coins at " + price + " Time: " + currentTime + " for a " + percentage + " percent change"); BinanceAPIMain.WriteToLog(currency.Name + ": sold " + tuplePair.Item1 + " coins at " + price + " on " + currentTime + Environment.NewLine + currency.Name + ": Made: " + (price - tuplePair.Item2) + " Percentage: " + percentage, color); } } BinanceAPIMain.TotalTrades--; currency.CoinsBought.Clear(); currency.LastSalePrice = data.Close; currency.LastTransactionTime = DateTime.Now; //Remove the file which indicated we had an open position on the trade. if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt")) { File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt"); } } catch (OperationCanceledException oce) { BinanceAPIMain.WriteToLog(oce.Message + " while selling " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.InnerException + " while selling " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } } }
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); } }