Skip to content

Commit ea4f2cc

Browse files
committed
feat: add last risk prices to dashboard
1 parent b88931f commit ea4f2cc

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

lib/compiler-v10.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,45 @@ export function compilePine(config: StrategyConfig): string {
4141
code = code.replace(oldHtfBackground, profileAwareHtfBackground);
4242
}
4343

44+
const hasVisualRisk = config.outputMode === "indicator" && (config.risk.stopMode !== "none" || config.risk.takeProfitMode !== "none");
45+
if (hasVisualRisk) {
46+
const outcomeState = 'var string lastRiskOutcome = "NONE"';
47+
const outcomeStateWithPrices = `${outcomeState}\nvar float lastOutcomeEntry = na\nvar float lastOutcomePrice = na`;
48+
if (!code.includes(outcomeState)) throw new Error("Compiler transform anchor missing: last risk outcome state");
49+
code = code.replace(outcomeState, outcomeStateWithPrices);
50+
51+
if (code.includes('lastRiskOutcome := "REVERSED"')) {
52+
code = code.replace(
53+
'lastRiskOutcome := "REVERSED"',
54+
'lastRiskOutcome := "REVERSED"\n lastOutcomeEntry := riskEntry\n lastOutcomePrice := close'
55+
);
56+
}
57+
58+
const outcomePriceAnchor = 'outcomePrice = riskAmbiguous ? math.avg(riskStop, riskTarget) : riskStopHit ? riskStop : riskTarget';
59+
if (!code.includes(outcomePriceAnchor)) throw new Error("Compiler transform anchor missing: risk outcome price");
60+
code = code.replace(
61+
outcomePriceAnchor,
62+
`${outcomePriceAnchor}\n lastOutcomeEntry := riskEntry\n lastOutcomePrice := outcomePrice`
63+
);
64+
65+
if (config.execution.showDashboard) {
66+
code = code.replace(
67+
/var table dashboard = table\.new\(position\.top_right, 2, (\d+), border_width=1\)/,
68+
(_match, rows) => `var table dashboard = table.new(position.top_right, 2, ${Number(rows) + 2}, border_width=1)`
69+
);
70+
71+
const sessionValue = / table\.cell\(dashboard, 1, (\d+), sessionOk \? "ACTIVE" : "CLOSED", text_color=sessionOk \? color\.lime : color\.gray\)/;
72+
const resultValue = / table\.cell\(dashboard, 1, (\d+), lastRiskOutcome, text_color=lastRiskOutcome == "TARGET HIT" \? color\.lime : lastRiskOutcome == "STOP HIT" \? color\.red : lastRiskOutcome == "NONE" \? color\.gray : color\.orange\)/;
73+
const match = code.match(sessionValue) ?? code.match(resultValue);
74+
if (!match) throw new Error("Compiler transform anchor missing: dashboard final risk row");
75+
76+
const entryRow = Number(match[1]) + 1;
77+
const priceRow = entryRow + 1;
78+
const anchor = match[0];
79+
const replacement = `${anchor}\n table.cell(dashboard, 0, ${entryRow}, "Last entry")\n table.cell(dashboard, 1, ${entryRow}, na(lastOutcomeEntry) ? "NONE" : str.tostring(lastOutcomeEntry, format.mintick), text_color=na(lastOutcomeEntry) ? color.gray : color.white)\n table.cell(dashboard, 0, ${priceRow}, "Result price")\n table.cell(dashboard, 1, ${priceRow}, na(lastOutcomePrice) ? "NONE" : str.tostring(lastOutcomePrice, format.mintick), text_color=lastRiskOutcome == "TARGET HIT" ? color.lime : lastRiskOutcome == "STOP HIT" ? color.red : lastRiskOutcome == "NONE" ? color.gray : color.orange)`;
80+
code = code.replace(anchor, replacement);
81+
}
82+
}
83+
4484
return code;
4585
}

0 commit comments

Comments
 (0)