#!/usr/bin/env python3 """Buy Doji — aceeași regulă pe trei perechi de timeframe-uri. Citește `gen/strategii/buy_doji_perechi_tf.spec.txt` și scrie `gen/strategii/buy_doji_perechi_tf.svg`. Trei rânduri (weekly→daily, monthly→weekly, daily→60 min); pe fiecare, stânga = TF mare (11 candele, buy doji + confirmare), dreapta = TF mic (15 candele, pullback de 8 candele la high-ul dojiului mare, doji acolo, confirmare, intrare). Rândul A e seria din spec; B și C se CALCULEAZĂ din A prin p' = round(100 + f*(p - 100), 2), cu f = 2.5 și 0.4. Cifrele din desen ies din serii, iar assert-urile le compară cu valorile afirmate în spec. Coordonatele sunt absolute (fără ), ca audit_diagrams.py să vadă pozițiile reale ale textelor. Rulare: python3 gen/strategii/buy_doji_perechi_tf.py """ from pathlib import Path OUT = Path(__file__).with_suffix(".svg") W = 720 # --- seria A din spec: (open, high, low, close) ---------------------------- BIG_A = [(122.0, 122.6, 119.4, 120.0), (120.0, 120.5, 117.0, 117.5), (117.5, 119.0, 117.1, 118.4), (118.4, 118.8, 114.3, 114.8), (114.8, 115.3, 111.5, 112.0), (112.0, 113.4, 111.6, 112.9), (112.9, 113.2, 109.1, 109.6), (109.6, 110.0, 106.3, 106.8), (106.8, 107.2, 103.6, 104.0), (104.0, 105.2, 101.9, 103.8), # 9: doji (103.8, 107.4, 103.5, 107.0)] # 10: confirmare SMALL_A = [(107.0, 107.2, 106.4, 106.6), (106.6, 106.8, 106.0, 106.1), (106.1, 106.6, 105.95, 106.4), (106.4, 106.5, 105.8, 105.9), (105.9, 106.0, 105.5, 105.6), (105.6, 105.95, 105.5, 105.8), (105.8, 105.9, 105.4, 105.5), (105.5, 105.6, 105.25, 105.35), (105.35, 105.75, 105.10, 105.40), # 8: doji la nivel (105.40, 106.40, 105.30, 106.30), # 9: confirmare => intrare (106.3, 107.0, 106.2, 106.9), (106.9, 107.5, 106.7, 107.4), (107.4, 107.55, 107.0, 107.2), (107.2, 108.0, 107.1, 107.9), (107.9, 108.6, 107.8, 108.5)] def scaled(bars, f): return [tuple(round(100 + f * (p - 100), 2) for p in b) for b in bars] # key, f, TF mare, TF mic, text sub rând, valori așteptate din spec ROWS = [ ("A", 1.0, "weekly", "daily", "stop {p:.2f} % (pe weekly ar fi {pb:.2f} %) · 8 candele = 8 zile de bursă", dict(dh=105.20, dl=101.90, conf=107.00, sdl=105.10, entry=106.30, sl=105.10, risk=1.20, pct=1.13, tp=107.50, b_entry=107.00, b_sl=101.90, b_risk=5.10, b_pct=4.77)), ("B", 2.5, "monthly", "weekly", "stop {p:.2f} % (pe monthly ar fi {pb:.2f} %) · 8 candele = 8 săptămâni", dict(dh=113.00, dl=104.75, conf=117.50, sdl=112.75, entry=115.75, sl=112.75, risk=3.00, pct=2.59, tp=118.75, b_entry=117.50, b_sl=104.75, b_risk=12.75, b_pct=10.85)), ("C", 0.4, "daily", "60 min", "stop {p:.2f} % (pe daily ar fi {pb:.2f} %) · 8 candele ≈ 1,2 zile de bursă (7 bare/zi)", dict(dh=102.08, dl=100.76, conf=102.80, sdl=102.04, entry=102.52, sl=102.04, risk=0.48, pct=0.47, tp=103.00, b_entry=102.80, b_sl=100.76, b_risk=2.04, b_pct=1.98)), ] def eq(a, b): return abs(a - b) < 0.005 def check(key, big, small, exp): """Assert-urile obligatorii din spec, pe seriile calculate; întoarce nivelurile.""" for name, bars in (("mare", big), ("mic", small)): for i, (o, h, l, c) in enumerate(bars): assert h >= max(o, c) and l <= min(o, c), f"{key} {name}[{i}] h/l incoerent" if i: assert eq(o, bars[i - 1][3]), f"{key} {name}[{i}] open != close precedent" assert len(big) == 11 and len(small) == 15 assert eq(small[0][0], big[10][3]), f"{key}: open mic[0] != close mare[10]" for name, (o, h, l, c) in (("doji mare", big[9]), ("doji mic", small[8])): assert abs(o - c) <= 0.10 * (h - l) + 1e-9, f"{key}: {name} nu e doji" assert big[10][3] > big[9][1] and small[9][3] > small[8][1], f"{key}: fără confirmare" sl = min(b[2] for b in small[6:10]) assert eq(sl, small[8][2]), f"{key}: SL nu e low-ul dojiului mic" entry = small[9][3] risk = round(entry - sl, 2) tp = round(entry + risk, 2) first = next(i for i in range(10, 15) if small[i][1] >= tp - 1e-9) assert first == 11, f"{key}: TP1 atins la {first}, nu la 11" pct = risk / entry * 100 b_entry, b_sl = big[10][3], big[9][2] b_risk = round(b_entry - b_sl, 2) b_pct = b_risk / b_entry * 100 got = dict(dh=big[9][1], dl=big[9][2], conf=big[10][3], sdl=small[8][2], entry=entry, sl=sl, risk=risk, pct=pct, tp=tp, b_entry=b_entry, b_sl=b_sl, b_risk=b_risk, b_pct=b_pct) for k, v in exp.items(): assert eq(round(got[k], 2), v), f"{key}: {k} = {got[k]:.4f}, spec {v}" assert abs(small[8][2] - big[9][1]) / big[9][1] <= 0.005, f"{key}: doji mic nu e la nivel" assert 4 <= b_pct / pct <= 5, f"{key}: raport stopuri {b_pct / pct:.2f} nu e 4-5x" return got # --- desen ------------------------------------------------------------------ GREEN, RED, GRAY, INK = "#188a3e", "#c00", "#888", "#333" PLOT_H = 160 # înălțimea zonei de preț, egală pe toate graficele BX0, BGAP = 40, 16 # TF mare: x candele SX0, SGAP = 392, 15 # TF mic BODY_W = 9 SL_X1, SL_X2 = 380, 614 # liniile de nivel pe graficul mic COL_X = 620 # coloana cu etichetele de nivel ale graficului mic out = [] def text(x, y, s, fill=INK, anchor="start", bold=False, size=None): st = [] if bold: st.append("font-weight:bold") if size: st.append(f"font-size:{size}px") style = f' style="{";".join(st)}"' if st else "" a = f' text-anchor="{anchor}"' if anchor != "start" else "" out.append(f'{s}') def hline(x1, x2, y, color, dash): out.append(f'') def candles(bars, x0, gap, yf): for i, (o, h, l, c) in enumerate(bars): cx = x0 + i * gap color = GREEN if c >= o else RED out.append(f'') top = yf(max(o, c)) hh = max(yf(min(o, c)) - top, 1.6) out.append(f'') out.append('' '') text(20, 22, "Aceeași regulă pe trei perechi — exemplu construit (fără ticker)", bold=True, size=14) def zone(x0, y0_, x1, y1, color): out.append(f'') def bracket(x, ya, yb_, color, label): """Paranteză verticală între două prețuri, cu eticheta la dreapta.""" out.append(f'') text(x + 5, (ya + yb_) / 2 + 4, label, fill=color, bold=True) y0 = 40 for key, f, tf_big, tf_small, cap, exp in ROWS: big, small = scaled(BIG_A, f), scaled(SMALL_A, f) g = check(key, big, small, exp) first = key == "A" # titlu rând + subtitluri text(20, y0 + 14, f"{key}. {tf_big} → {tf_small}", bold=True, size=13) text(BX0 - 16, y0 + 32, f"{tf_big}: direcția", fill="#555") text(SL_X1, y0 + 32, f"{tf_small}: intrarea", fill="#555") top = y0 + 50 # scară proprie pe fiecare grafic, constantă în interiorul lui bmax = max(b[1] for b in big); bmin = min(b[2] for b in big) smax = max(b[1] for b in small); smin = min(b[2] for b in small) bs, ss = PLOT_H / (bmax - bmin), PLOT_H / (smax - smin) yb = lambda p, t=top, m=bmax, s=bs: t + (m - p) * s # noqa: E731 ys = lambda p, t=top, m=smax, s=ss: t + (m - p) * s # noqa: E731 # TF mare: zonă peste doji + confirmare, eticheta dedesubt zx0, zx1 = BX0 + 9 * BGAP - 9, BX0 + 10 * BGAP + 9 zone(zx0, yb(big[10][1]) - 5, zx1, yb(g["dl"]) + 5, GREEN) text((zx0 + zx1) / 2, yb(g["dl"]) + 20, "doji + confirmare:", fill=GREEN, anchor="middle", bold=True) text((zx0 + zx1) / 2, yb(g["dl"]) + 34, "aici se decide direcția", fill=GREEN, anchor="middle", bold=True) hline(BX0 - 16, BX0 + 10 * BGAP + 12, yb(g["dh"]), GRAY, "3 3") text(BX0 - 14, yb(g["dh"]) + 14, f"high doji {tf_big} {g['dh']:.2f}", fill="#666") candles(big, BX0, BGAP, yb) # săgeata-zoom: de la close-ul confirmării mari la open-ul primei bare mici (același preț) x1, y1 = BX0 + 10 * BGAP + 8, yb(g["conf"]) x2, y2 = SX0 - 10, ys(g["conf"]) out.append(f'') text(290, y1 + 2, "apoi cobori", fill="#555") text(290, y1 + 16, "pe graficul mic", fill="#555") # TF mic: niveluri y_dh, y_e, y_sl, y_tp = ys(g["dh"]), ys(g["entry"]), ys(g["sl"]), ys(g["tp"]) assert abs((y_sl - y_e) / (y_e - y_tp) - 1) < 1e-6, f"{key}: SL-intrare != intrare-TP1 în px" # zonă peste doji-ul mic + confirmare, eticheta dedesubt sx0, sx1 = SX0 + 8 * SGAP - 9, SX0 + 9 * SGAP + 9 zone(sx0, y_e - 6, sx1, y_sl + 5, GREEN) text((sx0 + sx1) / 2, y_sl + 20, "doji la nivel:", fill=GREEN, anchor="middle", bold=True) text((sx0 + sx1) / 2, y_sl + 34, "aici intri", fill=GREEN, anchor="middle", bold=True) hline(SL_X1, SL_X2, y_dh, GRAY, "3 3") hline(SL_X1, SL_X2, y_e, GREEN, "6 3") hline(SL_X1, SL_X2, y_sl, RED, "6 3") hline(SL_X1, SL_X2, y_tp, GREEN, "2 3") text(COL_X, y_tp + 4, f"ținta 1:1 {g['tp']:.2f}", fill=GREEN, size=11) text(COL_X, y_e + 4, f"intrare {g['entry']:.2f}", fill=GREEN, size=11) text(COL_X, y_dh - 5, f"high doji {g['dh']:.2f}", fill="#666", size=11) text(COL_X, y_sl + 12, f"stop {g['sl']:.2f}", fill=RED, size=11) if first: text(COL_X, y_sl + 25, "sub ultimele", fill=RED, size=11) text(COL_X, y_sl + 38, "4 candele", fill=RED, size=11) candles(small, SX0, SGAP, ys) if first: # paranteză peste pullback-ul de 8 candele (barele 0..7), doar pe primul rând yb8 = y_tp - 12 xa, xb = SX0 - 5, SX0 + 7 * SGAP + 5 out.append(f'') text((xa + xb) / 2, yb8 - 5, "pullback: 8 candele", fill="#555", anchor="middle") # parantezele de stop: același setup, măsurat pe TF mare și pe TF mic bx = zx1 + 8 bracket(bx, yb(g["b_entry"]), yb(g["b_sl"]), RED, f"stop {g['b_pct']:.2f} %".replace(".", ",")) kx = SX0 + 9 * SGAP + 14 bracket(kx, y_e, y_sl, RED, f"stop {g['pct']:.2f} %".replace(".", ",")) ratio = g["b_pct"] / g["pct"] assert 4 <= ratio < 4.5, f"raportul stopurilor {ratio:.2f} nu e „de 4 ori”" # săgeata dintre ele, sub rând ya = top + PLOT_H + 58 out.append(f'') text((bx + kx) / 2 + 20, ya + 17, "același setup, stop de 4 ori mai mic", fill=RED, anchor="middle", bold=True) text((bx + kx) / 2 + 20, ya + 31, "→ de 4 ori mai multe acțiuni la același risc", fill=RED, anchor="middle", bold=True) print(f"{key}: doji mare {g['dh']:.2f}/{g['dl']:.2f} conf {g['conf']:.2f} | intrare {g['entry']:.2f} " f"SL {g['sl']:.2f} risc {g['risk']:.2f} ({g['pct']:.2f} %) TP1 {g['tp']:.2f} | " f"doar mare: risc {g['b_risk']:.2f} ({g['b_pct']:.2f} %)") y0 += 320 if first else 270 if key != "C": out.append(f'') H = y0 - 20 svg = (f'\n ' + "\n ".join(out) + "\n") assert "\n\n" not in svg assert ".md:" not in svg and "Surse" not in svg, "trimiteri la surse în SVG" OUT.write_text(svg, encoding="utf-8") print(f"scris {OUT} ({W}x{H:.0f})")