mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-18 12:20:12 +08:00
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.
67 lines
2.0 KiB
Python
Executable File
67 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Copyright 2014 Quantopian, Inc.
|
|
#
|
|
# 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 zipline.api import order, record, symbol
|
|
|
|
|
|
def initialize(context):
|
|
pass
|
|
|
|
|
|
def handle_data(context, data):
|
|
order(symbol('AAPL'), 10)
|
|
record(AAPL=data.current(symbol('AAPL'), "price"))
|
|
|
|
|
|
# Note: this function can be removed if running
|
|
# this algorithm on quantopian.com
|
|
def analyze(context=None, results=None):
|
|
import matplotlib.pyplot as plt
|
|
# Plot the portfolio and asset data.
|
|
ax1 = plt.subplot(211)
|
|
results.portfolio_value.plot(ax=ax1)
|
|
ax1.set_ylabel('Portfolio value (USD)')
|
|
ax2 = plt.subplot(212, sharex=ax1)
|
|
results.AAPL.plot(ax=ax2)
|
|
ax2.set_ylabel('AAPL price (USD)')
|
|
|
|
# Show the plot.
|
|
plt.gcf().set_size_inches(18, 8)
|
|
plt.show()
|
|
|
|
|
|
# Note: this if-block should be removed if running
|
|
# this algorithm on quantopian.com
|
|
if __name__ == '__main__':
|
|
from datetime import datetime
|
|
import pytz
|
|
from zipline.algorithm import TradingAlgorithm
|
|
from zipline.utils.factory import load_from_yahoo
|
|
|
|
# Set the simulation start and end dates
|
|
start = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc)
|
|
end = datetime(2014, 11, 1, 0, 0, 0, 0, pytz.utc)
|
|
|
|
# Load price data from yahoo.
|
|
data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,
|
|
end=end)
|
|
|
|
# Create and run the algorithm.
|
|
algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data)
|
|
results = algo.run(data)
|
|
|
|
analyze(results=results)
|