| 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'] |
|
|
| 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': |
| return (bot_body, top_body + wick_size, bot_body - wick_size, top_body) |
| elif c_type == 'Bull_2': |
| return (bot_body, top_body, bot_body - wick_size, top_body) |
| elif c_type == 'Bull_3': |
| return (bot_body, top_body + wick_size, bot_body, top_body) |
| elif c_type == 'Bull_4': |
| return (bot_body, top_body, bot_body, top_body) |
| elif c_type == 'Bear_1': |
| return (top_body, top_body + wick_size, bot_body - wick_size, bot_body) |
| elif c_type == 'Bear_2': |
| return (top_body, top_body + wick_size, bot_body, bot_body) |
| elif c_type == 'Bear_3': |
| return (top_body, top_body, bot_body - wick_size, bot_body) |
| elif c_type == 'Bear_4': |
| 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 |
| |
| 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' |
| } |
| 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))" |
| 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 = [] |
|
|
| |
| 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") |
|
|