File size: 11,025 Bytes
a51f593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import math

os.makedirs('images', exist_ok=True)

candle_types = ['Bull_1', 'Bull_2', 'Bull_3', 'Bull_4', 'Bear_1', 'Bear_2', 'Bear_3', 'Bear_4']

alignments = ['GAP_UP', 'GAP_DOWN', 'ENGULFING', 'HARAMI', 'EQUAL_HIGH', 'EQUAL_LOW', 'MEETING_LINES', 'OVERLAP_UPPER', 'OVERLAP_LOWER', 'PIERCING_CLOUD', 'CONTINUATION']

def get_base_candle(c_type, base_y=50.0, body_size=10.0, wick_size=5.0):
    half_body = body_size / 2.0
    top_body = base_y + half_body
    bot_body = base_y - half_body
    
    if c_type == 'Bull_1': # H > C > O > L
        return (bot_body, top_body + wick_size, bot_body - wick_size, top_body)
    elif c_type == 'Bull_2': # C=H > O > L
        return (bot_body, top_body, bot_body - wick_size, top_body)
    elif c_type == 'Bull_3': # H > C > O=L
        return (bot_body, top_body + wick_size, bot_body, top_body)
    elif c_type == 'Bull_4': # C=H > O=L
        return (bot_body, top_body, bot_body, top_body)
    elif c_type == 'Bear_1': # H > O > C > L
        return (top_body, top_body + wick_size, bot_body - wick_size, bot_body)
    elif c_type == 'Bear_2': # H > O > C=L
        return (top_body, top_body + wick_size, bot_body, bot_body)
    elif c_type == 'Bear_3': # H=O > C > L
        return (top_body, top_body, bot_body - wick_size, bot_body)
    elif c_type == 'Bear_4': # H=O > C=L
        return (top_body, top_body, bot_body, bot_body)

def get_c2(type1, align, type2):
    c1_body_size = 10.0
    c1_wick_size = 5.0
    O1, H1, L1, C1 = get_base_candle(type1, 50.0, c1_body_size, c1_wick_size)
    top1 = max(O1, C1)
    bot1 = min(O1, C1)
    mid1 = (O1 + C1) / 2.0
    
    c2_body_size = 10.0
    c2_wick_size = 5.0
    base_y = 50.0
    
    if align == 'GAP_UP':
        base_y = H1 + c2_body_size/2.0 + c2_wick_size + 2.0
    elif align == 'GAP_DOWN':
        base_y = L1 - c2_body_size/2.0 - c2_wick_size - 2.0
    elif align == 'ENGULFING':
        c2_body_size = c1_body_size * 1.5
        c2_wick_size = c1_wick_size * 1.5
        base_y = mid1
    elif align == 'HARAMI':
        c2_body_size = c1_body_size * 0.5
        c2_wick_size = c1_wick_size * 0.5
        base_y = mid1
    elif align == 'EQUAL_HIGH':
        _, H2_test, _, _ = get_base_candle(type2, 50.0, c2_body_size, c2_wick_size)
        base_y = 50.0 + (H1 - H2_test)
    elif align == 'EQUAL_LOW':
        _, _, L2_test, _ = get_base_candle(type2, 50.0, c2_body_size, c2_wick_size)
        base_y = 50.0 + (L1 - L2_test)
    elif align == 'MEETING_LINES':
        _, _, _, C2_test = get_base_candle(type2, 50.0, c2_body_size, c2_wick_size)
        base_y = 50.0 + (C1 - C2_test)
    elif align == 'OVERLAP_UPPER':
        base_y = top1
    elif align == 'OVERLAP_LOWER':
        base_y = bot1
    elif align == 'PIERCING_CLOUD':
        c2_body_size = 18.0
        base_y = mid1 + 4.0
    elif align == 'CONTINUATION':
        O2_test, _, _, _ = get_base_candle(type2, 50.0, c2_body_size, c2_wick_size)
        base_y = 50.0 + (C1 - O2_test)
        
    return get_base_candle(type2, base_y, c2_body_size, c2_wick_size)

def is_bullish(O, C): return C > O
def is_bearish(O, C): return C < O

def determine_standard_name(c1_type, O1, H1, L1, C1, c2_type, O2, H2, L2, C2):
    is_bull1 = is_bullish(O1, C1)
    is_bear1 = is_bearish(O1, C1)
    is_bull2 = is_bullish(O2, C2)
    is_bear2 = is_bearish(O2, C2)

    mid1 = (O1 + C1) / 2.0
    names = []

    if is_bear1 and is_bull2 and (O2 <= C1 and C2 >= O1) and (O2 < C1 or C2 > O1): 
        names.append("Bullish Engulfing")
    elif is_bull1 and is_bear2 and (O2 >= C1 and C2 <= O1) and (O2 > C1 or C2 < O1):
        names.append("Bearish Engulfing")
        
    if is_bear1 and is_bull2 and (O2 >= C1 and C2 <= O1) and (O2 > C1 or C2 < O1):
        names.append("Bullish Harami")
    elif is_bull1 and is_bear2 and (O2 <= C1 and C2 >= O1) and (O2 < C1 or C2 > O1):
        names.append("Bearish Harami")
        
    if is_bear1 and is_bull2 and O2 < L1 and C2 > mid1 and C2 < O1:
        names.append("Piercing Line")
        
    if is_bull1 and is_bear2 and O2 > H1 and C2 < mid1 and C2 > O1:
        names.append("Dark Cloud Cover")
        
    if is_bear1 and is_bull2 and O2 > O1 and L2 > H1 and c1_type == 'Bear_4' and c2_type == 'Bull_4':
        names.append("Bullish Kicking")
    if is_bull1 and is_bear2 and O2 < O1 and H2 < L1 and c1_type == 'Bull_4' and c2_type == 'Bear_4':
        names.append("Bearish Kicking")
        
    if is_bear1 and is_bull2 and abs(L1 - L2) < 0.05:
        names.append("Tweezer Bottom")
    if is_bull1 and is_bear2 and abs(H1 - H2) < 0.05:
        names.append("Tweezer Top")
        
    if is_bear1 and is_bull2 and abs(C1 - C2) < 0.05 and O2 < C1:
        names.append("Bullish Meeting Lines")
    if is_bull1 and is_bear2 and abs(C1 - C2) < 0.05 and O2 > C1:
        names.append("Bearish Meeting Lines")
        
    if is_bear1 and is_bull2 and O2 < L1 and C2 < mid1 and C2 > C1:
        names.append("Thrusting")
        
    if is_bear1 and is_bull2 and O2 < L1 and abs(C2 - C1) < 0.05:
        names.append("In Neck")
        
    if is_bear1 and is_bull2 and O2 < L1 and abs(C2 - L1) < 0.05:
        names.append("On Neck")
        
    return ", ".join(names) if names else "-"

def draw_candle(ax, x, O, H, L, C):
    color = 'green' if C > O else 'red'
    ax.plot([x, x], [L, H], color=color, linewidth=2)
    top = max(O, C)
    bottom = min(O, C)
    height = top - bottom
    rect = patches.Rectangle((x - 0.3, bottom), 0.6, height, linewidth=1, edgecolor=color, facecolor=color)
    ax.add_patch(rect)

def get_pattern_desc(align):
    descriptions = {
        'GAP_UP': 'C2 completely above C1',
        'GAP_DOWN': 'C2 completely below C1',
        'ENGULFING': 'C2 body engulfs C1 body',
        'HARAMI': 'C2 body inside C1 body',
        'EQUAL_HIGH': 'Higher shadow matches exactly',
        'EQUAL_LOW': 'Lower shadow matches exactly',
        'MEETING_LINES': 'Close of C2 matches Close of C1',
        'OVERLAP_UPPER': 'C2 centered on C1 High/Open',
        'OVERLAP_LOWER': 'C2 centered on C1 Low/Close',
        'PIERCING_CLOUD': 'Large gap and heavy overlap',
        'CONTINUATION': 'Open of C2 matches Close of C1'
    }
    return descriptions.get(align, align)

def get_candle_logic(c_type, suffix):
    if c_type == 'Bull_1':
        return f"H{suffix} > C{suffix} & O{suffix} & L{suffix} <br> C{suffix} > O{suffix} & L{suffix} <br> O{suffix} > L{suffix}"
    elif c_type == 'Bull_2':
        return f"(C{suffix} = H{suffix}) > O{suffix} & L{suffix} <br> C{suffix} > O{suffix} & L{suffix} <br> O{suffix} > L{suffix}"
    elif c_type == 'Bull_3':
        return f"H{suffix} > C{suffix} & O{suffix} & L{suffix} <br> C{suffix} > O{suffix} & L{suffix} <br> O{suffix} = L{suffix}"
    elif c_type == 'Bull_4':
        return f"(C{suffix} = H{suffix}) > O{suffix} & L{suffix} <br> C{suffix} > O{suffix} & L{suffix} <br> O{suffix} = L{suffix}"
    elif c_type == 'Bear_1':
        return f"L{suffix} < C{suffix} & O{suffix} & H{suffix} <br> C{suffix} < O{suffix} & H{suffix} <br> O{suffix} < H{suffix}"
    elif c_type == 'Bear_2':
        return f"(C{suffix} = L{suffix}) < O{suffix} & H{suffix} <br> C{suffix} < O{suffix} & H{suffix} <br> O{suffix} < H{suffix}"
    elif c_type == 'Bear_3':
        return f"L{suffix} < C{suffix} & O{suffix} & H{suffix} <br> C{suffix} < O{suffix} & H{suffix} <br> O{suffix} = H{suffix}"
    elif c_type == 'Bear_4':
        return f"(C{suffix} = L{suffix}) < O{suffix} & H{suffix} <br> C{suffix} < O{suffix} & H{suffix} <br> O{suffix} = H{suffix}"
    return ""

def get_alignment_logic(align):
    if align == 'GAP_UP': return "L_C0 > H_C-1"
    if align == 'GAP_DOWN': return "H_C0 < L_C-1"
    if align == 'ENGULFING': return "(max(O_C0, C_C0) > max(O_C-1, C_C-1)) & (min(O_C0, C_C0) < min(O_C-1, C_C-1))"
    if align == 'HARAMI': return "(max(O_C0, C_C0) < max(O_C-1, C_C-1)) & (min(O_C0, C_C0) > min(O_C-1, C_C-1))"
    if align == 'EQUAL_HIGH': return "H_C0 = H_C-1"
    if align == 'EQUAL_LOW': return "L_C0 = L_C-1"
    if align == 'MEETING_LINES': return "C_C0 = C_C-1"
    if align == 'OVERLAP_UPPER': return "((O_C0 + C_C0) / 2) = max(O_C-1, C_C-1)"
    if align == 'OVERLAP_LOWER': return "((O_C0 + C_C0) / 2) = min(O_C-1, C_C-1)"
    if align == 'PIERCING_CLOUD': return "(O_C0 > H_C-1) & (C_C0 < ((O_C-1 + C_C-1)/2))"
    if align == 'CONTINUATION': return "O_C0 = C_C-1"
    return ""

def get_logic_string(c1_type, align, c2_type):
    lines1 = get_candle_logic(c1_type, "_C-1")
    lines2 = get_candle_logic(c2_type, "_C0")
    align_rule = get_alignment_logic(align)
    return f"{{ <br> {lines1} <br> {lines2} <br> {align_rule} <br> }}"

patterns = []

# Generate all patterns
for t1 in candle_types:
    for align in alignments:
        for t2 in candle_types:
            patterns.append((t1, align, t2))

total_patterns = len(patterns)
patterns_per_img = 10

markdown_lines = []
markdown_lines.append("# Complete 2-Candle Patterns")
markdown_lines.append("")
markdown_lines.append("| Code_Name | Pattern_Description | Pattern_Logic | Standard_Name | Image Reference |")
markdown_lines.append("|---|---|---|---|---|")

for i in range(0, total_patterns, patterns_per_img):
    batch = patterns[i:i+patterns_per_img]
    
    fig, axes = plt.subplots(2, 5, figsize=(20, 8))
    fig.subplots_adjust(hspace=0.4, wspace=0.3)
    axes = axes.flatten()
    
    for ax in axes:
        ax.set_visible(False)
        
    for j, (t1, align, t2) in enumerate(batch):
        ax = axes[j]
        ax.set_visible(True)
        
        O1, H1, L1, C1 = get_base_candle(t1, 50.0, 10.0, 5.0)
        O2, H2, L2, C2 = get_c2(t1, align, t2)
        
        draw_candle(ax, 1, O1, H1, L1, C1)
        draw_candle(ax, 2, O2, H2, L2, C2)
        
        min_y = min(L1, L2) - 5
        max_y = max(H1, H2) + 5
        ax.set_ylim(min_y, max_y)
        ax.set_xlim(0, 3)
        ax.set_xticks([])
        ax.set_yticks([])
        
        code_name = f"{t1}_{align}_{t2}"
        std_name = determine_standard_name(t1, O1, H1, L1, C1, t2, O2, H2, L2, C2)
        desc = get_pattern_desc(align)
        
        ax.set_title(f"{code_name}\n({std_name})", fontsize=9)
        
        img_name = f"plot_{i//patterns_per_img + 1}.png"
        logic_str = get_logic_string(t1, align, t2)
        markdown_lines.append(f"| {code_name} | {desc} | {logic_str} | {std_name} | {img_name} |")
        
    img_path = os.path.join('images', img_name)
    plt.savefig(img_path, bbox_inches='tight')
    plt.close(fig)

with open('2C_patterns.md', 'w') as f:
    f.write("\n".join(markdown_lines))

print(f"Generated {total_patterns} patterns and saved to images folder. MD written to 2C_patterns.md")