Exemplo n.º 1
0
        public bool ResolveUserEndpoints()
        {
            bool ret = false;

            try
            {
                if (null != _channel && _channel.IsOpen)
                {
                    var dic = new GenericRequestResponseDictionary();
                    //dic.Add("username", _username);
                    //dic.Add("password", _password);
                    dic.SetIsEndpointRes();

                    IBasicProperties props = _channel.CreateBasicProperties();
                    props.ReplyTo = responseQueueName;
                    props.UserId  = _username;

                    _channel.BasicPublish(
                        System.Configuration.ConfigurationManager.AppSettings["exchangeNameAuth"],
                        "",
                        props,
                        UTF8Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dic))
                        );

                    ret = true; //indiicate request sent
                }
            }catch (Exception ex)
            {
                log.Error(ex);
            }

            return(ret);
        }
Exemplo n.º 2
0
        public void saveUpdateCloseCoverPosition(CoverPosition position, string command)
        {
            GenericRequestResponseDictionary request = new GenericRequestResponseDictionary();

            request["position"] = JsonConvert.SerializeObject(position);
            request["command"]  = command;

            sendCommand(request, "office");
        }
Exemplo n.º 3
0
        public void listCoverAccounts()
        {
            GenericRequestResponseDictionary request = new GenericRequestResponseDictionary();

            request["command"]       = "listCoverAccounts";
            request["responseQueue"] = consumerQueueName;

            sendCommand(request, "office");
        }
Exemplo n.º 4
0
        public void disconnectAndLockSession(string username, string[] mqNames)
        {
            GenericRequestResponseDictionary request = new GenericRequestResponseDictionary();

            request["username"] = username;
            request["mqName"]   = JsonConvert.SerializeObject(mqNames);
            request["command"]  = "lockuser";

            sendCommand(request, "office");
        }
Exemplo n.º 5
0
        public void approveRejectOrder(string clientUsername, Guid orderid, string commandVerb)
        {
            GenericRequestResponseDictionary request = new GenericRequestResponseDictionary();

            request["client"]  = clientUsername;
            request["orderId"] = orderid.ToString();
            request["command"] = commandVerb;

            sendCommand(request, "client");
        }
Exemplo n.º 6
0
        public void requoteOrder(string clientUsername, Guid orderid, decimal requotePrice)
        {
            GenericRequestResponseDictionary request = new GenericRequestResponseDictionary();

            request["client"]         = clientUsername;
            request["orderId"]        = orderid.ToString();
            request["command"]        = "requote";
            request["requoted_price"] = requotePrice.ToString();

            sendCommand(request, "client");
        }
Exemplo n.º 7
0
        private void sendCommand(GenericRequestResponseDictionary request, string messageType)
        {
            lock (lockChannel)
            {
                try
                {
                    IBasicProperties props = _channel.CreateBasicProperties();
                    props.UserId = _username;
                    props.Type   = messageType;

                    _channel.BasicPublish(
                        officeExchangeName,
                        "fromdealer",
                        props,
                        UTF8Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request))
                        );
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message, ex);
                }
            }
        }
Exemplo n.º 8
0
        void qResponseMsgConsumer_Received(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var body = e.Body;
                _channel.BasicAck(e.DeliveryTag, false);

                GenericRequestResponseDictionary dic = JsonConvert.DeserializeObject <GenericRequestResponseDictionary>(ASCIIEncoding.UTF8.GetString(body));

                Task.Factory.StartNew(() =>
                {
                    RaiseOnGenericResponseReceived(
                        new GenericResponseEventArgs()
                    {
                        GenericResponse = dic
                    }
                        );
                });
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
Exemplo n.º 9
0
        void qResponseMsgConsumer_Received(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var body = e.Body;
                IBasicProperties props = e.BasicProperties;
                _channel.BasicAck(e.DeliveryTag, false);

                Task.Factory.StartNew(() =>
                {
                    if (props.Type != null && props.Type.Equals("orderUpdate", StringComparison.OrdinalIgnoreCase))
                    {
                        if (null != OnOrderUpdateReceived)
                        {
                            GenericRequestResponseDictionary update = JsonConvert.DeserializeObject <GenericRequestResponseDictionary>(ASCIIEncoding.UTF8.GetString(body));
                            OnOrderUpdateReceived(this, new GenericResponseEventArgs()
                            {
                                GenericResponse = update
                            });
                        }
                    }
                    else if (props.Type != null && props.Type.Equals("update", StringComparison.OrdinalIgnoreCase))
                    {
                        if (null != OnUpdateReceived)
                        {
                            ClientUpdateEventArgs update = JsonConvert.DeserializeObject <ClientUpdateEventArgs>(ASCIIEncoding.UTF8.GetString(body));
                            if (null == update.Positions)
                            {
                                update.Positions = new Dictionary <Guid, TradePosition>();
                            }
                            //for net calculation
                            Dictionary <string, List <TradePosition> > netPosition = new Dictionary <string, List <TradePosition> >(update.Positions.Count);

                            //fill in the current price against each of the positions
                            foreach (var p in update.Positions)
                            {
                                if (p.Value.OrderType.Equals(TradePosition.ORDER_TYPE_BUY))
                                {
                                    p.Value.CurrentPrice = (update.ClientQuotes.ContainsKey(p.Value.Commodity)) ? update.ClientQuotes[p.Value.Commodity].Bid : decimal.Zero;
                                }
                                else
                                {
                                    p.Value.CurrentPrice = (update.ClientQuotes.ContainsKey(p.Value.Commodity)) ? update.ClientQuotes[p.Value.Commodity].Ask : decimal.Zero;
                                }

                                string key = p.Value.Commodity + "_" + p.Value.OrderType;
                                if (!netPosition.ContainsKey(key))
                                {
                                    netPosition[key] = new List <TradePosition>();
                                }
                                netPosition[key].Add(p.Value);
                            }

                            //determine net position
                            update.NetPosition = new Dictionary <Guid, TradePosition>();
                            foreach (var row in netPosition)
                            {
                                string commodity = row.Key.Split(new char[] { '_' })[0];
                                string type      = row.Key.Split(new char[] { '_' })[1];

                                decimal sumAmt = 0;
                                decimal sumPL  = 0;
                                decimal price  = 0;

                                foreach (var p in row.Value)
                                {
                                    sumAmt += p.Amount;
                                    sumPL  += p.CurrentPl;
                                    price  += p.OpenPrice;
                                }

                                price /= (decimal)row.Value.Count;

                                TradePosition position = new TradePosition();
                                position.OrderId       = Guid.NewGuid();
                                position.Commodity     = commodity;
                                position.OrderType     = type;
                                position.OpenPrice     = price;
                                position.Amount        = sumAmt;
                                position.CurrentPl     = sumPL;
                                if (type.Equals(TradePosition.ORDER_TYPE_BUY))
                                {
                                    position.CurrentPrice = update.ClientQuotes[commodity].Bid;
                                }
                                else
                                {
                                    position.CurrentPrice = update.ClientQuotes[commodity].Ask;
                                }

                                update.NetPosition.Add(position.OrderId, position);
                            }

                            //update net prices on the net positions

                            OnUpdateReceived(this, update);
                        }
                    }
                    else if (props.Type != null && props.Type.Equals("accountStatus", StringComparison.OrdinalIgnoreCase))
                    {
                        if (null != OnAccountStatusEventReceived)
                        {
                            GenericRequestResponseDictionary update = JsonConvert.DeserializeObject <GenericRequestResponseDictionary>(ASCIIEncoding.UTF8.GetString(body));
                            OnAccountStatusEventReceived(this, new GenericResponseEventArgs()
                            {
                                GenericResponse = update
                            });
                        }
                    }
                    else if (props.Type != null && props.Type.Equals("candlestick", StringComparison.OrdinalIgnoreCase))
                    {
                        if (null != OnCandleStickDataEventHandler)
                        {
                            List <CandleStick> data       = JsonConvert.DeserializeObject <List <CandleStick> >(ASCIIEncoding.UTF8.GetString(body));
                            CandleStickDataEventArgs args = new CandleStickDataEventArgs()
                            {
                                Data = data
                            };
                            OnCandleStickDataEventHandler(this, args);
                        }
                    }
                    else if (props.Type != null && props.Type.Equals("notification"))
                    {
                        string payload = ASCIIEncoding.UTF8.GetString(body);
                        if (null != OnNotificationReceived)
                        {
                            OnNotificationReceived(payload);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }