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.
var pointFiguresHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
{
Symbol = this.Symbol,
FromTime = DateTime.Now.AddDays(-1),
ToTime = DateTime.Now,
HistoryType = this.Symbol.HistoryType,
Aggregation = new HistoryAggregationPointsAndFigures(Period.TICK1, 100, 50, PointsAndFiguresStyle.HighLow),
});
Volume Bars aggregation
The HistoryAggregationVolume class is used to build the Volume bars chart.
new HistoryAggregationVolume(int volumeValue);
volumeValue - base volume value of bar
var volumeBarsHistoricalData = this.Symbol.GetHistory(new HistoryRequestParameters()
{
Symbol = this.Symbol,
FromTime = DateTime.Now.AddHours(-4),
ToTime = DateTime.Now,
Period = Period.TICK1,
HistoryType = this.Symbol.HistoryType,
Aggregation = new HistoryAggregationVolume(1000),
});
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 settigns.
[InputParameter("Symbol", 10)]
public Symbol Symbol;
[InputParameter("Renko period", 20)]
public Period RenkoPeriod = Period.MIN1;
[InputParameter("Brick size", 30)]
public int BrickSize = 10;
[InputParameter("Renko style", 40, variants: new object[]
{
"Classic", RenkoStyle.Classic,
"High/Low", RenkoStyle.HighLow,
"Adv. Classic", RenkoStyle.AdvancedClassic,
"Adv. High/Low", RenkoStyle.AdvancedHighLow,
})]
public RenkoStyle RenkoStyle = RenkoStyle.Classic;
[InputParameter("Fast SMA period", 50, 1, 9999, 1, 0)]
public int FastSmaPeriod = 10;
[InputParameter("Slow SMA period", 50, 1, 9999, 1, 0)]
public int SlowSmaPeriod = 30;
private HistoricalData renkoHistoricalData;
private Indicator fastSmaIndicator;
private Indicator slowSmaIndicator;
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 attentionto line 30. Here we subscribe 'NewHistoryItem' event. Another words, our 'RenkoHistoricalData_NewHistoryItem' handler will trigger on each new brick item.
protected override void OnRun()
{
//
// check is symbol is null
//
if (Symbol == null)
{
Log("Symbol is null", StrategyLoggingLevel.Error);
Stop();
return;
}
try
{
//
// Download history (use Renko aggregation)
//
renkoHistoricalData = Symbol.GetHistory(new HistoryRequestParameters()
{
Symbol = Symbol,
HistoryType = Symbol.HistoryType,
FromTime = Core.Instance.TimeUtils.DateTimeUtcNow.AddHours(-6),
ToTime = default,
Aggregation = new HistoryAggregationRenko(RenkoPeriod, BrickSize, RenkoStyle)
});
//
// Subscribe to 'NewHistoryItem' event
//
renkoHistoricalData.NewHistoryItem += RenkoHistoricalData_NewHistoryItem;
//
// Create Fast/Slow SMA indicators
//
fastSmaIndicator = Core.Instance.Indicators.BuiltIn.SMA(FastSmaPeriod, PriceType.Close);
slowSmaIndicator = Core.Instance.Indicators.BuiltIn.SMA(SlowSmaPeriod, PriceType.Close);
//
// Attach our indicators to downloaded HistoricalData
//
renkoHistoricalData.AddIndicator(fastSmaIndicator);
renkoHistoricalData.AddIndicator(slowSmaIndicator);
}
catch (Exception ex)
{
Log(ex.Message, StrategyLoggingLevel.Error);
Stop();
}
}
private void RenkoHistoricalData_NewHistoryItem(object sender, HistoryEventArgs e)
{
// get high price for new brick
var highPrice = e.HistoryItem[PriceType.High];
// get low price for new brick
var lowPrice = e.HistoryItem[PriceType.Low];
// print message
Log($"New brick -- High: {highPrice} | Low: {lowPrice}", StrategyLoggingLevel.Info);
}
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.
protected override List<StrategyMetric> OnGetMetrics()
{
var result = base.OnGetMetrics();
if (fastSmaIndicator != null)
{
result.Add(new StrategyMetric()
{
Name = "Fast SMA value",
FormattedValue = Symbol.FormatPrice(fastSmaIndicator.GetValue())
});
}
if (slowSmaIndicator != null)
{
result.Add(new StrategyMetric()
{
Name = "Slow SMA value",
FormattedValue = Symbol.FormatPrice(slowSmaIndicator.GetValue())
});
}
if (renkoHistoricalData != null)
{
result.Add(new StrategyMetric()
{
Name = "Current brick high price",
FormattedValue = Symbol.FormatPrice(renkoHistoricalData[0][PriceType.High])
});
result.Add(new StrategyMetric()
{
Name = "Current brick low price",
FormattedValue = Symbol.FormatPrice(renkoHistoricalData[0][PriceType.Low])
});
}
return result;
}
OnStop method
Never forget to remove unused objects and unsubscribe form unused events.
protected override void OnStop()
{
if (renkoHistoricalData != null)
{
//
// remove 'fastSmaIndicator' instance
//
if (fastSmaIndicator != null)
renkoHistoricalData.RemoveIndicator(fastSmaIndicator);
//
// remove 'slowSmaIndicator' instance
//
if (slowSmaIndicator != null)
renkoHistoricalData.RemoveIndicator(slowSmaIndicator);
//
// unsubscribe from 'NewHistoryItem' event and dispose our HistoricalData instance
//
renkoHistoricalData.NewHistoryItem -= RenkoHistoricalData_NewHistoryItem;
renkoHistoricalData.Dispose();
}
}