// before entry to this method, sanity check: side on execution is same as side on order void Position::HandleExecution( const std::pair<const Order&, const Execution&>& status ) { // should be able to calculate profit/loss & position cost as exections are encountered // should be able to calculate position cost basis as position is updated (with and without commissions) // will need market feed in order to calculate profit/loss -- handled in the OnQuote method const Order& order = status.first; const Execution& exec = status.second; Order::idOrder_t orderId = order.GetOrderId(); double dblOldRealizedPL = m_row.dblRealizedPL; double dblOldUnRealizedPL = m_row.dblUnRealizedPL; //std::cout << "Position Exec: " << exec.GetSize() << "," << exec.GetPrice() << std::endl; // update position, regardless of whether we see order open or closed UpdateRowValues( exec.GetPrice(), exec.GetSize(), exec.GetOrderSide() ); if ( ( 0 == m_row.nPositionActive ) && ( OrderSide::Unknown != m_row.eOrderSideActive ) ) { std::cout << "problems" << std::endl; } // check that we think that the order is still active bool bOrderFound = false; for ( std::vector<pOrder_t>::iterator iter = m_vOpenOrders.begin(); iter != m_vOpenOrders.end(); ++iter ) { if ( orderId == iter->get()->GetOrderId() ) { // update position based upon current position and what is executing // decrease position when execution is opposite position // increase position when execution is same as position m_row.nPositionPending -= exec.GetSize(); if ( 0 == m_row.nPositionPending ) m_row.eOrderSidePending = OrderSide::Unknown; if ( 0 == order.GetQuanRemaining() ) { // move from open to closed on order filled m_vClosedOrders.push_back( *iter ); m_vOpenOrders.erase( iter ); } bOrderFound = true; break; } } if ( !bOrderFound ) { // need to handle the case where order was cancelled, but not in time to prevent execution throw std::runtime_error( "Position::HandleExecution doesn't have an Open Order" ); } OnUnRealizedPL( PositionDelta_delegate_t( *this, dblOldUnRealizedPL, m_row.dblUnRealizedPL ) ); // used by portfolio updates OnExecutionRaw( execution_pair_t( *this, exec ) ); OnUpdateExecutionForPortfolioManager( *this ); OnExecution( PositionDelta_delegate_t( *this, dblOldRealizedPL, m_row.dblRealizedPL ) ); // used by portfolio updates OnPositionChanged( *this ); }
void ENT_GeometryOutln::InitTransformationTab() { QVBoxLayout* vbox = NULL; _grpPosition = new QGroupBox("position"); _grpPosition->setObjectName("vectorGroup"); vbox = new QVBoxLayout; for(int i = 0; i < 3; ++i) { _sbPosition[i] = new NBW_SpinBox; _sbPosition[i]->setText(QString("%1: ").arg(static_cast<char>('x' + i))); // TODO: actually, it's not good that these values are bounded _sbPosition[i]->setMinimum(-100); _sbPosition[i]->setMaximum( 100); _sbPosition[i]->setSingleStep(leda::rational(1, 10)); _sbPosition[i]->setValue(_subject.GetPosition().vec[i]); connect(_sbPosition[i], SIGNAL(SigValueChanged(leda::rational)), this, SLOT(OnPositionChanged(leda::rational))); vbox->addWidget(_sbPosition[i]); } _grpPosition->setLayout(vbox); _grpScale = new QGroupBox("scale"); _grpScale->setObjectName("vectorGroup"); vbox = new QVBoxLayout; for(int i = 0; i < 3; ++i) { _sbScale[i] = new NBW_SpinBox; _sbScale[i]->setText(QString("%1: ").arg(static_cast<char>('x' + i))); // TODO: actually, it's not good that these values are bounded _sbScale[i]->setMinimum(-100); _sbScale[i]->setMaximum( 100); _sbScale[i]->setSingleStep(leda::rational(1, 10)); _sbScale[i]->setValue(_subject.GetScale().vec[i]); connect(_sbScale[i], SIGNAL(SigValueChanged(leda::rational)), this, SLOT(OnScaleChanged(leda::rational))); vbox->addWidget(_sbScale[i]); } _grpScale->setLayout(vbox); vbox = new QVBoxLayout; vbox->addWidget(_grpPosition); vbox->addWidget(_grpScale); vbox->addStretch(); _vectors = new QWidget; _vectors->setLayout(vbox); vbox = new QVBoxLayout; vbox->addWidget(_vectors); _tabTransformation = new QWidget; _tabTransformation->setLayout(vbox); }
void Position::HandleCancellation( const Order& order ) { Order::idOrder_t idOrder = order.GetOrderId(); for ( vOrders_t::iterator iter = m_vOpenOrders.begin(); iter != m_vOpenOrders.end(); ++iter ) { if ( idOrder == iter->get()->GetOrderId() ) { if ( m_row.nPositionPending >= iter->get()->GetQuanRemaining() ) { m_row.nPositionPending -= iter->get()->GetQuanRemaining(); if ( 0 == m_row.nPositionPending ) m_row.eOrderSidePending = OrderSide::Unknown; //CancelOrder( iter ); m_vClosedOrders.push_back( *iter ); m_vOpenOrders.erase( iter ); break; } else { throw std::runtime_error( "problems" ); } } } OnPositionChanged( *this ); }
void Position::PlaceOrder( pOrder_t pOrder ) { // if ( OrderSide::Unknown != m_row.eOrderSidePending ) { // ensure new order matches existing orders // if ( ( m_row.eOrderSidePending != pOrder->GetOrderSide() ) && ( OrderType::Market == pOrder->GetOrderType() ) ) { // check only for market orders, not limit orders? // throw std::runtime_error( "Position::PlaceOrder, new order does not match pending order type" ); // } // } if ( 0 == m_row.nPositionPending ) m_row.eOrderSidePending = pOrder->GetOrderSide(); // first to set non-zero gives us our predominant side m_row.nPositionPending += pOrder->GetQuantity(); m_vAllOrders.push_back( pOrder ); m_vOpenOrders.push_back( pOrder ); pOrder->OnExecution.Add( MakeDelegate( this, &Position::HandleExecution ) ); pOrder->OnCommission.Add( MakeDelegate( this, &Position::HandleCommission ) ); pOrder->OnOrderCancelled.Add( MakeDelegate( this, &Position::HandleCancellation ) ); OrderManager::LocalCommonInstance().PlaceOrder( &(*m_pExecutionProvider), pOrder ); OnPositionChanged( *this ); }