Skip to content

Commit b234975

Browse files
committed
Update the documentation
1 parent 8d16078 commit b234975

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11
> [!CAUTION]
2-
> This project is intended for educational purposes only and does not constitute financial advice. The stock price predictions generated by this software are not guaranteed to be accurate and should not be relied upon for making investment decisions. The authors of this project are not responsible for any financial losses incurred as a result of using this software.
2+
> This project is intended for educational purposes only and does not constitute financial advice. The stock price
3+
> predictions generated by this software are not guaranteed to be accurate and should not be relied upon for making
4+
> investment decisions. The authors of this project are not responsible for any financial losses incurred as a result of
5+
> using this software.
36
47
# PATTERNITY
58

6-
This project focuses on predicting stock prices using a deterministic algorithm inspired by the Long Short-Term Memory (LSTM) model of deep learning. Unlike traditional statistical models, this approach leverages the power of pattern recognition to identify and repeat the most similar historical patterns in stock price data, with visualizations inspired by Google Finance day charts.
9+
This project focuses on predicting stock prices using a deterministic algorithm inspired by the LSTM model of deep
10+
learning. Unlike traditional statistical models, this approach leverages the power of pattern recognition to identify
11+
and repeat the most similar historical patterns in stock price data, with visualizations inspired by Google Finance
12+
charts.
713

8-
![Screenshot from 2024-07-26 14-58-12](https://github.com/user-attachments/assets/516c5acb-8324-4af1-9a7f-869acd185fb8)
14+
## Usage
15+
16+
```bash
17+
python3 -m pip install patternity
18+
```
19+
20+
Patterity comes with collector functions to get historical data of an instrument. The following example demonstrates how
21+
to predict and plot the price of Bitcoin. For stocks, you can use the `get_stock` function instead. The `Pattern`
22+
class (core) is used to predict the price of an instrument and has the following arguments.
23+
24+
- `history` - Historical data of an instrument (required).
25+
- `window` - The minimum length of the pattern window to be considered. (optional)
26+
- `progress` - Callback function to track the progress of the prediction. Recommended for large historical data.
27+
28+
```python
29+
from patternity import Pattern, get_crypto
30+
from rich.progress import track
31+
32+
if __name__ == "__main__":
33+
history = get_crypto("BTCUSDT", depth=200)
34+
pattern = Pattern(history, progress=track)
35+
pattern.predict()
36+
pattern.plot()
37+
```
38+
39+
If everything is set up correctly, you should see a plot similar to the one below. If nothing was predicted, try on
40+
larger historical data. Also, you can change the `window` parameter to adjust the minimum length of the pattern to be
41+
recognized. The higher the `window`, the more accurate the prediction will be and longer it will take to compute.
42+
43+
![](https://github.com/user-attachments/assets/516c5acb-8324-4af1-9a7f-869acd185fb8)
944

1045
## Contribute
1146

12-
Any contribution is welcome. Please don't hesitate to open an issue or a discussion if you have any questions not covered by the documentation. Please open a pull request if you have any ideas or suggestions on optimizing the algorithm.
47+
Any contribution is welcome. Don't hesitate to open an issue or a discussion if you have any questions not covered by
48+
the documentation. Please open a pull request if you have any ideas or suggestions on optimizing the algorithm.
1349

1450
## License
1551

src/patternity/pattern.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77

88
class Pattern:
9-
def __init__(self, history: np.array, min_length: int = 50, progress: Callable[[Iterable], Iterable] = None):
9+
def __init__(self, history: np.array, window: int = 50, progress: Callable[[Iterable], Iterable] = None):
1010
"""
1111
Initialize the pattern detector.
1212
1313
:param history: Historical data to be analyzed. Usually, the closing prices of an asset.
14-
:param min_length: Minimum length of the pattern to be considered.
14+
:param window: The minimum length of the pattern window to be considered.
1515
:param progress: Progress bar to be displayed during the analysis.
1616
"""
1717

1818
self.history = history
1919
self.horizon = len(history)
2020
self.progress = progress or (lambda x: x)
21-
self.min_length = min_length
21+
self.window = window
2222
self.predictions = np.array([])
2323

2424
@staticmethod
@@ -57,7 +57,7 @@ def slices(self) -> tuple[tuple, np.array]:
5757
"""Generates all possible slices of the historical data."""
5858

5959
for i in range(self.horizon):
60-
for j in range(i + self.min_length, self.horizon + 1):
60+
for j in range(i + self.window, self.horizon + 1):
6161
yield (i, j), self.history[i:j]
6262

6363
def patterns(self) -> list[tuple[tuple, np.array]]:
@@ -66,8 +66,8 @@ def patterns(self) -> list[tuple[tuple, np.array]]:
6666
def valid(x) -> bool:
6767
return all([
6868
self.horizon == x[0][1],
69-
len(x[1]) >= self.min_length,
70-
len(x[1]) <= self.horizon - self.min_length,
69+
len(x[1]) >= self.window,
70+
len(x[1]) <= self.horizon - self.window,
7171
])
7272

7373
return sorted([x for x in self.slices() if valid(x)], key=lambda x: (len(x[1]), x[0][1]), reverse=True)

0 commit comments

Comments
 (0)