-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathtest_pnf_traditional.py
More file actions
125 lines (106 loc) · 2.99 KB
/
Copy pathtest_pnf_traditional.py
File metadata and controls
125 lines (106 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import pytest
import pandas as pd
import numpy as np
import mplfinance as mpf
from mplfinance._utils import _get_traditional_box_size
print(mpf.__file__)
def make_df(prices):
return pd.DataFrame({
'Open': prices,
'High': prices,
'Low': prices,
'Close': prices,
'Volume': [100] * len(prices)
}, index=pd.date_range("2024-01-01", periods=len(prices)))
def test_traditional_boundaries():
assert _get_traditional_box_size(0.2) == 0.0625
assert _get_traditional_box_size(0.25) == 0.125
assert _get_traditional_box_size(1) == 0.25
assert _get_traditional_box_size(5) == 0.5
assert _get_traditional_box_size(20) == 1
assert _get_traditional_box_size(100) == 2
assert _get_traditional_box_size(200) == 4
assert _get_traditional_box_size(500) == 5
assert _get_traditional_box_size(1000) == 10
def test_traditional_runs():
prices = [100, 102, 101, 105, 107, 103]
df = make_df(prices)
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size='traditional'),
returnfig=True
)
def test_atr_no_crash():
prices = [100, 100.2, 100.1, 100.15, 100.05]
df = make_df(prices)
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size='atr'),
returnfig=True
)
def test_fixed_still_works():
prices = [100, 102, 101, 103]
df = make_df(prices)
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size=2),
returnfig=True
)
def test_low_price_range():
prices = [0.1, 0.2, 0.3, 0.4, 0.5]
df = make_df(prices)
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size='traditional'),
returnfig=True
)
def test_high_volatility():
np.random.seed(0)
prices = np.cumsum(np.random.randn(200)) + 100
df = pd.DataFrame({
'Open': prices,
'High': prices + 1,
'Low': prices - 1,
'Close': prices,
'Volume': [100] * len(prices)
}, index=pd.date_range("2024-01-01", periods=len(prices)))
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size='traditional'),
returnfig=True
)
def test_scale_consistency():
base = [10, 12, 11, 13, 15, 14]
scaled = [x * 100 for x in base]
df1 = make_df(base)
df2 = make_df(scaled)
mpf.plot(df1, type='pnf',
pnf_params=dict(box_size='traditional'),
returnfig=True)
mpf.plot(df2, type='pnf',
pnf_params=dict(box_size='traditional'),
returnfig=True)
def test_fixed_still_works():
prices = [100, 110, 105, 120] # bigger range
df = make_df(prices)
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size=2),
returnfig=True
)
def test_fixed_invalid_boxsize_raises():
prices = [100, 102, 101, 103]
df = make_df(prices)
with pytest.raises(ValueError):
mpf.plot(
df,
type='pnf',
pnf_params=dict(box_size=2),
returnfig=True
)