Beispiel #1
0
 /// <summary>
 /// Send GET request
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source"></param>
 /// <param name="query"></param>
 /// <param name="headers"></param>
 /// <param name="cts"></param>
 /// <returns></returns>
 public async Task <T> Get <T>(
     string source,
     IDictionary <dynamic, dynamic> query   = null,
     IDictionary <dynamic, dynamic> headers = null,
     CancellationTokenSource cts            = null)
 {
     return(ConversionManager.Deserialize <T>(await Send(HttpMethod.Get, source, query, headers, null, cts).ConfigureAwait(false)));
 }
Beispiel #2
0
        /// <summary>
        /// Send query
        /// </summary>
        protected async Task <dynamic> Query(dynamic inputs)
        {
            var query        = Convert.ToBase64String(ConversionManager.Bytes(ConversionManager.Serialize(inputs)));
            var queryHeaders = new Dictionary <dynamic, dynamic>()
            {
                ["Cache-Control"]      = "no-cache",
                ["Accept"]             = "application/json",
                ["X-GEMINI-APIKEY"]    = Token,
                ["X-GEMINI-PAYLOAD"]   = query,
                ["X-GEMINI-SIGNATURE"] = ConversionManager.Sha384(query, Secret)
            };

            return(ConversionManager.Deserialize <dynamic>(await _serviceClient.Post(Source + inputs.request, null, queryHeaders)));
        }
Beispiel #3
0
        /// <summary>
        /// Start streaming
        /// </summary>
        /// <returns></returns>
        public override async Task Subscribe()
        {
            await Unsubscribe();

            // Streaming

            var stream = await GetStream();

            var span      = TimeSpan.FromMilliseconds(0);
            var scheduler = InstanceManager <ScheduleService> .Instance.Scheduler;
            var reader    = new StreamReader(stream);
            var process   = Observable
                            .Interval(span, scheduler)
                            .Take(1)
                            .Subscribe(async o =>
            {
                while (_subscriptions.Any())
                {
                    var message = await reader.ReadLineAsync();

                    if (message.IndexOf("HEARTBEAT", StringComparison.OrdinalIgnoreCase) < 0)
                    {
                        OnInputData(ConversionManager.Deserialize <InputPointModel>(message));
                    }
                }
            });

            _subscriptions.Add(reader);
            _subscriptions.Add(stream);

            // Orders

            var orderSubscription = OrderSenderStream.Subscribe(message =>
            {
                switch (message.Action)
                {
                case ActionEnum.Create: CreateOrders(message.Next); break;

                case ActionEnum.Update: UpdateOrders(message.Next); break;

                case ActionEnum.Delete: DeleteOrders(message.Next); break;
                }
            });

            _subscriptions.Add(orderSubscription);
        }
Beispiel #4
0
        /// <summary>
        /// Start streaming
        /// </summary>
        /// <returns></returns>
        public override async Task Subscribe()
        {
            await Unsubscribe();

            // Orders

            var orderSubscription = OrderSenderStream.Subscribe(message =>
            {
                switch (message.Action)
                {
                case ActionEnum.Create: CreateOrders(message.Next); break;

                case ActionEnum.Update: UpdateOrders(message.Next); break;

                case ActionEnum.Delete: DeleteOrders(message.Next); break;
                }
            });

            _subscriptions.Add(orderSubscription);

            // Streaming

            _streamSession = await GetStreamSession();

            var client = new WebsocketClient(new Uri(StreamSource + "/markets/events"), _streamOptions)
            {
                Name                  = Account.Name,
                ReconnectTimeout      = TimeSpan.FromSeconds(30),
                ErrorReconnectTimeout = TimeSpan.FromSeconds(30)
            };

            var connectionSubscription    = client.ReconnectionHappened.Subscribe(message => { });
            var disconnectionSubscription = client.DisconnectionHappened.Subscribe(message => { });
            var messageSubscription       = client.MessageReceived.Subscribe(message =>
            {
                dynamic input = JObject.Parse(message.Text);

                switch ($"{ input.type }")
                {
                case "quote":

                    OnInputQuote(ConversionManager.Deserialize <InputPointModel>(message.Text));
                    break;

                case "trade": break;

                case "tradex": break;

                case "summary": break;

                case "timesale": break;
                }
            });

            _subscriptions.Add(messageSubscription);
            _subscriptions.Add(connectionSubscription);
            _subscriptions.Add(disconnectionSubscription);
            _subscriptions.Add(client);

            await client.Start();

            var query = new
            {
                linebreak       = true,
                advancedDetails = true,
                sessionid       = _streamSession,
                symbols         = Account.Instruments.Values.Select(o => o.Name)
            };

            client.Send(ConversionManager.Serialize(query));
        }