/// <summary> /// Disconnects the client from the broker's remote servers /// </summary> public override void Disconnect() { // request and wait for thread to stop _cancellationTokenSource.Cancel(); _connectionMonitorThread?.Join(); _sockClient.DisconnectAsync().SynchronouslyAwaitTask(); _natsClient.Close(); _isConnected = false; }
public async Task Run() { restClient = new RestClient(API_KEY, API_SECRET, API_URL, apiVersion: 2); // First, cancel any existing orders so they don't impact our buying power. var orders = await restClient.ListOrdersAsync(); foreach (var order in orders) { await restClient.DeleteOrderAsync(order.OrderId); } // Figure out when the market will close so we can prepare to sell beforehand. var calendars = (await restClient.ListCalendarAsync(DateTime.Today)).ToList(); var calendarDate = calendars.First().TradingDate; var closingTime = calendars.First().TradingCloseTime; closingTime = new DateTime(calendarDate.Year, calendarDate.Month, calendarDate.Day, closingTime.Hour, closingTime.Minute, closingTime.Second); var today = DateTime.Today; // Get the first group of bars from today if the market has already been open. var bars = await restClient.ListMinuteAggregatesAsync(symbol, 1, today.AddDays(-5), today); foreach (var bar in bars.Items) { if (bar.Time.Date == today) { closingPrices.Add(bar.Close); } } Console.WriteLine("Waiting for market open..."); await AwaitMarketOpen(); Console.WriteLine("Market opened."); // Connect to Polygon and listen for price updates. var natsClient = new NatsClient(API_KEY, false); natsClient.Open(); Console.WriteLine("Polygon client opened."); natsClient.AggReceived += async(agg) => { // If the market's close to closing, exit position and stop trading. TimeSpan minutesUntilClose = closingTime - DateTime.UtcNow; if (minutesUntilClose.TotalMinutes < 15) { Console.WriteLine("Reached the end of trading window."); await ClosePositionAtMarket(); natsClient.Close(); } else { // Decide whether to buy or sell and submit orders. await HandleMinuteAgg(agg); } }; natsClient.SubscribeMinuteAgg(symbol); // Connect to Alpaca and listen for updates on our orders. var sockClient = new SockClient(API_KEY, API_SECRET, API_URL); await sockClient.ConnectAsync(); Console.WriteLine("Socket client opened."); sockClient.OnTradeUpdate += HandleTradeUpdate; }