chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
# 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 QuantConnect.Tests import *
|
||||
from QuantConnect.Tests.Python import *
|
||||
from PandasMapper import PandasColumn
|
||||
|
||||
# TODO: Rename to PandasResearchTests and keep this class for QB related tests; rename py module to PandasTests
|
||||
class PandasIndexingTests():
|
||||
def __init__(self):
|
||||
self.qb = QuantBook()
|
||||
self.qb.SetStartDate(2020, 1, 1)
|
||||
self.qb.SetEndDate(2020, 1, 4)
|
||||
self.symbol = self.qb.AddEquity("SPY", Resolution.Daily).Symbol
|
||||
|
||||
def test_indexing_dataframe_with_list(self):
|
||||
symbols = [self.symbol]
|
||||
self.history = self.qb.History(symbols, 30)
|
||||
self.history = self.history['close'].unstack(level=0).dropna()
|
||||
test = self.history[[self.symbol]]
|
||||
return True
|
||||
|
||||
# Test class that sets up two dataframes to test on
|
||||
class PandasDataFrameTests():
|
||||
def __init__(self):
|
||||
self.spy = Symbols.SPY
|
||||
self.aapl = Symbols.AAPL
|
||||
|
||||
# Set our symbol cache
|
||||
SymbolCache.Set("SPY", self.spy)
|
||||
SymbolCache.Set("AAPL", self.aapl)
|
||||
|
||||
pdConverter = PandasConverter()
|
||||
|
||||
# Create our dataframes
|
||||
self.spydf = pdConverter.GetDataFrame(PythonTestingUtils.GetSlices(self.spy))
|
||||
|
||||
def test_contains_user_mapped_ticker(self):
|
||||
# Create a new DF that has a plain ticker, test that our mapper doesn't break
|
||||
# searching for it.
|
||||
df = pd.DataFrame({'spy': [2, 5, 8, 10]})
|
||||
return 'spy' in df
|
||||
|
||||
def test_expected_exception(self):
|
||||
# Try indexing a ticker that doesn't exist in this frame, but is still in our cache
|
||||
try:
|
||||
self.spydf['aapl']
|
||||
except KeyError as e:
|
||||
return str(e)
|
||||
|
||||
def test_contains_user_defined_columns_with_spaces(self, column_name):
|
||||
# Adds a column, then try accessing it.
|
||||
# If the colums has white spaces, it should not fail
|
||||
df = self.spydf.copy()
|
||||
df[column_name] = 1
|
||||
try:
|
||||
x = df[column_name]
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def test_column_equals_only_matching_string(self):
|
||||
# A column label should only equal a matching string, never None/ints/floats
|
||||
column = PandasColumn("shares")
|
||||
return (not (column == None)) and (not (column == 0)) and (not (column == 123)) and (column == "shares")
|
||||
@@ -0,0 +1,91 @@
|
||||
# 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.
|
||||
|
||||
'''
|
||||
To test this script directly you will need to import QuantConnect Dlls using clrloader from the appropriate
|
||||
location, the code below shows how to do this. Otherwise you can run it directly from C# in Lean without it.
|
||||
|
||||
To run as a solo script, add the following code to your script
|
||||
|
||||
Requires:
|
||||
clr-loader==0.1.6
|
||||
pandas
|
||||
|
||||
*********** CODE ***********
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Get to DLL location where we are testing, change as needed
|
||||
fileDirectory = os.path.dirname(os.path.abspath(__file__))
|
||||
dlldir = "../../bin/Debug"
|
||||
dlldir = os.path.join(fileDirectory, dlldir)
|
||||
|
||||
# Move us to dll directory and add it to path
|
||||
os.chdir(dlldir)
|
||||
sys.path.append(dlldir)
|
||||
|
||||
# Tell PythonNet to use .dotnet 6
|
||||
from pythonnet import set_runtime
|
||||
import clr_loader
|
||||
set_runtime(clr_loader.get_coreclr(os.path.join(dlldir, "QuantConnect.Lean.Launcher.runtimeconfig.json")))
|
||||
|
||||
'''
|
||||
|
||||
from clr import AddReference
|
||||
AddReference("QuantConnect.Common")
|
||||
AddReference("QuantConnect.Tests")
|
||||
|
||||
from QuantConnect import *
|
||||
from QuantConnect.Python import PandasConverter
|
||||
from QuantConnect.Tests import Symbols
|
||||
from QuantConnect.Tests.Python import PythonTestingUtils
|
||||
|
||||
# Import our mapper which wraps core pandas functions (included in build dir)
|
||||
import PandasMapper
|
||||
import pandas as pd
|
||||
|
||||
# Get some dataframes from Lean to test on
|
||||
spy = Symbols.SPY
|
||||
aapl = Symbols.AAPL
|
||||
SymbolCache.Set("SPY", spy)
|
||||
SymbolCache.Set("AAPL", aapl)
|
||||
|
||||
pdConverter = PandasConverter()
|
||||
|
||||
slices = PythonTestingUtils.GetSlices(spy)
|
||||
spydf = pdConverter.GetDataFrame(slices)
|
||||
|
||||
slices = PythonTestingUtils.GetSlices(aapl)
|
||||
aapldf = pdConverter.GetDataFrame(slices)
|
||||
|
||||
|
||||
def Test_Concat(dataFrame, dataFrame2, indexer):
|
||||
newDataFrame = pd.concat([dataFrame, dataFrame2])
|
||||
data = newDataFrame['lastprice'].unstack(level=0).iloc[-1][indexer]
|
||||
if data is 0:
|
||||
raise Exception('Data is zero')
|
||||
|
||||
def Test_Join(dataFrame, dataFrame2, indexer):
|
||||
newDataFrame = dataFrame.join(dataFrame2, lsuffix='_')
|
||||
base = newDataFrame['lastprice_'].unstack(level=0)
|
||||
data = base.iloc[-1][indexer]
|
||||
if data is 0:
|
||||
raise Exception('Data is zero')
|
||||
|
||||
Test_Concat(spydf, aapldf, "spy")
|
||||
Test_Concat(spydf, aapldf, spy)
|
||||
Test_Concat(spydf, aapldf, str(spy.ID))
|
||||
|
||||
Test_Join(spydf, aapldf, "spy")
|
||||
Test_Join(spydf, aapldf, spy)
|
||||
Test_Join(spydf, aapldf, str(spy.ID))
|
||||
Reference in New Issue
Block a user