1
1
import numpy as np
2
- from nose .tools import (
3
- assert_equals ,
4
- assert_true ,
5
- assert_false ,
6
- assert_raises ,
7
- )
2
+ import pytest
8
3
9
4
try :
10
5
from collections import OrderedDict
14
9
import talib
15
10
from talib import func
16
11
from talib import abstract
17
- from talib . test_data import ford_2012 , assert_np_arrays_equal , assert_np_arrays_not_equal
12
+ from test_data import ford_2012 , assert_np_arrays_equal , assert_np_arrays_not_equal
18
13
19
14
20
15
def test_pandas ():
@@ -24,20 +19,20 @@ def test_pandas():
24
19
25
20
expected_k , expected_d = func .STOCH (ford_2012 ['high' ], ford_2012 ['low' ], ford_2012 ['close' ]) # 5, 3, 0, 3, 0
26
21
output = abstract .Function ('stoch' , input_df ).outputs
27
- assert_true ( isinstance (output , pandas .DataFrame ) )
22
+ assert isinstance (output , pandas .DataFrame )
28
23
assert_np_arrays_equal (expected_k , output ['slowk' ])
29
24
assert_np_arrays_equal (expected_d , output ['slowd' ])
30
25
output = abstract .Function ('stoch' , input_dict ).outputs
31
- assert_true ( isinstance (output , list ) )
26
+ assert isinstance (output , list )
32
27
assert_np_arrays_equal (expected_k , output [0 ])
33
28
assert_np_arrays_equal (expected_d , output [1 ])
34
29
35
30
expected = func .SMA (ford_2012 ['close' ], 10 )
36
31
output = abstract .Function ('sma' , input_df , 10 ).outputs
37
- assert_true ( isinstance (output , pandas .Series ) )
32
+ assert isinstance (output , pandas .Series )
38
33
assert_np_arrays_equal (expected , output )
39
34
output = abstract .Function ('sma' , input_dict , 10 ).outputs
40
- assert_true ( isinstance (output , np .ndarray ) )
35
+ assert isinstance (output , np .ndarray )
41
36
assert_np_arrays_equal (expected , output )
42
37
43
38
@@ -86,12 +81,13 @@ def test_doji_candle():
86
81
87
82
def test_MAVP ():
88
83
mavp = abstract .MAVP
89
- assert_raises (Exception , mavp .set_input_arrays , ford_2012 )
84
+ with pytest .raises (Exception ):
85
+ mavp .set_input_arrays (ford_2012 )
90
86
input_d = {}
91
87
input_d ['close' ] = ford_2012 ['close' ]
92
88
input_d ['periods' ] = np .arange (30 )
93
- assert_true ( mavp .set_input_arrays (input_d ) )
94
- assert_equals ( mavp .input_arrays , input_d )
89
+ assert mavp .set_input_arrays (input_d )
90
+ assert mavp .input_arrays == input_d
95
91
96
92
def test_info ():
97
93
stochrsi = abstract .Function ('STOCHRSI' )
@@ -115,7 +111,7 @@ def test_info():
115
111
('fastd_matype' , 1 ),
116
112
]),
117
113
}
118
- assert_equals ( expected , stochrsi .info )
114
+ assert expected == stochrsi .info
119
115
120
116
expected = {
121
117
'display_name' : 'Bollinger Bands' ,
@@ -136,11 +132,11 @@ def test_info():
136
132
('matype' , 0 ),
137
133
]),
138
134
}
139
- assert_equals ( expected , abstract .Function ('BBANDS' ).info )
135
+ assert expected == abstract .Function ('BBANDS' ).info
140
136
141
137
def test_input_names ():
142
138
expected = OrderedDict ([('price' , 'close' )])
143
- assert_equals ( expected , abstract .Function ('MAMA' ).input_names )
139
+ assert expected == abstract .Function ('MAMA' ).input_names
144
140
145
141
# test setting input_names
146
142
obv = abstract .Function ('OBV' )
@@ -149,13 +145,13 @@ def test_input_names():
149
145
('prices' , ['volume' ]),
150
146
])
151
147
obv .input_names = expected
152
- assert_equals ( obv .input_names , expected )
148
+ assert obv .input_names == expected
153
149
154
150
obv .input_names = {
155
151
'price' : 'open' ,
156
152
'prices' : ['volume' ],
157
153
}
158
- assert_equals ( obv .input_names , expected )
154
+ assert obv .input_names == expected
159
155
160
156
def test_input_arrays ():
161
157
mama = abstract .Function ('MAMA' )
@@ -167,33 +163,34 @@ def test_input_arrays():
167
163
'close' : None ,
168
164
'volume' : None ,
169
165
}
170
- assert_equals ( expected , mama .get_input_arrays () )
166
+ assert expected == mama .get_input_arrays ()
171
167
# test setting/getting input_arrays
172
- assert_true ( mama .set_input_arrays (ford_2012 ) )
173
- assert_equals ( mama .get_input_arrays (), ford_2012 )
174
- assert_raises (Exception ,
175
- mama .set_input_arrays , {'hello' : 'fail' , 'world' : 'bye' })
168
+ assert mama .set_input_arrays (ford_2012 )
169
+ assert mama .get_input_arrays () == ford_2012
170
+ with pytest . raises (Exception ):
171
+ mama .set_input_arrays ( {'hello' : 'fail' , 'world' : 'bye' })
176
172
177
173
# test only required keys are needed
178
174
willr = abstract .Function ('WILLR' )
179
175
reqd = willr .input_names ['prices' ]
180
176
input_d = dict ([(key , ford_2012 [key ]) for key in reqd ])
181
- assert_true ( willr .set_input_arrays (input_d ) )
182
- assert_equals ( willr .input_arrays , input_d )
177
+ assert willr .set_input_arrays (input_d )
178
+ assert willr .input_arrays == input_d
183
179
184
180
# test extraneous keys are ignored
185
181
input_d ['extra_stuffs' ] = 'you should never see me'
186
182
input_d ['date' ] = np .random .rand (100 )
187
- assert_true ( willr .set_input_arrays (input_d ) )
183
+ assert willr .set_input_arrays (input_d )
188
184
189
185
# test missing keys get detected
190
186
input_d ['open' ] = ford_2012 ['open' ]
191
187
input_d .pop ('close' )
192
- assert_raises (Exception , willr .set_input_arrays , input_d )
188
+ with pytest .raises (Exception ):
189
+ willr .set_input_arrays (input_d )
193
190
194
191
# test changing input_names on the Function
195
192
willr .input_names = {'prices' : ['high' , 'low' , 'open' ]}
196
- assert_true ( willr .set_input_arrays (input_d ) )
193
+ assert willr .set_input_arrays (input_d )
197
194
198
195
def test_parameters ():
199
196
stoch = abstract .Function ('STOCH' )
@@ -204,31 +201,31 @@ def test_parameters():
204
201
('slowd_period' , 3 ),
205
202
('slowd_matype' , 0 ),
206
203
])
207
- assert_equals ( expected , stoch .parameters )
204
+ assert expected == stoch .parameters
208
205
209
206
stoch .parameters = {'fastk_period' : 10 }
210
207
expected ['fastk_period' ] = 10
211
- assert_equals ( expected , stoch .parameters )
208
+ assert expected == stoch .parameters
212
209
213
210
stoch .parameters = {'slowk_period' : 8 , 'slowd_period' : 5 }
214
211
expected ['slowk_period' ] = 8
215
212
expected ['slowd_period' ] = 5
216
- assert_equals ( expected , stoch .parameters )
213
+ assert expected == stoch .parameters
217
214
218
215
stoch .parameters = {'slowd_matype' : talib .MA_Type .T3 }
219
216
expected ['slowd_matype' ] = 8
220
- assert_equals ( expected , stoch .parameters )
217
+ assert expected == stoch .parameters
221
218
222
219
stoch .parameters = {
223
220
'slowk_matype' : talib .MA_Type .WMA ,
224
221
'slowd_matype' : talib .MA_Type .EMA ,
225
222
}
226
223
expected ['slowk_matype' ] = 2
227
224
expected ['slowd_matype' ] = 1
228
- assert_equals ( expected , stoch .parameters )
225
+ assert expected == stoch .parameters
229
226
230
227
def test_lookback ():
231
- assert_equals ( abstract .Function ('SMA' , 10 ).lookback , 9 )
228
+ assert abstract .Function ('SMA' , 10 ).lookback == 9
232
229
233
230
stochrsi = abstract .Function ('stochrsi' , 20 , 5 , 3 )
234
- assert_equals ( stochrsi .lookback , 26 )
231
+ assert stochrsi .lookback == 26
0 commit comments