/// <summary> /// Constructor. /// </summary> public MBTradingConnectionManager(MBTradingAdapter adapter) { ChangeOperationalState(OperationalStateEnum.Constructed); _adapter = adapter; _messageLoopOperator = new BackgroundMessageLoopOperator(false); _quotes = new MBTradingQuote(_messageLoopOperator); _history = new MBTradingHistory(_messageLoopOperator); _orders = new MBTradingOrders(_messageLoopOperator); }
/// <summary> /// Constructor. /// </summary> public MBTradingConnectionManager(MBTradingAdapter adapter) { ChangeOperationalState(OperationalStateEnum.Constructed); _adapter = adapter; _messageLoopOperator = new BackgroundMessageLoopOperator(false); _quotes = new MBTradingQuote(_messageLoopOperator); _history = new MBTradingHistory(_messageLoopOperator); _orders = new MBTradingOrders(_messageLoopOperator); }
/// <summary> /// /// </summary> public void Dispose() { try { lock (this) { if (_quotes != null) { _quotes.Dispose(); _quotes = null; } if (_history != null) { _history.Dispose(); _history = null; } if (_orders != null) { _orders.UnInitialize(); _orders.Dispose(); _orders = null; } if (_adapter != null) { _adapter = null; } if (_communicationManager != null) { _communicationManager.OnLogonSucceed -= new IMbtComMgrEvents_OnLogonSucceedEventHandler(_communicationManager_OnLogonSucceed); _communicationManager = null; } if (_messageLoopOperator != null) { _messageLoopOperator.Stop(); _messageLoopOperator.Dispose(); _messageLoopOperator = null; } ChangeOperationalState(OperationalStateEnum.Disposed); } } catch (Exception ex) { SystemMonitor.OperationError(ex.Message); } //GC.Collect(); }
void Quotes_QuoteUpdateEvent(MBTradingQuote keeper, MBTradingQuote.SessionQuoteInformation information) { RuntimeDataSessionInformation session = _dataSourceStub.GetSymbolSessionInformation(information.Symbol); if (session != null) { _dataSourceStub.UpdateQuote(session.Info, information.Quote, null); } else { SystemMonitor.OperationWarning("Symbol session not found, quote missed."); } }
/// <summary> /// Make sure to call on Invoke'd thread. /// </summary> /// <param name="account"></param> void PerformUpdate(MbtAccount account, bool updateAccount, bool updateOrders, bool updatePositions) { //SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Invoke must not be required in baseMethod call."); MbtOrderClient orderClient = _orderClient; MBTradingConnectionManager manager = _manager; MBTradingQuote quotes = null; if (orderClient == null || manager == null) { return; } quotes = manager.Quotes; if (quotes == null) { return; } AccountInfo info; Dictionary <string, OrderInfo> pendingOrderInfos = new Dictionary <string, OrderInfo>(); List <PositionInfo> positionsInfos = new List <PositionInfo>(); lock (this) { if (updateOrders) { // We need to send up only the latest of each orders histories, since prev ones are for previous states. foreach (MbtOrderHistory history in _orderClient.OrderHistories) { Order.UpdateTypeEnum updateType; OrderInfo? orderInfo = ConvertToOrderInfo(history, out updateType); if (orderInfo.HasValue) { pendingOrderInfos[orderInfo.Value.Id] = orderInfo.Value; } } // Make sure open orders orderInfo is always on top. foreach (MbtOpenOrder pOrd in _orderClient.OpenOrders) { OrderInfo?orderInfo = ConvertToOrderInfo(pOrd); if (orderInfo.HasValue) { pendingOrderInfos[orderInfo.Value.Id] = orderInfo.Value; } } } if (updatePositions) { foreach (MbtPosition position in _orderClient.Positions) { PositionInfo?positionInfo = ConvertToPositionInfo(position); if (positionInfo.HasValue) { positionsInfos.Add(positionInfo.Value); } } } if (_accountInfo.HasValue) { info = _accountInfo.Value; } else { info = new AccountInfo(); info.Guid = Guid.NewGuid(); } ConvertAccount(account, ref info); _accountInfo = info; } MBTradingAdapter adapter = _adapter; if (adapter != null) { OrderExecutionSourceStub stub = adapter.OrderExecutionSourceStub; if (stub != null) { if (updateAccount) { stub.UpdateAccountInfo(_accountInfo.Value); } if (updateOrders) { stub.UpdateOrdersInfo(_accountInfo.Value, GeneralHelper.GenerateSingleValueArray <Order.UpdateTypeEnum>(pendingOrderInfos.Count, Order.UpdateTypeEnum.Update), GeneralHelper.EnumerableToArray <OrderInfo>(pendingOrderInfos.Values)); } if (updatePositions) { stub.UpdatePositionsInfo(_accountInfo.Value, positionsInfos.ToArray()); } } } }
/// <summary> /// Helper, converts from MBT to OFxP object. /// </summary> PositionInfo? ConvertToPositionInfo(MbtPosition position, MBTradingQuote quotes) { PositionInfo result = new PositionInfo(); Symbol? symbol = _adapter.GetSymbolByName(position.Symbol, true); if (symbol.HasValue == false) { return null; } result.Symbol = symbol.Value; result.Volume = position.AggregatePosition;/*position.CloseableShares;*/ result.Commission = (decimal)Math.Round(position.Commission, IntegrationAdapter.AdvisedAccountDecimalsPrecision); result.PendingBuyVolume = position.PendingBuyShares; result.PendingSellVolume = position.PendingSellShares; result.MarketValue = 0; result.ClosedResult = (decimal)Math.Round(position.RealizedPNL, 4); if (quotes.SessionsQuotes.ContainsKey(symbol.Value.Name)) { double openPnL, basis; if (CalculatePositionOpenPnLAndBasis(position, quotes.SessionsQuotes[symbol.Value.Name].Quote, out openPnL, out basis)) { result.Result = (decimal)Math.Round(openPnL, IntegrationAdapter.AdvisedAccountDecimalsPrecision); result.Basis = (decimal)basis; } } return result; }
void Quotes_QuoteUpdateEvent(MBTradingQuote keeper, MBTradingQuote.SessionQuoteInformation information) { RuntimeDataSessionInformation session = DataSourceStub.GetSymbolSessionInformation(information.Symbol); if (session != null) { DataSourceStub.UpdateQuote(session.Info, information.Quote, null); } else { SystemMonitor.OperationWarning("Symbol session not found, quote missed."); } }
/// <summary> /// /// </summary> public void Dispose() { try { lock (this) { if (_quotes != null) { _quotes.Dispose(); _quotes = null; } if (_history != null) { _history.Dispose(); _history = null; } if (_orders != null) { _orders.UnInitialize(); _orders.Dispose(); _orders = null; } if (_adapter != null) { _adapter = null; } if (_communicationManager != null) { _communicationManager.OnLogonSucceed -= new IMbtComMgrEvents_OnLogonSucceedEventHandler(_communicationManager_OnLogonSucceed); _communicationManager = null; } if (_messageLoopOperator != null) { _messageLoopOperator.Stop(); _messageLoopOperator.Dispose(); _messageLoopOperator = null; } ChangeOperationalState(OperationalStateEnum.Disposed); } } catch (Exception ex) { SystemMonitor.OperationError(ex.Message); } //GC.Collect(); }