/*
* 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 QuantConnect.Util;
using System;
using ProtoBuf;
namespace QuantConnect.Data.Market
{
///
/// Defines a data type that represents open interest for given security
///
[ProtoContract(SkipConstructor = true)]
public class OpenInterest : Tick
{
///
/// Initializes a new instance of the OpenInterest class
///
public OpenInterest()
{
DataType = MarketDataType.Tick;
TickType = TickType.OpenInterest;
Value = 0;
Time = new DateTime();
Symbol = Symbol.Empty;
}
///
/// Cloner constructor for fill forward engine implementation. Clone the original OI into this new one:
///
/// Original OI we're cloning
public OpenInterest(OpenInterest original)
{
DataType = MarketDataType.Tick;
TickType = TickType.OpenInterest;
Value = original.Value;
Time = original.Time;
Symbol = original.Symbol;
}
///
/// Initializes a new instance of the OpenInterest class with data
///
/// Full date and time
/// Underlying equity security symbol
/// Open Interest value
public OpenInterest(DateTime time, Symbol symbol, decimal openInterest)
{
DataType = MarketDataType.Tick;
TickType = TickType.OpenInterest;
Time = time;
Symbol = symbol;
Value = openInterest;
}
///
/// Constructor for QuantConnect open interest data
///
/// Subscription configuration
/// Symbol for underlying asset
/// CSV line of data from QC OI csv
/// The base date of the OI
public OpenInterest(SubscriptionDataConfig config, Symbol symbol, string line, DateTime baseDate)
{
var csv = line.Split(',');
DataType = MarketDataType.Tick;
TickType = TickType.OpenInterest;
Symbol = symbol;
Time = (config.Resolution == Resolution.Daily || config.Resolution == Resolution.Hour) ?
// hourly and daily have different time format, and can use slow, robust c# parser.
DateTime.ParseExact(csv[0], DateFormat.TwelveCharacter,
System.Globalization.CultureInfo.InvariantCulture)
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone)
:
// Using custom "ToDecimal" conversion for speed on high resolution data.
baseDate.Date.AddMilliseconds(csv[0].ToInt32()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
Value = csv[1].ToDecimal();
}
///
/// Parse an open interest data line from quantconnect zip source files.
///
/// CSV source line of the compressed source
/// Base date for the open interest (date is stored as int milliseconds since midnight)
/// Subscription configuration object
public OpenInterest(SubscriptionDataConfig config, string line, DateTime date):
this(config, config.Symbol, line, date)
{
}
///
/// Tick implementation of reader method: read a line of data from the source and convert it to an open interest object.
///
/// Subscription configuration object for algorithm
/// Line from the datafeed source
/// Date of this reader request
/// true if we're in live mode, false for backtesting mode
/// New initialized open interest object
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
if (isLiveMode)
{
// currently OIs don't come through the reader function
return new OpenInterest();
}
return new OpenInterest(config, line, date);
}
///
/// Get source for OI data feed - not used with QuantConnect data sources implementation.
///
/// Configuration object
/// Date of this source request if source spread across multiple files
/// true if we're in live mode, false for backtesting mode
/// String source location of the file to be opened with a stream
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
if (isLiveMode)
{
// this data type is streamed in live mode
return new SubscriptionDataSource(string.Empty, SubscriptionTransportMedium.Streaming);
}
var source = LeanData.GenerateZipFilePath(Globals.DataFolder, config.Symbol, date, config.Resolution, config.TickType);
if (config.SecurityType == SecurityType.Future || config.SecurityType.IsOption())
{
source += "#" + LeanData.GenerateZipEntryName(config.Symbol, date, config.Resolution, config.TickType);
}
return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
}
///
/// Clone implementation for open interest class:
///
/// New tick object clone of the current class values.
public override BaseData Clone()
{
return new OpenInterest(this);
}
}
}