Example #1
0
        public void LastPriceFxDataIsInvertedWhenNecessary()
        {
            var inst = new Instrument {
                Symbol = "USD.CAD", ID = 1, AssetCategory = AssetClass.Cash
            };
            var startDate = new DateTime(2000, 1, 1);

            var fxrSetStub = new DbSetStub <FXRate>();

            fxrSetStub.Add(new FXRate {
                FromCurrencyID = 2, ToCurrencyID = 1, Date = startDate, Rate = 1.1m
            });

            var currencySetStub = new DbSetStub <Currency>();

            currencySetStub.Add(new Currency {
                ID = 2, Name = "CAD"
            });

            _contextMock.Setup(x => x.Currencies).Returns(currencySetStub);
            _contextMock.Setup(x => x.FXRates).Returns(fxrSetStub);

            decimal fxRate;
            var     price = _datasourcer.GetLastPrice(inst, out fxRate);

            Assert.AreEqual(1m / 1.1m, price);
        }
Example #2
0
        public async void ExternalDataIsSupplementedWithLocalDataWhenObservationsAreMissing()
        {
            DateTime startDate = new DateTime(2000, 1, 1);
            DateTime endDate   = new DateTime(2000, 1, 3);
            var      inst      = new Instrument {
                ID = 1, Symbol = "SPY", AssetCategory = AssetClass.Stock, QDMSInstrumentID = 2
            };

            var data = new List <OHLCBar>
            {
                new OHLCBar {
                    Open = 1, High = 2, Low = 0, Close = 1, DT = new DateTime(2000, 1, 1)
                },
                new OHLCBar {
                    Open = 1, High = 2, Low = 0, Close = 1, DT = new DateTime(2000, 1, 2)
                },
            };

            var ppSetStub = new DbSetStub <PriorPosition>();

            _contextMock.Setup(x => x.PriorPositions).Returns(ppSetStub);

            _externalSourceMock.Setup(
                x => x.GetData(It.IsAny <Instrument>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <BarSize>()))
            .ReturnsAsync(data);

            await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            _contextMock.Verify(x => x.PriorPositions);
        }
Example #3
0
        public async void FxDataIsInvertedWhenNecessary()
        {
            var inst = new Instrument {
                Symbol = "USD.CAD", ID = 1, AssetCategory = AssetClass.Cash
            };
            var startDate = new DateTime(2000, 1, 1);
            var endDate   = new DateTime(2000, 1, 1);

            var fxrSetStub = new DbSetStub <FXRate>();

            fxrSetStub.Add(new FXRate {
                FromCurrencyID = 2, ToCurrencyID = 1, Date = startDate, Rate = 1.1m
            });

            var currencySetStub = new DbSetStub <Currency>();

            currencySetStub.Add(new Currency {
                ID = 2, Name = "CAD"
            });

            _contextMock.Setup(x => x.Currencies).Returns(currencySetStub);
            _contextMock.Setup(x => x.FXRates).Returns(fxrSetStub);

            var data = await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            Assert.AreEqual(1m / 1.1m, data[0].Close);
        }
Example #4
0
        public async void IfExternalSourceHasNoDataLocalBackupIsUsedInstead()
        {
            DateTime startDate = new DateTime(2000, 1, 1);
            DateTime endDate   = new DateTime(2000, 2, 1);
            var      inst      = new Instrument {
                ID = 1, Symbol = "SPY", AssetCategory = AssetClass.Stock, QDMSInstrumentID = 2
            };

            var ppSetStub = new DbSetStub <PriorPosition>();

            _contextMock.Setup(x => x.PriorPositions).Returns(ppSetStub);

            _externalSourceMock.Setup(
                x => x.GetData(It.IsAny <Instrument>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <BarSize>()))
            .ReturnsAsync(new List <OHLCBar>());

            await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            _externalSourceMock.Verify(x => x.GetData(
                                           It.IsAny <Instrument>(),
                                           It.IsAny <DateTime>(),
                                           It.IsAny <DateTime>(),
                                           It.IsAny <BarSize>()));

            _contextMock.Verify(x => x.PriorPositions);
        }
Example #5
0
        public async void CashInstrumentLocalRequestsUseFxRates()
        {
            DateTime startDate = new DateTime(2000, 1, 1);
            DateTime endDate   = new DateTime(2000, 1, 3);
            var      inst      = new Instrument {
                ID = 1, Symbol = "EUR.USD", AssetCategory = AssetClass.Cash, QDMSInstrumentID = 2
            };

            var fxrSetStub = new DbSetStub <FXRate>();

            var currencySetStub = new DbSetStub <Currency>();

            currencySetStub.Add(new Currency {
                ID = 2, Name = "EUR"
            });

            _contextMock.Setup(x => x.Currencies).Returns(currencySetStub);
            _contextMock.Setup(x => x.FXRates).Returns(fxrSetStub);

            _externalSourceMock.Setup(
                x => x.GetData(It.IsAny <Instrument>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <BarSize>()))
            .ReturnsAsync(new List <OHLCBar>());

            await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            _contextMock.Verify(x => x.FXRates);
        }
Example #6
0
        public void SetUp()
        {
            _inst = new Instrument {
                ID = 1, Multiplier = 1, AssetCategory = AssetClass.Stock
            };

            _t = new Trade
            {
                Orders           = new List <Order>(),
                CashTransactions = new List <CashTransaction>(),
                FXTransactions   = new List <FXTransaction>()
            };

            _dsMock = new Mock <IDataSourcer>();

            _dsMock.Setup(x => x.GetData(It.IsAny <Instrument>(), It.IsAny <DateTime>(), It.IsAny <DateTime>(), It.IsAny <QDMS.BarSize>()))
            .Returns <Instrument, DateTime, DateTime, QDMS.BarSize>((a, b, c, d) => Task.FromResult(GenerateData(b, c)));

            _contextMock = new Mock <IDBContext>();

            _dbSetStub = new DbSetStub <EquitySummary>();
            var equitySummaries = new List <EquitySummary>
            {
                new EquitySummary
                {
                    Date  = new DateTime(2000, 1, 1),
                    Total = 10000
                }
            };

            _dbSetStub.AddRange(equitySummaries);
            _contextMock.SetupGet(x => x.EquitySummaries).Returns(_dbSetStub);

            _repository = new TradesRepository(_contextMock.Object, _dsMock.Object, 0.1m);
        }
Example #7
0
        public async void DataIsCachedBetweenRequests()
        {
            DateTime startDate = new DateTime(2000, 1, 1);
            DateTime endDate   = new DateTime(2000, 1, 2);
            var      inst      = new Instrument {
                ID = 1, Symbol = "SPY", AssetCategory = AssetClass.Stock, QDMSInstrumentID = 2
            };

            var data = new List <OHLCBar>
            {
                new OHLCBar {
                    Open = 1, High = 2, Low = 0, Close = 1, DT = new DateTime(2000, 1, 1)
                },
                new OHLCBar {
                    Open = 1, High = 2, Low = 0, Close = 1, DT = new DateTime(2000, 1, 2)
                },
            };

            var ppSetStub = new DbSetStub <PriorPosition>();

            _contextMock.Setup(x => x.PriorPositions).Returns(ppSetStub);

            _externalSourceMock.Setup(
                x => x.GetData(It.IsAny <Instrument>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <DateTime>(),
                               It.IsAny <BarSize>()))
            .ReturnsAsync(data);

            await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            //request a second time: should use cache instead of external source
            await _datasourcer.GetData(inst, startDate, endDate).ConfigureAwait(true);

            _externalSourceMock.Verify(x => x.GetData(
                                           It.IsAny <Instrument>(),
                                           It.IsAny <DateTime>(),
                                           It.IsAny <DateTime>(),
                                           It.IsAny <BarSize>()),
                                       Times.Once);
        }