Upload 2 files
Browse files- 1C_patterns_fast.md +18 -0
- 1_candle_pattern.py +147 -0
1C_patterns_fast.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Exhaustive Topological 1-Candle Patterns
|
| 2 |
+
|
| 3 |
+
**Total Patterns:** 12
|
| 4 |
+
|
| 5 |
+
| Pattern ID | Mathematical Logic |
|
| 6 |
+
|---|---|
|
| 7 |
+
| P_00000 | (O1 = H1 = L1 = C1) |
|
| 8 |
+
| P_00001 | (H1) > (O1 = L1 = C1) |
|
| 9 |
+
| P_00002 | (H1 = C1) > (O1 = L1) |
|
| 10 |
+
| P_00003 | (H1) > (C1) > (O1 = L1) |
|
| 11 |
+
| P_00004 | (O1 = H1) > (L1 = C1) |
|
| 12 |
+
| P_00005 | (O1 = H1 = C1) > (L1) |
|
| 13 |
+
| P_00006 | (H1) > (O1) > (L1 = C1) |
|
| 14 |
+
| P_00007 | (H1) > (O1 = C1) > (L1) |
|
| 15 |
+
| P_00008 | (H1 = C1) > (O1) > (L1) |
|
| 16 |
+
| P_00009 | (H1) > (C1) > (O1) > (L1) |
|
| 17 |
+
| P_00010 | (O1 = H1) > (C1) > (L1) |
|
| 18 |
+
| P_00011 | (H1) > (O1) > (C1) > (L1) |
|
1_candle_pattern.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import gc
|
| 4 |
+
import sys
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
import psutil
|
| 9 |
+
HAS_PSUTIL = True
|
| 10 |
+
except ImportError:
|
| 11 |
+
HAS_PSUTIL = False
|
| 12 |
+
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
import matplotlib.patches as patches
|
| 15 |
+
import torch
|
| 16 |
+
from tqdm import tqdm
|
| 17 |
+
from joblib import Parallel, delayed
|
| 18 |
+
|
| 19 |
+
# SOTA-Tier Hardware Configuration Target N <= 2
|
| 20 |
+
N = 1
|
| 21 |
+
V = 4 * N
|
| 22 |
+
BATCH_SIZE = max(10_000, 2_000_000 // N) if not torch.cuda.is_available() else max(20_000, 5_000_000 // N)PATTERNS_PER_IMG = 10
|
| 23 |
+
RAM_LIMIT_GB = 11.5
|
| 24 |
+
|
| 25 |
+
os.makedirs(f'images_1C', exist_ok=True)
|
| 26 |
+
|
| 27 |
+
def draw_candle(ax, x, O, H, L, C):
|
| 28 |
+
color = 'green' if C > O else 'red' if C < O else 'black'
|
| 29 |
+
ax.plot([x, x], [L, H], color=color, linewidth=2)
|
| 30 |
+
top, bottom = max(O, C), min(O, C)
|
| 31 |
+
height = max(top - bottom, 0.2) if top == bottom else (top - bottom)
|
| 32 |
+
rect_y = bottom if top != bottom else bottom - 0.1
|
| 33 |
+
ax.add_patch(patches.Rectangle((x - 0.3, rect_y), 0.6, height, linewidth=1, edgecolor=color, facecolor=color))
|
| 34 |
+
|
| 35 |
+
def get_logic_string(p):
|
| 36 |
+
labels = []
|
| 37 |
+
for i in range(1, N+1): labels.extend([f'O{i}', f'H{i}', f'L{i}', f'C{i}'])
|
| 38 |
+
groups = {}
|
| 39 |
+
for i, val in enumerate(p):
|
| 40 |
+
groups.setdefault(val, []).append(labels[i])
|
| 41 |
+
return " > ".join("(" + " = ".join(groups[val]) + ")" for val in sorted(groups.keys(), reverse=True))
|
| 42 |
+
|
| 43 |
+
def render_batch_sota(batch_idx_start, batch_patterns, images_dir):
|
| 44 |
+
fig, axes = plt.subplots(2, 5, figsize=(20, 8))
|
| 45 |
+
fig.subplots_adjust(hspace=0.5, wspace=0.3)
|
| 46 |
+
ax_array = axes.flatten()
|
| 47 |
+
batch_results = []
|
| 48 |
+
img_name = f"plot_{batch_idx_start//PATTERNS_PER_IMG + 1}.png"
|
| 49 |
+
|
| 50 |
+
for ax in ax_array: ax.set_visible(False)
|
| 51 |
+
for j, p in enumerate(batch_patterns):
|
| 52 |
+
ax = ax_array[j]
|
| 53 |
+
ax.set_visible(True)
|
| 54 |
+
scale = 5.0
|
| 55 |
+
for k in range(N):
|
| 56 |
+
draw_candle(ax, k+1, p[k*4]*scale, p[k*4+1]*scale, p[k*4+2]*scale, p[k*4+3]*scale)
|
| 57 |
+
ax.set_ylim(-5, V*scale + 5)
|
| 58 |
+
ax.set_xlim(0, N+1)
|
| 59 |
+
ax.set_xticks([]); ax.set_yticks([])
|
| 60 |
+
|
| 61 |
+
pattern_id = f"P_{batch_idx_start+j:05d}"
|
| 62 |
+
logic_str = get_logic_string(p)
|
| 63 |
+
ax.set_title(f"{pattern_id}", fontsize=10)
|
| 64 |
+
ax.text(0.5, -0.1, logic_str, transform=ax.transAxes, fontsize=max(3, 10 - len(logic_str)//20), ha='center', va='top', wrap=True)
|
| 65 |
+
batch_results.append(f"| {pattern_id} | {logic_str} | {img_name} |")
|
| 66 |
+
|
| 67 |
+
img_path = os.path.join(images_dir, img_name)
|
| 68 |
+
fig.savefig(img_path, bbox_inches='tight')
|
| 69 |
+
plt.close(fig)
|
| 70 |
+
return batch_results
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
print(f"--- SOTA Visual Pattern Engine (EXHAUSTIVE EXACT): 1-candle ---")
|
| 74 |
+
start_time = time.time()
|
| 75 |
+
|
| 76 |
+
valid_single_candles = []
|
| 77 |
+
for h in range(V):
|
| 78 |
+
for l in range(h + 1):
|
| 79 |
+
for o in range(l, h + 1):
|
| 80 |
+
for c in range(l, h + 1):
|
| 81 |
+
valid_single_candles.append((o, h, l, c))
|
| 82 |
+
|
| 83 |
+
M = len(valid_single_candles)
|
| 84 |
+
total_permutations = M ** N
|
| 85 |
+
print(f"Combinations: {total_permutations:,} | Initializing VRAM/RAM Context...")
|
| 86 |
+
|
| 87 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 88 |
+
base_tensor = torch.tensor(valid_single_candles, dtype=torch.int16, device=device)
|
| 89 |
+
powers = (M ** torch.arange(N-1, -1, -1, device=device)).unsqueeze(0)
|
| 90 |
+
|
| 91 |
+
global_unique_chunks = []
|
| 92 |
+
limit_hit = False
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
with tqdm(total=total_permutations, desc="Discovery Phase") as pbar:
|
| 96 |
+
for start_idx in range(0, total_permutations, BATCH_SIZE):
|
| 97 |
+
end_idx = min(start_idx + BATCH_SIZE, total_permutations)
|
| 98 |
+
curr_b = end_idx - start_idx
|
| 99 |
+
|
| 100 |
+
batch_idx = torch.arange(start_idx, end_idx, device=device).unsqueeze(1)
|
| 101 |
+
comb_idx = (batch_idx // powers) % M
|
| 102 |
+
candles = base_tensor[comb_idx].view(curr_b, 4 * N)
|
| 103 |
+
|
| 104 |
+
# Fast PyTorch Dense Ranking
|
| 105 |
+
sorted_c, indices = torch.sort(candles, dim=1)
|
| 106 |
+
diffs = torch.cat([torch.ones(curr_b, 1, device=device, dtype=torch.int16), (sorted_c[:, 1:] > sorted_c[:, :-1]).to(torch.int16)], dim=1)
|
| 107 |
+
cum_ranks = torch.cumsum(diffs, dim=1) - 1
|
| 108 |
+
ranks = torch.empty_like(candles)
|
| 109 |
+
ranks.scatter_(1, indices, cum_ranks.to(torch.int16))
|
| 110 |
+
|
| 111 |
+
global_unique_chunks.append(torch.unique(ranks, dim=0).cpu())
|
| 112 |
+
|
| 113 |
+
if len(global_unique_chunks) > 10:
|
| 114 |
+
merged = torch.cat(global_unique_chunks, dim=0)
|
| 115 |
+
global_unique_chunks = [torch.unique(merged, dim=0)]
|
| 116 |
+
pbar.update(curr_b)
|
| 117 |
+
if HAS_PSUTIL and psutil.virtual_memory().used / (1024**3) > RAM_LIMIT_GB:
|
| 118 |
+
limit_hit = True; break
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"Error: {e}"); limit_hit = True
|
| 122 |
+
|
| 123 |
+
final_patterns_tensor = torch.unique(torch.cat(global_unique_chunks, dim=0), dim=0) if global_unique_chunks else torch.empty((0, 4*N))
|
| 124 |
+
patterns = final_patterns_tensor.tolist()
|
| 125 |
+
total_patterns = len(patterns)
|
| 126 |
+
print(f"Found {total_patterns} exact topological patterns in {time.time()-start_time:.2f}s.")
|
| 127 |
+
|
| 128 |
+
images_dir = f'images_1C'
|
| 129 |
+
render_tasks = [(i, patterns[i:i+PATTERNS_PER_IMG], images_dir) for i in range(0, total_patterns, PATTERNS_PER_IMG)]
|
| 130 |
+
|
| 131 |
+
md_rows = Parallel(n_jobs=-1, backend="loky")(
|
| 132 |
+
delayed(render_batch_sota)(*t) for t in tqdm(render_tasks, desc="SOTA Parallel Render")
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
markdown_lines = [
|
| 136 |
+
f"# Exhaustive Topological 1-Candle Patterns\n",
|
| 137 |
+
f"**Total unique combinations found:** {total_patterns}\n",
|
| 138 |
+
"| Pattern ID | Mathematical Logic | Image Reference |\n",
|
| 139 |
+
"|---|---|---|"
|
| 140 |
+
]
|
| 141 |
+
if limit_hit: markdown_lines.insert(2, "*OOM Limitation protection triggered!*\n")
|
| 142 |
+
for row_batch in md_rows: markdown_lines.extend(row_batch)
|
| 143 |
+
|
| 144 |
+
with open(f'1C_patterns.md', 'w') as f:
|
| 145 |
+
f.write("\n".join(markdown_lines))
|
| 146 |
+
|
| 147 |
+
print(f"SUCCESS! Total Time: {time.time() - start_time:.2f}s | Results in 1C_patterns.md")
|