protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider) { var restClient = new RestClient("https://api.pro.coinbase.com"); var webSocketClient = new WebSocketClientWrapper(); var securities = new SecurityManager(new TimeKeeper(DateTime.UtcNow, TimeZones.NewYork)) { { Symbol, CreateSecurity(Symbol) } }; var transactions = new SecurityTransactionManager(null, securities); transactions.SetOrderProcessor(new FakeOrderProcessor()); var algorithm = new Mock <IAlgorithm>(); algorithm.Setup(a => a.Transactions).Returns(transactions); algorithm.Setup(a => a.BrokerageModel).Returns(new GDAXBrokerageModel()); algorithm.Setup(a => a.Portfolio).Returns(new SecurityPortfolioManager(securities, transactions)); algorithm.Setup(a => a.Securities).Returns(securities); var priceProvider = new Mock <IPriceProvider>(); priceProvider.Setup(a => a.GetLastPrice(It.IsAny <Symbol>())).Returns(1.234m); var aggregator = new AggregationManager(); return(new GDAXBrokerage(Config.Get("gdax-url", "wss://ws-feed.pro.coinbase.com"), webSocketClient, restClient, Config.Get("gdax-api-key"), Config.Get("gdax-api-secret"), Config.Get("gdax-passphrase"), algorithm.Object, priceProvider.Object, aggregator)); }
/// <summary> /// Creates the brokerage under test and connects it /// </summary> /// <returns>A connected brokerage instance</returns> protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider) { var environment = Config.Get("oanda-environment").ConvertTo <Environment>(); var accessToken = Config.Get("oanda-access-token"); var accountId = Config.Get("oanda-account-id"); var aggregator = new AggregationManager(); return(new OandaBrokerage(orderProvider, securityProvider, aggregator, environment, accessToken, accountId)); }
/// <summary> /// Creates the brokerage under test and connects it /// </summary> /// <returns>A connected brokerage instance</returns> protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider) { var keyId = Config.Get("alpaca-key-id"); var secretKey = Config.Get("alpaca-secret-key"); var tradingMode = Config.Get("alpaca-trading-mode"); var aggregator = new AggregationManager(); return(new AlpacaBrokerage(orderProvider, securityProvider, keyId, secretKey, tradingMode, false, aggregator)); }
public void AggregationTest_AddToMetaRepo() { // Assemble int count = 1; // Action AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToMetaRepo("testkey", count, _fakeMessage); // Assert Assert.True(objectUnderTest._metaRepo.Count == 1); }
public void AggregationTest_AddToAllocationRepo() { // Assemble Guid fileID = Guid.NewGuid(); int count = 1; // Action AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToAllocationRepo(fileID.ToString(), count.ToString()); // Assert Assert.True(objectUnderTest._allocationRepo.Count == 1); }
public void GetsHistory(Symbol symbol, Resolution resolution, TickType tickType, TimeSpan period, bool shouldBeEmpty) { var restClient = new RestClient("https://api.pro.coinbase.com"); var webSocketClient = new WebSocketClientWrapper(); var aggregator = new AggregationManager(); var brokerage = new GDAXBrokerage( Config.Get("gdax-url", "wss://ws-feed.pro.coinbase.com"), webSocketClient, restClient, Config.Get("gdax-api-key"), Config.Get("gdax-api-secret"), Config.Get("gdax-passphrase"), null, null, aggregator, null); var historyProvider = new BrokerageHistoryProvider(); historyProvider.SetBrokerage(brokerage); historyProvider.Initialize(new HistoryProviderInitializeParameters(null, null, null, null, null, null, null, false, new DataPermissionManager())); var now = DateTime.UtcNow; var requests = new[] { new HistoryRequest(now.Add(-period), now, typeof(TradeBar), symbol, resolution, SecurityExchangeHours.AlwaysOpen(TimeZones.Utc), DateTimeZone.Utc, resolution, false, false, DataNormalizationMode.Adjusted, tickType) }; var history = historyProvider.GetHistory(requests, TimeZones.Utc).ToList(); foreach (var slice in history) { var bar = slice.Bars[symbol]; Log.Trace($"{bar.Time}: {bar.Symbol} - O={bar.Open}, H={bar.High}, L={bar.Low}, C={bar.Close}, V={bar.Volume}"); } if (shouldBeEmpty) { Assert.IsTrue(history.Count == 0); } else { Assert.IsTrue(history.Count > 0); } Log.Trace("Data points retrieved: " + historyProvider.DataPointCount); }
public void AggregationTest_AddToMetaRepo_When_Exception() { // Assemble int count = 1; _logger.Setup(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); // Action AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); Action act = () => objectUnderTest.AddToMetaRepo(string.Empty, count, _fakeMessage); // Assert Assert.Throws <Exception>(act); _logger.Verify(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once()); }
public void AggregationTest_GetMetaCount_IfMetaMessageIsNull_Exception() { //Assemble int expectedValue = 0; _logger.Setup(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); //Action var result = objectUnderTest.GetMetaCount(null); //Assert Assert.True(result == expectedValue); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once()); }
public void AggregationTest_GetMetaCountByKey_With_NullOrEmptyKey() { //assemble int expectedValue = 1; _logger.Setup(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); //action int actualValue = objectUnderTest.GetMetaCountByKey(string.Empty); //assert Assert.NotEqual(expectedValue, actualValue); _logger.Verify(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once()); }
private static GDAXBrokerage GetBrokerage() { var wssUrl = Config.Get("gdax-url", "wss://ws-feed.pro.coinbase.com"); var webSocketClient = new WebSocketClientWrapper(); var restClient = new RestClient("https://api.pro.coinbase.com"); var apiKey = Config.Get("gdax-api-key"); var apiSecret = Config.Get("gdax-api-secret"); var passPhrase = Config.Get("gdax-passphrase"); var algorithm = new QCAlgorithm(); var userId = Config.GetInt("job-user-id"); var userToken = Config.Get("api-access-token"); var priceProvider = new ApiPriceProvider(userId, userToken); var aggregator = new AggregationManager(); return(new GDAXBrokerage(wssUrl, webSocketClient, restClient, apiKey, apiSecret, passPhrase, algorithm, priceProvider, aggregator)); }
protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider) { var restClient = new RestClient("https://api.pro.coinbase.com"); var webSocketClient = new WebSocketClientWrapper(); var algorithm = new Mock <IAlgorithm>(); algorithm.Setup(a => a.BrokerageModel).Returns(new GDAXBrokerageModel(AccountType.Cash)); var priceProvider = new ApiPriceProvider(Config.GetInt("job-user-id"), Config.Get("api-access-token")); var aggregator = new AggregationManager(); return(new GDAXBrokerage(Config.Get("gdax-url", "wss://ws-feed.pro.coinbase.com"), webSocketClient, restClient, Config.Get("gdax-api-key"), Config.Get("gdax-api-secret"), Config.Get("gdax-passphrase"), algorithm.Object, priceProvider, aggregator)); }
public void AggregationManager_EnrichAndSendMessage_When_FileAllocationCountIsZero() { //Arrange int fileAllocationCount = 0; string key = Guid.Parse("247d7eb3-2fbe-4b3c-84ef-f3580f5c6331").ToString(); _publisher.Setup(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToAllocationRepo(key, @"{""someJasonName"":""somevalue1""}"); //Action var result = objectUnderTest.EnrichAndSendMessage(key, Guid.Parse(key), fileAllocationCount, "topic"); //Assert Assert.True(result); _publisher.Verify(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()), Times.Never()); }
public void AggregationTest_GetMetaCount_ReturnExpectedValue() { //assemble JsonObject fakeMessage = TestHelper.getFileInfo(); fakeMessage.Add("fileID", Guid.NewGuid().ToString()); fakeMessage.Add(participantAmountKeyword, 100); string jsonMessage = fakeMessage.ToString(); int expectedValue = 100; AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); //action int actualValue = objectUnderTest.GetMetaCount(jsonMessage); //assert Assert.Equal(expectedValue, actualValue); }
public void AggregationTest_GetMetaCountByKey_With_InvalidKey() { //assemble int expectedValue = 1; _logger.Setup(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); //action objectUnderTest.AddToMetaRepo("testkey", 1, _fakeMessage); int actualValue = objectUnderTest.GetMetaCountByKey("blahblah"); //assert Assert.NotEqual(expectedValue, actualValue); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Never()); }
public void AggregationManager_EnrichAndSendMessage_WhenNoAllocationMessage() { //Arrange string key = Guid.Parse("247d7eb3-2fbe-4b3c-84ef-f3580f5c6331").ToString(); string actualMessageToPublish = getActualMessage(); _publisher.Setup(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToMetaRepo(key, 1, getMetaMessage(key)); //Action var result = objectUnderTest.EnrichAndSendMessage(key, Guid.Parse(key), 3, "topic"); //Assert Assert.True(result); _publisher.Verify(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()), Times.Never()); }
/// <summary> /// Initializes a new instance of the <see cref="FuncDataQueueHandler"/> class /// </summary> /// <param name="getNextTicksFunction">The functional implementation to get ticks function</param> /// <param name="timeProvider">The time provider to use</param> public FuncDataQueueHandler(Func <FuncDataQueueHandler, IEnumerable <BaseData> > getNextTicksFunction, ITimeProvider timeProvider) { _subscriptions = new HashSet <SubscriptionDataConfig>(); _cancellationTokenSource = new CancellationTokenSource(); _aggregationManager = new TestAggregationManager(timeProvider); _subscriptionManager = new FakeDataQueuehandlerSubscriptionManager((t) => "quote-trade"); Task.Factory.StartNew(() => { while (!_cancellationTokenSource.IsCancellationRequested) { var emitted = false; try { foreach (var baseData in getNextTicksFunction(this)) { if (_cancellationTokenSource.IsCancellationRequested) { break; } emitted = true; _aggregationManager.Update(baseData); } } catch (Exception exception) { if (exception is ObjectDisposedException) { return; } Log.Error(exception); } if (!emitted) { Thread.Sleep(50); } else { Thread.Sleep(10); } } }, TaskCreationOptions.LongRunning); }
public void AggregationManager_EnrichAndSendMessage_When_Exception_While_Enriching() { //Arrange int fileAllocationCount = 1; string key = Guid.Parse("247d7eb3-2fbe-4b3c-84ef-f3580f5c6331").ToString(); _logger.Setup(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); _publisher.Setup(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToAllocationRepo(key, "{'someJasonName':'somevalue1'}"); //Action var result = objectUnderTest.EnrichAndSendMessage(key, Guid.Parse(key), fileAllocationCount, "topic"); //Assert Assert.False(result); _publisher.Verify(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()), Times.Never()); _logger.Verify(x => x.Log(LogLevel.Critical, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once()); }
public void AggregationTest_GetMetaCount_IfNoParticipantAmountKeyword_Exception() { //Assemble int expectedValue = 0; JsonObject fakeMessage = TestHelper.getFileInfo(); fakeMessage.Add("fileID", Guid.NewGuid().ToString()); string jsonMessage = fakeMessage.ToString(); _logger.Setup(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); //Action var result = objectUnderTest.GetMetaCount(jsonMessage); //Assert Assert.True(result == expectedValue); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once()); }
public void AggregationManager_EnrichAndSendMessage() { //Arrange string key = Guid.Parse("247d7eb3-2fbe-4b3c-84ef-f3580f5c6331").ToString(); string actualMessageToPublish = getActualMessage(); string expectedMessageToPublish = string.Empty; _publisher.Setup(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())) .Callback <string, string, Guid>((c1, c2, c3) => { expectedMessageToPublish = c1; }); AggregationManager objectUnderTest = new AggregationManager(_logger.Object, _publisher.Object, _allocationRepo, _metaRepo); objectUnderTest.AddToMetaRepo(key, 1, getMetaMessage(key)); objectUnderTest.AddToAllocationRepo(key, @"{""someJasonName"":""somevalue1""}"); objectUnderTest.AddToAllocationRepo(key, @"{""someJasonName"":""somevalue2""}"); objectUnderTest.AddToAllocationRepo(key, @"{""someJasonName"":""somevalue3""}"); //Action var result = objectUnderTest.EnrichAndSendMessage(key, Guid.Parse(key), 3, "topic"); //Assert Assert.True(result); Assert.True(actualMessageToPublish == expectedMessageToPublish); _publisher.Verify(p => p.SendMessage(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()), Times.Once()); }
public void GetsHistory(Symbol symbol, Resolution resolution, TimeSpan period, bool shouldBeEmpty) { Log.LogHandler = new ConsoleLogHandler(); var keyId = Config.Get("alpaca-key-id"); var secretKey = Config.Get("alpaca-secret-key"); var tradingMode = Config.Get("alpaca-trading-mode"); var aggregator = new AggregationManager(); using (var brokerage = new AlpacaBrokerage(null, null, keyId, secretKey, tradingMode, true, aggregator)) { var historyProvider = new BrokerageHistoryProvider(); historyProvider.SetBrokerage(brokerage); historyProvider.Initialize(new HistoryProviderInitializeParameters(null, null, null, null, null, null, null, false, null)); var now = DateTime.UtcNow; var requests = new[] { new HistoryRequest( now.Add(-period), now, typeof(TradeBar), symbol, resolution, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), TimeZones.NewYork, null, false, false, DataNormalizationMode.Adjusted, TickType.Trade) }; var history = historyProvider.GetHistory(requests, TimeZones.NewYork).ToList(); foreach (var slice in history) { if (resolution == Resolution.Tick) { foreach (var tick in slice.Ticks[symbol]) { Console.WriteLine($"{tick.Time}: {tick.Symbol} - P={tick.Price}, Q={tick.Quantity}"); } } else { var bar = slice.Bars[symbol]; Console.WriteLine($"{bar.Time}: {bar.Symbol} - O={bar.Open}, H={bar.High}, L={bar.Low}, C={bar.Close}, V={bar.Volume}"); } } if (shouldBeEmpty) { Assert.IsTrue(history.Count == 0); } else { Assert.IsTrue(history.Count > 0); } brokerage.Disconnect(); Log.Trace("Data points retrieved: " + historyProvider.DataPointCount); } }