"""Synthetic verification. Native MetaEditor compilation is a separate check.""" import csv import importlib.util import math from pathlib import Path import shutil import subprocess import tempfile from datetime import datetime, timedelta ROOT = Path(__file__).resolve().parents[1] spec = importlib.util.spec_from_file_location('zones', ROOT/'python/zone_reference.py') zones = importlib.util.module_from_spec(spec) spec.loader.exec_module(zones) def bars(n=240): result = [] start = datetime(2025, 1, 1) for i in range(n): price = 2000+5*math.sin(i/6)+math.sin(i/2) result.append(dict(time=(start+timedelta(minutes=i)).isoformat(' '), open=price-.1, close=price, high=price+.7, low=price-.8)) return result def body(source, name): start = source.index(('bool ' if name == 'SRPivot' else 'double ' if name == 'SRHalfWidth' else 'int ')+name+'(') opening = source.index('{', start) depth, i = 1, opening+1 while depth: depth += (source[i] == '{')-(source[i] == '}') i += 1 return source[start:i] def main(): sample = bars() expected = zones.detect(sample, show_broken=True, max_zones=100) assert expected for z in expected: assert z['available'] > z['pivot'] assert z['lower'] < z['upper'] assert not z['broken'] or z['ended'] > z['available'] forming_changed = [dict(b) for b in sample] forming_changed[-1].update(high=999999, low=.01, close=123) assert zones.detect(forming_changed, show_broken=True, max_zones=100) == expected small = bars(6) for b in small: b.update(open=10, close=10, high=11, low=9) small[2]['low'] = 8 assert zones.detect(small[:-1], period=1) == [] one = zones.detect(small, period=1) assert len(one) == 1 and one[0]['pivot'] == small[2]['time'] assert one[0]['available'] == small[5]['time'] tied = [dict(b) for b in small] tied[1]['low'] = 8 assert zones.detect(tied, period=1) == [] assert len(zones.detect(sample, max_zones=3, show_broken=True)) == 3 with tempfile.TemporaryDirectory() as directory: directory = Path(directory) f = directory/'bars.csv' with f.open('w', newline='') as out: writer = csv.writer(out, delimiter='\t') writer.writerow(['','