Beispiel #1
0
        public void AddGranularity_Existing()
        {
            // Arrange
            var instrumentName = Contracts.InstrumentName.EUR_USD;
            var instrument     = new Database.Instrument()
            {
                Name = instrumentName
            };
            var granularityName = Contracts.Granularity.H1;
            var granularity     = new Database.InstrumentGranularity()
            {
                Id          = Guid.NewGuid(),
                Instrument  = instrument,
                Granularity = granularityName,
                State       = Database.GranularityState.New
            };

            _dbContext.Add(instrument);
            _dbContext.SaveChanges();

            var granularityToAdd = new Contracts.InstrumentGranularityCreation()
            {
                Granularity = granularityName
            };

            // Act
            var instrumentGranularity = _instrumentService.AddGranularity(instrumentName, granularityToAdd);

            // Assert
            Assert.Equal(instrumentName, instrumentGranularity.Name);
            Assert.Single(instrumentGranularity.Granularities);
            Assert.Equal(granularity.Granularity, instrumentGranularity.Granularities.ElementAt(0).Granularity);
        }
Beispiel #2
0
        public void AddInstrument_Existing()
        {
            // Arrange
            var instrumentName = Contracts.InstrumentName.EUR_USD;
            var instrument1    = new Database.Instrument()
            {
                Name = instrumentName
            };
            var granularity1 = new Database.InstrumentGranularity()
            {
                Id          = Guid.NewGuid(),
                Instrument  = instrument1,
                Granularity = Contracts.Granularity.H1,
                State       = Database.GranularityState.New
            };

            instrument1.Granularities.Add(granularity1);
            _dbContext.Add(instrument1);
            _dbContext.SaveChanges();

            var instrument = new Contracts.InstrumentCreation()
            {
                Name = instrumentName
            };

            // Act
            var instrumentAdded = _instrumentService.AddInstrument(instrument);

            // Assert
            Assert.Equal(instrumentName, instrumentAdded.Name);
        }
Beispiel #3
0
        public void GetAllInstruments()
        {
            // Arrange
            var instrument1 = new Database.Instrument()
            {
                Name = Contracts.InstrumentName.EUR_USD
            };
            var granularity1 = new Database.InstrumentGranularity()
            {
                Id          = Guid.NewGuid(),
                Instrument  = instrument1,
                Granularity = Contracts.Granularity.H1,
                State       = Database.GranularityState.New
            };

            instrument1.Granularities.Add(granularity1);
            _dbContext.Add(instrument1);

            var instrument2 = new Database.Instrument()
            {
                Name = Contracts.InstrumentName.NZD_USD
            };
            var granularity2 = new Database.InstrumentGranularity()
            {
                Id          = Guid.NewGuid(),
                Instrument  = instrument2,
                Granularity = Contracts.Granularity.M1,
                State       = Database.GranularityState.Tradeable
            };

            instrument2.Granularities.Add(granularity2);
            _dbContext.Add(instrument2);

            _dbContext.SaveChanges();

            // Act
            var instrumentGranularities = _instrumentService.GetAllInstruments();

            // Assert
            instrumentGranularities = instrumentGranularities.OrderBy(instrument => instrument.Name);
            Assert.Equal(2, instrumentGranularities.Count());
            Assert.Equal(instrument1.Name, instrumentGranularities.ElementAt(0).Name);
            Assert.Single(instrumentGranularities.ElementAt(0).Granularities);
            Assert.Equal(granularity1.Granularity, instrumentGranularities.ElementAt(0).Granularities.ElementAt(0).Granularity);
            Assert.Equal(instrument2.Name, instrumentGranularities.ElementAt(1).Name);
            Assert.Single(instrumentGranularities.ElementAt(1).Granularities);
            Assert.Equal(granularity2.Granularity, instrumentGranularities.ElementAt(1).Granularities.ElementAt(0).Granularity);
        }
Beispiel #4
0
        public async void Tick()
        {
            // Arrange
            var instrument = new Database.Instrument()
            {
                Name = Contracts.InstrumentName.EUR_USD
            };
            var granularity = new Database.InstrumentGranularity()
            {
                Id          = Guid.NewGuid(),
                Instrument  = instrument,
                Granularity = Contracts.Granularity.M1,
                State       = Database.GranularityState.Tradeable
            };

            instrument.Granularities.Add(granularity);
            _dbContext.Add(instrument);
            _dbContext.SaveChanges();

            _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "Oanda-MasterToken")]).Returns("TopSecretOandaToken");
            var oandaLastTwoCandles = new List <Candlestick>()
            {
                new Candlestick()
                {
                    Time     = DateTime.UtcNow.AddMinutes(-1).ToShortDateString(),
                    Volume   = 12345,
                    Complete = true,
                    Bid      = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    },
                    Mid = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    },
                    Ask = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    }
                },
                new Candlestick()
                {
                    Time     = DateTime.UtcNow.ToShortDateString(),
                    Volume   = 12345,
                    Complete = false,
                    Bid      = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    },
                    Mid = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    },
                    Ask = new CandlestickData()
                    {
                        O = 1.2345,
                        H = 1.2345,
                        L = 1.2345,
                        C = 1.2345
                    }
                }
            };
            var oandaInstrumentMock = new Mock <IInstrument>();

            oandaInstrumentMock.Setup(oi => oi.GetLastNCandlesAsync(
                                          CandlestickGranularity.M1,
                                          2,
                                          It.IsAny <IEnumerable <PricingComponent> >()
                                          )).Returns(Task.FromResult <IEnumerable <Candlestick> >(oandaLastTwoCandles));
            var oandaConnectionMock = new Mock <IOandaApiConnection>();

            oandaConnectionMock.Setup(oc => oc.GetInstrument(It.IsAny <InstrumentName>())).Returns(oandaInstrumentMock.Object);
            _oandaApiConnectionFactoryMock.Setup(oacf => oacf.CreateConnection(It.IsAny <OandaConnectionType>(), It.IsAny <string>(), It.IsAny <DateTimeFormat>())).Returns(oandaConnectionMock.Object);

            _instrumentStorageServiceMock.Setup(iss => iss.StoreInstrumentCandles(It.IsAny <Contracts.InstrumentWithCandles>()));

            var thorHttpClientFactoryName = "LokiIsSoOverrated";

            _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "forex-miner-thor:Name")]).Returns(thorHttpClientFactoryName);
            _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "forex-miner-thor:Content-Type")]).Returns("application/json");
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();
            var thorResponse           = new HttpResponseMessage()
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent(GetSerializedTradeSignal(instrument.Name))
            };

            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(thorResponse);
            var httpClient = new HttpClient(httpMessageHandlerMock.Object)
            {
                BaseAddress = new Uri("https://this-must-be-thor.ai")
            };

            _httpClientFactoryMock.Setup(hcf => hcf.CreateClient(thorHttpClientFactoryName)).Returns(httpClient);

            var connection = new Connection()
            {
                Id                = Guid.NewGuid(),
                Broker            = ForexMiner.Heimdallr.Common.Data.Database.Models.Connection.Broker.Oanda,
                ExternalAccountId = "123456",
                Name              = "This is a test connection",
                Owner             = null,
                Type              = ForexMiner.Heimdallr.Common.Data.Contracts.Connection.ConnectionType.Demo
            };

            _dbContext.Add(connection);
            _dbContext.SaveChanges();

            _connectionsSecretServiceMock.Setup(css => css.GetConnectionSecret(It.IsAny <Guid>()));
            var oandaAccountMock = new Mock <IAccount>();

            oandaAccountMock.Setup(oa => oa.GetDetailsAsync()).Returns(Task.FromResult(
                                                                           new GeriRemenyi.Oanda.V20.Client.Model.Account()
            {
                Balance      = 1000,
                UnrealizedPL = 100
            }
                                                                           ));
            var oandaTradesMock = new Mock <ITrades>();

            oandaTradesMock.Setup(ot => ot.OpenTradeAsync(
                                      It.IsAny <InstrumentName>(),
                                      It.IsAny <GeriRemenyi.Oanda.V20.Sdk.Trade.TradeDirection>(),
                                      It.IsAny <long>(),
                                      It.IsAny <int>()
                                      ));
            oandaAccountMock.SetupGet(oa => oa.Trades).Returns(oandaTradesMock.Object);
            oandaConnectionMock.Setup(oc => oc.GetAccount(It.IsAny <string>())).Returns(oandaAccountMock.Object);

            // Act
            await _tickerService.Tick();

            // Assert
            oandaInstrumentMock.Verify(oi => oi.GetLastNCandlesAsync(CandlestickGranularity.M1, 2, It.IsAny <IEnumerable <PricingComponent> >()), Times.Once());
            _instrumentStorageServiceMock.Verify(iss => iss.StoreInstrumentCandles(It.IsAny <Contracts.InstrumentWithCandles>()), Times.Once());
            _httpClientFactoryMock.Verify(hcf => hcf.CreateClient(thorHttpClientFactoryName), Times.Once());
            _connectionsSecretServiceMock.Verify(css => css.GetConnectionSecret(It.IsAny <Guid>()), Times.Once());
            oandaTradesMock.Verify(ot => ot.OpenTradeAsync(
                                       It.IsAny <InstrumentName>(),
                                       It.IsAny <GeriRemenyi.Oanda.V20.Sdk.Trade.TradeDirection>(),
                                       It.IsAny <long>(),
                                       It.IsAny <int>()
                                       ), Times.Once());
        }