|
| 1 | +import { compilePine as compileBase } from "./compiler-v8"; |
| 2 | +import type { StrategyConfig } from "./types"; |
| 3 | + |
| 4 | +export function compilePine(config: StrategyConfig): string { |
| 5 | + let code = compileBase(config); |
| 6 | + if (!config.execution.sessionEnabled || !config.execution.showDashboard) return code; |
| 7 | + |
| 8 | + code = code.replace( |
| 9 | + /var table dashboard = table\.new\(position\.top_right, 2, (\d+), border_width=1\)/, |
| 10 | + (_match, rows) => `var table dashboard = table.new(position.top_right, 2, ${Number(rows) + 1}, border_width=1)` |
| 11 | + ); |
| 12 | + |
| 13 | + const lastResultValue = / table\.cell\(dashboard, 1, (\d+), lastRiskOutcome, text_color=lastRiskOutcome == "TARGET HIT" \? color\.lime : lastRiskOutcome == "STOP HIT" \? color\.red : lastRiskOutcome == "NONE" \? color\.gray : color\.orange\)/; |
| 14 | + const chartTfValue = / table\.cell\(dashboard, 1, (\d+), chartTimeframeOk \? "OK" : "WRONG: " \+ expectedChartTimeframe, text_color=chartTimeframeOk \? color\.lime : color\.red\)/; |
| 15 | + const match = code.match(lastResultValue) ?? code.match(chartTfValue); |
| 16 | + if (!match) throw new Error("Compiler transform anchor missing: dashboard final status row"); |
| 17 | + |
| 18 | + const sessionRow = Number(match[1]) + 1; |
| 19 | + const anchor = match[0]; |
| 20 | + const replacement = `${anchor} |
| 21 | + table.cell(dashboard, 0, ${sessionRow}, "Session") |
| 22 | + table.cell(dashboard, 1, ${sessionRow}, sessionOk ? "ACTIVE" : "CLOSED", text_color=sessionOk ? color.lime : color.gray)`; |
| 23 | + |
| 24 | + return code.replace(anchor, replacement); |
| 25 | +} |
0 commit comments