Ejemplo n.º 1
0
        public async Task <ServiceResponse> SellIfConditionsMet()
        {
            // find every unsold transaction in db. unsold means it has been bought by the bot
            var result = await _db.GetUnsoldTransactions(_session.BaseCoin, _session.TargetCoin, _session.Stock);

            if (result.Success)
            {
                double greedyTick = 0;


                var tickResult = await _service.GetTicker(_session.BaseCoin, _session.TargetCoin);

                if (tickResult.Success)
                {
                    greedyTick = tickResult.Data.Last;
                }


                foreach (var item in result.Data)
                {
                    double?greedyRate = null;

                    // if tick is
                    if (greedyTick >= item.ToSellFor)
                    {
                        var percentageChange = 1 - (item.ToSellFor / greedyTick);

                        if (!_previousSellPercentage.ContainsKey(item.Id))
                        {
                            _previousSellPercentage[item.Id] = percentageChange;
                            continue;
                        }

                        if (Math.Round(_previousSellPercentage[item.Id], 3) <= Math.Round(percentageChange, 3))
                        {
                            _previousSellPercentage[item.Id] = percentageChange;
                            continue;
                        }
                        else
                        {
                            greedyRate = greedyTick;
                        }
                    }
                    else
                    {
                        continue;
                    }


                    var serviceResult = await _service.PlaceSellOrder(_session.BaseCoin, _session.TargetCoin, _session.Stack, _session.ApiKey, _session.Secret, item.QuantityBought, item.ToSellFor, greedyRate, _session.TestMode);

                    if (serviceResult.Success)
                    {
                        _logger.LogInformation(string.Format("Placed new sell order with id {0} for {1}", serviceResult.Data.OrderRefId, greedyRate));
                        var dbResult = await _db.TransactionOrderPlaced(item.Id, serviceResult.Data.OrderRefId, greedyRate);

                        if (dbResult.Success)
                        {
                            if (_service.GetStockInfo().FillOrKill)
                            {
                                _logger.LogInformation(string.Format("Sell order id {0} finalized. Sold {1}", serviceResult.Data.OrderRefId, serviceResult.Data.Quantity));
                                await _db.TransactionOrderFinalized(item.Id, serviceResult.Data.Quantity);
                            }
                        }
                        // we dont need to clear _previousSellPercentage[item.Id] as this wont be updated anymore. the item is considered sold and won't enter this code block

                        var profit = await _db.GetProfit(_session.BaseCoin, _session.TargetCoin, _session.Stock);

                        if (profit.Success)
                        {
                            _logger.LogInformation($"Simple bot earned {profit.Data.ToString("0.00000000")} {_session.BaseCoin} so far");
                        }
                    }
                    return(new ServiceResponse(serviceResult.Status, serviceResult.Message));
                }
            }
            return(new ServiceResponse(result.Status, result.Message));
        }
Ejemplo n.º 2
0
        public async void PlaceSellOrder()
        {
            var result = await _service.PlaceSellOrder("BTC", "BNB", 0.02, System.Environment.GetEnvironmentVariable("API_KEY"), System.Environment.GetEnvironmentVariable("SECRET"), 1, 0.008614000000, null, true);

            Assert.True(result.Success, "Couldn't place sell order ...");
        }