# How To Compute Parabolic SAR (Stop and Reverse) using TypeScript?

, in category: Technical Analysis , 5 months ago

How To Compute Parabolic SAR (Stop and Reverse) using TypeScript?

, 4 months ago

Parabolic SAR (Stop and Reverse) is a technical indicator used in financial markets to determine potential reversal points in the price direction of an asset. It is calculated based on the previous price data of the asset and is often used by traders to set stop-loss levels.

To compute Parabolic SAR using TypeScript, you can follow the steps below:

1. Define the initial values: Initial acceleration factor (AF): Typically set to 0.02 and increases by 0.02 each time the extreme point is reached. Maximum acceleration factor (Max AF): Typically set to 0.20. Initial long position: Set to true if the asset is in an uptrend, false if in a downtrend. Initial extreme point and acceleration factor: Set to the first data point in the price data.
2. Calculate the SAR values: If in an uptrend, calculate the SAR values using the formula: SAR = Prior SAR + Prior AF * (Prior Extreme Point - Prior SAR). If in a downtrend, calculate the SAR values using the formula: SAR = Prior SAR - Prior AF * (Prior SAR - Prior Extreme Point).
3. Update the extreme point and acceleration factor: If a new extreme point is reached, update the extreme point and reset the acceleration factor to the initial AF. If the current trend changes, switch the long position and set the extreme point and acceleration factor to the current data point.
4. Iterate through the price data: For each data point, calculate the SAR value using the formulas above and update the extreme point and acceleration factor accordingly.

Below is a sample TypeScript code snippet that demonstrates how to compute Parabolic SAR:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ``` ```function calculateSAR(priceData: number[], AF: number = 0.02, maxAF: number = 0.2): number[] { let sarValues: number[] = []; let longPosition: boolean = true; let extremePoint: number = priceData[0]; let accelerationFactor: number = AF; for (let i = 0; i < priceData.length; i++) { let sar = i === 0 ? priceData[i] : sarValues[i - 1]; if (longPosition) { if (priceData[i] < extremePoint) { sar += accelerationFactor * (extremePoint - sar); accelerationFactor = Math.min(accelerationFactor + AF, maxAF); } } else { if (priceData[i] > extremePoint) { sar -= accelerationFactor * (sar - extremePoint); accelerationFactor = Math.min(accelerationFactor + AF, maxAF); } } sarValues.push(sar); if (longPosition && priceData[i] > extremePoint) { extremePoint = priceData[i]; accelerationFactor = AF; longPosition = false; } else if (!longPosition && priceData[i] < extremePoint) { extremePoint = priceData[i]; accelerationFactor = AF; longPosition = true; } } return sarValues; } ```

You can use the `calculateSAR()` function with an array of price data to compute the Parabolic SAR values. Adjust the initial AF and maxAF values as needed for your specific use case.

Member

by patricia , 2 months ago

Above is the sample TypeScript code snippet that demonstrates how to compute Parabolic SAR. You can use this function by passing an array of price data.

Let me know if you have more questions or need further clarification.