Exemplo n.º 1
0
        public async Task Borrow(IExchangeClient client, string pair, decimal price)
        {
            var balanceItems = (await _balanceRepository.Get(client.Platform, pair)).Where(x => x.IsBorrowed);

            if (balanceItems.Any())
            {
                return;
            }
            var availableVolume = Math.Round(await client.GetAvailableMargin(pair.Substring(0, 3)), _config[pair].VolumeFormat);

            //availableVolume = availableVolume / 100m * (decimal)_config[pair].Share;
            if (availableVolume < _config[pair].MinVolume)
            {
                //Insufficient funds
                return;
            }

            _fileService.GatherDetails(pair, FileSessionNames.Borrow_Volume, availableVolume);
            var operationId = await _operationRepository.Add(client.Platform, "add order", pair, "borrow");

            var orderIds = await client.Sell(availableVolume, pair, _config[pair].IsMarket?(decimal?)null : price, operationId, true);

            if (orderIds == null || !orderIds.Any())
            {
                return;
            }
            await _operationRepository.Complete(operationId);

            foreach (var orderId in orderIds)
            {
                await _orderRepository.Add(client.Platform, pair, orderId);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> Sell(IExchangeClient client, string pair, decimal price)
        {
            var balanceItems = (await _balanceRepository.Get(client.Platform, pair)).Where(x => !x.IsBorrowed);
            var isNotSold    = false;
            var volume       = decimal.Zero;
            var profit       = decimal.Zero;

            foreach (var balanceItem in balanceItems)
            {
                var boughtPrice = balanceItem.Volume * balanceItem.Price
                                  + _moneyService.FeeToPay(pair, balanceItem.Volume, balanceItem.Price, 0.26m)
                                  + _moneyService.FeeToPay(pair, balanceItem.Volume, price, 0.26m);
                var sellPrice = balanceItem.Volume * price;

                if (boughtPrice < sellPrice || balanceItem.NotSold >= _config[pair].MaxMissedSells)
                {
                    volume += balanceItem.Volume;
                    profit += sellPrice - boughtPrice;
                }
                else
                {
                    if (balanceItem.NotSoldtDate > _dateTime.Now.AddMinutes(-_config[pair].ThresholdMinutes))
                    {
                        //_fileService.Write(pair, $"Not worths to sell, and too short.");
                    }
                    else
                    {
                        _fileService.GatherDetails(pair, FileSessionNames.Not_Sold_Volume, balanceItem.Volume);
                        await _notSoldRepository.SetNotSold(client.Platform, pair, false);
                    }

                    isNotSold = true;
                }
            }

            if (volume == decimal.Zero)
            {
                return(!isNotSold);
            }
            _fileService.GatherDetails(pair, FileSessionNames.Sell_Volume, volume);
            _fileService.GatherDetails(pair, FileSessionNames.Profit, profit);
            var operationId = await _operationRepository.Add(client.Platform, "add order", pair, "sell");

            var orderIds = await client.Sell(volume, pair, _config[pair].IsMarket?(decimal?)null : price, operationId);

            if (orderIds == null || !orderIds.Any())
            {
                return(false);
            }
            await _operationRepository.Complete(operationId);

            foreach (var orderId in orderIds)
            {
                await _orderRepository.Add(client.Platform, pair, orderId);
            }
            return(true);
        }
Exemplo n.º 3
0
        private void SetSell(Action <decimal, decimal> setVolumePrice)
        {
            _exchangeClient.When(x => x.Sell(Arg.Any <decimal>(), Arg.Any <string>(), Arg.Any <decimal?>(), Arg.Any <int?>()))
            .Do(x =>
            {
                setVolumePrice?.Invoke(Convert.ToDecimal((object)x.Args()[0]), Convert.ToDecimal((object)x.Args()[2]));
            });


            _exchangeClient.Sell(Arg.Any <decimal>(), Arg.Any <string>(), Arg.Any <decimal?>(), Arg.Any <int?>())
            .ReturnsForAnyArgs(new List <string> {
                "order"
            });
        }