Skip to main content

Volume Weighted Average Price & Accumulation/Distribution

This strategy uses two technical indicators, Volume Weighted Average Price (VWAP) and Accumulation/Distribution (AD), to generate entry and exit signals for trades. The strategy aims to buy and sell securities based on whether there is an accumulation or distribution of the security's volume.

The VWAP is a technical analysis tool that calculates the average price of a security based on its volume. It gives more weight to the price of the security with a higher volume, making it a more reliable indicator of the true average price of the security.

The AD is another technical indicator that is used to measure the accumulation or distribution of a security's volume. It uses the relationship between price and volume to determine whether there is buying pressure or selling pressure in the market.

Overall, the strategy aims to use technical analysis to identify the best entry and exit points for trades, with the goal of maximizing profits while minimizing losses.

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