Williams strategy
This contrarian example uses Stochastic K, a three-period average for D, and Williams %R from the last 14 candles. It logs the indicator values and current bag status, then evaluates entries after an 8-second cooldown to prevent rapid repeat orders.
Buy conditions require Stochastic K and D below 20, Williams %R below -80, and no bag. Sell conditions require Stochastic K and D above 80, Williams %R above -20, and an existing bag.
tip
This example strategy is machine generated using Gunbot AI. Review its behavior carefully in a simulated trading 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.slice(-3).reduce((a, b) => a + b, 0) / 3;
// calculate Williams %R
const williamsR = (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 oscillator K: ${stochK}`);
console.log(`Stochastic oscillator D: ${stochD}`);
console.log(`Williams %R: ${williamsR}`);
console.log(`Bag status: ${gb.data.gotBag ? 'Has bag' : 'No bag'}`);
if (enoughTimePassed) {
// calculate buy and sell conditions
const buyConditions = stochK < 20 && stochD < 20 && williamsR < -80 && !gb.data.gotBag;
const sellConditions = stochK > 80 && stochD > 80 && williamsR > -20 && 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();
console.log('Buy order placed');
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
setTimestamp();
console.log('Sell order placed');
}
}
// Code is machine generated, review it and run in simulator mode first