Automated Trading Dynamic Take Profit

dynamicTakeProfit()

offset, size, tag, reduceOnly, cancelOtherOnFill

Starts a dynamic order that will monitor the open position and adjust size and price of a limit order to close (or partially close) that position.

Important: it is higher recommended that size always be expressed as a percentage of the open position (eg, 100%p), and that the offset is expressed relative to the average entry (eg e10 or e1%).

You do not specify a side for this order, as the order determines the size by looking at the open position size. For open long positions, it will a sell order, for open short positions, it will be a buy order.

If there is no open position when this order starts, it will wait for a position to open if there are unfilled orders on the exchange.

Once a position is open, this order will evaluate the offset and size properties and place a limit order.

It will then continue to monitor the position. Each time the position size grows the dynamicTakeProfit order will re-evaluate the offset and size and update the limit order.

When the limit order is filled, or if the position reduces to zero by another means, this order will complete.

The order uses the following simple rules...

  1. Wait for there to be an open position.
  2. If there is no open position, and no open orders (that could result in an open position), then there is no point in waiting, so stop.
  3. When an open position is detected, place an order to close all or part of it (size is determined by the size property).
  4. If the position size grows (absolute size gets bigger), then replace the order, working out it's new price (from the offset property) and new size (from the size property).
Arguments
offset optional number, defaults to e1%

The offset to use when working out the price of the order. Whilst other options are supported, like offset elsewhere, it is strongly recommended to always use an offset relative to the average entry as the order will recalculate this offset every time the position size changes.

Example Meaning (assuming asset priced in USD)
offset=e50 $50 from the average entry
offset=e1% 1% from the average entry price
size optional number, defaults to 100%p

Determine the size of the order. Since the order is designed for closing open positions, it only makes sense to use an order size that is expressed as a percentage of the open position size, or a fixed value.

Example Meaning
size=100%p Make this order close 100% of the open position
size=50%p Close 50% (half) of the open position
size=100 Close 100 contracts/units of the position
cancelOtherOnFill optional string, one of all, buy, sell, entry, none. defaults to none

Optionally cancel open orders when the open position is closed.

Example Meaning
cancelOtherOnFill=none No orders are cancelled on completion
cancelOtherOnFill=all All open orders are cancelled on completion
cancelOtherOnFill=buy All open buy orders are cancelled on completion
cancelOtherOnFill=sell All open sell orders are cancelled on completion
cancelOtherOnFill=entry If the take profit is a closing a long position, all buys will be cancelled. If closing a short position, then all sells will be cancelled
cancelOtherOnFill=OtherValue Any other value given here is assumed to be an order tag. All orders with the given tag will be cancelled as if cancel(which=tagged, tag=OtherValue) was used.
tag optional string, defaults to varies by order type

Applies a tag to an order that can later be used to cancel this order, even from a different session. Many orders can be given the same tag, making it possible to cancel them all as a group at a later date. These tags can be referenced by the cancel() command to cancel specific named orders. See cancel() with which=taggedfor more details.

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

Attempt to submit the order as a reduce-only order. This is ignored in any spot markets / exchanges, as there is no concept of an open position there.

Example: Simple example to place a limit order and take profit

myApiKeys(XBTUSD) {
    # place a limit order a little below the current price
    limit(side=buy, amount=1000, offset=0.5%);

    # and trigger an order to wait for it to fill, to take profit
    dynamicTakeProfit(offset=e10, size=100%p);
}

Places the limit order and starts the dynamicTakeProfit order. As the limit order won't fill right away, the dynamicTakeProfit order will do nothing to start with.

Once the limit order fills (or partially fills), the take profit order will appear, $10 from the entry price to fully close the position again.

Example: Take profit from a scaled order entry

myApiKeys(XBTUSD) {
    # place 10 limit orders to buy, over a $20 range
    cancel(which=all);
    scaled(from=5, to=20, orderCount=10, side=buy, amount=1000);

    # start 2 dynamic take profit orders.
    # the first will be $10 above the average entry and 
    # close half the open position (50% of the open position)
    dynamicTakeProfit(offset=e10, size=50%p);

    # The second will sit 1% above average entry
    # and close the other half
    dynamicTakeProfit(offset=e1%, size=50%p);

    # both will grow as the scaled orders are filled
}

Places a scaled order, and 2 dynamic take profit orders. The 2 take profit will start out waiting for the position to open. As soon as the first bid from the scaled order is filled, dynamic take profit will place their sell order (half the position each in this example). As each of the limit orders is filled, these 2 take profit orders will be updated (both their price, as they are offset relative to the average entry, and their size, as that is determined as 50% of the open position).

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.