Stop Price Algos

A very useful trading tool is the ability to set a stop price on the value of a token. A "stop" is really just a trigger price that results in some action being taken once the trigger price is tripped. From Dexible's perspective, StopLoss, and what might be called a StopLimit, just uses a StopPrice policy. That policy specifies a trigger price and whether the market price needs to be above or below that trigger before Dexible will execute the round.

StopLoss

When you want to protect an asset position from a dramatically moving downside, you would tell Dexible to watch for a trigger price and execute the order when the market price is below (or not above) the given price. When using the algo factory, it looks like this:

let algo = new StopLossSwap({
    amountIn:  units.inBNETH(".146"),
    tokenIn: weth,
    tokenOut: matic,
    slippage: new Slippage(.5, false),
    trigger: new Price({
        inAmount: units.inBNETH("1"),
        inToken: weth
        outAmount: units.inBNETH("1800"),
        outToken: matic
    }),
    customizations: {
        maxNumberRounds: 1
    }
});

This example has a few new items. The trigger establishes the rate that will trip this execution. Once the trigger activates, it's like a circuit breaker and any subsequent rounds for the order will be executed as a Market order.

The maxNumberRounds tells Dexible that when executing this order, only a single round must be used and you don't care what the resulting price impact will be. In this case, a stop loss is attempting to bail out as quickly as possible so a single round is the quickest way to get out. But always keep in mind that liquidity can dry up fast in falling markets and you could see significant price impact if trading a large amount.

Later we will show you how could also inject a price range in a TWAP order to execute a kind of limit or stop order to keep losses within a specific range.

StopLimit

Another stop-related order type is the StopLimit. This allows you to specify an upside trigger to trip when a token price breakout happens. Here is an example:

const sl = new StopLimitSwap({
    amountIn:  units.inBNETH("1"),
    tokenIn: weth,
    tokenOut: matic,
    slippage: new Slippage(.5, false),
    limitPrice: new Price({
        inAmount: units.inBNETH("1"),
        inToken: weth,
        outAmount: units.inBNETH("1750"),
        outToken: matic
    }),
    trigger: new Price({
        inAmount: units.inBNETH("1"),
        inToken: weth,
        outAmount: units.inBNETH("1800"),
        outToken: matic
    }),
    customizations: {
        ...
    }
});

There is a little more going on here so let's dive in a bit. There are two prices for a stop limit: the stop trigger and the limit price. The trigger represents the price at which point the order should be converted to a limit order. In general, StopLimit is attempting to find a price that is higher than the current market price. Once it's tripped, you can protect yourself by setting a max price you're willing to pay once it breaks above a particular trigger. That's what the limit price represents. The trigger price should always be lower than the limit but higher than current market. In this example, it may seem counter-intuitive that the trigger looks higher than the limit. But if you think about it, if you get more MATIC for 1 ETH input, it means the MATIC is cheaper (price is lower).

Last updated