public void ApplyCommissionInPrices_1()
        {
            var setting = new SimBaseInstrumentSetting();

            setting.SourceExchange.Add("bitstamp");

            var store = new Mock <ITickPriceStore>();

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6000, 6100, "bitstamp", "BTCUSD"));

            var orderBookData = new FakeOrderBookProvider();
            var tickPriceData = new FakeTickPriceProvider();

            var commisiionRepo = new Mock <IExchangeCommissionSettingRepository>();

            commisiionRepo.Setup(e => e.GetSettingsByExchange(It.IsAny <string>())).Returns(Task.FromResult <IExchangeCommissionSetting>(new FakeExchangeCommissionSetting()));
            commisiionRepo.Setup(e => e.GetSettingsByExchange("bitstamp")).Returns(Task.FromResult <IExchangeCommissionSetting>(new FakeExchangeCommissionSetting()
            {
                ExchangeName           = "bitstamp",
                TradeCommissionPerc    = 10,
                WithdrawCommissionPerc = 0
            }));

            var service = new SimBaseInstrumentService(orderBookData, tickPriceData, store.Object, setting, commisiionRepo.Object, EmptyLogFactory.Instance);

            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(1, orderBookData.Data.Count);
            Assert.AreEqual(1, tickPriceData.Data.Count);

            var tp = tickPriceData.Data.First();

            Assert.AreEqual(6055.01m, tp.Ask);
            Assert.AreEqual(6054.99m, tp.Bid);
        }
        public void SkipAction_IfBigChangePrice()
        {
            var setting = new SimBaseInstrumentSetting();

            setting.SourceExchange.Add("bitstamp");
            setting.DangerChangePriceKoef = 0.1m;

            var store = new Mock <ITickPriceStore>();

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6049, 6051, "bitstamp", "BTCUSD"));

            var orderBookData = new FakeOrderBookProvider();
            var tickPriceData = new FakeTickPriceProvider();

            var service = new SimBaseInstrumentService(orderBookData, tickPriceData, store.Object, setting, GetEmptyCommission(), EmptyLogFactory.Instance);

            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(1, orderBookData.Data.Count);
            Assert.AreEqual(1, tickPriceData.Data.Count);

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6049, 6051, "bitstamp", "BTCUSD"));

            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(2, orderBookData.Data.Count);
            Assert.AreEqual(2, tickPriceData.Data.Count);
        }
        public void ProvidePriceFromOneSource()
        {
            var setting = new SimBaseInstrumentSetting();

            setting.SourceExchange.Add("bitstamp");

            var store = new Mock <ITickPriceStore>();

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6000, 6100, "bitstamp", "BTCUSD"));

            var orderBookData = new FakeOrderBookProvider();
            var tickPriceData = new FakeTickPriceProvider();

            var service = new SimBaseInstrumentService(orderBookData, tickPriceData, store.Object, setting, GetEmptyCommission(), EmptyLogFactory.Instance);

            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(1, orderBookData.Data.Count);
            Assert.AreEqual(1, tickPriceData.Data.Count);

            var ob = orderBookData.Data.First();
            var tp = tickPriceData.Data.First();

            Assert.AreEqual(1, ob.Asks.Count());
            Assert.AreEqual(1, ob.Bids.Count());
            Assert.AreEqual(6050.01m, ob.Asks.First().Price);
            Assert.AreEqual(6049.99m, ob.Bids.First().Price);
            Assert.AreEqual(setting.FakeVolume, ob.Asks.First().Volume);
            Assert.AreEqual(setting.FakeVolume, ob.Bids.First().Volume);

            Assert.AreEqual(6050.01m, tp.Ask);
            Assert.AreEqual(6049.99m, tp.Bid);
        }
        public void CalculateWithoutData()
        {
            var setting = new SimBaseInstrumentSetting();

            setting.SourceExchange.Add("bitfinex");
            setting.SourceExchange.Add("lykke");

            var store = new Mock <ITickPriceStore>();

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6000, 6050, "bitstamp", "BTCUSD"));
            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6000, 6050, "gdax", "BTCUSD"));

            var orderBookData = new FakeOrderBookProvider();
            var tickPriceData = new FakeTickPriceProvider();

            var service = new SimBaseInstrumentService(orderBookData, tickPriceData, store.Object, setting, GetEmptyCommission(), EmptyLogFactory.Instance);

            service.CalculateMarket().GetAwaiter().GetResult();
            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(0, orderBookData.Data.Count);
            Assert.AreEqual(0, tickPriceData.Data.Count);
        }
        public void ProvidePriceFromOneSource_withLinkedAssetPair()
        {
            var setting = new SimBaseInstrumentSetting();

            setting.SourceExchange.Add("bitstamp");
            setting.CrossInstrument.Add(new LinkedInstrumentSettings("BTCEUR", "EURUSD", "lykke", true));
            setting.CrossInstrument.Add(new LinkedInstrumentSettings("BTCCHF", "USDCHF", "lykke", false));

            var store = new Mock <ITickPriceStore>();

            store.Setup(e => e.GetTickPrice("bitstamp", "BTCUSD")).Returns(CreateCtickPrice(6000, 6100, "bitstamp", "BTCUSD"));
            store.Setup(e => e.GetTickPrice("lykke", "EURUSD")).Returns(CreateCtickPrice(10, 100, "lykke", "EURUSD"));
            store.Setup(e => e.GetTickPrice("lykke", "USDCHF")).Returns(CreateCtickPrice(10, 100, "lykke", "USDCHF"));

            var orderBookData = new FakeOrderBookProvider();
            var tickPriceData = new FakeTickPriceProvider();

            var service = new SimBaseInstrumentService(orderBookData, tickPriceData, store.Object, setting, GetEmptyCommission(), EmptyLogFactory.Instance);

            service.CalculateMarket().GetAwaiter().GetResult();

            Assert.AreEqual(3, orderBookData.Data.Count);
            Assert.AreEqual(3, tickPriceData.Data.Count);

            var btcusdOb = orderBookData.Data.First(e => e.Asset == "BTCUSD");
            var btcusdTp = tickPriceData.Data.First(e => e.Asset == "BTCUSD");

            var btceurOb = orderBookData.Data.First(e => e.Asset == "BTCEUR");
            var btceurTp = tickPriceData.Data.First(e => e.Asset == "BTCEUR");

            var btcchfOb = orderBookData.Data.First(e => e.Asset == "BTCCHF");
            var btcchfTp = tickPriceData.Data.First(e => e.Asset == "BTCCHF");

            // check btcusd

            Assert.AreEqual(1, btcusdOb.Asks.Count());
            Assert.AreEqual(1, btcusdOb.Bids.Count());
            Assert.AreEqual(6050.01m, btcusdOb.Asks.First().Price);
            Assert.AreEqual(6049.99m, btcusdOb.Bids.First().Price);
            Assert.AreEqual(setting.FakeVolume, btcusdOb.Asks.First().Volume);
            Assert.AreEqual(setting.FakeVolume, btcusdOb.Bids.First().Volume);

            Assert.AreEqual(6050.01m, btcusdTp.Ask);
            Assert.AreEqual(6049.99m, btcusdTp.Bid);

            // check eurusd

            var eurAsk = 605.00m;
            var eurBid = 60.50m;

            Assert.AreEqual(1, btceurOb.Asks.Count());
            Assert.AreEqual(1, btceurOb.Bids.Count());
            Assert.AreEqual(eurAsk, btceurOb.Asks.First().Price);
            Assert.AreEqual(eurBid, btceurOb.Bids.First().Price);
            Assert.AreEqual(setting.FakeVolume, btceurOb.Asks.First().Volume);
            Assert.AreEqual(setting.FakeVolume, btceurOb.Bids.First().Volume);

            Assert.AreEqual(eurAsk, btceurTp.Ask);
            Assert.AreEqual(eurBid, btceurTp.Bid);

            // check usdchf

            var chfAsk = 605001m;
            var chfBid = 60499.9m;

            Assert.AreEqual(1, btcchfOb.Asks.Count());
            Assert.AreEqual(1, btcchfOb.Bids.Count());
            Assert.AreEqual(chfAsk, btcchfOb.Asks.First().Price);
            Assert.AreEqual(chfBid, btcchfOb.Bids.First().Price);
            Assert.AreEqual(setting.FakeVolume, btcchfOb.Asks.First().Volume);
            Assert.AreEqual(setting.FakeVolume, btcchfOb.Bids.First().Volume);

            Assert.AreEqual(chfAsk, btcchfTp.Ask);
            Assert.AreEqual(chfBid, btcchfTp.Bid);
        }