Bar Events¶
Algorithm developers often need to refer to bar events in order to execute trading strategies on time in response to the updated prices. The API offers the BarOpened
and BarClosed
events for algo creators to fine-tune cBots and grasp opportunities at different points of the bar cycle.
Note
Bar events can only be handled when coding cBots. Indicators, instead, have the Calculate()
method that is called on every tick.
In this guide, we explain the difference between the OnBar()
and OnBarClosed()
methods and provide several use cases.
Bar Events in One Minute!
- The
OnBar()
method is triggered every time a new bar is drawn on the chart to which an instance is added. It is called for the newly formed bar. - The
OnBarClosed()
event handler is also triggered on each new bar. However, it is called for the last closed (i.e., previous to the current) bar. - Referring to the correct bar event, you increase the accuracy of your technical analysis and select appropriate timing for algorithm actions.
- The supported chart types for
BarOpened
andBarClosed
are time bars, candlesticks, tick, Renko, range, and Heikin-Ashi charts.
How to Use the OnBar() Method¶
The occurrence of the BarOpened
event depends on a symbol schedule and the frequency of coming prices. Usually, platform users can adjust the occurrence of BarClosed
with the chart timeframe settings.
Note
The OnBar()
method can be called as frequently as the chart timeframe settings allow this. Once a bar closes, OnBar()
will not be triggered until the next tick arrives, which logically triggers a new bar to be formed.
The OnBar()
method is invoked simultaneously with every new bar drawn on the chart to which an algorithm is attached. As a result, the OnBar()
handler is the perfect place to define custom trading logics that your cBot should repeat regularly.
In the code editor, you can specify the OnBar()
handler as follows.
override void OnBar()
How to Use the OnBarClosed() Method¶
The Algo API also allows for executing logics for the last closed bar, which, depending on the strategy you want to implement, may be a more convenient and accurate alternative to handling the BarOpened
event. On bar open, the new bar appears as a ‘doji candle’ and does not bear complete data for technical analysis.
The BarClosed
event is an alias of the BarOpened
event, meaning that it occurs only in case the BarOpened
event happens. For example, the BarClosed
event for the last bar on Friday will occur with the first opened bar on Monday, provided that the symbol is not traded over the weekend.
Note
The OnBarClosed()
method will not be called until a tick arrives to form the new bar.
The OnBarClosed()
event handler can be declared as follows.
override void OnBarClosed()
In the body of the OnBarClosed()
method, you can determine what connected actions your cBots will perform when the BarClosed
event is triggered (e.g., execute an order, outline a pattern, draw technical analysis visuals, etc.).
When accessed in this event handler, the Bars
collection does not contain the current live bar, whereas other collections (e.g., Positions
, Symbols
, etc.) contain actual data.
The OnBar()
and OnBarClosed()
event handlers work as intended in backtesting and optimisation. Both methods can be used on different chart types, including time bars, candlesticks, tick, Renko, range, and Heikin-Ashi charts.
Creating Example cBots¶
The following example cBot simply places a market order when every third bar on the EURCHF chart is opened.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
As the cBot was launched on an m1 chart, the approximate time interval between the executed market orders is three minutes.
Below is a cBot example with the nested Directional Movement System (DMS) indicator, which prints 'Uptrend!' if the DI+ value (green line) exceeds the DI- value (red line).
When DI- > DI+, 'Downtrend!' is printed in the BarClosed
event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Summary¶
The variety of bar events existing in the Algo API allows algorithm developers to choose accurate timing for the called methods. The OnBar()
method can be invoked with different frequency depending on the symbol schedule and chart timeframe settings, which also adds to the functionality of cBots. The OnBarClosed()
method is a convenient alias of OnBar()
for working with indicators.