Exemplo n.º 1
0
        public bool CreateUser(string username, string account, string host, int port)
        {
            try
            {
                IBUser user = _userManager.GetUser(username);

                if (user == null)
                {
                    user = _userManager.AddUser(username, host, port);
                }

                if (user != null)
                {
                    user.SubscribeAccountUpdates(account);
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 2
0
        public IBUser AddUser(string username, string host, int port)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(null);
            }

            IBUser user = null;

            username = username.ToLower().Trim();
            if (_IBUserMap.ContainsKey(username))
            {
                user = _IBUserMap[username];
            }
            else
            {
                user = new IBUser(host, port, GetNextClientId(), _log);
                if (user.Connect())
                {
                    _IBUserMap[username] = user;
                }
                else
                {
                    user = null;
                }
            }

            return(user);
        }
Exemplo n.º 3
0
        public AccountSummary[] GetAccountSummary(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    Messages.AccountSummaryMessage[] accountSummarys = user.GetAccountSummary();
                    if (accountSummarys != null)
                    {
                        Dictionary <string, Messages.AccountSummaryMessage[]> dictMessages = (from p in accountSummarys
                                                                                              group p by p.Account into g
                                                                                              select g).ToDictionary(k => k.Key, v => v.ToArray());
                        List <AccountSummary> accountSummaryList = new List <AccountSummary>();

                        foreach (string account in dictMessages.Keys)
                        {
                            Messages.AccountSummaryMessage[] messages = dictMessages[account];
                            AccountSummary accountSummary             = new AccountSummary();
                            Dictionary <string, System.Reflection.PropertyInfo> dictProperties = typeof(AccountSummary).GetProperties().ToDictionary(k => k.Name, v => v);
                            accountSummary.UserName = userName;
                            foreach (Messages.AccountSummaryMessage message in messages)
                            {
                                System.Reflection.PropertyInfo property = null;
                                if (dictProperties.TryGetValue(message.Tag, out property))
                                {
                                    if (property.PropertyType == typeof(string))
                                    {
                                        property.SetValue(accountSummary, message.Value);
                                    }
                                    else if (property.PropertyType == typeof(double))
                                    {
                                        double val;
                                        if (double.TryParse(message.Value, out val))
                                        {
                                            property.SetValue(accountSummary, val);
                                        }
                                    }
                                }
                            }

                            accountSummaryList.Add(accountSummary);
                        }

                        return(accountSummaryList.ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 4
0
        public void RemoveUser(string username)
        {
            username = username.ToLower().Trim();
            IBUser user = GetUser(username);

            if (user != null)
            {
                user.UnsubscribeAccountUpdates();
                user.Disconnect();
                _IBUserMap.TryRemove(username, out user);
            }
        }
Exemplo n.º 5
0
        public string GetAccountUpdateTime(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    return(user.GetAccountUpdateTime());
                }

                return(string.Empty);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 6
0
        public bool SubscribeAccountUpdates(string userName, string account)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    return(user.SubscribeAccountUpdates(account));
                }

                return(false);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 7
0
        public bool CancelOrder(string userName, int orderId)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    return(user.CancelOrder(orderId));
                }

                return(false);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 8
0
        public int PlaceSimpleOrder(SimpleOrder simpleOrder)
        {
            try
            {
                IBUser user = _userManager.GetUser(simpleOrder.UserName);
                if (user != null)
                {
                    IBApi.Contract contract = IBContractSamples.GetContract(simpleOrder);
                    IBApi.Order    order    = IBOrderSamples.GetOrder(simpleOrder);
                    return(user.PlaceOrder(contract, order, simpleOrder.Fund, simpleOrder.Strategy, simpleOrder.Folder));
                }

                return(-1);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 9
0
        public Trading.Position[] GetPositions(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    Messages.PositionMessage[] positions = user.GetPositions();
                    if (positions != null)
                    {
                        return(positions.Select(p => ConvertPosition(p)).ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 10
0
        public Trading.Trade[] GetAllTrades(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    Messages.ExecutionMessage[] executions = user.GetAllExecutions();
                    if (executions != null)
                    {
                        return(executions.Select(p => ConvertExecutionToTrade(p, user)).ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 11
0
        public PositionPortfolio[] GetPortfolios(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    Messages.UpdatePortfolioMessage[] portfolios = user.GetPortfolios();
                    if (portfolios != null)
                    {
                        return(portfolios.Select(p => ConvertPositionPortfolio(p)).ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 12
0
        public Trading.Order GetOpenOrder(string userName, int orderId)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    Messages.OpenOrderMessage openOrder = user.GetOpenOrderMessage(orderId, user.ClientId);
                    if (openOrder != null)
                    {
                        var order = ConvertOpenOrderMessageToOrder(openOrder, user);
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 13
0
        public Trading.Trade[] FilterTrades(string userName, TradeFilter tradeFilter)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);
                if (user != null)
                {
                    ExecutionFilter             filter     = new ExecutionFilter(tradeFilter.ClientId, tradeFilter.AcctCode, tradeFilter.Time, tradeFilter.Symbol, tradeFilter.SecType, tradeFilter.Exchange, tradeFilter.Side);
                    Messages.ExecutionMessage[] executions = user.FilterExecutions(filter);
                    if (executions != null)
                    {
                        return(executions.Select(p => ConvertExecutionToTrade(p, user)).ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 14
0
        public Trading.Order[] RefreshAllOpenOrders(string userName)
        {
            try
            {
                IBUser user = _userManager.GetUser(userName);

                if (user != null)
                {
                    Messages.OpenOrderMessage[] openOrders = user.RefreshAllOpenOrders();
                    if (openOrders != null)
                    {
                        return(openOrders.Select(p => ConvertOpenOrderMessageToOrder(p, user)).ToArray());
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Exemplo n.º 15
0
        public static Trading.Trade ConvertExecutionToTrade(Messages.ExecutionMessage execution, IBUser user)
        {
            if (execution != null)
            {
                Trading.Trade trade = new Trading.Trade();

                trade.Contract = ConvertContract(execution.Contract);

                if (execution.Execution != null)
                {
                    trade.OrderId      = execution.Execution.OrderId;
                    trade.ClientId     = execution.Execution.ClientId;
                    trade.ExecId       = execution.Execution.ExecId;
                    trade.Time         = execution.Execution.Time;
                    trade.AcctNumber   = execution.Execution.AcctNumber;
                    trade.Exchange     = execution.Execution.Exchange;
                    trade.Side         = execution.Execution.Side;
                    trade.Shares       = execution.Execution.Shares;
                    trade.Price        = execution.Execution.Price;
                    trade.PermId       = execution.Execution.PermId;
                    trade.Liquidation  = execution.Execution.Liquidation;
                    trade.CumQty       = execution.Execution.CumQty;
                    trade.AvgPrice     = execution.Execution.AvgPrice;
                    trade.OrderRef     = execution.Execution.OrderRef;
                    trade.EvRule       = execution.Execution.EvRule;
                    trade.EvMultiplier = execution.Execution.EvMultiplier;
                    trade.ModelCode    = execution.Execution.ModelCode;
                }

                if (execution.Commission != null)
                {
                    trade.Commission          = execution.Commission.Commission;
                    trade.Currency            = execution.Commission.Currency;
                    trade.RealizedPNL         = execution.Commission.RealizedPNL;
                    trade.Yield               = execution.Commission.Yield;
                    trade.YieldRedemptionDate = execution.Commission.YieldRedemptionDate;
                }

                if (execution.Execution != null && user != null)
                {
                    int orderId = execution.Execution.OrderId;
                    Tuple <string, string, string> portfolio = user.GetPortfolioFromOrderId(orderId);
                    if (portfolio != null)
                    {
                        trade.Fund     = portfolio.Item1;
                        trade.Strategy = portfolio.Item2;
                        trade.Folder   = portfolio.Item3;
                    }
                }

                return(trade);
            }

            return(null);
        }
Exemplo n.º 16
0
        public static Trading.Order ConvertOpenOrderMessageToOrder(Messages.OpenOrderMessage openOrder, IBUser user)
        {
            if (openOrder != null)
            {
                Trading.Order order = new Trading.Order();
                order.OrderId  = openOrder.OrderId;
                order.Contract = ConvertContract(openOrder.Contract);
                if (openOrder.Order != null)
                {
                    order.Account         = openOrder.Order.Account;
                    order.Action          = openOrder.Order.Action;
                    order.ActiveStartTime = openOrder.Order.ActiveStartTime;
                    order.ActiveStopTime  = openOrder.Order.ActiveStopTime;
                    order.OrderId         = openOrder.Order.OrderId;
                    order.ClientId        = openOrder.Order.ClientId;
                    order.PermId          = openOrder.Order.PermId;
                    order.TotalQuantity   = openOrder.Order.TotalQuantity;
                    order.OrderType       = openOrder.Order.OrderType;
                    order.LimitPrice      = openOrder.Order.LmtPrice;
                    order.AuxPrice        = openOrder.Order.AuxPrice;
                    order.Tif             = openOrder.Order.Tif;
                    order.OcaGroup        = openOrder.Order.OcaGroup;
                    order.OcaType         = openOrder.Order.OcaType;
                    order.OrderRef        = openOrder.Order.OrderRef;
                    order.Transmit        = openOrder.Order.Transmit;
                    order.ParentId        = openOrder.Order.ParentId;
                    order.BlockOrder      = openOrder.Order.BlockOrder;
                    order.SweepToFill     = openOrder.Order.SweepToFill;
                    order.DisplaySize     = openOrder.Order.DisplaySize;
                    order.TriggerMethod   = openOrder.Order.TriggerMethod;
                    order.OutsideRth      = openOrder.Order.OutsideRth;
                    order.Hidden          = openOrder.Order.Hidden;
                    order.GoodAfterTime   = openOrder.Order.GoodAfterTime;
                    order.GoodTillDate    = openOrder.Order.GoodTillDate;
                    order.OverridePercentageConstraints = openOrder.Order.OverridePercentageConstraints;
                    order.Rule80A   = openOrder.Order.Rule80A;
                    order.AllOrNone = openOrder.Order.AllOrNone;
                    order.MinQty    = openOrder.Order.MinQty;
                }

                if (openOrder.OrderState != null)
                {
                    order.Status             = openOrder.OrderState.Status;
                    order.InitMargin         = openOrder.OrderState.InitMargin;
                    order.MaintMargin        = openOrder.OrderState.MaintMargin;
                    order.EquityWithLoan     = openOrder.OrderState.EquityWithLoan;
                    order.Commission         = openOrder.OrderState.Commission;
                    order.MinCommission      = openOrder.OrderState.MinCommission;
                    order.MaxCommission      = openOrder.OrderState.MaxCommission;
                    order.CommissionCurrency = openOrder.OrderState.CommissionCurrency;
                    order.WarningText        = openOrder.OrderState.WarningText;
                }

                if (openOrder.OrderStatus != null)
                {
                    order.Status        = openOrder.OrderStatus.Status;
                    order.Filled        = openOrder.OrderStatus.Filled;
                    order.Remaining     = openOrder.OrderStatus.Remaining;
                    order.AvgFillPrice  = openOrder.OrderStatus.AvgFillPrice;
                    order.LastFillPrice = openOrder.OrderStatus.LastFillPrice;
                    order.WhyHeld       = openOrder.OrderStatus.WhyHeld;
                }

                if (user != null)
                {
                    var portfolio = user.GetPortfolioFromOrderId(order.OrderId);
                    if (portfolio != null)
                    {
                        order.Fund     = portfolio.Item1;
                        order.Strategy = portfolio.Item2;
                        order.Folder   = portfolio.Item3;
                    }
                }

                return(order);
            }

            return(null);
        }