This event is fired if a query has some progress: either new results arrived or its status is changed.
Visual Basic |
---|
Public Event OnQueryProgress( _ ByVal cqg_orders_query As CQGOrdersQuery, _ ByVal cqg_error As CQGError _ ) |
- cqg_orders_query
The CQGOrdersQuery object, to which the change refers.
This is the same object that was returned by QueryOrders
- cqg_error
CQGError object representing the last error occurred, while processing this order query.
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.
The results of a query are received via this event. There may be more than one event due to the fact that results can be sent via more than one chunk of orders.
Results are represented as CQGOrders collection available in Orders property of CQGOrdersQuery object.
If the results were sent via more than one chunk the last arrived chunk is available from LastChunk property and also all previously received chunks, including the last one, are already added to the Orders collection of current query.
The examples below show how to query 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.NewInstrumentChangeMode = True cel.APIConfiguration.PositionDetailing = pdAllTrades cel.APIConfiguration.PriceMode = pmTradesOnly cel.APIConfiguration.UseOrderSide = False 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 request all working orders on EP, placed on all accounts Dim query As CQGOrdersQuery Dim acc As CQGAccount For Each acc In cel.Accounts Set query = cel.QueryOrders(acc, Instrument, osfNotFinal) Debug.Print "Not final orders are requested for account " & acc.GWAccountName Next End Sub Private Sub cel_OnQueryProgress(ByVal query As CQG.ICQGOrdersQuery, ByVal Error As CQG.ICQGError) ' We have a chunk of requested historical orders Debug.Print "Orders chunk for query " & query.QueryID & " received." Dim order As CQGOrder For Each order In query.LastChunk Debug.Print " GW Account Name = " & order.Account.GWAccountName Debug.Print " Is Final - " & order.IsFinal Debug.Print " Instrument Name - " & order.InstrumentName Debug.Print " Quantity - " & order.Quantity Next If query.status = rsInProgress Then Debug.Print "Waiting for next chunk" ElseIf query.status = rsSuccess Then Debug.Print "No more chunks for this query" End If 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) Dim fullErr As String If TypeName(obj.Owner) = "CQGOrdersQuery" Then Dim query As CQGOrdersQuery Set query = obj.Owner fullErr = "Query " + query.QueryID + " failed, " End If fullErr = fullErr + errorDescription MsgBox fullErr ' Terminate application Close End Sub
using System; using System.Runtime.InteropServices; using CQG; namespace Sample { class OrdersQuerySample : IDisposable { // CQGCEL instance declaration. private CQGCEL cel = new CQGCELClass(); // Used to determine is the object manualy disposed. private bool disposed = false; public OrdersQuerySample() { // Initialize CQGCEL instance. try { // Configure CEL. cel.APIConfiguration.ReadyStatusCheck = eReadyStatusCheck.rscOff; cel.APIConfiguration.CollectionsThrowException = false; cel.APIConfiguration.DefPositionSubscriptionLevel = ePositionSubscriptionLevel.pslSnapshotAndUpdates; cel.APIConfiguration.NewInstrumentChangeMode = true; cel.APIConfiguration.PositionDetailing = ePositionDetailing.pdAllTrades; cel.APIConfiguration.PriceMode = ePriceMode.pmTradesOnly; cel.APIConfiguration.UseOrderSide = false; // 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.OnQueryProgress += new _ICQGCELEvents_OnQueryProgressEventHandler(cel_OnQueryProgress); cel.Startup(); } catch (COMException ex) { Console.WriteLine("Error occurred during CEL initialization. {0}", ex.Message); } } ~OrdersQuerySample() { 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. string fullErr = ""; CQGError objErr = (CQGError)obj; if (objErr.Owner.GetType().Name == "CQGOrdersQuery") { CQGOrdersQuery query = (CQGOrdersQuery)objErr.Owner; fullErr = "Query " + query.QueryID + " failed, "; } fullErr = fullErr + errorDescription; Console.WriteLine(fullErr); 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.aslAccountsAndUpdates; break; default: status = "Unknown"; break; } Console.WriteLine("GW Connection Status changed to " + status); } void cel_InstrumentSubscribed(string symbl, CQGInstrument Instrument) { // EP subscribed, let's request all working orders on EP, placed on all accounts. CQGOrdersQuery query; foreach (CQGAccount acc in cel.Accounts) { query = cel.QueryOrders(acc, Instrument, eOrderStatusFilter.osfNotFinal, DateTime.MinValue , eOrderSide.osdUndefined); Console.WriteLine("Not final orders are requested for account {0}", acc.GWAccountName); } } void cel_AccountChanged(eAccountChangeType change, CQGAccount Account, CQGPosition position) { // Request instument. if (change == eAccountChangeType.actAccountsReloaded) { cel.NewInstrument("EP"); } } void cel_OnQueryProgress(CQGOrdersQuery query, CQGError error) { // We have a chunk of requested historical orders. Console.WriteLine("Orders chunk for query {0} received.", query.QueryID); foreach (CQGOrder order in query.LastChunk) { Console.WriteLine("GW Account Name - {0}", order.Account.GWAccountName); Console.WriteLine("Is Final - {0}", order.IsFinal); Console.WriteLine("Instrument Name - {0}", order.InstrumentName); Console.WriteLine("Quantity - {0}", order.Quantity); } if (query.Status == eRequestStatus.rsInProgress) { Console.WriteLine("Waiting for the next chunk."); } else if(query.Status == eRequestStatus.rsSuccess) { Console.WriteLine("No more chunks for this query."); } } #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; } #endregion [STAThread] static void Main(string[] args) { OrdersQuerySample test = new OrdersQuerySample(); // Start Application. Will work until some error occurred. System.Windows.Forms.Application.Run(); } } }