TWAP

TWAP, or time-weighted average price, is a way to achieve dollar cost averaging (DCA) to enter or exit a position. It is a more complicated algo but Dexible makes it simple. Let's start with an example:

const twap = new TWAPSwap({
    amountIn:  units.inBNETH("1"),
    tokenIn: weth,
    tokenOut: matic,
    slippage: new Slippage(.5, false),
    timeWindowSeconds: 86400,
    priceRange: {
        basePrice: new Price({
            inAmount: units.inBNETH("1"),
            inToken: weth,
            outAmount: units.inBNETH("2000"),
            outToken: matic
        }),
        upperBoundPercent: 5
    },
    customizations: {
        dexFilters: {
            include: [MainnetDexFilter.SushiSwap]
        }
    }
});

Note the use of "timeWindowSeconds". This tells Dexible execute the order across a 24-hour time period (86400 seconds). Dexible will compute the optimum number of rounds based on current market conditions and insert an evaluation delay based on timeWindow/numberOfRounds.

The timeWindow is used to determine how often to evaluate the market for execution of each round. A 24-hour time period, with say 5 rounds, means it will look at the market every 4.8 hours and attempt to execute. Dexible uses some randomization to compute the actual delay between evaluations but will execute the full order within the time window.

maxNumberRounds Warning

In the StopLoss example earlier, maxNumberRounds was used to tell Dexible to force the execution in a single round, regardless of consequences. In that use case, it might make sense to ignore market impact. maxNumberRounds can also be used in TWAP algos; however, you will want to be careful with how you use it because it may expose your order to excessive market impact. For example, if you were trading 100 ETH and forced it to limit each round to 10 ETH (i.e. maxNumberRounds = 10), 10 ETH may be a significant amount to trade in a single round depending on the token you're buying. It could result in higher losses compared to Dexible's recommended number of rounds. Our optimized recommendation will always choose the maximum net output after reducing costs.

Price Range

TWAP can also have a bounded price range. This limits market impact exposure as it executes within the time window. The range takes a "base" price and then an upper and lower bound percentage. The example above used a price range to limit the price fluctuation during execution. Note that the price range can be completely outside of the current market, allowing you to create price bands.

Last updated