public void Test_ManyTrades_Limit()
        {
            var trades = new List <LimitQueueItem.LimitTradeInfo>()
            {
                new LimitQueueItem.LimitTradeInfo
                {
                    Volume         = 0.9,
                    OppositeVolume = 8100,
                    Price          = 9000,
                    Asset          = "BTC",
                    OppositeAsset  = "USD"
                },
                new LimitQueueItem.LimitTradeInfo
                {
                    Volume         = 0.3,
                    OppositeVolume = 2745,
                    Price          = 9150,
                    Asset          = "BTC",
                    OppositeAsset  = "USD"
                },
                new LimitQueueItem.LimitTradeInfo
                {
                    Volume         = 0.1,
                    OppositeVolume = 920,
                    Price          = 9200,
                    Asset          = "BTC",
                    OppositeAsset  = "USD"
                }
            };

            Assert.Equal(9050, ConvertExtensions.CalcEffectivePrice(trades, GetDefaultPair(), false));
        }
        public void Test_Trade_Price_Rounding()
        {
            var pair = GetDefaultPair();

            var trades = new List <TradeQueueItem.TradeInfo>()
            {
                new TradeQueueItem.TradeInfo
                {
                    MarketVolume = 1,
                    LimitVolume  = 10000,
                    Price        = 10000,
                    MarketAsset  = "BTC",
                    LimitAsset   = "USD"
                },
                new TradeQueueItem.TradeInfo
                {
                    MarketVolume = 2,
                    LimitVolume  = 22000,
                    Price        = 11000,
                    MarketAsset  = "BTC",
                    LimitAsset   = "USD"
                }
            };

            Assert.Equal(10666.666, ConvertExtensions.CalcEffectivePrice(trades, pair, false), pair.Accuracy);
            Assert.Equal(10666.667, ConvertExtensions.CalcEffectivePrice(trades, pair, true), pair.Accuracy);
        }
Exemple #3
0
        public async Task <ClientTrade[]> Create(TradeQueueItem.MarketOrder order, List <TradeQueueItem.TradeInfo> trades, string clientId)
        {
            var assetPair = await _assetsServiceWithCache.TryGetAssetPairAsync(order.AssetPairId);

            var trade        = trades[0];
            var price        = ConvertExtensions.CalcEffectivePrice(trades, assetPair, order.Volume > 0);
            var marketVolume = trades.Sum(x => x.MarketVolume);
            var limitVolume  = trades.Sum(x => x.LimitVolume);

            var marketAssetRecord = CreateCommonPartForTradeRecord(trade, order.Id, price, order.AssetPairId);
            var limitAssetRecord  = CreateCommonPartForTradeRecord(trade, order.Id, price, order.AssetPairId);

            marketAssetRecord.ClientId = limitAssetRecord.ClientId = clientId;

            marketAssetRecord.Amount = marketVolume;
            if (Math.Sign(marketVolume) == Math.Sign(limitVolume))
            {
                marketAssetRecord.Amount *= -1;
            }
            marketAssetRecord.AssetId = trade.MarketAsset;

            limitAssetRecord.Amount  = limitVolume;
            limitAssetRecord.AssetId = trade.LimitAsset;

            foreach (var t in trades)
            {
                var transfer = t.Fees?.FirstOrDefault()?.Transfer;

                if (transfer != null)
                {
                    if (marketAssetRecord.AssetId == transfer.Asset)
                    {
                        marketAssetRecord.FeeSize += (double)transfer.Volume;
                        marketAssetRecord.FeeType  = FeeType.Absolute;
                    }
                    else
                    {
                        limitAssetRecord.FeeSize += (double)transfer.Volume;
                        limitAssetRecord.FeeType  = FeeType.Absolute;
                    }
                }
            }

            marketAssetRecord.Id = Core.Domain.CashOperations.Utils.GenerateRecordId(marketAssetRecord.DateTime);
            limitAssetRecord.Id  = Core.Domain.CashOperations.Utils.GenerateRecordId(limitAssetRecord.DateTime);

            return(new[] { marketAssetRecord, limitAssetRecord });
        }
        public void Test_OneTrade_Market()
        {
            var trades = new List <TradeQueueItem.TradeInfo>()
            {
                new TradeQueueItem.TradeInfo
                {
                    MarketVolume = 1,
                    LimitVolume  = 8000,
                    Price        = 9000,
                    MarketAsset  = "BTC",
                    LimitAsset   = "USD"
                }
            };

            Assert.Equal(9000, ConvertExtensions.CalcEffectivePrice(trades, GetDefaultPair(), false));
        }
        public void Test_OneTrade_Limit()
        {
            var trades = new List <LimitQueueItem.LimitTradeInfo>()
            {
                new LimitQueueItem.LimitTradeInfo
                {
                    Volume         = 1,
                    OppositeVolume = 8000,
                    Price          = 9000,
                    Asset          = "BTC",
                    OppositeAsset  = "USD"
                }
            };

            Assert.Equal(9000, ConvertExtensions.CalcEffectivePrice(trades, GetDefaultPair(), false));
        }
        public void Test_Trade_With_Zero_Volume()
        {
            var trades = new List <TradeQueueItem.TradeInfo>()
            {
                new TradeQueueItem.TradeInfo
                {
                    MarketVolume = 0.000001,
                    LimitVolume  = 0,
                    Price        = 9000,
                    MarketAsset  = "BTC",
                    LimitAsset   = "USD"
                },
                new TradeQueueItem.TradeInfo
                {
                    MarketVolume = 0.3,
                    LimitVolume  = 2745,
                    Price        = 9150,
                    MarketAsset  = "BTC",
                    LimitAsset   = "USD"
                }
            };

            Assert.Equal(9150, ConvertExtensions.CalcEffectivePrice(trades, GetDefaultPair(), false));
        }