Access to advanced aggregations
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 9 aggregation 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.

Available aggregation classes

Tick aggregation

The HistoryAggregationTick class is used to buid simple Tick chart.
1
new HistoryAggregationTick(int ticksCount);
Copied!
    ticksCount - the number of ticks for aggregation.
1
var tickhistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddHours(-3),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.VolumeType == SymbolVolumeType.Volume ? HistoryType.Last : HistoryType.BidAsk,
7
Period = Period.TICK1,
8
Aggregation = new HistoryAggregationTick(1),
9
});
Copied!

Time aggregation

The HistoryAggregationTime class is used to build the Time chart.
1
new HistoryAggregationTime(Period period);
Copied!
    period - period of time (8s, 30min, 4h etc). Instance of Period structure.
1
var timeBarHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-3),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Period = Period.MIN15,
8
Aggregation = new HistoryAggregationTime(Period.MIN15),
9
});
Copied!

Heiken-Ashi aggregation

The HistoryAggregationHeikenAshi class is used to build the Heiken-Ashi chart.
1
new HistoryAggregationHeikenAshi(HeikenAshiSource source, int value);
Copied!
    source - enum, base period of time (Tick, Seconds. Minutes etc).
    value - the amount of 'source' time.
1
var heikenAshiHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-3),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationHeikenAshi(HeikenAshiSource.Minute, 1),
8
});
Copied!

Range Bars aggregation

The HistoryAggregationRangeBars class is used to build the Range Bars chart.
1
new HistoryAggregationRangeBars(int rangeBars);
Copied!
    rangeBars - the height (in ticks) of each bar.
1
var rangeBarHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddHours(-3),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationRangeBars(10),
8
});
Copied!

Renko aggregation

The HistoryAggregationRenko class is used to build the Renko chart.
1
new HistoryAggregationRenko(Period period, int brickSize, RenkoStyle renkoStyle, int extension = 100, int inversion = 100, bool showWicks = false, bool buildCurrentBar = true)
Copied!
    period - base period of time. Instance of Period structure.
    brickSize - required size of renko brick
    renkoStyle - enum, calculation methods (Classic, HighLow, AdvancedClassic, AdvancedHighLow)
1
var renkoHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-1),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationRenko(Period.MIN1, 10, RenkoStyle.AdvancedClassic, 100, 100, true, true),
8
});
Copied!

Line break aggregation

The HistoryAggregationLineBreak class is used to build the Line break chart.
1
new HistoryAggregationLineBreak(Period period, int lineBreak);
Copied!
    period - base period of time. Instance of Period structure.
    lineBreak - line break value.
1
var lineBreakHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-1),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationLineBreak(Period.MIN15, 3),
8
});
Copied!

Kagi aggregation

The HistoryAggregationKagi class is used to build the Kagi chart.
1
new HistoryAggregationKagi(Period period, int reversal);
Copied!
    period - base period of time. Instance of Period structure.
    reversal - the amount of price movement that required for the Kagi line to reverse direction.
1
var kagiHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-1),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationKagi(Period.MIN15, 10),
8
});
Copied!

Points & Figures aggregation

The HistoryAggregationPointsAndFigures class is used to build the Points & Figures chart.
1
new HistoryAggregationPointsAndFigures(Period period, int boxSize, int reversal, PointsAndFiguresStyle style);
Copied!
    period - base period of time. Instance of Period structure.
    boxSize - price range (the number of ticks) for X-Columns or O-Columns
    reversal - a parameter that indicates the number of Box Sizes that the price should go in the opposite direction to begin a new column.
    style - enum, calculation methods (Classic, HighLow)
1
var pointFiguresHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddDays(-1),
5
ToTime = DateTime.Now,
6
HistoryType = this.Symbol.HistoryType,
7
Aggregation = new HistoryAggregationPointsAndFigures(Period.TICK1, 100, 50, PointsAndFiguresStyle.HighLow),
8
});
Copied!

Volume Bars aggregation

The HistoryAggregationVolume class is used to build the Volume bars chart.
1
new HistoryAggregationVolume(int volumeValue);
Copied!
    volumeValue - base volume value of bar
1
var volumeBarsHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
2
{
3
Symbol = this.Symbol,
4
FromTime = DateTime.Now.AddHours(-4),
5
ToTime = DateTime.Now,
6
Period = Period.TICK1,
7
HistoryType = this.Symbol.HistoryType,
8
Aggregation = new HistoryAggregationVolume(1000),
9
});
Copied!

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:
    1.
    Create HistoricalData instance by loading 6 hours of Renko history.
    2.
    Create Fast SMA and Slow SMA indicators and then attach them to our HistoricalData.
    3.
    Display metrics:
      1.
      Fast SMA value
      2.
      Slow SMA value
      3.
      Current brick high price
      4.
      Current brick low price
    4.
    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 settigns.
1
[InputParameter("Symbol", 10)]
2
public Symbol Symbol;
3
4
[InputParameter("Renko period", 20)]
5
public Period RenkoPeriod = Period.MIN1;
6
7
[InputParameter("Brick size", 30)]
8
public int BrickSize = 10;
9
10
[InputParameter("Renko style", 40, variants: new object[]
11
{
12
"Classic", RenkoStyle.Classic,
13
"High/Low", RenkoStyle.HighLow,
14
"Adv. Classic", RenkoStyle.AdvancedClassic,
15
"Adv. High/Low", RenkoStyle.AdvancedHighLow,
16
})]
17
public RenkoStyle RenkoStyle = RenkoStyle.Classic;
18
19
[InputParameter("Fast SMA period", 50, 1, 9999, 1, 0)]
20
public int FastSmaPeriod = 10;
21
22
[InputParameter("Slow SMA period", 50, 1, 9999, 1, 0)]
23
public int SlowSmaPeriod = 30;
24
25
private HistoricalData renkoHistoricalData;
26
private Indicator fastSmaIndicator;
27
private Indicator slowSmaIndicator;
Copied!

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. Another words, our 'RenkoHistoricalData_NewHistoryItem' handler will trigger on each new brick item.
1
protected override void OnRun()
2
{
3
//
4
// check is symbol is null
5
//
6
if (Symbol == null)
7
{
8
Log("Symbol is null", StrategyLoggingLevel.Error);
9
Stop();
10
return;
11
}
12
13
try
14
{
15
//
16
// Download history (use Renko aggregation)
17
//
18
renkoHistoricalData = Symbol.GetHistory(new HistoryRequestParameters()
19
{
20
Symbol = Symbol,
21
HistoryType = Symbol.HistoryType,
22
FromTime = Core.Instance.TimeUtils.DateTimeUtcNow.AddHours(-6),
23
ToTime = default,
24
Aggregation = new HistoryAggregationRenko(RenkoPeriod, BrickSize, RenkoStyle)
25
});
26
27
//
28
// Subscribe to 'NewHistoryItem' event
29
//
30
renkoHistoricalData.NewHistoryItem += RenkoHistoricalData_NewHistoryItem;
31
32
//
33
// Create Fast/Slow SMA indicators
34
//
35
fastSmaIndicator = Core.Instance.Indicators.BuiltIn.SMA(FastSmaPeriod, PriceType.Close);
36
slowSmaIndicator = Core.Instance.Indicators.BuiltIn.SMA(SlowSmaPeriod, PriceType.Close);
37
38
//
39
// Attach our indicators to downloaded HistoricalData
40
//
41
renkoHistoricalData.AddIndicator(fastSmaIndicator);
42
renkoHistoricalData.AddIndicator(slowSmaIndicator);
43
}
44
catch (Exception ex)
45
{
46
Log(ex.Message, StrategyLoggingLevel.Error);
47
Stop();
48
}
49
}
50
51
private void RenkoHistoricalData_NewHistoryItem(object sender, HistoryEventArgs e)
52
{
53
// get high price for new brick
54
var highPrice = e.HistoryItem[PriceType.High];
55
56
// get low price for new brick
57
var lowPrice = e.HistoryItem[PriceType.Low];
58
59
// print message
60
Log(quot;New brick -- High: {highPrice} | Low: {lowPrice}", StrategyLoggingLevel.Info);
61
}
Copied!

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.
1
protected override List<StrategyMetric> OnGetMetrics()
2
{
3
var result = base.OnGetMetrics();
4
5
if (fastSmaIndicator != null)
6
{
7
result.Add(new StrategyMetric()
8
{
9
Name = "Fast SMA value",
10
FormattedValue = Symbol.FormatPrice(fastSmaIndicator.GetValue())
11
});
12
}
13
14
if (slowSmaIndicator != null)
15
{
16
result.Add(new StrategyMetric()
17
{
18
Name = "Slow SMA value",
19
FormattedValue = Symbol.FormatPrice(slowSmaIndicator.GetValue())
20
});
21
}
22
23
if (renkoHistoricalData != null)
24
{
25
result.Add(new StrategyMetric()
26
{
27
Name = "Current brick high price",
28
FormattedValue = Symbol.FormatPrice(renkoHistoricalData[0][PriceType.High])
29
});
30
result.Add(new StrategyMetric()
31
{
32
Name = "Current brick low price",
33
FormattedValue = Symbol.FormatPrice(renkoHistoricalData[0][PriceType.Low])
34
});
35
}
36
37
return result;
38
}
Copied!

OnStop method

Never forget to remove unused objects and unsubscribe form unused events.
1
protected override void OnStop()
2
{
3
if (renkoHistoricalData != null)
4
{
5
//
6
// remove 'fastSmaIndicator' instance
7
//
8
if (fastSmaIndicator != null)
9
renkoHistoricalData.RemoveIndicator(fastSmaIndicator);
10
11
//
12
// remove 'slowSmaIndicator' instance
13
//
14
if (slowSmaIndicator != null)
15
renkoHistoricalData.RemoveIndicator(slowSmaIndicator);
16
17
//
18
// unsubscribe from 'NewHistoryItem' event and dispose our HistoricalData instance
19
//
20
renkoHistoricalData.NewHistoryItem -= RenkoHistoricalData_NewHistoryItem;
21
renkoHistoricalData.Dispose();
22
}
23
}
Copied!
Last modified 6mo ago