Skip to content

Commit db377d6

Browse files
committed
add tests and update ohlc
Updates to ohlc -make tests -make default for kwargs so the default colours are red/green (typical for this chart)
1 parent 726d0b7 commit db377d6

File tree

2 files changed

+315
-106
lines changed

2 files changed

+315
-106
lines changed

plotly/tests/test_core/test_tools/test_trace_factory.py

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import plotly.tools as tls
77
from plotly.exceptions import PlotlyError
8-
from plotly.graph_objs import Line, graph_objs
8+
from plotly.graph_objs import Data, Line, graph_objs
99

1010

1111
class TestQuiver(TestCase):
@@ -100,3 +100,180 @@ def test_more_kwargs(self):
100100
'type': 'scatter',
101101
'mode': 'lines', }
102102
self.assertEqual(quiver, expected_quiver)
103+
104+
105+
class TestOHLC(TestCase):
106+
107+
def test_unequal_o_h_l_c_length(self):
108+
109+
# check: PlotlyError if op, hi, lo, cl are not the same length for
110+
# TraceFactory.ohlc_increase and TraceFactory.ohlc_decrease
111+
112+
kwargs = {'op': [1], 'hi': [1, 3], 'lo': [1, 2], 'cl': [1, 2]}
113+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_increase,
114+
**kwargs)
115+
116+
kwargs = {'op': [1, 2], 'hi': [1, 2, 3], 'lo': [1, 2], 'cl': [1, 2]}
117+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_increase,
118+
**kwargs)
119+
120+
kwargs = {'op': [1], 'hi': [1, 3], 'lo': [1, 2], 'cl': [2]}
121+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_decrease,
122+
**kwargs)
123+
124+
kwargs = {'op': [1, 2], 'hi': [1, 2], 'lo': [1, 2, .5], 'cl': [1, 2]}
125+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_decrease,
126+
**kwargs)
127+
128+
def test_hi_highest_value(self):
129+
130+
# check: PlotlyError if the "hi" value is less than the corresponding
131+
# op, lo, or cl value because if the "high" value is not the highest
132+
# (or equal) then the data may have been entered incorrectly.
133+
134+
# create_ohlc_increase
135+
kwargs = {'op': [2, 3], 'hi': [4, 2], 'lo': [1, 1], 'cl': [1, 2]}
136+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_increase,
137+
**kwargs)
138+
139+
# create_ohlc_decrease
140+
kwargs = {'op': [2, 3], 'hi': [4, 3], 'lo': [1, 1], 'cl': [1, 6]}
141+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_decrease,
142+
**kwargs)
143+
144+
def test_lo_lowest_value(self):
145+
146+
# check: PlotlyError if the "lo" value is greater than the
147+
# corresponding op, hi, or cl value because if the "lo" value is not
148+
# the lowest (or equal) then the data may have been entered incorrectly
149+
150+
# create_ohlc_increase
151+
kwargs = {'op': [2, 3], 'hi': [4, 6], 'lo': [3, 1], 'cl': [1, 2]}
152+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_increase,
153+
**kwargs)
154+
155+
# create_ohlc_decrease
156+
kwargs = {'op': [2, 3], 'hi': [4, 6], 'lo': [1, 5], 'cl': [1, 6]}
157+
self.assertRaises(PlotlyError, tls.TraceFactory.create_ohlc_decrease,
158+
**kwargs)
159+
160+
def test_one_ohlc_increase(self):
161+
162+
# This should create one "increase" (i.e. close > open) ohlc stick
163+
164+
ohlc_incr = tls.TraceFactory.create_ohlc_increase(op=[33.0], hi=[33.2],
165+
lo=[32.7], cl=[33.1])
166+
167+
expected_ohlc_incr = {
168+
'mode': 'lines',
169+
'y': [33.0, 33.0, 33.2, 32.7, 33.1, 33.1, None],
170+
'line': {'color': 'rgb(44, 160, 44)'},
171+
'x': [0.8, 1, 1, 1, 1, 1.2, None],
172+
'name': 'Increasing',
173+
'type': 'scatter',
174+
'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', '')}
175+
self.assertEqual(ohlc_incr, expected_ohlc_incr)
176+
177+
def test_ohlc_increase_with_kwargs(self):
178+
179+
# This should create one "increase" (i.e. close > open) ohlc stick
180+
# and change the name to "POSITIVE!!"
181+
182+
ohlc_incr = tls.TraceFactory.create_ohlc_increase(op=[1.5], hi=[30],
183+
lo=[1], cl=[25],
184+
name="POSITIVE!!")
185+
186+
expected_ohlc_incr = {
187+
'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', ''),
188+
'type': 'scatter',
189+
'name': 'POSITIVE!!',
190+
'x': [0.8, 1, 1, 1, 1, 1.2, None],
191+
'y': [1.5, 1.5, 30, 1, 25, 25, None],
192+
'mode': 'lines',
193+
'line': {'color': 'rgb(44, 160, 44)'}}
194+
195+
self.assertEqual(ohlc_incr, expected_ohlc_incr)
196+
197+
def test_one_ohlc_decrease(self):
198+
199+
# This should create one "decrease" (i.e. close < open) ohlc stick
200+
201+
ohlc_decr = tls.TraceFactory.create_ohlc_decrease(op=[33.3], hi=[33.3],
202+
lo=[32.7], cl=[32.9])
203+
204+
expected_ohlc_decr = {
205+
'mode': 'lines',
206+
'y': [33.3, 33.3, 33.3, 32.7, 32.9, 32.9, None],
207+
'line': {'color': 'rgb(214, 39, 40)'},
208+
'x': [0.8, 1, 1, 1, 1, 1.2, None],
209+
'name': 'Decreasing',
210+
'type': 'scatter',
211+
'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', '')}
212+
self.assertEqual(ohlc_decr, expected_ohlc_decr)
213+
214+
def test_ohlc_decrease_with_kwargs(self):
215+
216+
# This should create one "decrease" (i.e. close < open) ohlc stick
217+
# and change the line width to 4
218+
219+
ohlc_decr = tls.TraceFactory.create_ohlc_decrease(op=[15], hi=[30],
220+
lo=[1], cl=[5],
221+
line=Line(
222+
color='rgb(214, 39, 40)',
223+
width=4))
224+
225+
expected_ohlc_decr = {
226+
'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', ''),
227+
'type': 'scatter',
228+
'name': 'Decreasing',
229+
'x': [0.8, 1, 1, 1, 1, 1.2, None],
230+
'y': [15, 15, 30, 1, 5, 5, None],
231+
'mode': 'lines',
232+
'line': {'color': 'rgb(214, 39, 40)', 'width': 4}}
233+
234+
self.assertEqual(ohlc_decr, expected_ohlc_decr)
235+
236+
def test_ohlc_increase_and_decrease(self):
237+
238+
# This should add multiple increasing and decreasing sticks
239+
# and check that what we expect (i.e. the data and default kwargs)
240+
# is resulting from data=Data([ohlc_increasing, ohlc_decreasing])
241+
242+
o = [3.3, 4, 9.3, 8.9, 4.9, 9, 2.9, 5]
243+
h = [7, 6.4, 10, 10, 10, 14.6, 12, 7]
244+
l = [3, 2, 7, 3, 2, 2, 1.1, 2.3]
245+
c = [3.3, 6.3, 10, 9, 9.2, 3, 2.9, 6.1]
246+
247+
ohlc_incr = tls.TraceFactory.create_ohlc_increase(o, h, l, c)
248+
ohlc_decr = tls.TraceFactory.create_ohlc_decrease(o, h, l, c)
249+
ohlc_data = Data([ohlc_incr, ohlc_decr])
250+
251+
expected_ohlc_data = [{
252+
'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
253+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
254+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
255+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
256+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', ''),
257+
'type': 'scatter',
258+
'name': 'Increasing',
259+
'x': [1.8, 2, 2, 2, 2, 2.2, None, 2.8, 3, 3, 3, 3, 3.2, None,
260+
3.8, 4, 4, 4, 4, 4.2, None, 4.8, 5, 5, 5, 5, 5.2, None,
261+
7.8, 8, 8, 8, 8, 8.2, None],
262+
'y': [4, 4, 6.4, 2, 6.3, 6.3, None, 9.3, 9.3, 10, 7, 10, 10, None,
263+
8.9, 8.9, 10, 3, 9, 9, None, 4.9, 4.9, 10, 2, 9.2, 9.2, None,
264+
5, 5, 7, 2.3, 6.1, 6.1, None],
265+
'mode': 'lines',
266+
'line': {'color': 'rgb(44, 160, 44)'}},
267+
{'text': ('Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
268+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', '',
269+
'Open', 'Open', 'High', 'Low', 'Close', 'Close', ''),
270+
'type': 'scatter', 'name': 'Decreasing',
271+
'x': [0.8, 1, 1, 1, 1, 1.2, None, 5.8, 6, 6, 6, 6, 6.2, None,
272+
6.8, 7, 7, 7, 7, 7.2, None],
273+
'y': [3.3, 3.3, 7, 3, 3.3, 3.3, None, 9, 9, 14.6, 2, 3, 3, None,
274+
2.9, 2.9, 12, 1.1, 2.9, 2.9, None],
275+
'mode': 'lines',
276+
'line': {'color': 'rgb(214, 39, 40)'}}]
277+
278+
self.assertEqual(ohlc_data, expected_ohlc_data)
279+

0 commit comments

Comments
 (0)