public void OnAccountLoaded(MbtAccount account) { this.m_account = account; if (firstTime) { orderLog.Notice("Starting account = " + Display(m_account)); m_position = m_OrderClient.Positions.Find(m_account, symbol.Symbol); if (m_position != null) { orderLog.Notice("Starting position = " + Display(m_position)); } MbtOpenOrders orders = m_OrderClient.OpenOrders; orders.LockItems(); for (int i = 0; i < orders.Count; i++) { orderLog.Notice("Order " + i + ": " + Display(orders[i])); m_orders[orders[i].OrderNumber] = orders[i]; } orders.UnlockItems(); foreach (string orderNum in m_orders.Keys) { string bstrRetMsg = null; if (m_OrderClient.Cancel(orderNum, ref bstrRetMsg) == true) { orderLog.Notice("Order " + orderNum + ": Canceled. " + bstrRetMsg); } else { orderLog.Notice("Order " + orderNum + ": Cancel Failed: " + bstrRetMsg); } } CheckSignalSync(); } firstTime = false; }
void _ordersClient_OnBalanceUpdate(MbtAccount pAcct) { if (_accountInfo.HasValue && pAcct.Account == _accountInfo.Value.Id && pAcct.RoutingID == _accountInfo.Value.Name) { PerformUpdate(pAcct, true, false, false); } }
public void OnAccountUnavailable(MbtAccount account) { this.m_account = null; if (debug) { orderLog.Debug("OnAccountUnavailable( " + account.Account + "," + account.Customer + " )"); } }
public void OnDefaultAccountChanged(MbtAccount account) { this.m_account = account; if (debug) { orderLog.Debug("OnDefaultAccountChanged( " + account.Account + "," + account.Customer + " )"); } }
public void OnBalanceUpdate(MbtAccount account) { this.m_account = account; if (debug) { orderLog.Debug("OnBalanceUpdate( " + account.Account + "," + account.Customer + " )"); } }
public void OnClose(int x) { m_account = null; if (debug) { orderLog.Debug("Account: OnClose( " + x + " )"); } }
string DisplayAccount(MbtAccount acct) { // TickConsole.Notice("Account: Old Account: " + Display(acct)); string retVal = ""; retVal += "Equity=" + acct.CurrentEquity; retVal += ",Excess=" + acct.CurrentExcess; retVal += ",MMRUsed=" + acct.MMRUsed; retVal += ",MMRMultiplier=" + acct.MMRMultiplier; return(retVal); }
void ConvertAccount(MbtAccount account, ref AccountInfo info) { info.Name = account.RoutingID; info.Id = account.Account; info.Server = account.Branch; info.Leverage = (decimal)account.MMRMultiplier; info.BaseCurrency = new Symbol(Symbol.SymbolGroup.Forex, account.BaseCurrency); info.Credit = (decimal)Math.Round(account.Credit, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Equity = null; info.Balance = (decimal)Math.Round(account.CurrentExcess, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Profit = null; info.Margin = null; }
void _ordersClient_OnAccountLoaded(MbtAccount pAcct) { _accountsLoadingCount--; if (_accountsLoadingCount == 0) {// All accountInfos loaded. PerformUpdate(pAcct, true, true, true); if (OperationalState != OperationalStateEnum.Operational && _manager.OperationalState == OperationalStateEnum.Operational) {// We are ready to follow the manager to operational state now. ChangeOperationalState(OperationalStateEnum.Operational); } } }
bool OrderExecutionSourceStub.IImplementation.IsPermittedSymbol(AccountInfo accountInfo, Symbol symbol) { GeneralHelper.GenericReturnDelegate <bool> operation = delegate() { MbtAccount acc = GetAccountByInfo(accountInfo); if (acc == null) { return(false); } return(acc.IsPermedForSymbol(symbol.Name)); }; object result; if (_messageLoopOperator.Invoke(operation, TimeSpan.FromSeconds(5), out result) == false) { return(false); } return((bool)result); }
void tl_newSendOrderRequest(Order o) { test(); int side = o.side ? MBConst.VALUE_BUY : MBConst.VALUE_SELL; int tif = MBConst.VALUE_DAY; if (o.TIF == "GTC") { tif = MBConst.VALUE_GTC; } int otype = MBConst.VALUE_MARKET; if (o.isLimit) { otype = MBConst.VALUE_LIMIT; } string route = "MBTX"; int voltype = MBConst.VALUE_NORMAL; DateTime dt = new DateTime(0); string res = null; MbtAccount m_account = getaccount(o.Account); bool good = m_OrderClient.Submit(side, o.UnsignedSize, o.symbol, (double)o.price, (double)o.stopp, tif, 0, otype, voltype, 0, m_account, route, "", 0, 0, dt, dt, 0, 0, 0, 0, 0, ref res); }
bool OrderExecutionSourceStub.IImplementation.CloseOrCancelOrder(AccountInfo accountInfo, string orderId, string orderTag, decimal?allowedSlippage, decimal?desiredPrice, out decimal closingPrice, out DateTime closingTime, out string modifiedId, out string operationResultMessage) { operationResultMessage = string.Empty; closingPrice = 0; closingTime = DateTime.MinValue; modifiedId = orderId; string operationResultMessageLocal = string.Empty; OperationInformation operation = null; GeneralHelper.GenericReturnDelegate <string> operationDelegate = delegate() { operationResultMessageLocal = "Operation not supported."; operation = null; MbtAccount pAcct = GetAccountByInfo(accountInfo); if (pAcct == null) { operationResultMessageLocal = "Failed to retrieve account."; SystemMonitor.OperationWarning(operationResultMessageLocal); return(null); } string message = string.Empty; lock (this) {// Make sure to keep the entire package here locked, since the order operation get placed after the submit // so we need to make sure we shall catch the responce in OnSubmit() too. if (_orderClient.Cancel(orderTag, ref message) == false) {// Error requestMessage. operationResultMessageLocal = message; return(null); } operation = new CancelOrderOperation() { Id = message }; // The message, or operation Id is the order token (further stored in OrderInfo.id) _operationStub.RegisterOperation(operation, false); } return(message); }; object result; if (_messageLoopOperator.Invoke(operationDelegate, TimeSpan.FromSeconds(8), out result) == false) {// Timed out. operationResultMessage = "Timeout submiting order cancelation."; return(false); } if (string.IsNullOrEmpty((string)result)) {// Operation error. operationResultMessage = operationResultMessageLocal; return(false); } // Return the ID of the submitted order. return(true); }
/// <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()); } } } }
void m_OrderClient_OnAccountUnavailable(MbtAccount account) { debug(String.Format("account unavailable:{0}", account.Account)); }
void m_OrderClient_OnAccountLoading(MbtAccount pAcct) { debug("m_OrderClient_OnAccountLoading: " + pAcct.Account); }
void m_OrderClient_OnAccountLoading(MbtAccount account) { debug(String.Format("account loading:{0}", account.Account)); }
/// <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, allows to extract account info from the MBT account structure. /// </summary> void ConvertAccount(MbtAccount account, decimal openPnL, ref AccountInfo info) { // All account parameters. //double credit = account.Credit; //double currentBP = account.CurrentBP; //double currentEquity = account.CurrentEquity; //double c1 = account.CurrentExcess; //double c2 = account.CurrentOvernightBP; //double c3 = account.DailyRealizedPL; //double c4 = account.MMRUsed; //double c5 = account.MMRMultiplier; //double c6 = account.MorningBP; //double c7 = account.MorningCash; //double c8 = account.MorningEquity; //double c9 = account.MorningExcess; //double c10 = account.MornOvernightBP; //double c11 = account.OvernightExcess; info.Name = account.RoutingID; info.Id = account.Account; info.Server = account.Branch; info.Leverage = (decimal)account.MMRMultiplier; info.BaseCurrency = new Symbol(Symbol.SymbolGroup.Forex, account.BaseCurrency); info.Credit = (decimal)Math.Round(account.Credit, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Equity = (decimal)account.CurrentEquity + (decimal)Math.Round(openPnL, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Balance = (decimal)Math.Round((decimal)account.CurrentEquity, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Profit = (decimal)Math.Round(account.DailyRealizedPL, IntegrationAdapter.AdvisedAccountDecimalsPrecision); info.Margin = null; }
void m_OrderClient_OnBalanceUpdate(MbtAccount account) { debug(String.Format("account balance updated:{0}", account.Account)); }
void m_OrderClient_OnAccountUnavailable(MbtAccount pAcct) { debug("m_OrderClient_OnAccountUnavailable: " + pAcct.Account); }
string DisplayAccount(MbtAccount acct) { string retVal = ""; retVal += "Equity=" + acct.CurrentEquity; retVal += ",Excess=" + acct.CurrentExcess; retVal += ",MMRUsed=" + acct.MMRUsed; retVal += ",MMRMultiplier=" + acct.MMRMultiplier; retVal += ",PermedForForex=" + acct.PermedForForex; return retVal; }
void m_OrderClient_OnBalanceUpdate(MbtAccount pAcct) { debug("m_OrderClient_OnBalanceUpdate: " + pAcct.Account); }
string DisplayAccount(MbtAccount acct) //pmh - spelled things out. { string retVal = ""; retVal += "CurrentEquity=" + acct.CurrentEquity; retVal += " CurrentExcess=" + acct.CurrentExcess; retVal += " MMRUsed=" + acct.MMRUsed; retVal += " MMRMultiplier=" + acct.MMRMultiplier; retVal += " PermedForForex=" + acct.PermedForForex; return retVal; }
void m_OrderClient_OnAccountLoaded(MbtAccount account) { debug(String.Format("account loaded:{0}", account.Account)); debug(String.Format("account details:{0}", DisplayAccount(account))); debug(String.Format("Default is:{0}", m_OrderClient.Accounts.DefaultAccount.Account)); }
/// <summary> /// Submits the order over to the orders interface, make sure to call in Invocation thread. /// </summary> string DoSubmitOrder(AccountInfo accountInfo, Symbol symbol, OrderTypeEnum orderType, int volume, Decimal?allowedSlippage, Decimal?desiredPrice, Decimal?takeProfit, Decimal?stopLoss, string comment, out PlaceOrderOperation operation, out string operationResultMessage) { SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Invoke required."); operationResultMessage = "Operation not supported."; operation = null; MbtAccount pAcct = GetAccountByInfo(accountInfo); if (pAcct == null) { operationResultMessage = "Failed to retrieve account."; SystemMonitor.OperationWarning(operationResultMessage); return(null); } if (orderType != OrderTypeEnum.SELL_MARKET && orderType != OrderTypeEnum.BUY_MARKET) { operationResultMessage = "Order type [" + orderType.ToString() + "] not supported or tested by this provider."; return(null); //if (desiredPrice.HasValue) //{ // dStopPrice = (double)desiredPrice.Value; //} //else //{ // SystemMonitor.Error("Desired price not assigned, on placing order type [" + orderType.ToString() + " ], not submitted."); // return null; //} } // --- int iVolume = volume; int iOrdType, iBuySell; double dPrice = desiredPrice.HasValue ? (double)desiredPrice.Value : 0; double dPrice2 = 0; int lTimeInForce = -1; if (ConvertToMBTOrderType(orderType, desiredPrice, allowedSlippage, out iOrdType, out iBuySell, out lTimeInForce) == false) { operationResultMessage = "Failed to convert type of order."; SystemMonitor.OperationWarning(operationResultMessage); return(null); } if (allowedSlippage.HasValue && dPrice != 0) {// Put the slippage in as a limit price. // This forms the "limit" price we are willing to pay for this order. if (OrderInfo.TypeIsBuy(orderType)) { dPrice = dPrice + (double)allowedSlippage.Value; } else { dPrice = dPrice - (double)allowedSlippage.Value; } } string message = string.Empty; lock (this) {// Make sure to keep the entire package here locked, since the order operation get placed after the submit // so we need to make sure we shall catch the responce in OnSubmit() too. //if (_orderClient.Submit(iBuySell, iVolume, symbol.Name, dPrice, dStopPrice, (int)TifEnum.VALUE_GTC, 10020, iOrdType, // 10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false) //{// Error requestMessage. // operationResultMessage = message; // return null; //} // Instead of using Market Orders, we shall use Limit Orders, since they allow to set an execution limit price. // The VALUE_IOC instructs to execute or cancel the order, instead of GTC (Good Till Cancel) if (_orderClient.Submit(iBuySell, iVolume, symbol.Name, dPrice, dPrice2, lTimeInForce, 10020, iOrdType, 10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false) {// Error requestMessage. operationResultMessage = message; return(null); } operation = new PlaceOrderOperation() { Id = message }; // The message, or operation Id is the order token (further stored in OrderInfo.id) _operationStub.RegisterOperation(operation, false); } return(message); }
//pmh MbtOrderClient events ############################################################################# void m_OrderClient_OnAccountLoaded(MbtAccount pAcct) { debug("m_OrderClient_OnAccountLoaded: " + pAcct.Account + ";" + pAcct.Customer); //pmh - 9/21/12 - See note on DefaultAccount at end of document! // + " DefaultAccount is: " + m_OrderClient.Accounts.DefaultAccount.Account + ";" + m_OrderClient.Accounts.DefaultAccount.Customer); debug(DisplayAccount(pAcct)); }