Exemple #1
0
        public async Task <ServiceResponse> BuyIfConditionsMet()
        {
            // we need to know how much the bot has bought till now so it won't exceed the limit
            var amountBought = await _db.GetAwaitingSellSum(_session.BaseCoin, _session.TargetCoin, _session.Stock);

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

            if (result.Success && amountBought.Success)
            {
                var saveTickResult = await _db.SaveTick(_session.BaseCoin, _session.TargetCoin, _session.Stock, result.Data.Ask, result.Data.Bid, result.Data.Last, _session.Id);

                _stack.Push(result.Data.Ask);
                var percentageChange = 1 - (_stack.Peek() / _stack.Max());
                _previousPercentage.Push(percentageChange);

                _logger.LogTrace("Max of stack: {0} Tick: {1}", _stack.Max().ToString("0.00000000", CultureInfo.InvariantCulture), _stack.Peek().ToString("0.00000000", CultureInfo.InvariantCulture));
                _logger.LogTrace($"Percentage change: {_previousPercentage.Peek()}. Maximum percentage change: {_previousPercentage.Max()}");

                // here we check if the next transaction would exceed the limit
                if (amountBought.Data + _session.Stack > _session.BuyoutCeiling)
                {
                    // if it exceeds the limit we should start from the beginning
                    ClearStacks();
                }
                else
                {
                    if (!_huntingModeActive && (percentageChange > _session.ChangeToBuy))
                    {
                        // hunting mode has been triggered when exceeded change to buy parameter. Now it will try to buy as low as it can
                        _huntingModeActive = true;
                    }

                    if (_previousPercentage.Count >= 2 && _huntingModeActive)
                    {
                        var percents = _previousPercentage.Take(2).ToArray();

                        if (percents[0] < percents[1])
                        {
                            // if the actual percentage is 0 it means the rate has reached max in stack. This means we shouldn't buy any coins at this price, and yet we start from the beginning from the top of the chart
                            if (percents[0] == 0)
                            {
                                ClearStacks();
                                return(new ServiceResponse(0, "The rate has reached max in stack"));
                            }

                            var serviceResult = await _service.PlaceBuyOrder(_session.BaseCoin, _session.TargetCoin, _session.Stack, _session.ApiKey, _session.Secret, _stack.Peek(), _session.TestMode);

                            if (serviceResult.Success)
                            {
                                var dbResult = await _db.SaveTransaction(_stack.Peek(), serviceResult.Data.OrderRefId, _session.BaseCoin, _session.TargetCoin, _session.ChangeToSell, _session.Id, _session.Stack, _session.Stock);

                                if (dbResult.Success)
                                {
                                    _logger.LogInformation(string.Format("Placed {0} buy order with id {2} for {1}", _session.TargetCoin, _stack.Peek().ToString("0.00000000"), serviceResult.Data.OrderRefId));
                                    ClearStacks();

                                    if (_service.GetStockInfo().FillOrKill)
                                    {
                                        _logger.LogInformation(string.Format("Buy order id {0} finalized. Bought {1}", serviceResult.Data.OrderRefId, serviceResult.Data.Quantity));
                                        await _db.TransactionBuyFinalized(dbResult.Data.Id, serviceResult.Data.Quantity);
                                    }
                                }
                            }
                            else
                            {
                                // probably something went wrong - no funds on exchange or problem with api or fillOrKill enabled. let's clear percentage so it won't make false assumptions. It may try to buy some coins after a while when the change is not profitable anymore
                                ClearStacks();

                                return(new ServiceResponse(0, "Fill or Kill couldn't complete an order or there was problem contacting StockApi"));
                            }
                        }
                    }
                }
            }
            else
            {
                return(new ServiceResponse(100, result.Message));
            }

            return(new ServiceResponse(0, "Conditions didn't met yet. Waiting ..."));
        }
Exemple #2
0
        public async void PlaceBuyOrder()
        {
            var result = await _service.PlaceBuyOrder("BTC", "BNB", 0.02, System.Environment.GetEnvironmentVariable("API_KEY"), System.Environment.GetEnvironmentVariable("SECRET"), 0.00861400000, true);

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