Exemple #1
0
        public Result ExecuteTrade(Trade trade, decimal rate)
        {
            var strategyAssets = _assetRepository.GetByStrategyId(trade.StrategyId);
            var coins          = trade.MarketId.Split('_');
            var boughtCoin     = coins[trade.OrderType == OrderType.Buy ? 1 : 0];

            var boughtCoinAsset = strategyAssets.FirstOrDefault(o =>
                                                                !o.IsReserved && o.Exchange == trade.ReservedAsset.Exchange &&
                                                                o.Currency == boughtCoin);

            if (boughtCoinAsset == null)
            {
                _assetRepository.AddNotSave(new Asset
                {
                    Amount      = trade.OrderType == OrderType.Buy ? trade.Quantity : trade.Total,
                    Currency    = boughtCoin,
                    IsActive    = true,
                    IsReserved  = false,
                    StrategyId  = trade.ReservedAsset.StrategyId,
                    TradingMode = trade.ReservedAsset.TradingMode,
                    UserId      = trade.ReservedAsset.UserId,
                    Exchange    = trade.ReservedAsset.Exchange,
                });
            }
            else
            {
                boughtCoinAsset.Amount += trade.OrderType == OrderType.Buy ? trade.Quantity : trade.Total;
                _assetRepository.EditNotSave(boughtCoinAsset);
            }
            trade.Closed     = DateTime.Now;
            trade.TradeState = TradeState.Fulfilled;
            _assetRepository.DeleteNotSave(trade.ReservedAsset);
            trade.ReservedAsset = null;
            _tradeRepository.EditNotSave(trade);

            _tradeRepository.Save();
            _assetRepository.Save();

            return(new Result(true, null));
        }