private static void CheckTolerance(double profit, double highestProfit, double highestProfitDiff, double lossTolerance, IMarketData marketToTrade, double pricePaid, CurrencyPair currencyPair, ulong idOrder) { double volume = marketToTrade.Volume24HourQuote; if (profit < TOLERANCE_PROFIT_PRICEPAID) { System.Media.SystemSounds.Exclamation.Play(); Console.Title += string.Format( " | {0} (limit: {1}% - High_diff: {2}%)", ReasonToSell.TOLERANCE_PROFIT_PRICEPAID, TOLERANCE_PROFIT_PRICEPAID, string.Format("{0:0.0000}", highestProfitDiff)); Console.WriteLine(string.Format("\n{0} ({1}%)\n", ReasonToSell.TOLERANCE_PROFIT_PRICEPAID, TOLERANCE_PROFIT_PRICEPAID)); //if (mode == Mode.REAL) //BIZ.PostBestSellOrder(currencyPair, idOrder); lastState = EnumLastState.SOLD; } if (highestProfitDiff < -0.3 && highestProfitDiff < lossTolerance) { System.Media.SystemSounds.Exclamation.Play(); Console.Title += string.Format( " | {0} (limit: {1}% - High_diff: {2}%)", ReasonToSell.TOLERANCE_PROFIT_HIGHDIFF, lossTolerance, string.Format("{0:0.0000}", highestProfitDiff)); Console.WriteLine(string.Format("\n{0} ({1}%)\n", ReasonToSell.TOLERANCE_PROFIT_HIGHDIFF, TOLERANCE_PROFIT_HIGHDIFF)); //if (mode == Mode.REAL) //BIZ.PostBestSellOrder(currencyPair, idOrder); lastState = EnumLastState.SOLD; } }
private static void StartTrade(string baseCurrency, string quoteCurrency, double amountToTrade, double pricePaid, ulong idOrder = 0) { IDictionary <CurrencyPair, IMarketData> markets; IMarketData marketToTrade = null; IMarketData usdtMarket = null; lastState = EnumLastState.BOUGHT; double previousProfit = -1; double profit = 0; double highestProfit = 0; double highestProfitDiff = 0; double highestPrice = 0; double baseCurrencyTotal = 0; double usdtValue = 0; double lossTolerance = 0; IsAlertProfitPricePaidSent = false; IsAlertProfitHighestDiffSent = false; var currencyPair = new CurrencyPair(baseCurrency, quoteCurrency); var currencyPairUsdt = new CurrencyPair(CURRENCY_USDT, baseCurrency); double baseCurrencyUnitPrice = 0; // Aller chercher le marché une 1ère fois en dehors du while pour figer dans le temps les données de départ markets = BIZ.GetSummary(); marketToTrade = BIZ.GetCurrency(markets, currencyPair); if (idOrder != 0) { mode = Mode.REAL; // Aller chercher les vraies chiffres amountToTrade = (from x in BIZ.GetBalances() where x.Type == currencyPair.QuoteCurrency select x.USDT_Value).SingleOrDefault(); pricePaid = BIZ.GetTrades(currencyPair).Where(x => x.IdOrder == idOrder).FirstOrDefault().PricePerCoin; } else { mode = Mode.SIMULATION; // Si le prix payé n'a pas été défini par l'utilisateur, prendre le prix courant if (pricePaid == 0) { pricePaid = BIZ.FindBestPrice(currencyPair, OrderType.Buy); } } // Enlève un pourcentage du montant à transiger si le prix payé était plus haut que le prix courant amountToTrade = amountToTrade + (marketToTrade.PriceLast / pricePaid) * 100 - 100; // Identifier l'unité de base de la monnaie à marchander if (baseCurrency == CURRENCY_USDT) { baseCurrencyUnitPrice = amountToTrade / marketToTrade.PriceLast; } else { usdtMarket = BIZ.GetCurrency(markets, currencyPairUsdt); baseCurrencyUnitPrice = (amountToTrade / usdtMarket.PriceLast) / marketToTrade.PriceLast; } try { while (lastState == EnumLastState.BOUGHT) { marketToTrade = BIZ.GetCurrency(currencyPair); profit = (marketToTrade.PriceLast / pricePaid) * 100 - 100; highestProfit = profit > highestProfit ? profit : highestProfit; lossTolerance = TOLERANCE_PROFIT_HIGHDIFF + (highestProfit * LOSS_TOLERANCE_MULTIPLICATOR); // Évite d'afficher les données si rien n'a changé if (Math.Round(previousProfit, 4) != Math.Round(profit, 4)) { previousProfit = profit; highestPrice = marketToTrade.PriceLast > highestPrice ? marketToTrade.PriceLast : highestPrice; highestProfitDiff = (marketToTrade.PriceLast / highestPrice) * 100 - 100; baseCurrencyTotal = marketToTrade.PriceLast * baseCurrencyUnitPrice; usdtValue = baseCurrencyTotal * (baseCurrency == CURRENCY_USDT ? 1 : usdtMarket.PriceLast); Display(currencyPair, marketToTrade, profit, highestProfit, highestProfitDiff, lossTolerance, highestPrice, baseCurrencyTotal, usdtValue, pricePaid, amountToTrade); CheckTolerance(profit, highestProfit, highestProfitDiff, lossTolerance, marketToTrade, pricePaid, currencyPair, idOrder); } Thread.Sleep(5000); } ts.Cancel(); Console.WriteLine(string.Format("Console paused... press any key to continue")); Console.ReadKey(); // press any key to continue } catch (Exception ex) { Console.Write($"{ex}\n"); } Console.WriteLine(string.Format("C'EST VENDU!!!")); Console.WriteLine(string.Format("Console paused... press any key to continue")); Console.ReadKey(); // press any key to continue }