This event is fired when the account or position information is changed.
Visual Basic |
---|
Public Event AccountChanged( _ ByVal change_type As eAccountChangeType, _ ByVal cqg_account As CQGAccount, _ ByVal cqg_position As CQGPosition _ ) |
- change_type
Account change type which allows to differentiate the occurred changesValue Description actAccountChanged An account-specific change occurred, e.g. date of last statement is changed.
After an event of this type, another AccountChange event of actPositionsReloaded type will be fired.
An event of this type is also fired for each account if the account subscription level is set from aslAccountsOnly to aslAccountsAndUpdates.
actAccountsReloaded All accounts are reloaded.
This change type is passed in two cases:
- when the user logs on to CQG Gateway from CQGIC
- when the user programmatically switches initial account subscription level to aslNone and then back.
actPositionAdded A new position is created. For example, due to trading new instruments.
actPositionChanged A position-specific change occurred, e.g. position quantity or price is changed. The number of fired events of this type depends on the FireEventOnChangedPrices configuration setting.
See the FireEventsOnChangedPrices configuration setting for possible options of controlling the flow of position changed events.
actPositionsReloaded - All positions of an account are reloaded.
There are many cases when this change type is passed. For example:
- when we change position subscription level from pslNoPositions to any other value
- when an account-specific change occurs for the current account
- when CQG Gateway environment change occurs, etc.
actTradersReloaded Authorized traders of accounts are reloaded.
- cqg_account
A CQGAccount object representing the account to which the current change refers.
If the change type is actAccountsReloaded this parameter will be either Nothing or a not valid instance of CQGAccount. See Invalid Object for details.
- cqg_position
A CQGPosition object representing the position to which the current change refers.
If the change type is actAccountsReloaded, actAccountChanged or actPositionsReloaded this parameter will be either Nothing or a not valid instance of CQGPosition. See Invalid Object for details.
If account subscription level is set to aslNone no AccountChanged event will be fired.
For account-specific changes:
If account subscription level is set to aslAccountsOnly only one AccountChanged event will be fired, and that upon the subscription level change.
If account subscription level is set to aslAccountsAndUpdates, an AccountChanged event will be fired on each change.
AccountChanged event firing for position-specific changes is controlled on account level and can be configured for each account separately.
Note: if a currency rate is changed obviously it will lead to re-calculations of account summaries. However in this situation only CurrencyRatesChanged event will be fired. The corresponding account changes will be available upon access to account summaries.
The examples below show how to track accounts and positions changes 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_AccountChanged(ByVal change As CQG.eAccountChangeType, ByVal Account As CQG.ICQGAccount, ByVal position As CQG.ICQGPosition) Dim trade As CQGTrade ' Account/Position is changed Select Case change Case actAccountsReloaded Dim acc As CQGAccount For Each acc In cel.Accounts ' Print balance of all If cel.IsValid(acc.Summary.Balance) Then Debug.Print "Account yesterday balance is " & acc.Summary.Balance(1) End If ' We need all open poisition instruments to be autosubscribed acc.AutoSubscribeInstruments = True Next acc Case actPositionAdded Debug.Print "New position on " & position.InstrumentName & " is added." Debug.Print "Account Positions number is " & Account.Positions.Count Case actPositionChanged If Not position.Instrument Is Nothing Then If (position.Instrument.InstrumentType And itAllOptions) = 0 Then If cel.IsValid(position.OTE) Then Debug.Print "Position OTE is " & position.OTE End If Else If cel.IsValid(position.MVO) Then Debug.Print "Position MVO is " & position.MVO End If End If Debug.Print "Number of open trades on " & position.Instrument.FullName & " is " & position.OpenTrades.Count For Each trade In position.OpenTrades Debug.Print "Open Trade quantity is " & trade.Quantity Next End If Case Else ' Not processed End Select End Sub Private Sub cel_CELStarted() ' Everything is OK with enablements Debug.Print "CEL Version : " + cel.Environment.CELVersion End Sub 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 update functionality cel.AccountSubscriptionLevel = aslAccountsAndUpdates Case Else status = "Unknown" End Select Debug.Print "GW Connection Status changed to " + status End Sub Private Sub cel_InstrumentSubscribed(ByVal symbl As String, ByVal Instrument As CQG.ICQGInstrument) Debug.Print "There is a position on " + Instrument.FullName + ". Now the instrument is subscribed" 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) ' Some error occurred MsgBox errorDescription ' Terminate application Close End Sub
using System; using System.Runtime.InteropServices; using CQG; namespace Sample { class APIConfigSample : IDisposable { // CQGCEL instance declaration. private CQGCEL cel = new CQGCELClass(); // Used to determine is the object manualy disposed. private bool disposed = false; public APIConfigSample() { // 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.Startup(); } catch( COMException ex ) { Console.WriteLine( "Error occurred during CEL initialization. {0}", ex.Message ); } } ~APIConfigSample() { 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 update 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) { Console.WriteLine("There is a position on " + Instrument.FullName + ". Now the instrument is subscribed."); } private void cel_AccountChanged(eAccountChangeType change, CQGAccount Account, CQGPosition position) { // Account/Position is changed. switch (change) { case eAccountChangeType.actAccountsReloaded : foreach (ICQGAccount acc in cel.Accounts) { // Print balance of all. if (cel.IsValid(acc.Summary.Balance(1))) { Console.WriteLine("Account Balance is : {0}", acc.Summary.Balance(1)); Console.WriteLine("Account Positions number is : {0}", acc.Positions.Count); } // We need all open poisition instruments to be auto subscribed. acc.AutoSubscribeInstruments = true; } break; case eAccountChangeType.actPositionAdded : Console.WriteLine(position.Instrument.FullName); break; case eAccountChangeType.actPositionChanged : if (position.Instrument != null) { if ((position.Instrument.InstrumentType & eInstrumentType.itAllOptions) == 0) { if (cel.IsValid(position.OTE)) { Console.WriteLine("Position OTE is : {0}", position.OTE); } } else { if (cel.IsValid(position.MVO)) { Console.WriteLine("Position MVO is : {0}", position.MVO); } } Console.WriteLine("Number of open trades on {0} is {1}.", position.Instrument.FullName, position.OpenTrades.Count); foreach (CQGTrade trade in position.OpenTrades) { Console.WriteLine("Open Trade quantity is : {0}", trade.Quantity); } } break; default : // Not processed. 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; } #endregion [STAThread] static void Main(string[] args) { APIConfigSample test = new APIConfigSample(); // Start Application. Will work until some error occurred. System.Windows.Forms.Application.Run(); } } }