Skip to main content

Contrarian

The strategy is a contrarian trading strategy that uses two technical indicators, the Stochastic oscillator and Williams %R, to identify potential entry and exit points for trades.

The Stochastic oscillator is a momentum indicator that compares the closing price of an asset to its price range over a specified period of time. It measures whether the asset is overbought or oversold and can indicate potential turning points in the market.

Williams %R is another momentum indicator that measures overbought and oversold levels by comparing the closing price to the highest high and lowest low over a specified period of time. It is similar to the Stochastic oscillator but is calculated differently.

It looks for situations where the market is overbought or oversold according to these indicators and then take trades in the opposite direction of the current trend.

For example, if the Stochastic oscillator and Williams %R both indicate that the market is overbought, you would consider taking a short position (betting that the price will go down) in the asset. Conversely, if both indicators indicate that the market is oversold, you would consider taking a long position (betting that the price will go up).

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 Stochastic oscillator
const stochK = (gb.data.candlesClose[gb.data.candlesClose.length-1] - Math.min(...gb.data.candlesLow.slice(-14))) / (Math.max(...gb.data.candlesHigh.slice(-14)) - Math.min(...gb.data.candlesLow.slice(-14))) * 100;
const stochD = gb.data.slowSma(gb.data.stochK, 3);

// calculate Williams %R
const willR = (Math.max(...gb.data.candlesHigh.slice(-14)) - gb.data.candlesClose[gb.data.candlesClose.length-1]) / (Math.max(...gb.data.candlesHigh.slice(-14)) - Math.min(...gb.data.candlesLow.slice(-14))) * -100;

// log indicators and trading conditions
console.log(`Stochastic K: ${stochK}`);
console.log(`Stochastic D: ${stochD}`);
console.log(`Williams %R: ${willR}`);

const buyConditions = stochK < 20 && willR < -80 && !gb.data.gotBag;
const sellConditions = stochK > 80 && willR > -20 && gb.data.gotBag;

if (enoughTimePassed) {
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