Files
quantconnect--lean/Common/Data/Market/Tick.cs
Gerardo Salazar eb1181f5f7 Adds Futures Options Asset Class w/ IB Support (#4928)
* Adds preliminary universe selection for Future Options

* Fixes scaling issues with Future Options

* Fixes scaling multiplying by 10000x instead of using _scaleFactor

* Fixes scaling for Tick

* Revert changes to Tick since it divides the scaling factor

* Changes stale method name to new method name after rebase

* Fixes selection bugs, adds new methods, and adds unit tests

  * Fixes bug where Equity Symbol was created for an underlying
    non-equity Symbol, resulting in equity data trying to be loaded

  * Adds unit tests covering changes to Tick, QuoteBar, TradeBar and
    LeanData

  * Adds regression test for AddUniverseOption filter contract selection
    for Future Options

* Addresses review - modifies the AddFutureOption signature

  * Adds new AddUniverseOptions method overload
  * Removes and adds a new unit test
  * Misc. modifications to account for new changes

* Fixes bug where futures were loaded using default SID Date

  * Refactors and removes unnecessary work
  * Fixes regression algorithm, which previously made no trades

* Adds future option data

  * Adds the corresponding underlying data, in this case, futures data
    to enable usage of future options data

* Replaces data with new data (ES18Z20)

  * Improves Future chain filtering and updates regression stats

* Add AddFutureOptionContract API

* Expands regression and unit tests to test in finer detail

* Adds Python regression algorithms for AddFutureOption[Contract] methods

* Adds new unit test for BacktestingOptionChainProvider

  * Fixes bug with BacktesingOptionChainProvider where we
    attempted to load the Trades option chain first, resulting
    in breakage of backwards compatibility and limitation of the
    option chain.

  * Adds new regression algorithms (Py) to Algorithm.Python project

* Adds FutureOptionMarginBuyingPowerModel

  * Modifies code paths used to select margin model
  * Adds related unit tests for margin model

* Fixes issue with unit test and MHDB/SPDB lookup for Future Options

* Preliminary regression algorithm testing ITM call/put option buying

  * Fixes bug where fee model used did not find non-US market
    options fee model. We now use the futures fee model for future
    options because IB charges the same commissions per contract
    between futures and futures options

* Adds proper regression algorithm for ITM future options expiration

* Pushing broken algorithm for review

  * Currently, algorithm does not fill forward, causing
    a single future option to not get exercised when it is delisted.

* Adds FutureOptionPutITMExpiryRegressionAlgorithm

  * Improves existing regression algorithm for call side
  * Fixes bug in existing regression algorithm
  * Adds AAPL daily data to advance enumerator for ^^^ fix

* Adds additional future option regression algorithms

  * Adds Buy OTM expiration regression algorithms
  * Adds Sell ITM/OTM expiration regression algorithms
  * Adds missing Python regression algorithms

* Adds remaining Python regression algorithms and fixes issues

  * Fixes naming issues and statistics
  * Adds short option OTM regression algorithms (Py)

* Add license header and class comments to python algorithms

  * Cleans up comments and docstrings
  * Create Buy/Sell call intraday regression algo

* Redirects future options symbol properties to futures symbol properties

  * Asserts exercise/assignment price and updates stats in regression algos
  * Adds new unit test covering changes to SecurityService

* Adds comments and fixes failing test

* Partially fixes future option mis-calculated profit/loss

* Adjusts portfolio model to calculate FOP as a no upfront pay asset class

  * Updates regression algorithm statistics

* Begin IB FOP support

* Initial support for FOP IB data streaming, live í¾‰

  * Adds additional functionality to LiveOptionChainProvider
    - Allows querying CME API to retrieve option chains for CME products
    - Ultimately, it's also the groundwork for the CME
      LiveFutureChainProvider

  * Edits IDataQueueUniverseProvider interface to provide greater
    control to implementors of it

  * Misc. bug fixes required to get FOP data streaming through IB

* Adds comments, adds missing rategate call, and cleans up code

* Force exchange for FOP and Futures when no exchange is provided

* Fixes bug with Portfolio modeling across all asset classes

* Adds LiveOptionChainProvider tests for Future Options

* IB brokerage option symbol bug fixes and improvements

* Fixes contract multiplier lookup bug

  * Fixes issue where we attempted to subscribe to IB data feed with canonical security
  * Adds ES MHDB entry

* Reverts portfolio modeling changes for Futures Options

  * Since IB eats into our account's cash balance when
    a new FOP contract is purchased, we must model by applying funds
    to our cash whenever a new purchase/sell occurs.
    If we choose to model FOPs exactly as we do with futures, we
    will end up with an invalid TotalPortfolioValue on algorithm
    restart. By all means and purposes, FOPs are modeled exactly
    the same as equity options with respect to the portfolio.

  * Adds comments clarifying portfolio modeling and clarifies
    existing portfolio modeling comments with additional context.

* Fixes IB symbol lookup for future options

  * Fixes LiveOptionChainProvider looping 5 times per option chain
    request, even on success

  * Sets OptionChainedUniverseSelectionModel to produce a canonical
    future/future option/option Symbol to avoid creating two Symbols

  * Adds GLOBEX future option symbol mapping from future -> fop

* Fixes LiveOptionChainProvider loading wrong contract option chains

  * Fixes loading of futures options ZIP files when backtesting
  * Adds a string -> decimal JSON converter
  * Additional fixes/refactoring to the LiveOptionChainProvider

* Adds tests for changes to Symbol and LeanData

  * Reverts changes to IB-symbol-map

* Fixes Value for mapped future options tickers

  * Fixes Symbol test

* Changes path of future options to future's expiry date

  * Extra changes made to remove scaling from writing CSV
  * Added method to map from FOP Globex -> FUT Globex

* Fixes MOO and MOC orders for future options

  * Note: this order type might not be supported by IB or CME.

* Bug fixes and updates unit tests

* Update regression tests and data format

* Rebase changes

* 1. Multiple bug fixes for LiveOptionChainProvider, reverts IQFeed changes
2. Address review (partial): Code reuse and cleanup

1.
  * Modifies check in
    `AddFutureOptionShort(Call|Put)ITMExpiryRegressionAlgorithm`
    to ensure no buys have negative quantity

  * Code reuse changes in IB brokerage

  * Bug fix in IB brokerage where we assigned the FOP expiry
    as the futures expiry (requires verification)

  * Doc changes and adds missing summaries/license banners
  * Disposes of HTTP client resources in LiveOptionChainProvider
  * Renames classes and adds FutureOption folder in Common/Securities

2.
  * We revert back to the quotes API for the option chain,
    since the settlement API sometimes had missing strikes.

  * Fixes future option expiry being set as future's expiry
    in LiveOptionChainProvider

  * Fixes bug where wrong option chain was selected because of bad
    expiry lookup in the futures expiries returned from CME

  * Fixes multiple looping bug in LiveOptionChainProvider
  * Adds strike price scaling for LiveOptionChainProvider

  * Reverts IQFeed changes and simplifies interface upgrade changes

  Some additional challenges we'll have to solve as part of FOPs:

    - The `OptionSymbol.IsStandard` method makes the assumption that
      weeklies contracts follow the pattern equities follows, which
      does not apply to Futures Options

    - The Subscription created in:
        `OptionChainUniverseSubscriptionEnumeratorFactory`

      ...adds a Trade config. For illiquid contracts, this
      will delay universe selection for the option symbol
      until we get a trade. However, if we add a quote config,
      the data would instead be loaded based on the first quote
      we received from the brokerage.

      But since we're currently using a trade config, illiquid
      contracts won't start streaming data until it receives a trade.

NOTE: this commit is a WIP to addressing the reviews received in the PR,
but has been committed early for efficiency in the review process

* Fixes regression algorithms and misc. bugs

  * Fixes map file lookup for non-equity options
  * Adds extra assertion at end of algorithm to ensure no holdings are
    left when the algorithm ends.

  * Adds FutureOptionSymbol, allowing all contracts through as standard
  * Changes SPDB to allow defaulting to underlying future symbol
    properties if no entry is found for the given FOP

  * Fixes calls to SPDB in SecurityService, IBBrokerage
  * Reverts AAPL daily ZIP file to fix majority of regression algorithms
  * Adds FOPs symbol properties
  * Fixes existing symbol properties for a few futures
  * Adds tests for changes to Symbol Properties Database

* Removes string SPDB lookup method

  * Updates tests and misc callees of previous method

* Updates all regression tests to use data of already expired contracts

  * Adds Futures Options Expiry Functions tests
  * Adds required futures data for 2020-01-05

* Address review (partial): Expands test coverage and fixes tests

* Set option chain tests parallelism to fixture only

* Fixes broken test for contract month delta for FuturesOptionsExpiryFunctions

* Changes delisting date logic for Futures Options

* Address review: removes duplicate code, misc code fixes

  * Bug fix in MarketHoursDatabase.GetDatabaseSymbolKey() where
    we would use the underlying's Symbol for lookup in the MHDB

  * Adds missing license banner
  * Removes Futures Options entries from MHDB
  * Adds new tests

* Adds SecurityType.FutureOption

  * Converts any underlying comparisons and uses SecurityType directly
    instead for FOP specific behavior

  * Extra code modifications to acommodate new SecurityType

* Addresses review: fixes order fee bug on exercise

  * Additional bug fixes and adding of SecurityType.FutureOption
  * Updates regression algorithms OrderListHash

* Fixes various bugs in IB live implementation

  * Fixes bug setting the right contract expiration date for FOP
    generated by LiveOptionChainProvider

  * Adds new function to FuturesOptionsExpiryFunctions

  * Clarifies parameter names better in some functions/methods

  * Fixes bugs in IB brokerage for FOPs

* Address review - code cleanup and refactor

  * Remove MappingEventProvider, SplitEventProvider, and
    DividendEventProvider for Futures Options in
    CorporateEventEnumeratorFactory

* Address review: Use MHDB key resolver in SPDB

* Makes regression tests pass and adds comment for expiry issue

* Fixes MHDB lookup on string symbol method

* Adds Futures Options greeks regression algorithm (C# only)

* Adds explanitory comment on MHDB FOP lookup

* Remove python from FutureOptionCallITMGreeksExpiryRegressionAlgorithm
2020-12-02 21:49:59 -03:00

733 lines
29 KiB
C#

/*
* 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 System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using ProtoBuf;
using QuantConnect.Logging;
using QuantConnect.Util;
namespace QuantConnect.Data.Market
{
/// <summary>
/// Tick class is the base representation for tick data. It is grouped into a Ticks object
/// which implements IDictionary and passed into an OnData event handler.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class Tick : BaseData
{
private uint? _parsedSaleCondition;
/// <summary>
/// Type of the Tick: Trade or Quote.
/// </summary>
[ProtoMember(10)]
public TickType TickType = TickType.Trade;
/// <summary>
/// Quantity exchanged in a trade.
/// </summary>
[ProtoMember(11)]
public decimal Quantity = 0;
/// <summary>
/// Exchange we are executing on. String short code expanded in the MarketCodes.US global dictionary
/// </summary>
[ProtoMember(12)]
public string Exchange = "";
/// <summary>
/// Sale condition for the tick.
/// </summary>
public string SaleCondition = "";
/// <summary>
/// For performance parsed sale condition for the tick.
/// </summary>
[JsonIgnore]
public uint ParsedSaleCondition
{
get
{
if (!_parsedSaleCondition.HasValue)
{
_parsedSaleCondition = uint.Parse(SaleCondition, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return _parsedSaleCondition.Value;
}
set
{
_parsedSaleCondition = value;
}
}
/// <summary>
/// Bool whether this is a suspicious tick
/// </summary>
[ProtoMember(14)]
public bool Suspicious = false;
/// <summary>
/// Bid Price for Tick
/// </summary>
[ProtoMember(15)]
public decimal BidPrice = 0;
/// <summary>
/// Asking price for the Tick quote.
/// </summary>
[ProtoMember(16)]
public decimal AskPrice = 0;
/// <summary>
/// Alias for "Value" - the last sale for this asset.
/// </summary>
public decimal LastPrice
{
get
{
return Value;
}
}
/// <summary>
/// Size of bid quote.
/// </summary>
[ProtoMember(17)]
public decimal BidSize = 0;
/// <summary>
/// Size of ask quote.
/// </summary>
[ProtoMember(18)]
public decimal AskSize = 0;
//In Base Class: Alias of Closing:
//public decimal Price;
//Symbol of Asset.
//In Base Class: public Symbol Symbol;
//In Base Class: DateTime Of this TradeBar
//public DateTime Time;
/// <summary>
/// Initialize tick class with a default constructor.
/// </summary>
public Tick()
{
Value = 0;
Time = new DateTime();
DataType = MarketDataType.Tick;
Symbol = Symbol.Empty;
TickType = TickType.Trade;
Quantity = 0;
Exchange = "";
SaleCondition = "";
Suspicious = false;
BidSize = 0;
AskSize = 0;
}
/// <summary>
/// Cloner constructor for fill forward engine implementation. Clone the original tick into this new tick:
/// </summary>
/// <param name="original">Original tick we're cloning</param>
public Tick(Tick original)
{
Symbol = original.Symbol;
Time = new DateTime(original.Time.Ticks);
Value = original.Value;
BidPrice = original.BidPrice;
AskPrice = original.AskPrice;
Exchange = original.Exchange;
SaleCondition = original.SaleCondition;
Quantity = original.Quantity;
Suspicious = original.Suspicious;
DataType = MarketDataType.Tick;
TickType = original.TickType;
BidSize = original.BidSize;
AskSize = original.AskSize;
}
/// <summary>
/// Constructor for a FOREX tick where there is no last sale price. The volume in FX is so high its rare to find FX trade data.
/// To fake this the tick contains bid-ask prices and the last price is the midpoint.
/// </summary>
/// <param name="time">Full date and time</param>
/// <param name="symbol">Underlying currency pair we're trading</param>
/// <param name="bid">FX tick bid value</param>
/// <param name="ask">FX tick ask value</param>
public Tick(DateTime time, Symbol symbol, decimal bid, decimal ask)
{
DataType = MarketDataType.Tick;
Time = time;
Symbol = symbol;
Value = (bid + ask) / 2;
TickType = TickType.Quote;
BidPrice = bid;
AskPrice = ask;
}
/// <summary>
/// Initializer for a last-trade equity tick with bid or ask prices.
/// </summary>
/// <param name="time">Full date and time</param>
/// <param name="symbol">Underlying equity security symbol</param>
/// <param name="bid">Bid value</param>
/// <param name="ask">Ask value</param>
/// <param name="last">Last trade price</param>
public Tick(DateTime time, Symbol symbol, decimal last, decimal bid, decimal ask)
{
DataType = MarketDataType.Tick;
Time = time;
Symbol = symbol;
Value = last;
TickType = TickType.Quote;
BidPrice = bid;
AskPrice = ask;
}
/// <summary>
/// Trade tick type constructor
/// </summary>
/// <param name="time">Full date and time</param>
/// <param name="symbol">Underlying equity security symbol</param>
/// <param name="saleCondition">The ticks sale condition</param>
/// <param name="exchange">The ticks exchange</param>
/// <param name="quantity">The quantity traded</param>
/// <param name="price">The price of the trade</param>
public Tick(DateTime time, Symbol symbol, string saleCondition, string exchange, decimal quantity, decimal price)
{
Value = price;
Time = time;
DataType = MarketDataType.Tick;
Symbol = symbol;
TickType = TickType.Trade;
Quantity = quantity;
Exchange = exchange;
SaleCondition = saleCondition;
Suspicious = false;
}
/// <summary>
/// Quote tick type constructor
/// </summary>
/// <param name="time">Full date and time</param>
/// <param name="symbol">Underlying equity security symbol</param>
/// <param name="saleCondition">The ticks sale condition</param>
/// <param name="exchange">The ticks exchange</param>
/// <param name="bidSize">The bid size</param>
/// <param name="bidPrice">The bid price</param>
/// <param name="askSize">The ask size</param>
/// <param name="askPrice">The ask price</param>
public Tick(DateTime time, Symbol symbol, string saleCondition, string exchange, decimal bidSize, decimal bidPrice, decimal askSize, decimal askPrice)
{
Time = time;
DataType = MarketDataType.Tick;
Symbol = symbol;
TickType = TickType.Quote;
Exchange = exchange;
SaleCondition = saleCondition;
Suspicious = false;
AskPrice = askPrice;
AskSize = askSize;
BidPrice = bidPrice;
BidSize = bidSize;
}
/// <summary>
/// Constructor for QuantConnect FXCM Data source:
/// </summary>
/// <param name="symbol">Symbol for underlying asset</param>
/// <param name="line">CSV line of data from FXCM</param>
public Tick(Symbol symbol, string line)
{
var csv = line.Split(',');
DataType = MarketDataType.Tick;
Symbol = symbol;
Time = DateTime.ParseExact(csv[0], DateFormat.Forex, CultureInfo.InvariantCulture);
Value = (BidPrice + AskPrice) / 2;
TickType = TickType.Quote;
BidPrice = Convert.ToDecimal(csv[1], CultureInfo.InvariantCulture);
AskPrice = Convert.ToDecimal(csv[2], CultureInfo.InvariantCulture);
}
/// <summary>
/// Constructor for QuantConnect tick data
/// </summary>
/// <param name="symbol">Symbol for underlying asset</param>
/// <param name="line">CSV line of data from QC tick csv</param>
/// <param name="baseDate">The base date of the tick</param>
public Tick(Symbol symbol, string line, DateTime baseDate)
{
var csv = line.Split(',');
DataType = MarketDataType.Tick;
Symbol = symbol;
Time = baseDate.Date.AddMilliseconds(csv[0].ToInt32());
Value = csv[1].ToDecimal() / GetScaleFactor(symbol);
TickType = TickType.Trade;
Quantity = csv[2].ToDecimal();
Exchange = csv[3].Trim();
SaleCondition = csv[4];
Suspicious = csv[5].ToInt32() == 1;
}
/// <summary>
/// Parse a tick data line from quantconnect zip source files.
/// </summary>
/// <param name="reader">The source stream reader</param>
/// <param name="date">Base date for the tick (ticks date is stored as int milliseconds since midnight)</param>
/// <param name="config">Subscription configuration object</param>
public Tick(SubscriptionDataConfig config, StreamReader reader, DateTime date)
{
try
{
DataType = MarketDataType.Tick;
Symbol = config.Symbol;
// Which security type is this data feed:
var scaleFactor = GetScaleFactor(config.Symbol);
switch (config.SecurityType)
{
case SecurityType.Equity:
{
TickType = config.TickType;
Time = date.Date.AddMilliseconds((double)reader.GetDecimal()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
bool pastLineEnd;
if (TickType == TickType.Trade)
{
Value = reader.GetDecimal() / scaleFactor;
Quantity = reader.GetDecimal(out pastLineEnd);
if (!pastLineEnd)
{
Exchange = reader.GetString();
SaleCondition = reader.GetString();
Suspicious = reader.GetInt32() == 1;
}
}
else if (TickType == TickType.Quote)
{
BidPrice = reader.GetDecimal() / scaleFactor;
BidSize = reader.GetDecimal();
AskPrice = reader.GetDecimal() / scaleFactor;
AskSize = reader.GetDecimal(out pastLineEnd);
SetValue();
if (!pastLineEnd)
{
Exchange = reader.GetString();
SaleCondition = reader.GetString();
Suspicious = reader.GetInt32() == 1;
}
}
else
{
throw new InvalidOperationException($"Tick(): Unexpected tick type {TickType}");
}
break;
}
case SecurityType.Forex:
case SecurityType.Cfd:
{
TickType = TickType.Quote;
Time = date.Date.AddMilliseconds((double) reader.GetDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
BidPrice = reader.GetDecimal();
AskPrice = reader.GetDecimal();
SetValue();
break;
}
case SecurityType.Crypto:
{
TickType = config.TickType;
Exchange = config.Market;
if (TickType == TickType.Trade)
{
Time = date.Date.AddMilliseconds((double)reader.GetDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
Value = reader.GetDecimal();
Quantity = reader.GetDecimal();
}
if (TickType == TickType.Quote)
{
Time = date.Date.AddMilliseconds((double)reader.GetDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
BidPrice = reader.GetDecimal();
BidSize = reader.GetDecimal();
AskPrice = reader.GetDecimal();
AskSize = reader.GetDecimal();
SetValue();
}
break;
}
case SecurityType.Future:
case SecurityType.Option:
case SecurityType.FutureOption:
{
TickType = config.TickType;
Time = date.Date.AddMilliseconds((double)reader.GetDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
if (TickType == TickType.Trade)
{
Value = reader.GetDecimal() / scaleFactor;
Quantity = reader.GetDecimal();
Exchange = reader.GetString();
SaleCondition = reader.GetString();
Suspicious = reader.GetInt32() == 1;
}
else if (TickType == TickType.OpenInterest)
{
Value = reader.GetDecimal();
}
else
{
BidPrice = reader.GetDecimal() / scaleFactor;
BidSize = reader.GetDecimal();
AskPrice = reader.GetDecimal() / scaleFactor;
AskSize = reader.GetDecimal();
Exchange = reader.GetString();
Suspicious = reader.GetInt32() == 1;
SetValue();
}
break;
}
}
}
catch (Exception err)
{
Log.Error(err);
}
}
/// <summary>
/// Parse a tick data line from quantconnect zip source files.
/// </summary>
/// <param name="line">CSV source line of the compressed source</param>
/// <param name="date">Base date for the tick (ticks date is stored as int milliseconds since midnight)</param>
/// <param name="config">Subscription configuration object</param>
public Tick(SubscriptionDataConfig config, string line, DateTime date)
{
try
{
DataType = MarketDataType.Tick;
Symbol = config.Symbol;
// Which security type is this data feed:
var scaleFactor = GetScaleFactor(config.Symbol);
switch (config.SecurityType)
{
case SecurityType.Equity:
{
var index = 0;
TickType = config.TickType;
var csv = line.ToCsv(TickType == TickType.Trade ? 6 : 8);
Time = date.Date.AddMilliseconds(csv[index++].ToInt64()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
if (TickType == TickType.Trade)
{
Value = csv[index++].ToDecimal() / scaleFactor;
Quantity = csv[index++].ToDecimal();
if (csv.Count > index)
{
Exchange = csv[index++];
SaleCondition = csv[index++];
Suspicious = (csv[index++] == "1");
}
}
else if (TickType == TickType.Quote)
{
BidPrice = csv[index++].ToDecimal() / scaleFactor;
BidSize = csv[index++].ToDecimal();
AskPrice = csv[index++].ToDecimal() / scaleFactor;
AskSize = csv[index++].ToDecimal();
SetValue();
if (csv.Count > index)
{
Exchange = csv[index++];
SaleCondition = csv[index++];
Suspicious = (csv[index++] == "1");
}
}
else
{
throw new InvalidOperationException($"Tick(): Unexpected tick type {TickType}");
}
break;
}
case SecurityType.Forex:
case SecurityType.Cfd:
{
var csv = line.ToCsv(3);
TickType = TickType.Quote;
var ticks = (long)(csv[0].ToDecimal() * TimeSpan.TicksPerMillisecond);
Time = date.Date.AddTicks(ticks)
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
BidPrice = csv[1].ToDecimal();
AskPrice = csv[2].ToDecimal();
SetValue();
break;
}
case SecurityType.Crypto:
{
TickType = config.TickType;
Exchange = config.Market;
if (TickType == TickType.Trade)
{
var csv = line.ToCsv(3);
Time = date.Date.AddMilliseconds((double)csv[0].ToDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
Value = csv[1].ToDecimal();
Quantity = csv[2].ToDecimal();
}
if (TickType == TickType.Quote)
{
var csv = line.ToCsv(6);
Time = date.Date.AddMilliseconds((double)csv[0].ToDecimal())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
BidPrice = csv[1].ToDecimal();
BidSize = csv[2].ToDecimal();
AskPrice = csv[3].ToDecimal();
AskSize = csv[4].ToDecimal();
SetValue();
}
break;
}
case SecurityType.Future:
case SecurityType.Option:
case SecurityType.FutureOption:
{
var csv = line.ToCsv(7);
TickType = config.TickType;
Time = date.Date.AddMilliseconds(csv[0].ToInt64())
.ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
if (TickType == TickType.Trade)
{
Value = csv[1].ToDecimal()/scaleFactor;
Quantity = csv[2].ToDecimal();
Exchange = csv[3];
SaleCondition = csv[4];
Suspicious = csv[5] == "1";
}
else if (TickType == TickType.OpenInterest)
{
Value = csv[1].ToDecimal();
}
else
{
if (csv[1].Length != 0)
{
BidPrice = csv[1].ToDecimal()/scaleFactor;
BidSize = csv[2].ToDecimal();
}
if (csv[3].Length != 0)
{
AskPrice = csv[3].ToDecimal()/scaleFactor;
AskSize = csv[4].ToDecimal();
}
Exchange = csv[5];
Suspicious = csv[6] == "1";
SetValue();
}
break;
}
}
}
catch (Exception err)
{
Log.Error(err);
}
}
/// <summary>
/// Tick implementation of reader method: read a line of data from the source and convert it to a tick object.
/// </summary>
/// <param name="config">Subscription configuration object for algorithm</param>
/// <param name="line">Line from the datafeed source</param>
/// <param name="date">Date of this reader request</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>New Initialized tick</returns>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
if (isLiveMode)
{
// currently ticks don't come through the reader function
return new Tick();
}
return new Tick(config, line, date);
}
/// <summary>
/// Tick implementation of reader method: read a line of data from the source and convert it to a tick object.
/// </summary>
/// <param name="config">Subscription configuration object for algorithm</param>
/// <param name="reader">The source stream reader</param>
/// <param name="date">Date of this reader request</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>New Initialized tick</returns>
public override BaseData Reader(SubscriptionDataConfig config, StreamReader reader, DateTime date, bool isLiveMode)
{
if (isLiveMode)
{
// currently ticks don't come through the reader function
return new Tick();
}
return new Tick(config, reader, date);
}
/// <summary>
/// Get source for tick data feed - not used with QuantConnect data sources implementation.
/// </summary>
/// <param name="config">Configuration object</param>
/// <param name="date">Date of this source request if source spread across multiple files</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>String source location of the file to be opened with a stream</returns>
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.Option ||
config.SecurityType == SecurityType.Future ||
config.SecurityType == SecurityType.FutureOption)
{
source += "#" + LeanData.GenerateZipEntryName(config.Symbol, date, config.Resolution, config.TickType);
}
return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
}
/// <summary>
/// Update the tick price information - not used.
/// </summary>
/// <param name="lastTrade">This trade price</param>
/// <param name="bidPrice">Current bid price</param>
/// <param name="askPrice">Current asking price</param>
/// <param name="volume">Volume of this trade</param>
/// <param name="bidSize">The size of the current bid, if available</param>
/// <param name="askSize">The size of the current ask, if available</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Update(decimal lastTrade, decimal bidPrice, decimal askPrice, decimal volume, decimal bidSize, decimal askSize)
{
Value = lastTrade;
BidPrice = bidPrice;
AskPrice = askPrice;
BidSize = bidSize;
AskSize = askSize;
Quantity = Convert.ToDecimal(volume);
}
/// <summary>
/// Check if tick contains valid data (either a trade, or a bid or ask)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsValid()
{
return (TickType == TickType.Trade && LastPrice > 0.0m && Quantity > 0) ||
(TickType == TickType.Quote && AskPrice > 0.0m && AskSize > 0) ||
(TickType == TickType.Quote && BidPrice > 0.0m && BidSize > 0) ||
(TickType == TickType.OpenInterest && Value > 0);
}
/// <summary>
/// Clone implementation for tick class:
/// </summary>
/// <returns>New tick object clone of the current class values.</returns>
public override BaseData Clone()
{
return new Tick(this);
}
/// <summary>
/// Formats a string with the symbol and value.
/// </summary>
/// <returns>string - a string formatted as SPY: 167.753</returns>
public override string ToString()
{
switch (TickType)
{
case TickType.Trade:
return $"{Symbol}: Price: {Price} Quantity: {Quantity}";
case TickType.Quote:
return $"{Symbol}: Bid: {BidSize}@{BidPrice} Ask: {AskSize}@{AskPrice}";
case TickType.OpenInterest:
return $"{Symbol}: OpenInterest: {Value}";
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Sets the tick Value based on ask and bid price
/// </summary>
public void SetValue()
{
Value = BidPrice + AskPrice;
if (BidPrice * AskPrice != 0)
{
Value /= 2m;
}
}
/// <summary>
/// Gets the scaling factor according to the <see cref="SecurityType"/> of the <see cref="Symbol"/> provided.
/// Non-equity data will not be scaled, including options with an underlying non-equity asset class.
/// </summary>
/// <param name="symbol">Symbol to get scaling factor for</param>
/// <returns>Scaling factor</returns>
private static decimal GetScaleFactor(Symbol symbol)
{
return symbol.SecurityType == SecurityType.Equity || symbol.SecurityType == SecurityType.Option ? 10000m : 1;
}
}
}