chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:02:50 +08:00
commit 0fc60fdcb1
5008 changed files with 910633 additions and 0 deletions
@@ -0,0 +1,103 @@
# 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.
from AlgorithmImports import *
from custom_data import *
class SecurityHistoryTest():
def __init__(self, start_date, security_type, symbol):
self.qb = QuantBook()
self.qb.SetStartDate(start_date)
self.symbol = self.qb.AddSecurity(security_type, symbol).Symbol
self.column = 'close'
def __str__(self):
return "{} on {}".format(self.symbol.ID, self.qb.StartDate)
def test_period_overload(self, period):
history = self.qb.History([self.symbol], period)
return history[self.column].unstack(level=0)
def test_daterange_overload(self, end):
start = end - timedelta(1)
history = self.qb.History([self.symbol], start, end)
return history[self.column].unstack(level=0)
class OptionHistoryTest(SecurityHistoryTest):
def test_daterange_overload(self, end, start = None):
if start is None:
start = end - timedelta(1)
history = self.qb.GetOptionHistory(self.symbol, start, end)
return history.GetAllData()
class FutureHistoryTest(SecurityHistoryTest):
def test_daterange_overload(self, end, start = None, maxFilter = 182):
if start is None:
start = end - timedelta(1)
self.qb.Securities[self.symbol].SetFilter(0, maxFilter) # default is 35 days
history = self.qb.GetFutureHistory(self.symbol, start, end)
return history.GetAllData()
class FutureContractHistoryTest():
def __init__(self, start_date, security_type, symbol):
self.qb = QuantBook()
self.qb.SetStartDate(start_date)
self.symbol = symbol
self.column = 'close'
def test_daterange_overload(self, end):
start = end - timedelta(1)
history = self.qb.GetFutureHistory(self.symbol, start, end)
return history.GetAllData()
class OptionContractHistoryTest(FutureContractHistoryTest):
def test_daterange_overload(self, end):
start = end - timedelta(1)
history = self.qb.GetOptionHistory(self.symbol, start, end)
return history.GetAllData()
class CustomDataHistoryTest(SecurityHistoryTest):
def __init__(self, start_date, security_type, symbol):
self.qb = QuantBook()
self.qb.SetStartDate(start_date)
if security_type == 'Nifty':
type = Nifty
self.column = 'close'
elif security_type == 'CustomPythonData':
type = CustomPythonData
self.column = 'close'
else:
raise
self.symbol = self.qb.AddData(type, symbol, Resolution.Daily).Symbol
class MultipleSecuritiesHistoryTest(SecurityHistoryTest):
def __init__(self, start_date, security_type, symbol):
self.qb = QuantBook()
self.qb.SetStartDate(start_date)
self.qb.AddEquity('SPY', Resolution.Daily)
self.qb.AddForex('EURUSD', Resolution.Daily)
self.qb.AddCrypto('BTCUSD', Resolution.Daily)
def test_period_overload(self, period):
history = self.qb.History(self.qb.Securities.Keys, period)
return history['close'].unstack(level=0)
class FundamentalHistoryTest():
def __init__(self):
self.qb = QuantBook()
def getFundamentals(self, ticker, selector, start, end):
return self.qb.GetFundamental(ticker, selector, start, end)
@@ -0,0 +1,47 @@
# 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.
from AlgorithmImports import *
class IndicatorTest():
def __init__(self, start_date, security_type, symbol):
self.qb = QuantBook()
self.qb.SetStartDate(start_date)
self.symbol = self.qb.AddSecurity(security_type, symbol).Symbol
def __str__(self):
return "{} on {}".format(self.symbol.ID, self.qb.StartDate)
def test_bollinger_bands(self, symbol, start, end, resolution):
ind = BollingerBands(10, 2)
return self.qb.IndicatorHistory(ind, symbol, start, end, resolution)
def test_average_true_range(self, symbol, start, end, resolution):
ind = AverageTrueRange(14)
return self.qb.IndicatorHistory(ind, symbol, start, end, resolution)
def test_on_balance_volume(self, symbol, start, end, resolution):
ind = OnBalanceVolume(symbol)
return self.qb.IndicatorHistory(ind, symbol, start, end, resolution)
def test_bollinger_bands_backwards_compatibility(self, symbol, start, end, resolution):
ind = BollingerBands(10, 2)
return self.qb.Indicator(ind, symbol, start, end, resolution)
def test_average_true_range_backwards_compatibility(self, symbol, start, end, resolution):
ind = AverageTrueRange(14)
return self.qb.Indicator(ind, symbol, start, end, resolution)
def test_on_balance_volume_backwards_compatibility(self, symbol, start, end, resolution):
ind = OnBalanceVolume(symbol)
return self.qb.Indicator(ind, symbol, start, end, resolution)
@@ -0,0 +1,70 @@
# 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.
from AlgorithmImports import *
import decimal
class CustomPythonData(PythonData):
def get_source(self, config, date, is_live):
source = Globals.DataFolder + "/equity/usa/daily/ibm.zip"
return SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv)
def reader(self, config, line, date, is_live):
if line == None:
return None
customPythonData = CustomPythonData()
customPythonData.Symbol = config.Symbol
scaleFactor = 1 / 10000
csv = line.split(",")
customPythonData.Time = datetime.strptime(csv[0], '%Y%m%d %H:%M')
customPythonData["Open"] = float(csv[1]) * scaleFactor
customPythonData["High"] = float(csv[2]) * scaleFactor
customPythonData["Low"] = float(csv[3]) * scaleFactor
customPythonData["Close"] = float(csv[4]) * scaleFactor
customPythonData["Volume"] = float(csv[5])
return customPythonData
class Nifty(PythonData):
'''NIFTY Custom Data Class'''
def get_source(self, config, date, is_live_mode):
return SubscriptionDataSource("https://www.dropbox.com/s/rsmg44jr6wexn2h/CNXNIFTY.csv?dl=1", SubscriptionTransportMedium.REMOTE_FILE)
def reader(self, config, line, date, is_live_mode):
if not (line.strip() and line[0].isdigit()): return None
# New Nifty object
index = Nifty()
index.symbol = config.symbol
try:
# Example File Format:
# Date, Open High Low Close Volume Turnover
# 2011-09-13 7792.9 7799.9 7722.65 7748.7 116534670 6107.78
data = line.split(',')
index.time = datetime.strptime(data[0], "%Y-%m-%d")
index.value = decimal.Decimal(data[4])
index["Open"] = float(data[1])
index["High"] = float(data[2])
index["Low"] = float(data[3])
index["Close"] = float(data[4])
except ValueError:
# Do nothing
return None
return index