Skip to content

Commit 7814439

Browse files
author
Alexey Hurko
committed
Visualization added
1 parent 4145960 commit 7814439

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

pricelevels/visualization/_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import matplotlib.pyplot as plt
2+
3+
4+
def _plot_levels(levels):
5+
for l in levels:
6+
if isinstance(l, float):
7+
plt.axhline(y=l, color='black', linestyle='-')
8+
elif isinstance(l, dict):
9+
if 'score' in l.keys():
10+
color = 'red' if l['score'] < 0 else 'blue'
11+
plt.axhline(y=l['price'], color=color, linestyle='-', linewidth=0.2 * abs(l['score']))
12+
else:
13+
plt.axhline(y=l['price'], color='black', linestyle='-')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
from zigzag import peak_valley_pivots
4+
5+
from ._helpers import _plot_levels
6+
7+
8+
def plot_with_pivots(X, levels, zigzag_percent=1):
9+
pivots = peak_valley_pivots(X, zigzag_percent / 100, -zigzag_percent / 100)
10+
plt.xlim(0, len(X))
11+
plt.ylim(X.min() * 0.995, X.max() * 1.005)
12+
plt.plot(np.arange(len(X)), X, 'k-', alpha=0.9)
13+
plt.plot(np.arange(len(X))[pivots != 0], X[pivots != 0], 'k:', alpha=0.5)
14+
15+
plt.scatter(np.arange(len(X))[pivots == 1], X[pivots == 1], color='g')
16+
plt.scatter(np.arange(len(X))[pivots == -1], X[pivots == -1], color='r')
17+
18+
_plot_levels(levels)
19+
plt.show()
20+
plt.close()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'scikit-learn==0.21.2',
2828
'pandas==0.25.0',
2929
'ZigZag==0.2.2',
30+
'matplotlib==3.1.1',
3031
],
3132
setup_requires=[
3233
'pytest-runner'

tests/check_vizualization.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pricelevels.scoring.touch_scorer import TouchScorer
2+
from pricelevels.visualization.levels_with_zigzag import plot_with_pivots
3+
from tests.fixtures import get_levels_from_fixture, get_quotes_from_fixture
4+
5+
6+
def check_zigzag():
7+
levels = get_levels_from_fixture()
8+
quotes = get_quotes_from_fixture().iloc[-100:]
9+
scoring = TouchScorer()
10+
scoring.fit(levels, quotes)
11+
12+
plot_with_pivots(quotes.Close.values, levels, zigzag_percent=0.2)
13+
14+
for level, score in zip(levels, scoring.scores):
15+
level['score'] = score[-1].score
16+
17+
plot_with_pivots(quotes.Close.values, levels, zigzag_percent=0.2)
18+
19+
20+
if __name__ == '__main__':
21+
check_zigzag()

0 commit comments

Comments
 (0)