Contrarian strategy
This contrarian example calculates Stochastic K/D and Williams %R from the last 14 candles. It logs those values and only evaluates entries after an 8-second cooldown to avoid rapid repeat orders.
The entry logic is straightforward: it buys when Stochastic K is below 20 and Williams %R is below -80 with no existing bag, and sells when Stochastic K is above 80 and Williams %R is above -20 while holding a bag.
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