Events
Events
AgenaTrader is an event-oriented application by definition.
Programming in AgenaTrader using the various application programming interface (API) methods is based initially on the Overwriting of routines predefined for event handling.
The following methods can be used and therefore overwritten:
OnBrokerConnect()
OnBrokerConnect() is only available for overwriting in UserStrategies!
Description
OnBrokerConnect() method is invoked each time the connection to the broker is established. With the help of OnBrokerConnect(), it is possible to reassign the existing or still open orders to the strategy in the event of a connection abort with the broker and thus allow it to be managed again.
More information can be found here: Events.
Parameter
none
Usage
Example
OnBrokerDisconnect()
OnBrokerDisconnect() is only available for overwriting in UserStrategies!
Description
OnBrokerDisconnect() method is invoked each time the connection to the broker is interrupted.
More information can be found here: Events.
Parameter
An object from TradingDatafeedChangedEventArgs
Usage
Example
OnInit()
Description
The OnInit() method is called up once at the beginning of an indicator or strategy calculation. This method can be used to set indicator properties, initialize your own variables, or add plots.
Parameter
none
Return Value
none
Usage
Important Keywords
IsAutoScale
Additional Keywords for Strategies
EntriesPerDirection
More Information
Caution: The OnInit() method is not only called up at the beginning of an indicator or strategy calculation, but also if the chart is reloaded unexpectedly or if the properties dialog of indicators is opened and so on. Developers of custom AgenaScripts should NOT use this method for running their own routines, opening forms, performing license checks, etc. The OnStart() method should be used for these kind of tasks.
Example
OnCalculate()
Description
The OnCalculate() method is called up whenever a bar changes; depending on the variables of CalculateOnClosedBar, this will happen upon every incoming tick or when the bar has completed/closed. OnCalculate is the most important method and also, in most cases, contains the largest chunk of code for your self-created indicators or strategies. The editing begins with the oldest bar and goes up to the newest bar within the chart. The oldest bar has the number 0. The indexing and numbering will continue to happen; in order to obtain the numbering of the bars you can use the current bar variable. You can see an example illustrating this below.
Caution: the numbering/indexing is different from the bar index – see Bars.
More information can be found here: Events.
Parameter
none
Usage
Example
OnChartPanelMouseDown()
Description
In an indicator, or strategy, the click event of the mouse can be processed. For this, it is necessary to program an EventHandler as a method and add this method to the Chart.ChartPanelMouseDown event.
Attention!
It is important to remove the EventHandler from the OnDispose() method, otherwise the EventHandler will still be executed even if the indicator has been removed from the chart.
Example
OnChartPanelMouseMove()
Description
In an indicator, or strategy, the current position of the mouse can be evaluated and processed. For this, it is necessary to program an EventHandler as a method and add this method to the Chart.ChartPanelMouseMove event.
Attention!
It is important to remove the EventHandler from the OnDispose() method, otherwise the EventHandler will still be executed even if the indicator has been removed from the chart.
Example
OnDispose()
Description
The OnDispose() method can also be overridden in order to once again free up all the resources used in the script.
See OnInit() and OnStart().
More information can be found here: Events.
Parameter
none
Usage
More Information
Caution: Please do not override the Dispose() method since this can only be used much later within the script. This would lead to resources being used and held for an extended period and thus potentially causing unexpected consequences for the entire application.
Example
OnLevel1()
Description
The OnLevel1() method is called up when a change in level 1 data has occurred, meaning whenever there is a change in the bid price, ask price, bid volume, or ask volume, and of course in the last price after a real turnover has occurred. In a multibar indicator, the rocessingBarSeriesIndex property identifies the data series that was used for an information request for OnLevel1(). OnLevel1() will not be called up for historical data.
More information can be found here: Events.
Notes regarding data from Yahoo (YFeed)
The field "LastPrice" equals – as usual – either the bid price or the ask price, depending on the last revenue turnover.
The MarketDataType" field always equals the "last" value
The fields "Volume", "BidSize" and "AskSize" are always 0.
Usage
Parameter
Example
OnLevel2()
Description
The OnLevel2() method is called up whenever there is a change in the level 2 data (market depth). In a multibar indicator, the ProcessingBarSeriesIndex property identifies the data series for which the OnLevel2() method is called up. OnLevel2 is not called up for historical data.
More information can be found here: Events.
Usage
Parameter
An object from Level2Args
Example
OnOrderChanged()
Description
The OnOrderChanged() method is called up whenever the status is changed by a strategy-managed order. A status change can therefore occur due to a change in the volume, price or status of the exchange (from “working” to “filled”). It is guaranteed that this method will be called up in the correct order for the relevant events.
Important note: If a strategy is to be controlled by order executions, we highly recommend that you use OnOrderExecution() instead of OnOrderChanged(). Otherwise there may be problems with partial executions.
More information can be found here: Events.
Parameter
An order object of the type IOrder
Usage
Example
OnOrderExecution()
Description
The OnOrderExecution() method is called up when an order is executed (filled). The status of a strategy can be changed by a strategy-managed order. This status change can be initiated by the changing of a volume, price or the status of the exchange (from “working” to “filled”). It is guaranteed that this method will be called up in the correct order for all events.
OnOrderExecution() will always be executed AFTER OnOrderChanged().
More information can be found here: Events.
Parameter
An execution object of the type IExecution
Usage
Example
OnStart()
Description
The OnStart() method can be overridden to initialize your own variables, perform license checks or call up user forms etc. OnStart() is only called up once at the beginning of the script, after OnInit() and before OnCalculate() are called up which results in the following execution sequence:
OnInit OnStart OnStop OnDispose
See OnDispose().
More information can be found here: Events.
Parameter
none
Usage
Example
OnStop()
Description
The OnStop() method is called up once a script is terminated. This can be when e.g. an indicator was removed from the chart or a column with an indicator / a scripted condition was removed from the scanner.
See OnDispose().
More information can be found here: Events.
Parameter
none
Usage
Example
Last updated