| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic Sharpe Ratio 0% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
using QuantConnect.Data;
using QuantConnect.Indicators;
namespace QuantConnect.Algorithm.CSharp
{
public class VolumeProfileAlgorithm : QCAlgorithm
{
private Symbol _aapl;
private VolumeProfileIndicator _vp30;
public override void Initialize()
{
SetStartDate(2021, 1, 1); //Set Start Date
SetEndDate(2021, 1, 30);
SetCash(10000); //Set Strategy Cash
SetBrokerageModel(new Brokerages.InteractiveBrokersBrokerageModel());
_aapl = AddEquity("AAPL", Resolution.Hour).Symbol;
SetWarmup(250);
var symbols = new Symbol[] { _aapl };
foreach (var symbol in symbols)
{
var security = Securities[symbol];
}
_vp30 = new VolumeProfileIndicator(30);
RegisterIndicator(_aapl, _vp30, Resolution.Daily);
}
public override void OnData(Slice slice)
{
if (!IsWarmingUp && Time.Hour == 10 && Time.Minute == 0)
{
var va = _vp30.ValueArea;
var rv = _vp30.RelativeVolume();
var pos = _vp30.UpDownPercent();
Debug($"DATE: {Time.ToShortDateString()} POC: {_vp30:C2} VA: {va.Item1:C2} - {va.Item2:C2} RV: {rv:F2} POS:{pos:F2}");
}
}
}
}using System;
using System.Linq;
using QuantConnect.Data.Market;
namespace QuantConnect.Indicators
{
/// <summary>
/// Produces a Volume Profile and returns the Point of Control.
/// </summary>
public class VolumeProfileIndicator : TradeBarIndicator, IIndicatorWarmUpPeriodProvider
{
private int _period = 0;
private RollingWindow<TradeBar> _bars;
private Maximum _max;
private Minimum _min;
private int _numBins = 24;
private decimal _binSize = 0M;
private decimal[] _profile;
private decimal[] _upProfile;
private decimal[] _downProfile;
private decimal _pointOfControl = 0M;
private decimal _lowerValueAreaBound = 0M;
private decimal _upperValueAreaBound = 0M;
/// <summary>
/// Volume Profile Indicator
/// Implemented based on TradingView implementation
/// https://www.tradingview.com/support/solutions/43000480324-how-is-volume-profile-calculated/
/// </summary>
/// <param name="name">string - a name for the indicator</param>
/// <param name="period">int - the number of periods to calculate the VP</param>
public VolumeProfileIndicator(string name, int period)
: base(name)
{
if (period < 2) throw new ArgumentException("The Volume Profile period should be greater or equal to 2", "period");
_period = period;
WarmUpPeriod = period;
_bars = new RollingWindow<TradeBar>(_period);
_max = new Maximum(_period);
_min = new Minimum(_period);
}
/// <summary>
/// A Volume Profile Indicator
/// </summary>
/// <param name="period">int - the number of periods over which to calculate the VP</param>
public VolumeProfileIndicator(int period)
: this($"VP({period})", period)
{
}
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset()
{
base.Reset();
}
/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady => _bars.IsReady;
/// <summary>
/// Required period, in data points, for the indicator to be ready and fully initialized.
/// </summary>
public int WarmUpPeriod { get; }
/// <summary>
/// Computes the next value of this indicator from the given state
/// </summary>
/// <param name="input">The input given to the indicator</param>
/// <returns>
/// A new value for this indicator
/// </returns>
protected override decimal ComputeNextValue(TradeBar input)
{
_bars.Add(input);
_max.Update(input.Time, input.Close);
_min.Update(input.Time, input.Close);
if (!_bars.IsReady)
return 0m;
// Calculate size of each histogram bin
_binSize = (_max - _min) / _numBins;
_profile = new decimal[_numBins];
_upProfile = new decimal[_numBins];
_downProfile = new decimal[_numBins];
int binIndex = 0;
foreach (var bar in _bars)
{
// Determine histogram bin for this bar
binIndex = GetBinIndex(bar.Close);
// Add volume to the bin
_profile[binIndex] += bar.Volume;
// Add volume to the up or down profile
if (bar.Close >= bar.Open)
_upProfile[binIndex] += bar.Volume;
else
_downProfile[binIndex] += bar.Volume;
}
// Calculate Point of Control
int pocIndex = 0;
decimal maxVolume = 0;
for (int i = 0; i < _numBins; i++)
{
if (_profile[i] > maxVolume)
{
pocIndex = i;
maxVolume = _profile[i];
}
}
// POC will be midpoint of the bin
_pointOfControl = _min + (_binSize * pocIndex) + _binSize / 2;
CalculateValueArea(pocIndex);
return _pointOfControl;
}
private void CalculateValueArea(int pocIndex)
{
// Calculate Value Area
// https://www.oreilly.com/library/view/mind-over-markets/9781118659762/b01.html
int upperIdx = pocIndex;
int lowerIdx = pocIndex;
decimal totalVolume = _profile.Sum();
decimal valueAreaVolume = 0m;
decimal percentVolume = 0m;
while (percentVolume < .7m)
{
var lowerVolume = 0m;
var upperVolume = 0m;
var nextUpperIdx = 0;
var nextLowerIdx = 0;
// Total the volume of the next two price bins above and below the value area.
if (lowerIdx >= 2)
{
lowerVolume = _profile[lowerIdx - 1] + _profile[lowerIdx - 2];
nextLowerIdx = lowerIdx - 2;
}
else if (lowerIdx == 1)
{
lowerVolume = _profile[lowerIdx - 1];
nextLowerIdx = lowerIdx - 1;
}
if (upperIdx <= _numBins - 3)
{
upperVolume = _profile[upperIdx + 1] + _profile[upperIdx + 2];
nextUpperIdx = upperIdx + 2;
}
else if (upperIdx == _numBins - 2)
{
upperVolume = _profile[upperIdx + 1];
nextUpperIdx = upperIdx + 1;
}
// Compare volume of the next upper and lower area. Add the higher to the value area.
if (upperVolume >= lowerVolume)
{
valueAreaVolume += upperVolume;
upperIdx = nextUpperIdx;
}
else
{
valueAreaVolume += lowerVolume;
lowerIdx = nextLowerIdx;
}
percentVolume = valueAreaVolume / totalVolume;
}
_lowerValueAreaBound = _min + lowerIdx * _binSize;
_upperValueAreaBound = _min + (upperIdx+1) * _binSize;
}
/// <summary>
/// Returns a tuple where the first element is the lower bound of the value area and the second is the upper bound.
/// </summary>
/// <returns></returns>
public Tuple<decimal, decimal> ValueArea
{
get
{
return new Tuple<decimal, decimal>(_lowerValueAreaBound, _upperValueAreaBound);
}
}
/// <summary>
/// Returns the relative volume of a price area as a percentage of total volume in the profile
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
public decimal RelativeVolume(decimal price)
{
decimal totalVolume = _profile.Sum();
int binIndex = GetBinIndex(price);
return _profile[binIndex] / totalVolume;
}
/// <summary>
/// Returns the relative volume of current price area as a percentage of total volume in the profile
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
public decimal RelativeVolume()
{
return RelativeVolume(_bars[0].Close);
}
/// <summary>
/// Returns the ratio of Up vs Down candles in the given price area. Over .5 means more Up candles.
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
public decimal UpDownPercent(decimal price)
{
int binIndex = GetBinIndex(price);
return _upProfile[binIndex] / _profile[binIndex];
}
/// <summary>
/// Returns the ratio of Up vs Down candles in the current price area. Over .5 means more Up candles.
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
public decimal UpDownPercent()
{
return UpDownPercent(_bars[0].Close);
}
/// <summary>
/// Get the histogram bin index for a given price area.
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
private int GetBinIndex(decimal price)
{
int binIndex = (int)Math.Truncate((price - _min) / _binSize);
// In the case where the price is the maximum, the formula will push the index out of bounds. We return the max bin index in that case.
if (binIndex == _numBins)
return binIndex - 1;
return binIndex;
}
}
}