mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-13 12:00:16 +08:00
MAINT: make the data loading apis more consistent.
Changes BcolzDailyBarWriter to not be an abc, data is passed as an iterator of (sid, dataframe) pairs to the write method. Changes the AssetsDBWriter to be a single class which accepts an engine at construction time and has a `write` method for writing dataframes for the various tables. We no longer support writing the various other data types, callers should coerce their data into a dataframe themselves. See zipline.assets.synthetic for some helpers to do this. Adds many new fixtures and updates some existing fixtures to use the new ones: WithDefaultDateBounds A fixture that provides the suite a START_DATE and END_DATE. This is meant to make it easy for other fixtures to synchronize their date ranges without depending on eachother in strange ways. For example, WithBcolzMinuteBarReader and WithBcolzDailyBarReader by default should both have data for the same dates, so they may use depend on WithDefaultDates without forcing a dependency between them. WithTmpDir, WithInstanceTmpDir Provides the suite or individual test case a temporary directory. WithBcolzDailyBarReader Provides the suite a BcolzDailyBarReader which reads from bcolz data written to a temporary directory. The data will be read from dataframes and then converted to bcolz files with BcolzDailyBarWriter.write WithBcolzDailyBarReaderFromCSVs Provides the suite a BcolzDailyBarReader which reads from bcolz data written to a temporary directory. The data will be read from a collection of CSV files and then converted into the bcolz data through BcolzDailyBarWriter.write_csvs WithBcolzMinuteBarReader Provides the suite a BcolzMinuteBarReader which reads from bcolz data written to a temporary directory. The data will be read from dataframes and then converted to bcolz files with BcolzMinuteBarWriter.write WithAdjustmentReader Provides the suite a SQLiteAdjustmentReader which reads from an in memory sqlite database. The data will be read from dataframes and then converted into sqlite with SQLiteAdjustmentWriter.write WithDataPortal Provides each test case a DataPortal object with data from temporary resources.
This commit is contained in:
+52
-21
@@ -12,8 +12,9 @@
|
||||
# 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.
|
||||
import warnings
|
||||
from copy import copy
|
||||
import operator as op
|
||||
import warnings
|
||||
|
||||
import logbook
|
||||
import pytz
|
||||
@@ -33,6 +34,7 @@ from six import (
|
||||
)
|
||||
|
||||
from zipline._protocol import handle_non_market_minutes
|
||||
from zipline.assets.synthetic import make_simple_equity_info
|
||||
from zipline.data.data_portal import DataPortal
|
||||
from zipline.errors import (
|
||||
AttachPipelineAfterInitialize,
|
||||
@@ -96,6 +98,7 @@ from zipline.utils.events import (
|
||||
TimeRuleFactory,
|
||||
)
|
||||
from zipline.utils.factory import create_simulation_parameters
|
||||
from zipline.utils.functional import unzip
|
||||
from zipline.utils.math_utils import (
|
||||
tolerant_equals,
|
||||
round_if_near_integer
|
||||
@@ -252,11 +255,18 @@ class TradingAlgorithm(object):
|
||||
self.trading_environment = TradingEnvironment()
|
||||
|
||||
# Update the TradingEnvironment with the provided asset metadata
|
||||
self.trading_environment.write_data(
|
||||
equities_data=kwargs.pop('equities_metadata', {}),
|
||||
equities_identifiers=kwargs.pop('identifiers', []),
|
||||
futures_data=kwargs.pop('futures_metadata', {}),
|
||||
)
|
||||
if 'equities_metadata' in kwargs or 'futures_metadata' in kwargs:
|
||||
warnings.warn(
|
||||
'passing metadata to TradingAlgorithm is deprecated; please'
|
||||
' write this data into the asset db before passing it to the'
|
||||
' trading environment',
|
||||
DeprecationWarning,
|
||||
stacklevel=1,
|
||||
)
|
||||
self.trading_environment.write_data(
|
||||
equities=kwargs.pop('equities_metadata', None),
|
||||
futures=kwargs.pop('futures_metadata', None),
|
||||
)
|
||||
|
||||
# set the capital base
|
||||
self.capital_base = kwargs.pop('capital_base', DEFAULT_CAPITAL_BASE)
|
||||
@@ -563,6 +573,17 @@ class TradingAlgorithm(object):
|
||||
data = data.swapaxes(0, 2)
|
||||
|
||||
if isinstance(data, pd.Panel):
|
||||
# For compatibility with existing examples allow start/end
|
||||
# to be inferred.
|
||||
if overwrite_sim_params:
|
||||
self.sim_params.period_start = data.major_axis[0]
|
||||
self.sim_params.period_end = data.major_axis[-1]
|
||||
# Changing period_start and period_close might require
|
||||
# updating of first_open and last_close.
|
||||
self.sim_params.update_internal_from_env(
|
||||
env=self.trading_environment
|
||||
)
|
||||
|
||||
copy_panel = data.copy()
|
||||
copy_panel.items = self._write_and_map_id_index_to_sids(
|
||||
copy_panel.items, copy_panel.major_axis[0],
|
||||
@@ -586,17 +607,6 @@ class TradingAlgorithm(object):
|
||||
self.trading_environment,
|
||||
equity_daily_reader=equity_daily_reader)
|
||||
|
||||
# For compatibility with existing examples allow start/end
|
||||
# to be inferred.
|
||||
if overwrite_sim_params:
|
||||
self.sim_params.period_start = data.major_axis[0]
|
||||
self.sim_params.period_end = data.major_axis[-1]
|
||||
# Changing period_start and period_close might require
|
||||
# updating of first_open and last_close.
|
||||
self.sim_params.update_internal_from_env(
|
||||
env=self.trading_environment
|
||||
)
|
||||
|
||||
# Force a reset of the performance tracker, in case
|
||||
# this is a repeat run of the algorithm.
|
||||
self.perf_tracker = None
|
||||
@@ -620,7 +630,8 @@ class TradingAlgorithm(object):
|
||||
def _write_and_map_id_index_to_sids(self, identifiers, as_of_date):
|
||||
# Build new Assets for identifiers that can't be resolved as
|
||||
# sids/Assets
|
||||
identifiers_to_build = []
|
||||
identifiers_to_build = set()
|
||||
next_sid = max(self.asset_finder.sids or (0,)) + 1
|
||||
for identifier in identifiers:
|
||||
asset = None
|
||||
|
||||
@@ -631,10 +642,30 @@ class TradingAlgorithm(object):
|
||||
asset = self.asset_finder.retrieve_asset(sid=identifier,
|
||||
default_none=True)
|
||||
if asset is None:
|
||||
identifiers_to_build.append(identifier)
|
||||
try:
|
||||
sid = op.index(identifier)
|
||||
except TypeError:
|
||||
sid = next_sid
|
||||
next_sid += 1
|
||||
identifiers_to_build.add((identifier, sid))
|
||||
|
||||
self.trading_environment.write_data(
|
||||
equities_identifiers=identifiers_to_build)
|
||||
if identifiers_to_build:
|
||||
warnings.warn(
|
||||
'writing unknown identifiers into the assets db of the trading'
|
||||
' environment is deprecated; please write this information'
|
||||
' to the assets db before constructing the environment',
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
symbols, sids = unzip(identifiers_to_build, 2)
|
||||
self.trading_environment.write_data(
|
||||
equities=make_simple_equity_info(
|
||||
sids,
|
||||
start_date=self.sim_params.period_start,
|
||||
end_date=self.sim_params.period_end,
|
||||
symbols=symbols,
|
||||
),
|
||||
)
|
||||
|
||||
# We need to clear out any cache misses that were stored while trying
|
||||
# to do lookups. The real fix for this problem is to not construct an
|
||||
|
||||
Reference in New Issue
Block a user