Exemplo n.º 1
0
        protected override void PlaceBuyOrder(TradingData trade, List <TradingData> tradesList, bool forReal)
        {
            decimal fiatValue = _client.GetBalance(trade.CoinPair.Pair2);

            decimal capitalCost = fiatValue / _settings.HydraFactor;

            if (trade.UpdateMarketPrice(_client.GetPrice(trade.CoinPair)))
            {
                Bot.WriteConsole();

                decimal fees         = _client.GetTradeFee(trade.CoinPair);
                decimal myInvestment = capitalCost / (1 + fees);

                if (trade.CoinQuantity == 0)
                {
                    trade.CoinQuantity = myInvestment / trade.Price;
                }

                var buyOrder = forReal ? _client.PlaceBuyOrder(trade) : _client.PlaceTestBuyOrder(trade);
                if (buyOrder)
                {
                    trade.BuyPriceAfterFees = capitalCost / trade.CoinQuantity;
                    trade.ID         = tradesList.Count;
                    trade.LastAction = OrderSide.Buy;
                    tradesList.Add(trade);
                    Bot.WriteLog(trade.ToStringBought());
                    OnOrderSucceeded(trade);
                }
            }
        }
Exemplo n.º 2
0
 protected void PlaceCompleteSellOrder(TradingData trade, bool forReal)
 {
     if (trade.UpdateMarketPrice(Math.Round(_client.GetPrice(trade.CoinPair), 2)))
     {
         trade.CoinQuantityToTrade = trade.CoinQuantity;
         base.PlaceSellOrder(trade, forReal);
     }
 }
        protected override void PlaceSellOrder(TradingData trade, bool forReal)
        {
            if (trade.UpdateMarketPrice(Math.Round(_client.GetPrice(trade.CoinPair), 2)))
            {
                if (trade.Price > trade.BuyPriceAfterFees)
                {
                    trade.CoinQuantityToTrade = trade.CoinQuantity; // trading the full amount

                    decimal dmReceived = trade.CoinQuantityToTrade * trade.Price;

                    if (dmReceived > _settings.InvestmentMin)
                    {
                        base.PlaceSellOrder(trade, forReal);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public override void Trade()
        {
            using (var tempClient = new BinanceFuturesClient())
            {
                var openOrders = tempClient.GetOpenOrders().Data;

                TradingData trade = new TradingData(new CoinPair("BTC", "USDT", 3));
                var         pos   = tempClient.GetOpenPositions().Data.Single(s => s.Symbol == trade.CoinPair.ToString());
                trade.UpdateMarketPrice(pos.MarkPrice);

                Console.WriteLine($"{DateTime.Now} Entry Price: {pos.EntryPrice} Unrealised PnL: {pos.UnrealizedPnL}");

                var dataLast24hr = tempClient.Get24HPrice(trade.CoinPair.ToString()).Data;

                decimal priceDiff = dataLast24hr.WeightedAveragePrice - dataLast24hr.LowPrice;

                if (_settings.IsAutoAdjustShortAboveAndLongBelow)
                {
                    trade.PriceLongBelow = Math.Round(dataLast24hr.WeightedAveragePrice - priceDiff * 0.618m, 2);
                    decimal entryPrice = pos.EntryPrice == 0 ? dataLast24hr.WeightedAveragePrice : pos.EntryPrice;
                    trade.PriceShortAbove = Math.Round(priceDiff * 0.618m + entryPrice, 2);
                }
                else
                {
                    trade.PriceLongBelow  = _settings.LongBelow;
                    trade.PriceShortAbove = _settings.ShortAbove;
                }

                if (_settings.IsAutoAdjustTargetProfit)
                {
                    trade.ProfitTarget = Math.Round(Math.Abs(pos.Quantity) / pos.Leverage * pos.EntryPrice * 0.618m, 2);
                }
                else
                {
                    trade.ProfitTarget = _settings.FuturesProfitTarget;
                }

                // If zero positions
                if (pos.EntryPrice == 0 && openOrders.Count() == 0)
                {
                    decimal investment = _client.GetBalance(trade.CoinPair.Pair2) / _settings.FuturesSafetyFactor; // 11 is to have the liquidation very low or high
                    trade.CoinQuantity = investment / trade.Price * pos.Leverage;                                  // 20x leverage

                    // Short above or Long below
                    if (trade.Price < trade.PriceLongBelow)
                    {
                        _client.PlaceBuyOrder(trade);
                    }
                    else if (trade.Price > trade.PriceShortAbove)
                    {
                        _client.PlaceSellOrder(trade);
                    }
                }

                // When there is an existing position
                else if (pos.EntryPrice > 0)
                {
                    if (pos.LiquidationPrice < pos.EntryPrice) // Long position
                    {
                        trade.LastAction = Binance.Net.Enums.OrderSide.Buy;
                    }
                    else
                    {
                        trade.LastAction = Binance.Net.Enums.OrderSide.Sell;
                    }

                    if (pos.UnrealizedPnL > 0)
                    {
                        trade.CoinQuantity = pos.Quantity;

                        bool success;
                        bool targetProfitMet = trade.ProfitTarget > 0 && pos.UnrealizedPnL > trade.ProfitTarget;

                        if (trade.LastAction == Binance.Net.Enums.OrderSide.Buy && (targetProfitMet || pos.MarkPrice > trade.PriceShortAbove)) // i.e. Long position
                        {
                            success = _client.PlaceSellOrder(trade, closePosition: true);
                        }
                        else if (trade.LastAction == Binance.Net.Enums.OrderSide.Sell && (targetProfitMet || pos.MarkPrice < trade.PriceLongBelow))
                        {
                            success = _client.PlaceBuyOrder(trade, closePosition: true);
                        }
                        else
                        {
                            success = false;
                        }

                        if (success)
                        {
                            _settings.TotalProfit += pos.UnrealizedPnL;
                        }
                    }
                }

                // Below is for statistics and to keep UI up-to-date
                trade.SetQuantity(pos.Quantity);
                trade.SellPriceAfterFees = trade.BuyPriceAfterFees = pos.EntryPrice;
                trade.SetPriceChangePercentage(pos.MarkPrice, isFutures: true);
                OnTradeListItemHandled(trade);
            }
        }