mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 12:22:42 +08:00
a4a4d38a73
- added LSE reference rrules calendar (thanks to Edward Johns)
- added tests to verify LSE environment matches rrule calendar
- added a test to verify global environment behavior can be set.
- moved DailyReturn class to trading to eliminate circularity from
risk <-> trading.
- updated TradingEnvironment to be a context manager. This allows users
to run algorithms in individually isolated environments in one python
process. This is useful for managing multiple algorithms in a single
ipython notebook.
- added comments to explain behavior and useage of the global environment
108 lines
2.8 KiB
Python
108 lines
2.8 KiB
Python
#
|
|
# Copyright 2012 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 datetime import datetime
|
|
|
|
import csv
|
|
|
|
from StringIO import StringIO
|
|
from functools import partial
|
|
|
|
import requests
|
|
|
|
from loader_utils import (
|
|
date_conversion,
|
|
source_to_records
|
|
)
|
|
|
|
from loader_utils import Mapping
|
|
|
|
|
|
_BENCHMARK_MAPPING = {
|
|
# Need to add 'symbol'
|
|
'volume': (int, 'Volume'),
|
|
'open': (float, 'Open'),
|
|
'close': (float, 'Close'),
|
|
'high': (float, 'High'),
|
|
'low': (float, 'Low'),
|
|
'adj_close': (float, 'Adj Close'),
|
|
'date': (partial(date_conversion, date_pattern='%Y-%m-%d'), 'Date')
|
|
}
|
|
|
|
|
|
def benchmark_mappings():
|
|
return {key: Mapping(*value)
|
|
for key, value
|
|
in _BENCHMARK_MAPPING.iteritems()}
|
|
|
|
|
|
def get_raw_benchmark_data(start_date, end_date, symbol):
|
|
|
|
# create benchmark files
|
|
# ^GSPC 19500103
|
|
params = {
|
|
's': symbol,
|
|
# end_date month, zero indexed
|
|
'd': end_date.month - 1,
|
|
# end_date day str(int(todate[6:8])) #day
|
|
'e': end_date.day,
|
|
# end_date year str(int(todate[0:4]))
|
|
'f': end_date.year,
|
|
# daily frequency
|
|
'g': 'd',
|
|
# start_date month, zero indexed
|
|
'a': start_date.month - 1,
|
|
# start_date day
|
|
'b': start_date.day,
|
|
# start_date year
|
|
'c': start_date.year
|
|
}
|
|
|
|
res = requests.get('http://ichart.yahoo.com/table.csv',
|
|
params=params)
|
|
|
|
return csv.DictReader(StringIO(res.content))
|
|
|
|
|
|
def get_benchmark_data(symbol):
|
|
"""
|
|
Benchmarks from Yahoo.
|
|
"""
|
|
start_date = datetime(year=1950, month=1, day=3)
|
|
end_date = datetime.utcnow()
|
|
|
|
raw_benchmark_data = get_raw_benchmark_data(start_date, end_date, symbol)
|
|
# Reverse data so we can load it in reverse chron order.
|
|
benchmarks_source = reversed(list(raw_benchmark_data))
|
|
|
|
mappings = benchmark_mappings()
|
|
|
|
return source_to_records(mappings, benchmarks_source)
|
|
|
|
|
|
def get_benchmark_returns(symbol):
|
|
from zipline.finance.trading import DailyReturn
|
|
|
|
benchmark_returns = []
|
|
|
|
for data_point in get_benchmark_data(symbol):
|
|
returns = (data_point['close'] - data_point['open']) / \
|
|
data_point['open']
|
|
daily_return = DailyReturn(date=data_point['date'], returns=returns)
|
|
benchmark_returns.append(daily_return)
|
|
|
|
return benchmark_returns
|