PSAR (Parabolic SAR)
Parabolic SAR (Stop And Reverse) trails price with an accelerating curve. When the indicator flips from above to below price (or vice versa), it signals a potential trend reversal.
Declaration
function init(ctx) { ctx.indicator('psar', 'PSAR', { step: 0.02, max: 0.2 }); }
Options
| Option | Type | Default | Description |
|---|---|---|---|
step | number | 0.02 | Acceleration factor increment |
max | number | 0.2 | Maximum acceleration factor |
Output
Returns a single number[] series (the SAR value for each bar).
Accessing Values
function onBar(ctx, i) { const sar = ctx.ind.psar[i]; const close = ctx.series.close[i]; // SAR below price = uptrend // SAR above price = downtrend const isBullish = close > sar; }
Use Cases
Trend Following
function onBar(ctx, i) { const close = ctx.series.close[i]; const sar = ctx.ind.psar[i]; const prevSar = ctx.ind.psar[i - 1]; const prevClose = ctx.series.close[i - 1]; // SAR flipped from above to below price if (prevClose <= prevSar && close > sar) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'psar_bullish' }); } // SAR flipped from below to above price if (prevClose >= prevSar && close < sar) { ctx.order.close('ASSET', { signal: 'sell', reason: 'psar_bearish' }); } }
Trailing Stop
function onBar(ctx, i) { const sar = ctx.ind.psar[i]; // Use SAR as a dynamic trailing stop level if (ctx.position.qty > 0 && ctx.series.close[i] < sar) { ctx.order.close('ASSET', { signal: 'sell', reason: 'trailing_stop' }); } }
Calculation
The SAR value accelerates toward price using an acceleration factor (AF) that increases by step each bar (up to max) when a new extreme point is reached.
Related
Related
indicatorpsarparabolicsartrendstop-and-reverseqsl