Completed
Pull Request — master (#858)
by Eddie
02:13 queued 19s
created

tests.TestTradeSimulation.fake_benchmark()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
#
2
# Copyright 2014 Quantopian, Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
import pandas as pd
16
from mock import patch
17
18
from nose_parameterized import parameterized
19
from six.moves import range
20
from unittest import TestCase
21
from zipline import TradingAlgorithm
22
from zipline.sources.benchmark_source import BenchmarkSource
23
from zipline.test_algorithms import NoopAlgorithm
24
from zipline.utils import factory
25
from zipline.utils.test_utils import FakeDataPortal
26
27
28
class BeforeTradingAlgorithm(TradingAlgorithm):
29
    def __init__(self, *args, **kwargs):
30
        self.before_trading_at = []
31
        super(BeforeTradingAlgorithm, self).__init__(*args, **kwargs)
32
33
    def before_trading_start(self, data):
34
        self.before_trading_at.append(self.datetime)
35
36
    def handle_data(self, data):
37
        pass
38
39
40
FREQUENCIES = {'daily': 0, 'minute': 1}  # daily is less frequent than minute
41
42
43
class TestTradeSimulation(TestCase):
44
45
    def fake_minutely_benchmark(self, dt):
46
        return 0.01
47
48
    def test_minutely_emissions_generate_performance_stats_for_last_day(self):
49
        params = factory.create_simulation_parameters(num_days=1,
50
                                                      data_frequency='minute',
51
                                                      emission_rate='minute')
52
        with patch.object(BenchmarkSource, "get_value",
53
                          self.fake_minutely_benchmark):
54
            algo = NoopAlgorithm(sim_params=params)
55
            algo.run(data_portal=FakeDataPortal())
56
            self.assertEqual(algo.perf_tracker.day_count, 1.0)
57
58
    @parameterized.expand([('%s_%s_%s' % (num_days, freq, emission_rate),
59
                            num_days, freq, emission_rate)
60
                           for freq in FREQUENCIES
61
                           for emission_rate in FREQUENCIES
62
                           for num_days in range(1, 4)
63
                           if FREQUENCIES[emission_rate] <= FREQUENCIES[freq]])
64
    def test_before_trading_start(self, test_name, num_days, freq,
65
                                  emission_rate):
66
        params = factory.create_simulation_parameters(
67
            num_days=num_days, data_frequency=freq,
68
            emission_rate=emission_rate)
69
70
        def fake_benchmark(self, dt):
71
            return 0.01
72
73
        with patch.object(BenchmarkSource, "get_value",
74
                          self.fake_minutely_benchmark):
75
            algo = BeforeTradingAlgorithm(sim_params=params)
76
            algo.run(data_portal=FakeDataPortal())
77
78
            self.assertEqual(algo.perf_tracker.day_count, num_days)
79
80
            self.assertTrue(params.trading_days.equals(
81
                pd.DatetimeIndex(algo.before_trading_at)),
82
                "Expected %s but was %s."
83
                % (params.trading_days, algo.before_trading_at))
84