Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yfinance
pytest
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
pandas
matplotlib
45 changes: 45 additions & 0 deletions tests/TestAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from tradingpatterns.hard_data import generate_sample_df_with_pattern
from tradingpatterns.tradingpatterns import detect_patterns, plot_patterns

def test_detect_patterns():
# Generate data with different patterns
df_head_shoulder = generate_sample_df_with_pattern("Head and Shoulder")
df_inv_shoulder = generate_sample_df_with_pattern("Inverse Head and Shoulder")
df_double_top = generate_sample_df_with_pattern("Double Top")
df_double_bottom = generate_sample_df_with_pattern("Double Bottom")
df_triple_top = generate_sample_df_with_pattern("Triple Top")
df_triple_bottom = generate_sample_df_with_pattern("Triple Bottom")
df_at = generate_sample_df_with_pattern("Ascending Triangle")
df_dt = generate_sample_df_with_pattern("Descending Triangle")

plot_patterns(df_head_shoulder)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do let me know if you are working on improving the plotting function based on detection rather than just plotting the candlesticks

plot_patterns(df_inv_shoulder)
plot_patterns(df_double_top)
plot_patterns(df_double_bottom)
plot_patterns(df_triple_top)
plot_patterns(df_triple_bottom)
plot_patterns(df_at)
plot_patterns(df_dt)


# Detect patterns
df_with_head_shoulder_detection = detect_patterns(df_head_shoulder)
df_with_inv_shoulder_detection = detect_patterns(df_inv_shoulder)
df_with_double_top_detection = detect_patterns(df_double_top)
df_with_double_bottom_detection = detect_patterns(df_double_bottom)
df_with_triple_top_detection = detect_patterns(df_triple_top)
df_with_triple_bottom_detection = detect_patterns(df_triple_bottom)
df_with_at_detection = detect_patterns(df_at)
df_with_dt_detection = detect_patterns(df_dt)

# Assert that the detected patterns are present in the respective DataFrames
assert "Head and Shoulder" in df_with_head_shoulder_detection['head_shoulder_pattern'].values
assert "Inverse Head and Shoulder" in df_with_inv_shoulder_detection['head_shoulder_pattern'].values
assert "Double Top" in df_with_double_top_detection['double_pattern'].values
assert "Double Bottom" in df_with_double_bottom_detection['double_pattern'].values
#assert "Triple Top" in df_with_triple_top_detection['double_pattern'].values
#assert "Triple Bottom" in df_with_triple_bottom_detection['pattern'].values
assert "Ascending Triangle" in df_with_at_detection['triangle_pattern'].values
#assert "Descending Triangle" in df_with_dt_detection['triangle_pattern'].values

#Now you should modify the detect_patterns function in the tradingpatterns.tradingpatterns module to detect all the patterns mentioned: Head and Shoulder, Inverse Head and Shoulder, Double Top, Double Bottom, Triple Top, and Triple Bottom. The function should add a new column called 'pattern' to the DataFrame with the detected pattern.
31 changes: 31 additions & 0 deletions tests/ascending_triangle.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
date,Price
1,100.00
2,101.00
3,102.00
4,103.00
5,104.00
6,103.50
7,104.00
8,104.50
9,105.00
10,105.50
11,105.75
12,106.00
13,106.25
14,106.50
15,106.75
16,107.00
17,107.25
18,107.50
19,107.75
20,108.00
21,108.25
22,108.50
23,108.75
24,109.00
25,109.50
26,110.00
27,110.50
28,111.00
29,111.50
30,112.00
30 changes: 30 additions & 0 deletions tests/ascending_triangle_explained.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1 | 100.00
2 | 101.00
3 | 102.00
4 | 103.00
5 | 104.00 (Upper trendline)
6 | 103.50
7 | 104.00 (Upper trendline)
8 | 104.50
9 | 105.00
10 | 105.50 (Upper trendline)
11 | 105.75
12 | 106.00
13 | 106.25
14 | 106.50 (Upper trendline)
15 | 106.75
16 | 107.00
17 | 107.25
18 | 107.50
19 | 107.75 (Upper trendline)
20 | 108.00
21 | 108.25
22 | 108.50
23 | 108.75
24 | 109.00 (Breakout)
25 | 109.50
26 | 110.00
27 | 110.50
28 | 111.00
29 | 111.50
30 | 112.00
31 changes: 31 additions & 0 deletions tests/descending_triangle.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
date,Price
1,100.00
2,99.00
3,98.00
4,97.00
5,96.00
6,97.00
7,96.00
8,95.00
9,94.00
10,93.00
11,94.00
12,93.00
13,92.00
14,91.00
15,92.00
16,91.00
17,90.00
18,89.00
19,88.00
20,89.00
21,88.00
22,87.00
23,86.00
24,85.00
25,84.00
26,83.00
27,82.00
28,81.00
29,80.00
30,79.00
30 changes: 30 additions & 0 deletions tests/descending_triangle_explained.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1 | 100.00
2 | 99.00
3 | 98.00
4 | 97.00
5 | 96.00 (Lower trendline)
6 | 97.00
7 | 96.00 (Lower trendline)
8 | 95.00
9 | 94.00
10 | 93.00 (Lower trendline)
11 | 94.00
12 | 93.00
13 | 92.00
14 | 91.00 (Lower trendline)
15 | 92.00
16 | 91.00
17 | 90.00
18 | 89.00
19 | 88.00 (Lower trendline)
20 | 89.00
21 | 88.00
22 | 87.00
23 | 86.00
24 | 85.00 (Breakdown)
25 | 84.00
26 | 83.00
27 | 82.00
28 | 81.00
29 | 80.00
30 | 79.00
31 changes: 31 additions & 0 deletions tests/double_bottom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
date,Price
1,100.00
2,102.50
3,105.00
4,107.50
5,110.00
6,107.50
7,105.00
8,107.50
9,110.00
10,107.50
11,105.00
12,102.50
13,100.00
14,97.50
15,95.00
16,92.50
17,90.00
18,92.50
19,95.00
20,92.50
21,90.00
22,87.50
23,85.00
24,82.50
25,80.00
26,77.50
27,75.00
28,72.50
29,70.00
30,67.50
30 changes: 30 additions & 0 deletions tests/double_bottom_explained.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1 | 100.00
2 | 102.50
3 | 105.00
4 | 107.50
5 | 110.00 (1st Top)
6 | 107.50
7 | 105.00
8 | 107.50
9 | 110.00 (2nd Top)
10 | 107.50
11 | 105.00
12 | 102.50
13 | 100.00
14 | 97.50
15 | 95.00
16 | 92.50
17 | 90.00 (Neckline)
18 | 92.50
19 | 95.00
20 | 92.50
21 | 90.00 (Break below neckline)
22 | 87.50
23 | 85.00
24 | 82.50
25 | 80.00
26 | 77.50
27 | 75.00
28 | 72.50
29 | 70.00
30 | 67.50
31 changes: 31 additions & 0 deletions tests/double_top.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
date,Price
2023-01-01,100
2023-01-02,102
2023-01-03,105
2023-01-04,110
2023-01-05,120
2023-01-06,130
2023-01-07,135
2023-01-08,130
2023-01-09,125
2023-01-10,120
2023-01-11,115
2023-01-12,110
2023-01-13,105
2023-01-14,102
2023-01-15,100
2023-01-16,102
2023-01-17,105
2023-01-18,110
2023-01-19,120
2023-01-20,130
2023-01-21,135
2023-01-22,130
2023-01-23,125
2023-01-24,120
2023-01-25,115
2023-01-26,110
2023-01-27,105
2023-01-28,100
2023-01-29,95
2023-01-30,90
20 changes: 20 additions & 0 deletions tests/test_gold_from_yfinance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yfinance as yf
from tradingpatterns.tradingpatterns import detect_head_shoulder
import datetime as dt


def test_gold_yfinance_long_term():
ticker = yf.Ticker("GOLD")
history = ticker.history(period="1mo")
df_with_detection = detect_head_shoulder(history)
assert "Head and Shoulder" in df_with_detection['head_shoulder_pattern'].values


def test_tesla_2_hours():

start_date = dt.datetime.fromisoformat('2023-05-04T02:51:56.734028')
end_date = dt.datetime.fromisoformat('2023-05-04T04:51:56.734028')

gold_data = yf.download("TSLA", start=start_date, end=end_date, interval="15m")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the name of variable is correct and also cross verify if the start and end params are working

df_with_detection = detect_head_shoulder(gold_data)
assert "Head and Shoulder" in df_with_detection['head_shoulder_pattern'].values
41 changes: 41 additions & 0 deletions tests/triple_bottom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
date,Price
1,100.00
2,102.50
3,105.00
4,107.50
5,110.00
6,107.50
7,105.00
8,107.50
9,110.00
10,107.50
11,105.00
12,107.50
13,110.00
14,107.50
15,105.00
16,102.50
17,100.00
18,97.50
19,95.00
20,92.50
21,90.00
22,92.50
23,95.00
24,92.50
25,90.00
26,87.50
27,85.00
28,82.50
29,80.00
30,77.50
31,75.00
32,72.50
33,70.00
34,67.50
35,65.00
36,62.50
37,60.00
38,57.50
39,55.00
40,52.50
40 changes: 40 additions & 0 deletions tests/triple_bottom_explained.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1 | 100.00
2 | 102.50
3 | 105.00
4 | 107.50
5 | 110.00 (1st Top)
6 | 107.50
7 | 105.00
8 | 107.50
9 | 110.00 (2nd Top)
10 | 107.50
11 | 105.00
12 | 107.50
13 | 110.00 (3rd Top)
14 | 107.50
15 | 105.00
16 | 102.50
17 | 100.00
18 | 97.50
19 | 95.00
20 | 92.50
21 | 90.00 (Neckline)
22 | 92.50
23 | 95.00
24 | 92.50
25 | 90.00 (Break below neckline)
26 | 87.50
27 | 85.00
28 | 82.50
29 | 80.00
30 | 77.50
31 | 75.00
32 | 72.50
33 | 70.00
34 | 67.50
35 | 65.00
36 | 62.50
37 | 60.00
38 | 57.50
39 | 55.00
40 | 52.50
Loading