Skip to main content

Parabolic SAR

The strategy is a type of short-term trading strategy known as scalping, which involves making numerous small trades over a short period of time in an attempt to capture small price movements in a given market. The strategy incorporates two technical indicators, the Parabolic SAR and the Commodity Channel Index (CCI), to generate signals for both entry and exit points.

The Parabolic SAR (Stop and Reverse) indicator is a popular trend-following indicator that helps identify potential trend reversals. It consists of a series of dots that are plotted above or below price, depending on the direction of the trend. When the dots are below price, the trend is considered to be bullish, and when they are above price, the trend is considered to be bearish.

The Commodity Channel Index (CCI) is another popular technical indicator used in trading to identify overbought and oversold conditions in a market. The CCI is calculated by measuring the difference between the average price of an asset and its moving average, relative to the standard deviation of the average price. A high CCI reading indicates that the asset is overbought, while a low reading indicates that it is oversold.

In this strategy, the Parabolic SAR and CCI indicators are calculated without using built-in methods, which means that the strategy itself computes the values of these indicators.

Overall, this scalping strategy appears to be a relatively complex approach to short-term trading that uses two technical indicators to generate signals for entry and exit points, and includes a filter to avoid trading in directionless markets.

tip

This example strategy is machine generated using Gunbot AI. Review its behavior carefully in a simulated bot instance before using parts of this code in production.

// initialize customStratStore within pairLedger object
gb.data.pairLedger.customStratStore = gb.data.pairLedger.customStratStore || {};

// forced wait time reduces risk of double orders
function checkTime() {
return !gb.data.pairLedger.customStratStore.timeCheck || typeof gb.data.pairLedger.customStratStore.timeCheck !== "number"
? (gb.data.pairLedger.customStratStore.timeCheck = Date.now(), false)
: (Date.now() - gb.data.pairLedger.customStratStore.timeCheck > 8000);
}
const enoughTimePassed = checkTime();

// set timestamp for checkTime in next round
const setTimestamp = () => gb.data.pairLedger.customStratStore.timeCheck = Date.now();

// calculate Parabolic SAR
function parabolicSAR(high, low, accelerationFactor, maxAccelerationFactor, prevSAR, prevEP, prevAF) {
let SAR = prevSAR;
let EP = prevEP;
let AF = prevAF;
if (prevSAR === null || prevEP === null || prevAF === null) {
SAR = low[0];
EP = high[0];
AF = accelerationFactor;
} else {
if (prevSAR < low[0]) {
SAR = prevSAR + prevAF * (prevEP - prevSAR);
SAR = Math.min(SAR, high[0]);
} else if (prevSAR > high[0]) {
SAR = prevSAR + prevAF * (prevEP - prevSAR);
SAR = Math.max(SAR, low[0]);
} else {
if (prevEP > high[0]) {
EP = high[0];
AF = Math.min(AF + accelerationFactor, maxAccelerationFactor);
} else if (prevEP < low[0]) {
EP = low[0];
AF = Math.min(AF + accelerationFactor, maxAccelerationFactor);
}
SAR = prevSAR + prevAF * (prevEP - prevSAR);
SAR = Math.min(SAR, high[0]);
SAR = Math.max(SAR, low[0]);
}
}
return [SAR, EP, AF];
}

// calculate Commodity Channel Index (CCI)
function cci(high, low, close, period) {
const typicalPrices = [];
for (let i = 0; i < period; i++) {
typicalPrices.push((high[i] + low[i] + close[i]) / 3);
}
const typicalPriceSMA = typicalPrices.reduce((a, b) => a + b, 0) / period;
const meanDeviation = typicalPrices.reduce((a, b) => a + Math.abs(b - typicalPriceSMA), 0) / period;
const cci = (typicalPrices[0] - typicalPriceSMA) / (0.015 * meanDeviation);
return cci;
}

// calculate indicators
const sar = parabolicSAR(gb.data.candlesHigh, gb.data.candlesLow, 0.02, 0.2, null, null, null)[0];
const cciValue = cci(gb.data.candlesHigh, gb.data.candlesLow, gb.data.candlesClose, 20);

// log indicators
console.log(`Parabolic SAR: ${sar}`);
console.log(`CCI: ${cciValue}`);

if (enoughTimePassed) {
const buyConditions = sar > gb.data.candlesClose[1] && cciValue < -100 && !gb.data.gotBag;
const sellConditions = sar < gb.data.candlesClose[1] && cciValue > 100 && gb.data.gotBag;

// fire orders when conditions are met
if (buyConditions) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
gb.method.buyMarket(buyAmount, gb.data.pairName);
setTimestamp();
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
setTimestamp();
}
}

// Code is machine generated, review it and run in simulator mode first