Automated Trading TWAP Time-Weighted Average Price Order

twap()

side, amount, orderCount, duration, position, tag

TWAP order, also known as Time-Weighted Average Price Order, places a series of market orders over an extended period of time.

The amount will be split between all the orders. After each market order is executed there will be a delay for (duration / orderCount) seconds. For example, if duration is 60 seconds, and the orderCount is 10, then there will be a (60 / 10) = 6 second delay between each order.

Arguments
side required text, one of buy, sell. defaults to buy

Is this a buy or sell order.
Required if you don't use position.

amount required number, defaults to 0

The size of the order. The units depend on the symbol and exchange. Can be given as an absolute amount, percentage of your balance or percentage of your available balance.

Example Meaning (if on BitMEX XBTUSD)
amount=1000 Uses an amount of 1000 contracts
amount=50% Will calculate the amount as a percentage of your balance/equity. For example, if your balance is 1 BTC and the current price is $10,000, 50% of 1 btc is 0.5 btc. 0.5 btc is worth $5,000. At $1 per contract, the order will be for 5,000 contracts.
amount=50%% Similar to above, only will use 50% of your available balance/equity. Equity already locked up in positions and pending orders will not be considered.
amount=50%p Will work out 50% of your position size and use that as the amount. The p stands for Position Size
amount=50%b Same as 50% - the b stands for Balance
amount=50%a Same as 50%% - the a stands for Available Balance

Percentage amounts are supported on all exchanges. On BitMEX they are supported on most (but not all) symbols. XBTUSD is supported and all symbols against XBT (eg ETHXBT), but not on symbols backed against other currencies (eg ETHUSD, XBTJPY etc can not use % amounts and must use fixed amounts - this is because we need live price information for additional symbols to complete the calculation, which are not tracked to by default).

Required if you don't use position.

orderCount required number, defaults to 10

The number of market orders to place over the duration of the order.

Can be any whole number from 2 to 100.

duration required time, defaults to 60s

The total amount of time to spread the orders over. Can be a number with optional time units...

Duration How long is it?
duration=10 10 seconds
duration=10s Also 10 seconds
duration=10m 10 minutes
duration=10h 10 hours
position optional number,

An alternative way to calculate the side and amount for the order.

The amount traded will be the difference between your current open position and the target position size. For example, if your current position is 100 contracts long, and you request a target position of 300, then an order will be placed to buy 200 contracts (the difference between your current position and target position). In fact, regardless of your current position (long, short, nothing), the order will be whatever size it needs to be to ensure you finish with an open position of the target size. Some more examples...

Example What happens, if you current position is 500 contracts
position=1000 Buy 500 contracts, resulting in a position size of 1000
position=200 Sell 300 contracts, leaving you with 200.
position=0 Sell 500 contracts. This will effectively close your position, regardless of it's current size.
position=-500 Sell 1000 contracts, taking you from 500 contracts long to 500 contracts short.
position=50% Target position size will be 50% of your current balance size
position=50%% Target position size will be 50% of your available balance size
position=50%p Target position size will be 50% of your current position size
position=50%b Same as 50% - the b stands for Balance
position=50%a Same as 50%% - the a stands for Available Balance

On spot exchanges, like Bitfinex, the target amount represents the amount of the asset you'd like to end up with. For example, on the BTCUSD pair, position would represent the amount of BTC you'd like to own after the order completes.

Required if you don't use side and amount.

tag optional boolean, one of true, false. defaults to false

Tags the order with the value given. This allows you can cancel the order using it's tag. See cancel() for more details.

Example: Buy 1000 contracts over the next 10 minutes

myApiKeys(XBTUSD) {
    twap(side=buy, amount=1000, orderCount=20, duration=10m);
}

Places 20 orders of 50 contracts each, using a market order. The orders will be spread over a 10 minute period, with an order being placed about every 30 seconds.

Some images from Unsplash

About Alertatron.com

Your TradingView alerts, with charts, PM'd to you.

Get all your alerts from TradingView, sent to Telegram, Discord, Slack, Email or webhooks, with a chart snapshot attached.

Capture Charts

See a high quality chart with every alert, captured at the moment the alert was triggered. See exactly what the market was doing and make informed decisions.

Automate Trading

Trigger orders on popular cryptocurrency exchanges and trade 24/7. Access powerful algorithmic orders to maximise your profits.

Integrations

Connect your alerts to Telegram (channels, groups and private chats), Discord, Slack, email or send push notifications to other services with webhooks.

Easy To Setup

Nothing to install and no browser extensions. Captures alerts 24/7, even with your computer switched off. Be up and running in seconds.