B013
strategy position read in setup
Severity Error
strategy.position.*, strategy.equity, strategy.netProfit and the other read-only strategy values change from bar to bar as the emulator fills orders. Setup values are computed once before the first bar, when there is no position yet.
Read these values inside a handler. To remember a starting value, store it in state on the first bar.
Failing example
script "Equity guard"
strategy initialCapital = 10_000.0
data chart = subscribe(data.ohlcv)
setup startEquity = strategy.equity
on chart.close {
if strategy.equity < startEquity * 0.8 {
strategy.closeAll(comment: "drawdown guard")
}
if strategy.position.size == 0.0 && chart.close > chart.open {
strategy.entry("Long", direction.long)
}
}Fixed example
script "Equity guard"
strategy initialCapital = 10_000.0
data chart = subscribe(data.ohlcv)
setup startEquity = 10_000.0
on chart.close {
if strategy.equity < startEquity * 0.8 {
strategy.closeAll(comment: "drawdown guard")
}
if strategy.position.size == 0.0 && chart.close > chart.open {
strategy.entry("Long", direction.long)
}
}