chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace QuantConnect.Data.Shortable
|
||||
{
|
||||
/// <summary>
|
||||
/// Sources the InteractiveBrokers short availability data from the local disk for the given brokerage
|
||||
/// </summary>
|
||||
public class InteractiveBrokersShortableProvider : LocalDiskShortableProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance
|
||||
/// </summary>
|
||||
public InteractiveBrokersShortableProvider()
|
||||
: base("interactivebrokers")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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.IO;
|
||||
using QuantConnect.Util;
|
||||
using System.Threading.Tasks;
|
||||
using QuantConnect.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
namespace QuantConnect.Data.Shortable
|
||||
{
|
||||
/// <summary>
|
||||
/// Sources short availability data from the local disk for the given brokerage
|
||||
/// </summary>
|
||||
public class LocalDiskShortableProvider : IShortableProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The data provider instance to use
|
||||
/// </summary>
|
||||
protected static IDataProvider DataProvider = Composer.Instance.GetPart<IDataProvider>();
|
||||
|
||||
private string _ticker;
|
||||
private bool _scheduledCleanup;
|
||||
private Dictionary<DateTime, ShortableData> _shortableDataPerDate;
|
||||
|
||||
/// <summary>
|
||||
/// The short availability provider
|
||||
/// </summary>
|
||||
protected string Brokerage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the class. Establishes the directory to read from.
|
||||
/// </summary>
|
||||
/// <param name="brokerage">Brokerage to read the short availability data</param>
|
||||
public LocalDiskShortableProvider(string brokerage)
|
||||
{
|
||||
Brokerage = brokerage.ToLowerInvariant();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets interest rate charged on borrowed shares for a given asset.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup fee rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>Fee rate. Zero if the data for the brokerage/date does not exist.</returns>
|
||||
public decimal FeeRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
if (symbol != null && GetCacheData(symbol).TryGetValue(localTime.Date, out var result))
|
||||
{
|
||||
return result.FeeRate;
|
||||
}
|
||||
// Any missing entry will be considered to be zero.
|
||||
return 0m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Fed funds or other currency-relevant benchmark rate minus the interest rate charged on borrowed shares for a given asset.
|
||||
/// E.g.: Interest rate - borrow fee rate = borrow rebate rate: 5.32% - 0.25% = 5.07%.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup rebate rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>Rebate fee. Zero if the data for the brokerage/date does not exist.</returns>
|
||||
public decimal RebateRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
if (symbol != null && GetCacheData(symbol).TryGetValue(localTime.Date, out var result))
|
||||
{
|
||||
return result.RebateFee;
|
||||
}
|
||||
// Any missing entry will be considered to be zero.
|
||||
return 0m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the quantity shortable for the Symbol at the given date.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup shortable quantity</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>Quantity shortable. Null if the data for the brokerage/date does not exist.</returns>
|
||||
public long? ShortableQuantity(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
if (symbol != null && GetCacheData(symbol).TryGetValue(localTime.Date, out var result))
|
||||
{
|
||||
return result.ShortableQuantity;
|
||||
}
|
||||
// Any missing entry will be considered to be Shortable.
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// We cache data per ticker
|
||||
/// </summary>
|
||||
/// <param name="symbol">The requested symbol</param>
|
||||
private Dictionary<DateTime, ShortableData> GetCacheData(Symbol symbol)
|
||||
{
|
||||
var result = _shortableDataPerDate;
|
||||
if (_ticker == symbol.Value)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!_scheduledCleanup)
|
||||
{
|
||||
// we schedule it once
|
||||
_scheduledCleanup = true;
|
||||
ClearCache();
|
||||
}
|
||||
|
||||
// create a new collection
|
||||
_ticker = symbol.Value;
|
||||
result = _shortableDataPerDate = new();
|
||||
|
||||
// Implicitly trusts that Symbol.Value has been mapped and updated to the latest ticker
|
||||
var shortableSymbolFile = Path.Combine(Globals.DataFolder, symbol.SecurityType.SecurityTypeToLower(), symbol.ID.Market,
|
||||
"shortable", Brokerage, "symbols", $"{_ticker.ToLowerInvariant()}.csv");
|
||||
|
||||
foreach (var line in DataProvider.ReadLines(shortableSymbolFile))
|
||||
{
|
||||
if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
|
||||
{
|
||||
// ignore empty or comment lines
|
||||
continue;
|
||||
}
|
||||
// Data example. The rates, if available, are expressed in percentage.
|
||||
// 20201221,2000,5.0700,0.2500
|
||||
var csv = line.Split(',');
|
||||
var date = Parse.DateTimeExact(csv[0], "yyyyMMdd");
|
||||
var lenght = csv.Length;
|
||||
var shortableQuantity = csv[1].IfNotNullOrEmpty(s => long.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));
|
||||
var rebateRate = csv.Length > 2 ? csv[2].IfNotNullOrEmpty(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture)) : 0;
|
||||
var feeRate = csv.Length > 3 ? csv[3].IfNotNullOrEmpty(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture)) : 0;
|
||||
result[date] = new ShortableData(shortableQuantity, rebateRate / 100, feeRate / 100);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For live deployments we don't want to have stale short quantity so we refresh them every day
|
||||
/// </summary>
|
||||
private void ClearCache()
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var tomorrowMidnight = now.Date.AddDays(1);
|
||||
var delayToClean = tomorrowMidnight - now;
|
||||
|
||||
Task.Delay(delayToClean).ContinueWith((_) =>
|
||||
{
|
||||
// create new instances so we don't need to worry about locks
|
||||
_ticker = null;
|
||||
_shortableDataPerDate = new();
|
||||
|
||||
ClearCache();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the shortable data
|
||||
/// </summary>
|
||||
protected record ShortableData(long? ShortableQuantity, decimal RebateFee, decimal FeeRate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 QuantConnect.Interfaces;
|
||||
|
||||
namespace QuantConnect.Data.Shortable
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the default shortable provider in the case that no local data exists.
|
||||
/// This will allow for all assets to be infinitely shortable, with no restrictions.
|
||||
/// </summary>
|
||||
public class NullShortableProvider : IShortableProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The null shortable provider instance
|
||||
/// </summary>
|
||||
public static NullShortableProvider Instance { get; } = new ();
|
||||
|
||||
/// <summary>
|
||||
/// Gets interest rate charged on borrowed shares for a given asset.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup fee rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>zero indicating that it is does have borrowing costs</returns>
|
||||
public decimal FeeRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return 0m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Fed funds or other currency-relevant benchmark rate minus the interest rate charged on borrowed shares for a given asset.
|
||||
/// E.g.: Interest rate - borrow fee rate = borrow rebate rate: 5.32% - 0.25% = 5.07%.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup rebate rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>zero indicating that it is does have borrowing costs</returns>
|
||||
public decimal RebateRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return 0m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the quantity shortable for the Symbol at the given time.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to check</param>
|
||||
/// <param name="localTime">Local time of the algorithm</param>
|
||||
/// <returns>null, indicating that it is infinitely shortable</returns>
|
||||
public long? ShortableQuantity(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 Python.Runtime;
|
||||
using QuantConnect.Interfaces;
|
||||
using QuantConnect.Python;
|
||||
using System;
|
||||
|
||||
namespace QuantConnect.Data.Shortable
|
||||
{
|
||||
/// <summary>
|
||||
/// Python wrapper for custom shortable providers
|
||||
/// </summary>
|
||||
public class ShortableProviderPythonWrapper : BasePythonWrapper<IShortableProvider>, IShortableProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance
|
||||
/// </summary>
|
||||
/// <param name="shortableProvider">The python custom shortable provider</param>
|
||||
public ShortableProviderPythonWrapper(PyObject shortableProvider)
|
||||
: base(shortableProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fee rate for the Symbol at the given date.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup fee rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>zero indicating that it is does have borrowing costs</returns>
|
||||
public decimal FeeRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return InvokeMethod<decimal>(nameof(FeeRate), symbol, localTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Fed funds or other currency-relevant benchmark rate minus the interest rate charged on borrowed shares for a given asset.
|
||||
/// E.g.: Interest rate - borrow fee rate = borrow rebate rate: 5.32% - 0.25% = 5.07%.
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to lookup rebate rate</param>
|
||||
/// <param name="localTime">Time of the algorithm</param>
|
||||
/// <returns>zero indicating that it is does have borrowing costs</returns>
|
||||
public decimal RebateRate(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return InvokeMethod<decimal>(nameof(RebateRate), symbol, localTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the quantity shortable for a <see cref="Symbol"/>, from python custom shortable provider
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol to check shortable quantity</param>
|
||||
/// <param name="localTime">Local time of the algorithm</param>
|
||||
/// <returns>The quantity shortable for the given Symbol as a positive number. Null if the Symbol is shortable without restrictions.</returns>
|
||||
public long? ShortableQuantity(Symbol symbol, DateTime localTime)
|
||||
{
|
||||
return InvokeMethod<long?>(nameof(ShortableQuantity), symbol, localTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user