Example #1
0
        public void StreamTest()
        {
            const string testAlphaId = "79e963f0f1160ff5789450b09";

            // set credentials for connecting to your alpha streams exchange
            var info = new AlphaStreamCredentials(
                "35.231.13.1",
                5672,
                "quantconnect_test",
                "",
                "QCAlphaExchange_quantconnect_test",
                "quantconnect_test"
                );

            var client = new AlphaStreamEventClient(info);

            client.Connect();

            client.InsightReceived += (sender, args) =>
            {
                Assert.AreEqual(args.Insight.Direction, InsightDirection.Flat);
                Assert.AreEqual(args.Insight.Source, Source.LiveTrading);
                Assert.AreEqual(args.Insight.Period, Time.OneMinute);
                Assert.AreEqual(args.Insight.Symbol.ID.ToString(), "EURUSD 8G");
                Assert.AreEqual(args.Insight.Type, InsightType.Price);
                Assert.AreEqual(args.Insight.SourceModel, "eef0aede-d827-454c-ab61-c3e410cdd449");
                Assert.IsNull(args.Insight.Weight);
                Assert.IsNull(args.Insight.Confidence);
                Assert.IsNull(args.Insight.Magnitude);
                Assert.LessOrEqual(args.Insight.GeneratedTimeUtc, DateTime.UtcNow);
                Assert.Greater(args.Insight.CloseTimeUtc, DateTime.UtcNow);
                Assert.AreEqual(args.Insight.GeneratedTimeUtc.Add(args.Insight.Period), args.Insight.CloseTimeUtc);
            };

            client.HeartbeatReceived += (sender, args) =>
            {
                Assert.LessOrEqual(args.MachineTime, DateTime.UtcNow);
                Assert.AreEqual(args.AlphaId, testAlphaId);
                Assert.AreEqual(args.AlgorithmId, "A-a0b454181d0ec497d2989453a79b16c9");
            };

            client.OrderReceived += (sender, args) =>
            {
                Assert.AreNotEqual(args.Order.Direction, OrderDirection.Hold);
                Assert.AreEqual(args.Order.Source, Source.LiveTrading);
                Assert.AreEqual(args.Order.AlgorithmId, "A-a0b454181d0ec497d2989453a79b16c9");
                Assert.AreEqual(args.Order.Symbol, "EURUSD 8G");
                var events = args.Order.OrderEvents;
                Assert.GreaterOrEqual(events.Count, 0);
                Assert.AreEqual(events.First().Symbol, "EURUSD 8G");
            };

            client.AddAlphaStream(new AddAlphaStreamRequest {
                AlphaId = testAlphaId
            });

            Thread.Sleep(60000);
            client.Dispose();
        }
Example #2
0
        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);
        }