Using the available aggregation types in your code
Theory
As we know, chart aggregation is a type of displaying aggregated values. These values are price, volume and time. The main idea of each aggregation is to help traders analyze the state of the market in history and in real time.
At this moment, Quantower API supports 9aggregation types. All of them you can use in your scripts easily. But before we continue, please read the article how to download history by using Quantower API.
To download aggregated history we need use GetHistory method which takes instanse of HistoryRequestParameters class as input parameter. This class contains the necessary properties such as FromTime, ToTime, HistoryType, etc. with which we can flexibly customize our request. But today we are interested in the Aggregation property. This property contains instance of HistoryAggregation class which is base class for all available aggregation types. All we need to get the aggregated history is to set to this property instance of required aggregation type.
Listed below are all available aggregation classes with examples of history requests.
The HistoryAggregationVolume class is used to build the Volume bars chart.
volumeValue - base volume value of bar
Practice
In this part of the article, we will create a simple strategy script in which we will try to apply the knowledge. Let's describe our actions step by step:
Create HistoricalData instance by loading 6 hours of Renko history.
Create Fast SMA and Slow SMA indicators and then attach them to our HistoricalData.
Display metrics:
Fast SMA value
Slow SMA value
Current brick high price
Current brick low price
Log high and low prices of each new brick.
Input parameters
First, let’s define input parameters. In this section, we want to be able to change the aggregation parameters and indicator base settings.
OnRun method
In this section, we will carry out the first, second and fourth points.
Pay attention to line 24. Here we create instance of HistoryAggregationRenko class and pass required parameters.
Pay attention to line 30. Here we subscribe 'NewHistoryItem' event. In other words, our 'RenkoHistoricalData_NewHistoryItem' handler will trigger on each new brick item.
OnGetMetrics method
Here we create required metrics.
Pay attention to line 10. Here we use 'FormatPrice' method to format indicator value to symbol tick size.
OnStop method
Never forget to remove unused objects and unsubscribe form unused events.