MCPcopy Index your code
hub / github.com/kernc/backtesting.py / test_assertions

Method test_assertions

backtesting/test/_test.py:130–225  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

128 Backtest(GOOG.iloc[:0], SmaCross).run()
129
130 def test_assertions(self):
131 class Assertive(Strategy):
132 def init(self):
133 self.sma = self.I(SMA, self.data.Close, 10)
134 self.remains_indicator = np.r_[2] * np.cumsum(self.sma * 5 + 1) * np.r_[2]
135
136 self.transpose_invalid = self.I(lambda: np.column_stack((self.data.Open,
137 self.data.Close)))
138
139 resampled = resample_apply('W', SMA, self.data.Close, 3)
140 resampled_ind = resample_apply('W', SMA, self.sma, 3)
141 assert np.unique(resampled[-5:]).size == 1
142 assert np.unique(resampled[-6:]).size == 2
143 assert resampled in self._indicators, "Strategy.I not called"
144 assert resampled_ind in self._indicators, "Strategy.I not called"
145
146 assert 1 == try_(lambda: self.data.X, 1, AttributeError)
147 assert 1 == try_(lambda: self.data['X'], 1, KeyError)
148
149 assert self.data.pip == .01
150
151 assert float(self.data.Close) == self.data.Close[-1]
152
153 def next(self, _FEW_DAYS=pd.Timedelta('3 days')): # noqa: N803
154 assert self.equity >= 0
155
156 assert isinstance(self.sma, _Indicator)
157 assert isinstance(self.remains_indicator, _Indicator)
158 assert self.remains_indicator.name
159 assert isinstance(self.remains_indicator._opts, dict)
160
161 assert not np.isnan(self.data.Open[-1])
162 assert not np.isnan(self.data.High[-1])
163 assert not np.isnan(self.data.Low[-1])
164 assert not np.isnan(self.data.Close[-1])
165 assert not np.isnan(self.data.Volume[-1])
166 assert not np.isnan(self.sma[-1])
167 assert self.data.index[-1]
168
169 self.position
170 self.position.size
171 self.position.pl
172 self.position.pl_pct
173 self.position.is_long
174
175 if crossover(self.sma, self.data.Close):
176 self.orders.cancel() # cancels only non-contingent
177 price = self.data.Close[-1]
178 sl, tp = 1.05 * price, .9 * price
179
180 n_orders = len(self.orders)
181 self.sell(size=.21, limit=price, stop=price, sl=sl, tp=tp)
182 assert len(self.orders) == n_orders + 1
183
184 order = self.orders[-1]
185 assert order.limit == price
186 assert order.stop == price
187 assert order.size == -.21

Callers

nothing calls this directly

Calls 2

runMethod · 0.95
BacktestClass · 0.90

Tested by

no test coverage detected