/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using MathNet.Numerics.Statistics;
using QuantConnect.Data;
using QuantConnect.Indicators;
using QuantConnect.Data.Market;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Securities.Volatility;
using QuantConnect.Util;
namespace QuantConnect.Securities
{
///
/// Provides an implementation of that computes the
/// relative standard deviation as the volatility of the security
///
public class RelativeStandardDeviationVolatilityModel : BaseVolatilityModel
{
private bool _needsUpdate;
private decimal _volatility;
private DateTime _lastUpdate;
private readonly TimeSpan _periodSpan;
private readonly object _sync = new object();
private readonly RollingWindow _window;
///
/// Gets the volatility of the security as a percentage
///
public override decimal Volatility
{
get
{
lock (_sync)
{
if (_window.Count < 2)
{
return 0m;
}
if (_needsUpdate)
{
_needsUpdate = false;
var mean = Math.Abs(_window.Mean().SafeDecimalCast());
if (mean != 0m)
{
// volatility here is supposed to be a percentage
var std = _window.StandardDeviation().SafeDecimalCast();
_volatility = std / mean;
}
}
}
return _volatility;
}
}
///
/// Initializes a new instance of the class
///
/// The time span representing one 'period' length
/// The nuber of 'period' lengths to wait until updating the value
public RelativeStandardDeviationVolatilityModel(
TimeSpan periodSpan,
int periods)
{
if (periods < 2) throw new ArgumentOutOfRangeException("periods", "'periods' must be greater than or equal to 2.");
_periodSpan = periodSpan;
_window = new RollingWindow(periods);
_lastUpdate = DateTime.MinValue + TimeSpan.FromMilliseconds(periodSpan.TotalMilliseconds * periods);
}
///
/// Updates this model using the new price information in
/// the specified security instance
///
/// The security to calculate volatility for
///
public override void Update(Security security, BaseData data)
{
var timeSinceLastUpdate = data.EndTime - _lastUpdate;
if (timeSinceLastUpdate >= _periodSpan && security.Price > 0)
{
lock (_sync)
{
_needsUpdate = true;
// we purposefully use security.Price for consistency in our reporting
// some streams of data will have trade/quote data, so if we just use
// data.Value we could be mixing and matching data streams
_window.Add((double)security.Price);
}
_lastUpdate = data.EndTime;
}
}
///
/// Returns history requirements for the volatility model expressed in the form of history request
///
/// The security of the request
/// The date/time of the request
/// History request object list, or empty if no requirements
public override IEnumerable GetHistoryRequirements(Security security, DateTime utcTime)
{
if (SubscriptionDataConfigProvider == null)
{
throw new Exception(
"RelativeStandardDeviationVolatilityModel.GetHistoryRequirements(): " +
"SubscriptionDataConfigProvider was not set."
);
}
var configurations = SubscriptionDataConfigProvider
.GetSubscriptionDataConfigs(security.Symbol)
.ToList();
var barCount = _window.Size + 1;
var extendedMarketHours = configurations.IsExtendedMarketHours();
var localStartTime = Time.GetStartTimeForTradeBars(
security.Exchange.Hours,
utcTime.ConvertFromUtc(security.Exchange.TimeZone),
_periodSpan,
barCount,
extendedMarketHours);
var utcStartTime = localStartTime.ConvertToUtc(security.Exchange.TimeZone);
var configuration = configurations.First();
return new[]
{
new HistoryRequest(utcStartTime,
utcTime,
typeof(TradeBar),
configuration.Symbol,
configurations.GetHighestResolution(),
security.Exchange.Hours,
configuration.DataTimeZone,
configurations.GetHighestResolution(),
extendedMarketHours,
configurations.IsCustomData(),
configurations.DataNormalizationMode(),
LeanData.GetCommonTickTypeForCommonDataTypes(typeof(TradeBar), security.Type))
};
}
}
}