|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { compilePine } from "../lib/compiler"; |
| 3 | +import { presets } from "../lib/presets"; |
| 4 | + |
| 5 | +const clone = <T,>(value: T): T => JSON.parse(JSON.stringify(value)); |
| 6 | + |
| 7 | +describe("strategy ATR entry freeze", () => { |
| 8 | + it("captures ATR once for Fast EMA Scalper strategy entries", () => { |
| 9 | + const preset = presets.find((item) => item.name === "Fast EMA Scalper"); |
| 10 | + expect(preset).toBeDefined(); |
| 11 | + |
| 12 | + const config = clone(preset!); |
| 13 | + config.outputMode = "strategy"; |
| 14 | + const code = compilePine(config); |
| 15 | + |
| 16 | + expect(code).toContain("var float strategyAtrAtEntry = na"); |
| 17 | + expect(code).toContain("if longSignal and strategy.position_size <= 0\n strategyAtrAtEntry := atrValue"); |
| 18 | + expect(code).toContain("if shortSignal and strategy.position_size >= 0\n strategyAtrAtEntry := atrValue"); |
| 19 | + expect(code).toContain("longStop = strategy.position_avg_price - strategyAtrAtEntry * atrMultiple"); |
| 20 | + expect(code).toContain("shortStop = strategy.position_avg_price + strategyAtrAtEntry * atrMultiple"); |
| 21 | + expect(code).not.toContain("longStop = strategy.position_avg_price - atrValue * atrMultiple"); |
| 22 | + expect(code).not.toContain("shortStop = strategy.position_avg_price + atrValue * atrMultiple"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("keeps the aligned divergence engine and freezes ATR for RSI Divergence Reversal strategy", () => { |
| 26 | + const preset = presets.find((item) => item.name === "RSI Divergence Reversal"); |
| 27 | + expect(preset).toBeDefined(); |
| 28 | + |
| 29 | + const config = clone(preset!); |
| 30 | + config.outputMode = "strategy"; |
| 31 | + const code = compilePine(config); |
| 32 | + |
| 33 | + expect(code).toContain("// Confirmed regular RSI divergence shared with Indicator mode."); |
| 34 | + expect(code).toContain("strategyAtrAtEntry := atrValue"); |
| 35 | + expect(code).toContain("longStop = strategy.position_avg_price - strategyAtrAtEntry * atrMultiple"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("does not add strategy ATR state to indicator output", () => { |
| 39 | + const preset = presets.find((item) => item.name === "Fast EMA Scalper"); |
| 40 | + expect(preset).toBeDefined(); |
| 41 | + |
| 42 | + const config = clone(preset!); |
| 43 | + config.outputMode = "indicator"; |
| 44 | + const code = compilePine(config); |
| 45 | + |
| 46 | + expect(code).not.toContain("strategyAtrAtEntry"); |
| 47 | + }); |
| 48 | +}); |
0 commit comments