Volume Weighted Average Price & Accumulation/Distribution strategy
This strategy combines Volume Weighted Average Price (VWAP) with Accumulation/Distribution (AD) to generate entry and exit signals. VWAP estimates a volume-weighted average price, while AD links price and volume to gauge accumulation or distribution pressure. The script uses both indicators together to decide when to buy or sell.
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 VWAP
function calculateVWAP() {
let sumPriceVolume = 0;
let sumVolume = 0;
for (let i = 0; i < gb.data.candlesClose.length; i++) {
sumPriceVolume += gb.data.candlesClose[i] * gb.data.candlesVolume[i];
sumVolume += gb.data.candlesVolume[i];
}
return sumPriceVolume / sumVolume;
}
const vwap = calculateVWAP();
console.log("VWAP:", vwap);
// calculate AD
function calculateAD() {
let sumAD = 0;
for (let i = 0; i < gb.data.candlesClose.length; i++) {
const mfMultiplier = ((gb.data.candlesClose[i] - gb.data.candlesLow[i]) - (gb.data.candlesHigh[i] - gb.data.candlesClose[i])) / (gb.data.candlesHigh[i] - gb.data.candlesLow[i]);
const mfVolume = mfMultiplier * gb.data.candlesVolume[i];
sumAD += mfVolume;
}
return sumAD;
}
const ad = calculateAD();
console.log("AD:", ad);
if (enoughTimePassed) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
const buyConditions = gb.data.vwma > vwap && gb.data.adx > 25 && ad > 0 && !gb.data.gotBag;
const sellConditions = gb.data.vwma < vwap && gb.data.adx > 25 && ad < 0 && gb.data.gotBag;
console.log("VWMA:", gb.data.vwma);
console.log("ADX:", gb.data.adx);
console.log("Got Bag:", gb.data.gotBag);
console.log("Buy Conditions:", buyConditions);
console.log("Sell Conditions:", sellConditions);
if (buyConditions) {
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