/// <summary> /// Connects the client to the broker's remote servers /// </summary> public override void Connect() { if (IsConnected) { return; } _sockClient.ConnectAsync().SynchronouslyAwaitTask(); _natsClient.Open(); _isConnected = true; // create new thread to manage disconnections and reconnections var connectionMonitorStartedEvent = new AutoResetEvent(false); _cancellationTokenSource = new CancellationTokenSource(); _connectionMonitorThread = new Thread(() => { connectionMonitorStartedEvent.Set(); var nextReconnectionAttemptSeconds = 1; try { while (!_cancellationTokenSource.IsCancellationRequested) { var isAlive = true; try { isAlive = _sockClient.IsAlive; } catch (Exception) { // ignored } if (isAlive && _connectionLost) { _connectionLost = false; nextReconnectionAttemptSeconds = 1; OnMessage(BrokerageMessageEvent.Reconnected("Connection with Alpaca server restored.")); } else if (!isAlive) { if (_connectionLost) { try { Thread.Sleep(TimeSpan.FromSeconds(nextReconnectionAttemptSeconds)); _sockClient.ConnectAsync().SynchronouslyAwaitTask(); } catch (Exception exception) { // double the interval between attempts (capped to 1 minute) nextReconnectionAttemptSeconds = Math.Min(nextReconnectionAttemptSeconds * 2, 60); Log.Error(exception); } } else { _connectionLost = true; OnMessage( BrokerageMessageEvent.Disconnected( "Connection with Alpaca server lost. " + "This could be because of internet connectivity issues. ")); } } Thread.Sleep(1000); } } catch (Exception exception) { Log.Error(exception); } }) { IsBackground = true }; _connectionMonitorThread.Start(); connectionMonitorStartedEvent.WaitOne(); }
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; }