I have following C# code for Alpaca data streaming API (with SDK v3.8.0-beta2) and it seem to work sometimes with paper trading during market hours. But it is flaky. When I restart the code, it starts throwing AuthStatus.Unauthorized error. During off market hours, it never works. Is this the expected behavior for paper trading?
When I switch to Live trading by commenting the first line and uncommenting the second line (in below code), it does not work at all. The code hangs on the line with await _dataStreamingClient.ConnectAndAuthenticateAsync(); I can successfully call trading API with live account. There were no issues. I know there is a web socket connection used and if it works for paper trading, it should work for live trading. Right?
Please let me know if I need to do something for this code to work in live trading.
Here is my code:
_dataStreamingClient = Alpaca.Markets.Environments.Paper.GetAlpacaDataStreamingClient(securityKey);
//_dataStreamingClient = Alpaca.Markets.Environments.Live.GetAlpacaDataStreamingClient(oAuthKey);
Console.WriteLine($"Hooking to OnError event ...");
_dataStreamingClient.OnError += (Exception ex) =>
{
Console.WriteLine($"OnError event => Exception: {ex}");
};
Console.WriteLine($"Hooking to Connected event ...");
_dataStreamingClient.Connected += (AuthStatus authStatus) =>
{
Console.WriteLine($"Connected event => AuthStatus: {authStatus}");
};
Console.WriteLine($"Hooking to SocketClosed event ...");
_dataStreamingClient.SocketClosed += () =>
{
Console.WriteLine("SocketClosed event => Socket closed.");
};
Console.WriteLine($"Hooking to SocketOpened event ...");
_dataStreamingClient.SocketOpened += () =>
{
Console.WriteLine("SocketOpened event => Socket opened.");
};
await _dataStreamingClient.ConnectAndAuthenticateAsync();
if (!string.IsNullOrWhiteSpace(symbol))
{
var tradeSubscription = _dataStreamingClient.GetTradeSubscription(symbol);
Console.WriteLine($"Hooking to trade subscription for symbol {symbol} event ...");
tradeSubscription.Received += (IStreamTrade trade) =>
{
Console.WriteLine($"Trade Streaming: {trade.Symbol}, Exchange: {trade.Exchange}, Price: {trade.Price}, Size: {trade.Size}, TimeUtc: {trade.TimeUtc}, TradeId: {trade.TradeId}, Current Time: {DateTime.UtcNow}");
};
Console.WriteLine($"Subscribing to trade for symbol {symbol} ...");
_dataStreamingClient.Subscribe(tradeSubscription);
//Quote
var quoteSubscription = _dataStreamingClient.GetQuoteSubscription(symbol);
Console.WriteLine($"Hooking to quote subscription for symbol {symbol} event ...");
quoteSubscription.Received += (IStreamQuote quote) =>
{
Console.WriteLine($"Quote Streaming: {quote.Symbol}, Exchange: {quote.AskExchange}, AskPrice: {quote.AskPrice}, AskSize: {quote.AskSize}, BidExchange: {quote.BidExchange}, BidPrice: {quote.BidPrice}, BidSize: {quote.BidSize}, TimeUtc: {quote.TimeUtc}, Current Time: {DateTime.UtcNow}");
};
Console.WriteLine($"Subscribing to quotes for symbol {symbol} ...");
_dataStreamingClient.Subscribe(quoteSubscription);
//Minute aggregated data for symbol
var minAggrSubscription = _dataStreamingClient.GetMinuteAggSubscription(symbol);
Console.WriteLine($"Hooking to minute aggregated data subscription for symbol {symbol} event ...");
minAggrSubscription.Received += (IStreamAgg agg) =>
{
Console.WriteLine($"Minute Aggregation Streaming: {agg.Symbol}, Open: {agg.Open}, High: {agg.High}, Low: {agg.Low}, Close: {agg.Close}, Average: {agg.Average}, StartTimeUtc: {agg.StartTimeUtc}, EndTimeUtc: {agg.EndTimeUtc}, Volume: {agg.Volume}, Current Time: {DateTime.UtcNow}");
};
Console.WriteLine($"Subscribing to minute aggregated data for symbol {symbol} ...");
_dataStreamingClient.Subscribe(minAggrSubscription);
}
int waitTime = 60000;
Console.WriteLine($"Keeping the data streaming alive for {((float)waitTime) / 60000} minutes ...");
await Task.Delay(waitTime);
Console.WriteLine($"Exiting data streaming after {((float)waitTime) / 60000} minutes ...");