#!/usr/bin/env python3 import json import urllib.request import subprocess import sys def scan(sid): bars = json.loads(urllib.request.urlopen( "http://localhost:8080/api/candles/history?market=KRW-BTC&type=3m&count=300" ).read()) payload = { "market": "KRW-BTC", "strategyId": sid, "timeframe": "3m", "evaluationBarCount": 200, "bars": [ { "time": b["time"], "open": b["open"], "high": b["high"], "low": b["low"], "close": b["close"], "volume": b["volume"], } for b in bars ], } req = urllib.request.Request( "http://localhost:8080/api/strategy/live-conditions/scan-signals", json.dumps(payload).encode(), headers={ "Content-Type": "application/json", "x-user-id": "1", "x-device-id": "202b4020-2b4d-43f0-8baa-72513df2eb9e", }, method="POST", ) return len(json.loads(urllib.request.urlopen(req).read())) def get_json(): out = subprocess.check_output([ "docker", "exec", "gc-mysql", "mysql", "-ustock", "-panalyzer", "stockAnalyzer", "-N", "-e", "SELECT buy_condition_json FROM gc_strategy WHERE id=195", ]) return out.decode().strip() def set_json(js: str): # pass via stdin to mysql to avoid escaping hell sql = "UPDATE gc_strategy SET buy_condition_json=%s WHERE id=195" proc = subprocess.Popen( ["docker", "exec", "-i", "gc-mysql", "mysql", "-ustock", "-panalyzer", "stockAnalyzer"], stdin=subprocess.PIPE, ) proc.communicate(f"UPDATE gc_strategy SET buy_condition_json='{js.replace(chr(39), chr(39)+chr(39))}' WHERE id=195;".encode()) def main(): orig = get_json() j = json.loads(orig) variants = {} c50 = json.loads(json.dumps(j)) c50["children"][0]["condition"]["rightField"] = "K_50" c50["children"][0]["condition"]["targetValue"] = 50 c50["children"][0]["condition"]["thresholdOverride"] = True variants["K_50"] = json.dumps(c50, ensure_ascii=False) cH = json.loads(json.dumps(j)) cond = cH["children"][0]["condition"] cond["rightField"] = "HL_MID" cond.pop("targetValue", None) cond["thresholdOverride"] = False variants["HL_MID"] = json.dumps(cH, ensure_ascii=False) c55n = json.loads(json.dumps(j)) c55n["children"][0]["condition"]["thresholdOverride"] = False variants["K_55_no_override"] = json.dumps(c55n, ensure_ascii=False) variants["K_55_orig"] = orig for name, js in variants.items(): set_json(js) n = scan(195) print(f"{name}: {n} signals") set_json(orig) print("restored original") if __name__ == "__main__": main()