private static async Task <T> ExecuteRequest <T>(IRequest <T> request) { var service = new AlphaStreamRestClient(Credentials.Test); return(await service.Execute(request).ConfigureAwait(false)); }
static void Main(string[] args) { Title("QuantConnect: Alpha Streams Demo Project v0.2"); //Initialize: //Basic credentials for Demo Client var credentials = AlphaCredentials.FromConfiguration(); //Enable tracing within the SDK //Trace.Listeners.Add(new ConsoleTraceListener()); //AlphaStreamRestClient.RequestTracingEnabled = true; //AlphaStreamRestClient.ResponseTracingEnabled = true; //Alpha Streams REST Client // This is the search and subscription manager var client = new AlphaStreamRestClient(credentials); //1.Search to find the demo alpha. var assetClass = SecurityType.Equity; Title("1. Alpha Search"); Log($"1. /alpha/search: Searching alphas matching asset class: {assetClass}..."); var alphas = client.Execute(new SearchAlphasRequest { AssetClasses = { assetClass } }).Result; Log($"1. /alpha/search: Located {alphas.Count}.. "); foreach (var a in alphas) { Log($"1. /alpha/search: Alpha.Id: {a.Id} - Alpha.Project: {a.Project.Name}"); } Pause(); // 3. Search for information on a specific alpha: Title("2. Alpha Detail View"); var alphaId = "03cd7746623f09e029a22da43"; Log("2. /alpha/id: Pulling information for specific Alpha..."); var alpha = client.Execute(new GetAlphaByIdRequest { Id = alphaId }).Result; Log($"2. /alpha/{alphaId}: Specific Alpha.Project.Name: {alpha.Project.Name} Capacity: {alpha.Capacity:C} Allocated Capacity Available: {alpha.CapacityAllocated:C} Reserved Price: {alpha.ReservePrice:C}"); Pause(); // 3. List current insights generated by specific alpha: Title("3. Alpha Insights Generated"); Log("3. /alpha/alpha-id/insights: Pulling information for specific Alpha..."); var insightAlphaId = "03cd7746623f09e029a22da43"; var insights = client.Execute(new GetAlphaInsightsRequest { Id = insightAlphaId, Start = 500 }).Result; foreach (var i in insights.Take(5)) { Log($"3. /alpha/{insightAlphaId}/insights: Prediction for { (i.Symbol ?? "").ToUpper().PadRight(8, ' ') }\t going {i.Direction}\t by {i.Magnitude ?? 0:P}\t from {i.ReferenceValue:C}\t created at {i.GeneratedTimeUtc:u} from {i.Source}\t for {i.Period.TotalSeconds} period of seconds."); } Pause(); // List current orders and order events generated by specific alpha Title("3. Alpha Order & OrderEvents Generated"); Log("3. /alpha/alpha-id/orders: Pulling information for specific Alpha..."); var ordersAlphaId = "21a2a00a097117a84788c1434"; var orders = client.Execute(new GetAlphaOrdersRequest { Id = ordersAlphaId, Start = 0 }).Result; foreach (var order in orders.Take(5)) { Log($"3. /alpha/{ordersAlphaId}/orders: \tOrder: {order} {Environment.NewLine}"); } Pause(); // 3. Search by author information: Title("4. Author Search"); var language = "C#"; Log($"4. /author/search: Searching authors who code in: '{language}'"); var authors = client.Execute(new SearchAuthorsRequest { Languages = new List <string> { language }, Projects = new NumberRange <int> { Minimum = 5 } }).Result; foreach (var b in authors.OrderByDescending(c => c.Projects).Take(5)) { Log($"4. /author/search: Author.Id: {b.Id.Substring(0, 5)} \t Projects: {b.Projects} \t Last Online: {b.LastOnlineTime:u} \t Location: {b.Location}"); } Pause(); // 5. Detailed information about a specific author: Title("5. Author Detail View"); var authorId = "1f48359f6c6cbad65b091232eaae73ce"; Log($"5. /author/id: Pulling information for specific author: '{authorId}'"); var author = client.Execute(new GetAuthorByIdRequest { Id = authorId }).Result; Log($"5. /author/id: Specific Author Details:" + $"\r\n-> Id: \t\t\t {author.Id} " + $"\r\n-> Bio: \t\t {author.Biography.Substring(0, 100)}..." + $"\r\n-> Backtests: \t\t {author.Backtests}" + $"\r\n-> Projects: \t\t {author.Projects}" + $"\r\n-> Language: \t\t {author.Language}" + $"\r\n-> Signed Up: \t\t {author.SignupTime}"); Pause(); // 6. Get Equity Curve Title("6. Get Equity Curve"); Log($"6. /alpha/alpha-id/equity: Pulling equity curve data for specific author: '{alphaId}'"); var equityCurve = client.GetAlphaEquityCurveCSharp(alphaId); foreach (var dataPoint in equityCurve) { Log(dataPoint.ToString()); } Pause(); // 7. Streaming Real Time Insights and Orders Title("7. Live Insights and Orders Streaming"); // Credentials for streaming client var streamingCredentials = AlphaStreamCredentials.FromConfiguration(); var streamingClient = new AlphaStreamEventClient(streamingCredentials); //Configure client to handle received insights streamingClient.InsightReceived += (sender, e) => { Log($"7. AlphaId: {e.AlphaId.Substring(0, 5)} \t InsightId: {e.Insight.Id} " + $"Created: {e.Insight.GeneratedTimeUtc:u} \t " + $"Type: {e.Insight.Type} \t " + $"Ticker: {e.Insight.Symbol.ToString().PadRight(8, ' ')} \t " + $"Direction: {e.Insight.Direction}... \t " + (e.Insight.Magnitude == null ? "" : $"Magnitude: {e.Insight.Magnitude:P} \t") + (e.Insight.Confidence == null ? "" : $"Confidence: {e.Insight.Confidence:P}")); }; //Configure client to handle received heartbeats streamingClient.HeartbeatReceived += (sender, e) => { Log($"7. AlphaId: {e.AlphaId.Substring(0, 5)} \t " + $"AlgorithmId: {e.AlgorithmId.Substring(0, 7)} \t " + $"MachineTime: {e.MachineTime:u}"); }; streamingClient.OrderReceived += (sender, eventArgs) => { Log($"7. Order: {eventArgs.Order}"); }; //Request insights and orders from an alpha stream alphaId = "79e963f0f1160ff5789450b09"; // First we need to subscribe, if not already subscribed. It will subscribe us for a month and charge the alphas fee // We add the alpha IDs we want to stream streamingClient.AddAlphaStream(new AddAlphaStreamRequest { AlphaId = alphaId }); // wait 30 seconds while insights stream in Thread.Sleep(300000); // We can unsubscribe if we don't want to consume an alpha anymore streamingClient.Dispose(); Pause(); Environment.Exit(0); }