This event is fired when a new order is added to the collection, the order status is changed, or the order is removed from the collection.
Visual Basic |
---|
Public Event OrderChanged( _ ByVal change_type As eChangeType, _ ByVal cqg_order As CQGOrder, _ ByVal old_properties As CQGOrderProperties, _ ByVal cqg_fill As CQGFill, _ ByVal cqg_error As CQGError _ ) |
- change_type
Value Description ctAdded New order is added and is already in the collection. ctChanged The order is changed. ctRemoved The order is removed from the collection. The type of the occurred change.
The actual change can be traced through LastEvent property of the order.
- cqg_order
CQGOrder object representing the order to which the change refers.
- old_properties
- CQGOrderProperties collection representing old values of all order properties.
- cqg_fill
CQGFill object representing the last fill of the order, i.e. the last item of order's Fills collection.
If there was no fill, Nothing or invalid fill object is passed depending on the UsedFromATLClient setting of the configuration.
- cqg_error
CQGError object representing either an error sent by CQG Gateway or an intermediate error. If the error is final the same instance of CQGError will be set to order's LastError property.
If no error has occurred, the parameter value is either Nothing or invalid error object depending on the UsedFromATLClient setting of the configuration.
The parameter can be checked for validity via the CQGCEL.IsValid method.
In order to receive order events the AccountSubscriptionLevel property of CQGCEL should be set to aslAccountUpdatesAndOrders.
The examples below show how to place orders via CQG API.
Option Explicit Private WithEvents cel As CQGCEL Private ReadyFlagExists As Boolean ' Called when workbook is opened Private Sub Workbook_Open() ' Create CQG API instance Set cel = New CQGCEL ' Configure it cel.APIConfiguration.CollectionsThrowException = False cel.APIConfiguration.DefPositionSubscriptionLevel = pslSnapshotAndUpdates cel.APIConfiguration.UseOrderSide = True cel.Startup 'Check if ready flag exists in excel Dim ExcelMajorVersion As Integer ExcelMajorVersion = CInt(Left$(Application.Version, InStr(Application.Version, ".") - 1)) If ExcelMajorVersion > 9 Then ' Excel 2002 and higher ReadyFlagExists = True Else ReadyFlagExists = False End If End Sub ' Called before workbook is going to be closed Private Sub Workbook_BeforeClose(Cancel As Boolean) cel.Shutdown Set cel = Nothing End Sub ' CQG API event handlers Private Sub cel_GWConnectionStatusChanged(ByVal newStatus As CQG.eConnectionStatus) Dim status As String Select Case newStatus Case csConnectionDelayed status = "Delayed" Case csConnectionDown status = "Down" Case csConnectionUp status = "Up" ' Request accounts and orders functionality cel.AccountSubscriptionLevel = aslAccountUpdatesAndOrders End Select Debug.Print "GW Connection Status changed to " + status End Sub Private Sub cel_AccountChanged(ByVal change As CQG.eAccountChangeType, ByVal account As CQG.ICQGAccount, ByVal position As CQG.ICQGPosition) ' Request instument If change = actAccountsReloaded Then cel.NewInstrument "EP" End Sub Private Sub cel_InstrumentSubscribed(ByVal symbl As String, ByVal instrument As CQG.ICQGInstrument) ' EP subscribed, let's place single parked market order on EP for each account Dim Order As CQGOrder Dim acc As CQGAccount Dim qty As Long Dim isParked As Boolean qty = 1 isParked = True For Each acc In cel.Accounts Set Order = cel.CreateOrder(eOrderType.otMarket, instrument, acc, qty, eOrderSide.osdBuy, _ ueNamePart:="Mine #" & qty) If isParked Then Order.Properties(opParked).Value = True Debug.Print "Parked market order is created for " & acc.GWAccountName & " account" Else Debug.Print "Market order is created for " & acc.GWAccountName & " account" End If Debug.Print "Instrument - " & instrument.FullName Debug.Print "Quantity - " & qty Order.Place Debug.Print "Order " & Order.UEName & " is placed" qty = qty + 1 isParked = Not isParked Next End Sub Private Sub cel_OrderChanged(ByVal change As CQG.eChangeType, ByVal Order As CQG.ICQGOrder, _ ByVal oldProperties As CQG.ICQGOrderProperties, ByVal fill As CQG.ICQGFill, _ ByVal objError As CQG.ICQGError) If Not objError Is Nothing Then Debug.Print "An error occured while processing the order " & Order.GWOrderID & ": " _ & objError.Description End If Select Case change Case eChangeType.ctAdded Debug.Print "Order " & Order.GWOrderID & " is added." PrintOrder Order, fill Case eChangeType.ctChanged PrintOrder Order, fill Case Else Debug.Print "Order " & Order.GWOrderID & " is removed." End Select End Sub Private Sub cel_IsReady(readyStatus As CQG.eReadyStatus) If ReadyFlagExists Then ' Excel 2002 and higher ' ready status should reflect Application.Ready value readyStatus = IIf(Application.Ready, rsReady, rsNotReady) Else readyStatus = rsReady End If End Sub Private Sub cel_DataError(ByVal obj As Object, ByVal errorDescription As String) ' Print the error description MsgBox obj.Description ' Terminate application Close End Sub ' Represents Order Gateway Status as String Public Function OrderStatusToString(orderState As eOrderStatus) As String Select Case orderState Case eOrderStatus.osActiveAt OrderStatusToString = "ActiveAt" Case eOrderStatus.osBusted OrderStatusToString = "Busted" Case eOrderStatus.osCanceled OrderStatusToString = "Canceled" Case eOrderStatus.osContingent OrderStatusToString = "Contingent" Case eOrderStatus.osDisconnected OrderStatusToString = "Disconnected" Case eOrderStatus.osExpired OrderStatusToString = "Expired" Case eOrderStatus.osFilled OrderStatusToString = "Filled" Case eOrderStatus.osInCancel OrderStatusToString = "InCancel" Case eOrderStatus.osInClient OrderStatusToString = "InClient" Case eOrderStatus.osInModify OrderStatusToString = "InModify" Case eOrderStatus.osInOrderBook OrderStatusToString = "InOrderBook" Case eOrderStatus.osInTransit OrderStatusToString = "InTransit" Case eOrderStatus.osInTransitTimeout OrderStatusToString = "InTransitTimeout" Case eOrderStatus.osNotSent OrderStatusToString = "NotSent" Case eOrderStatus.osParked OrderStatusToString = "Parked" Case eOrderStatus.osRejectFCM OrderStatusToString = "RejectFCM" Case eOrderStatus.osRejectGW OrderStatusToString = "RejectGW" End Select End Function ' Prints order information in Debug window Private Sub PrintOrder(Order As CQG.ICQGOrder, ByVal fill As CQG.ICQGFill) Debug.Print "Order " & Order.GWOrderID & ":" Debug.Print "Status - " & OrderStatusToString(Order.GWStatus) Debug.Print "Account - " & Order.account.GWAccountName Debug.Print "Instrument - " & Order.InstrumentName Debug.Print "Order Side - " & IIf(Order.Side = osdBuy, "Buy", "Sell") Debug.Print "Quantity - " & Order.quantity Debug.Print "Filled Quantity - " & Order.FilledQuantity Debug.Print "Place Time - " & Order.PlaceTime If fill Is Nothing Then ' Order hasn't filled yet. Debug.Print "Current Fill Price - N/A" Else ' Order is filled. Debug.Print "Current Fill Price - " & fill.Price End If End Sub
using System; using System.Runtime.InteropServices; using CQG; namespace Sample { class OrdersPlacingSample : IDisposable { // CQGCEL instance declaration. private CQGCEL cel = new CQGCELClass(); // Used to determine is the object manualy disposed. private bool disposed = false; public OrdersPlacingSample() { // Initialize CQGCEL instance. try { // Configure CEL. cel.APIConfiguration.ReadyStatusCheck = eReadyStatusCheck.rscOff; cel.APIConfiguration.CollectionsThrowException = false; cel.APIConfiguration.DefPositionSubscriptionLevel = ePositionSubscriptionLevel.pslSnapshotAndUpdates; cel.APIConfiguration.UseOrderSide = true; // Register to CEL's events. cel.CELStarted += new _ICQGCELEvents_CELStartedEventHandler(cel_CELStarted); cel.DataError += new _ICQGCELEvents_DataErrorEventHandler(cel_DataError); cel.GWConnectionStatusChanged += new _ICQGCELEvents_GWConnectionStatusChangedEventHandler(cel_GWConnectionStatusChanged); cel.InstrumentSubscribed += new _ICQGCELEvents_InstrumentSubscribedEventHandler(cel_InstrumentSubscribed); cel.AccountChanged += new _ICQGCELEvents_AccountChangedEventHandler(cel_AccountChanged); cel.OrderChanged += new _ICQGCELEvents_OrderChangedEventHandler(cel_OrderChanged); cel.Startup(); } catch (COMException ex) { Console.WriteLine("Error occurred during CEL initialization. {0}", ex.Message); } } ~OrdersPlacingSample() { Dispose(false); } #region CEL Event Handlers void cel_CELStarted() { // Everything is OK with enablements. Console.WriteLine("CEL Version : {0}", cel.Environment.CELVersion); } void cel_DataError(object obj, string errorDescription) { // Some error occurred. cel.Logger.Log("ERROR : " + errorDescription, eLogSeverity.lsError); // Terminate application. System.Windows.Forms.Application.Exit(); } void cel_GWConnectionStatusChanged(eConnectionStatus newStatus) { string status; switch (newStatus) { case eConnectionStatus.csConnectionDelayed: status = "Delayed"; break; case eConnectionStatus.csConnectionDown: status = "Down"; break; case eConnectionStatus.csConnectionUp: status = "Up"; // Request accounts and orders functionality. cel.AccountSubscriptionLevel = eAccountSubscriptionLevel.aslAccountUpdatesAndOrders; break; default: status = "Unknown"; break; } Console.WriteLine("GW Connection Status changed to " + status); } void cel_InstrumentSubscribed(string symbl, CQGInstrument Instrument) { // EP subscribed, let's place single parked market order on EP for each account. CQGOrder order; int qty = 1; bool isParked = true; foreach (CQGAccount acc in cel.Accounts) { order = cel.CreateOrder(eOrderType.otMarket, Instrument, acc, qty, eOrderSide.osdBuy, 0, 0, "Mine #" + qty); if (isParked) { order.Properties[eOrderProperty.opParked].Value = true; Console.WriteLine("Parked marked order is created for {0} account", acc.GWAccountName); } else { Console.WriteLine("Marked order is created for {0} account", acc.GWAccountName); } Console.WriteLine("Instrument - {0}", Instrument.FullName); Console.WriteLine("Quantity - {0}", qty); Console.WriteLine("Side - Buy"); order.Place(); Console.WriteLine("Order {0} is placed.", order.UEName); qty = qty + 1; isParked = !isParked; } Console.WriteLine("There is a position on " + Instrument.FullName + ". Now the instrument is subscribed."); } void cel_AccountChanged(eAccountChangeType change, CQGAccount Account, CQGPosition position) { // Request instument. if (change == eAccountChangeType.actAccountsReloaded) { cel.NewInstrument("EP"); } } void cel_OrderChanged(eChangeType change, CQGOrder Order, CQGOrderProperties oldProperties, CQGFill fill, CQGError objError) { if (objError != null) { Console.WriteLine("An error occured while processing the order {0} : {1}", Order, objError.Description); } switch (change) { case eChangeType.ctAdded: Console.WriteLine("Order {0} is added.", Order.GWOrderID); break; case eChangeType.ctChanged: PrintOrder(Order, fill); break; case eChangeType.ctRemoved: Console.WriteLine("Order {0} is removed.", Order.GWOrderID); break; default: Console.WriteLine("Order {0} change is unknown.", Order.GWOrderID); break; } } #endregion #region IDisposable Members public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion #region Private Methods private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. } // If disposing is false, // only the following code is executed. cel.Shutdown(); } disposed = true; } private void PrintOrder(CQGOrder order, CQGFill fill) { Console.WriteLine("Order {0} :", order.GWOrderID); Console.WriteLine("Status - {0}", OrderStatusToString(order.GWStatus)); Console.WriteLine("Account - {0}", order.Account.GWAccountName); Console.WriteLine("Instrument - {0}", order.InstrumentName); Console.WriteLine("Order Side - {0}", order.Side == eOrderSide.osdBuy ? "Buy" : "Sell"); Console.WriteLine("Quantity - {0}", order.Quantity); Console.WriteLine("Filled Quantity - {0}", order.FilledQuantity); Console.WriteLine("Place Time - {0}", order.PlaceTime); Console.WriteLine("Current Fill Price - {0}", fill == null ? "N/A" : fill.get_Price(0).ToString()); } private string OrderStatusToString(eOrderStatus orderStatus) { switch (orderStatus) { case eOrderStatus.osActiveAt: return "ActiveAt"; case eOrderStatus.osBusted: return "Busted"; case eOrderStatus.osCanceled: return "Canceled"; case eOrderStatus.osContingent: return "Contingent"; case eOrderStatus.osDisconnected: return "Disconnected"; case eOrderStatus.osExpired: return "Expired"; case eOrderStatus.osFilled: return "Filled"; case eOrderStatus.osInCancel: return "InCancel"; case eOrderStatus.osInClient: return "InClient"; case eOrderStatus.osInModify: return "InModify"; case eOrderStatus.osInOrderBook: return "InOrderBook"; case eOrderStatus.osInTransit: return "InTransit"; case eOrderStatus.osInTransitTimeout: return "InTransitTimeout"; case eOrderStatus.osNotSent: return "NotSent"; case eOrderStatus.osParked: return "Parked"; case eOrderStatus.osRejectFCM: return "RejectFCM"; case eOrderStatus.osRejectGW: return "RejectGW"; default: return "Unknown"; } } #endregion [STAThread] static void Main(string[] args) { OrdersPlacingSample test = new OrdersPlacingSample(); // Start Application. Will work until some error occurred. System.Windows.Forms.Application.Run(); } } }