-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark_portfolio support old properties for compatible
- Loading branch information
Showing
24 changed files
with
189 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
rqalpha/mod/rqalpha_mod_sys_benchmark/benchmark_portfolio.py
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
rqalpha/mod/rqalpha_mod_sys_benchmark/benchmark_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2017 Ricequant, 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. | ||
|
||
import numpy as np | ||
|
||
from rqalpha.interface import AbstractBenchmarkProvider | ||
from rqalpha.environment import Environment | ||
from rqalpha.events import EVENT | ||
from rqalpha.utils.i18n import gettext as _ | ||
|
||
|
||
class BackTestPriceSeriesBenchmarkProvider(AbstractBenchmarkProvider): | ||
def __init__(self, order_book_id): | ||
self._order_book_id = order_book_id | ||
self._daily_return_series = None | ||
self._total_return_series = None | ||
self._index = 0 | ||
|
||
event_bus = Environment.get_instance().event_bus | ||
event_bus.add_listener(EVENT.POST_SYSTEM_INIT, self._on_system_init) | ||
event_bus.prepend_listener(EVENT.AFTER_TRADING, self._on_after_trading) | ||
|
||
def _on_system_init(self, _): | ||
env = Environment.get_instance() | ||
bar_count = len(env.config.base.trading_calendar) + 1 | ||
end_date = env.config.base.end_date | ||
close_series = env.data_proxy.history_bars( | ||
self._order_book_id, bar_count, "1d", "close", end_date, skip_suspended=False, adjust_type='pre' | ||
) | ||
if len(close_series) < bar_count: | ||
raise RuntimeError(_("Valid benchmark: unable to load enough close price.")) | ||
|
||
self._total_return_series = (close_series - close_series[0]) / close_series[0] | ||
|
||
self._daily_return_series = np.zeros((bar_count, )) | ||
self._daily_return_series[1:] = (close_series[1:] - close_series[:-1]) / close_series[:-1] | ||
|
||
def _on_after_trading(self, _): | ||
self._index += 1 | ||
|
||
@property | ||
def daily_returns(self): | ||
return self._daily_return_series[self._index] | ||
|
||
@property | ||
def total_returns(self): | ||
return self._total_return_series[self._index] | ||
|
||
|
||
class RealTimePriceSeriesBenchmarkProvider(AbstractBenchmarkProvider): | ||
def __init__(self, order_book_id): | ||
self._order_book_id = order_book_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
from rqalpha.utils.testing import DataProxyFixture | ||
|
||
|
||
class BackTestPriceSeriesBenchmarkPortfolioFixture(DataProxyFixture): | ||
class BackTestPriceSeriesBenchmarkProviderFixture(DataProxyFixture): | ||
def __init__(self, *args, **kwargs): | ||
super(BackTestPriceSeriesBenchmarkPortfolioFixture, self).__init__(*args, **kwargs) | ||
super(BackTestPriceSeriesBenchmarkProviderFixture, self).__init__(*args, **kwargs) | ||
|
||
self.benchmark_portfolio = None | ||
self.benchmark_provider = None | ||
self.benchmark_order_book_id = None | ||
|
||
def init_fixture(self): | ||
from rqalpha.mod.rqalpha_mod_sys_benchmark.benchmark_portfolio import BackTestPriceSeriesBenchmarkPortfolio | ||
from rqalpha.mod.rqalpha_mod_sys_benchmark.benchmark_provider import BackTestPriceSeriesBenchmarkProvider | ||
|
||
super(BackTestPriceSeriesBenchmarkPortfolioFixture, self).init_fixture() | ||
self.benchmark_portfolio = BackTestPriceSeriesBenchmarkPortfolio(self.benchmark_order_book_id) | ||
super(BackTestPriceSeriesBenchmarkProviderFixture, self).init_fixture() | ||
self.benchmark_provider = BackTestPriceSeriesBenchmarkProvider(self.benchmark_order_book_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2017 Ricequant, 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 rqalpha.environment import Environment | ||
from rqalpha.const import DAYS_CNT | ||
|
||
|
||
class BenchmarkPortfolio(object): | ||
def __init__(self, benchmark_provider, units): | ||
self._provider = benchmark_provider | ||
self._units = units | ||
|
||
@property | ||
def units(self): | ||
return self._units | ||
|
||
@property | ||
def daily_returns(self): | ||
return self._provider.daily_returns | ||
|
||
@property | ||
def total_returns(self): | ||
return self._provider.total_returns | ||
|
||
@property | ||
def annualized_returns(self): | ||
# fixme: do not rely on env | ||
if self.unit_net_value <= 0: | ||
return -1 | ||
|
||
env = Environment.get_instance() | ||
date_count = float(env.data_proxy.count_trading_dates(env.config.base.start_date, env.trading_dt.date())) | ||
return self.unit_net_value ** (DAYS_CNT.TRADING_DAYS_A_YEAR / date_count) - 1 | ||
|
||
@property | ||
def unit_net_value(self): | ||
return 1 + self.total_returns | ||
|
||
@property | ||
def static_unit_net_value(self): | ||
return self.unit_net_value / (1 + self.daily_returns) | ||
|
||
@property | ||
def total_value(self): | ||
return self.units * self.unit_net_value | ||
|
||
@property | ||
def cash(self): | ||
return 0 | ||
|
||
@property | ||
def market_value(self): | ||
return self.total_value |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
date_time,time_spend | ||
2018-06-13 10:46:12.018948,23.042125 | ||
2018-06-13 10:47:01.146415,23.110269 |
28 changes: 0 additions & 28 deletions
28
tests/unittest/test_mod/test_sys_benchmark/test_benchmark_portfolio.py
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
tests/unittest/test_mod/test_sys_benchmark/test_benchmark_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from datetime import date | ||
|
||
from rqalpha.utils.testing import RQAlphaTestCase | ||
from rqalpha.mod.rqalpha_mod_sys_benchmark.testing import BackTestPriceSeriesBenchmarkProviderFixture | ||
from rqalpha.events import EVENT, Event | ||
|
||
|
||
class BackTestPriceSeriesBenchmarkProviderTestCase(BackTestPriceSeriesBenchmarkProviderFixture, RQAlphaTestCase): | ||
def __init__(self, *args, **kwargs): | ||
super(BackTestPriceSeriesBenchmarkProviderTestCase, self).__init__(*args, **kwargs) | ||
self.benchmark_order_book_id = "000300.XSHG" | ||
self.env_config["base"].update({ | ||
"start_date": date(2018, 9, 3), "end_date": date(2018, 9, 25) | ||
}) | ||
|
||
def test_returns(self): | ||
self.env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT)) | ||
self.assertEqual(self.benchmark_provider.daily_returns, 0) | ||
self.env.event_bus.publish_event(Event(EVENT.AFTER_TRADING)) | ||
self.assertAlmostEqual(self.benchmark_provider.daily_returns, (3321.82 - 3334.50) / 3334.50) | ||
self.assertAlmostEqual(self.benchmark_provider.total_returns, (3321.82 - 3334.50) / 3334.50) | ||
for i in range(10): | ||
self.env.event_bus.publish_event(Event(EVENT.AFTER_TRADING)) | ||
self.assertAlmostEqual(self.benchmark_provider.daily_returns, (3204.92 - 3242.09) / 3242.09) | ||
self.assertAlmostEqual(self.benchmark_provider.total_returns, (3204.92 - 3334.50) / 3334.50) | ||
|
||
|
||
if __name__ == "__main__": | ||
import unittest | ||
unittest.main() |