private async void MakeTrade() { if (isOrderWaiting) { // if is order was waiting check it for filling status var order = await Api.GetOrderAsync(User, Symbol, waitingOrderId); if (order.Status == OrderStatus.Filled) { if (order.Type == Binance.OrderType.Limit) { LogManager.Instance.AddLog(new TradeLog() { Amount = order.ExecutedQuantity, Price = order.Price, Side = order.Side == OrderSide.Buy ? TradeSide.Buy : TradeSide.Sell, Symbol = order.Symbol }); } if (order.Side == OrderSide.Buy) { tradeSide = TradeSide.Sell; lastBuyDate = DateTime.Now; } else if (order.Side == OrderSide.Sell) { tradeSide = TradeSide.Buy; lastSellDate = DateTime.Now; } ResetValuesAfterTrade(); await Task.Delay(500); } else if (order.Status == OrderStatus.Canceled || order.Status == OrderStatus.Expired || order.Status == OrderStatus.Rejected) { isOrderWaiting = false; } } if (!isOrderWaiting) { // there is no open order so lets trade var lastTrade = LogManager.Instance.GetLastTradeLog(Symbol); if (lastTrade != null) { if (lastTrade.Side == TradeSide.Buy) { // time to sell lastBuyDate = lastTrade.Date; lastBuyPrice = lastTrade.Price; CheckForSell(lastTrade); } else if (lastTrade.Side == TradeSide.Sell) { // time to buy lastSellDate = lastTrade.Date; lastSellPrice = lastTrade.Price; CheckForBuy(lastTrade); } } else { if (tradeSide == TradeSide.None) // if TradeSide is none decide tradeSide by checking base and quote asset value { var account = await Api.GetAccountInfoAsync(User); var symbol = TradingServiceHelper.GetSymbolByName(base.Symbol); decimal baseAssetValue = 0; decimal quoteAssetValue = 0; foreach (var balance in account.Balances) { if (balance.Asset == symbol.BaseAsset) { baseAssetValue = balance.Free; } else if (balance.Asset == symbol.QuoteAsset) // MUST BE BTC { quoteAssetValue = balance.Free; } } var price = await Api.GetPriceAsync(symbol); var baseAssetValueInQuoteAsset = price.Value * baseAssetValue; // base asset's value in quote asset currency if (baseAssetValueInQuoteAsset > quoteAssetValue) { tradeSide = TradeSide.Sell; } else { tradeSide = TradeSide.Buy; } } if (tradeSide == TradeSide.Sell) { CheckForSell(); } else if (tradeSide == TradeSide.Buy) { CheckForBuy(); } } } }
public virtual async void Initialize(bool subscribeSymbolsAutomatically = true) { var apiKey = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.API_KEY, string.Empty); var secretKey = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.SECRET_KEY, string.Empty); if (!string.IsNullOrEmpty(apiKey)) { apiKey = EncryptionHelper.DecryptText(apiKey); } if (!string.IsNullOrEmpty(secretKey)) { secretKey = EncryptionHelper.DecryptText(secretKey); } Symbol = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.TRADING_SYMBOL, Defaults.TRADING_SYMBOL); Decimals = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.DECIMALS, Defaults.DECIMALS); var unitTypeStr = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.UNIT_TYPE, UnitType.Value.ToString()); UnitType = (UnitType)System.Enum.Parse(typeof(UnitType), unitTypeStr, true); var orderTypeStr = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.ORDER_TYPE, Enum.OrderType.Limit.ToString()); OrderType = (Enum.OrderType)System.Enum.Parse(typeof(Enum.OrderType), orderTypeStr, true); Api = new BinanceApi(); if (subscribeSymbolsAutomatically) { SubscribeSymbols(); } if (!string.IsNullOrEmpty(apiKey) && !string.IsNullOrEmpty(secretKey)) { User = new BinanceApiUser(apiKey, secretKey); } // get last orders LogManager.Instance.ClearTradeLogs(); if (User != null) { try { var ordersSymbol = await Api.GetOrdersAsync(User, Symbol, -1, 30); foreach (var order in ordersSymbol) { if (order.Status == OrderStatus.Filled) { if (order.Type == Binance.OrderType.Limit) { LogManager.Instance.AddLog(new TradeLog() { Amount = order.ExecutedQuantity, Date = order.Time.ToLocalTime(), OrderId = order.Id, Price = order.Price != 0 ? order.Price.RoundTo(6) : await TradingServiceHelper.GetPriceByDate(order.Symbol, order.Time), Side = order.Side == OrderSide.Buy ? TradeSide.Buy : TradeSide.Sell, Symbol = order.Symbol }, false); } else { var price = 0m; var trades = await Api.GetAccountTradesAsync(User, Symbol, 200); foreach (var trade in trades) { if (trade.OrderId == order.Id) { price += trade.Price * trade.Quantity; } } price = price / order.ExecutedQuantity; price = price.RoundTo(6); LogManager.Instance.AddLog(new TradeLog() { Amount = order.ExecutedQuantity, Date = order.Time.ToLocalTime(), OrderId = order.Id, Price = price, Side = order.Side == OrderSide.Buy ? TradeSide.Buy : TradeSide.Sell, Symbol = order.Symbol }, false); } } } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } }
private async void MakeTrade() { if (!isOrderWaiting) { var lastTrade = LogManager.Instance.GetLastTradeLog(Symbol); if (lastTrade != null) { if (lastTrade.Side == TradeSide.Buy) { // time to create sell order CreateSellOrderAsync(lastTrade); } else if (lastTrade.Side == TradeSide.Sell) { // time to buy CheckForBuy(lastTrade); } } else { if (tradeSide == TradeSide.None) // if TradeSide is none decide tradeSide by checking base and quote asset value { var account = await Api.GetAccountInfoAsync(User); var symbol = TradingServiceHelper.GetSymbolByName(Symbol); decimal baseAssetValue = 0; decimal quoteAssetValue = 0; foreach (var balance in account.Balances) { if (balance.Asset == symbol.BaseAsset) { baseAssetValue = balance.Free; } else if (balance.Asset == symbol.QuoteAsset) // MUST BE BTC { quoteAssetValue = balance.Free; } } var price = await Api.GetPriceAsync(symbol); var baseAssetValueInQuoteAsset = price.Value * baseAssetValue; // base asset's value in quote asset currency if (baseAssetValueInQuoteAsset > quoteAssetValue) { tradeSide = TradeSide.Sell; } else { tradeSide = TradeSide.Buy; dateLastSold = DateTime.Now; // set dateLastSold to now because from now on service will be look for buy } } if (tradeSide == TradeSide.Sell) { throw new Exception($"You must start HitNRun trading with quote asset! ({SymbolObject.QuoteAsset.Symbol})"); } else if (tradeSide == TradeSide.Buy) { CheckForBuy(); } } } }