Automated Trading Managed Order

managed()

side, amount, entry, takeProfit, stopLoss, takeProfitTrail, stopLossTrail, timeLimit, position, tag, background

Run a fully managed order, starting with an order to enter a position, then placing a stop loss and a take profit order to close the position again.

This is an advanced order type with a lot of options. It is intended to used to enter a position, and automatically add a stop loss and take profit order. There are options to have stop loss and take profit act as trailing order, and there are options to control how you enter your position (market or limit). Finally, both the stop loss and take profit are optional.

Important notes when using position=xxx

First off, here's an article about position, side and amount normally behave.

When using position=xxx to set the order size, this command behaves in a different way though, compared to using side and amount. To understand why, it first help to understand the problem introduced when using position, using an example.

Say you have an open position of 1000 contracts (long), and you call this command using position=200. This will function like position elsewhere, and work out that it needs to sell 800 contracts to get your position to the desired final target size of 200. However, the problem arises when it is time set the stop loss and take profit orders. Should these orders attempt to return your position to the previous 1000 contracts long? Should they be trying to close your position back to 0 contracts? Should they do something else? It seems likely that the prefered behaviour would be to close the position, so the stop loss and take profit orders should be submitted as sell orders, to sell 200 contracts. We also need to consider that the position might be changed elsewhere by other active orders. So, we actually want to be sure we sell 200 contracts, even if the position has changed up or down between starting to open the position and placing the stops, so as not to interfere with those actions.

How is this different from using side and amount? When using side and amount, the stops will always be the opposite of the entry. Buy 100 contracts on entry, the stop loss and take profit will always be sell 100 contracts. When using position, the initial order might be buy 100 contracts, by the stop loss and take profit might be either a buy or sell, for a whatever size is needed to close your position.

If this doesn't make sense, just use side and amount.

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.

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.

entry required number/string, defaults to market

Describes how to place the entry order. Set this to market to enter your position using a market order. If set to a value (number or percentage), then a limit order will be used and the value behaves like a normal offset. For an asset quoted in USD (eg XBTUSD), entry can be given in the following ways:-

Example Meaning (assuming asset priced in USD)
entry=market Enter position using a market order.
entry=aggressive Enter position using an aggressive limit order.
entry=50 Enter with a limit order at an offset of $50 from the current price.
entry=1% Enter with a limit order at an offset that is calculated as a percentage of the current price. If the price is $10,000, and offset is 1%, then 1% of 10,000 is 100. An offset of $100 will be used.
entry=@950 Enter with a limit order at a specific price. The @ at the start of the value indicates this should be treated as an absolute price of $950, regardless of what the current price is.
entry=e50 With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
entry=e1% With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
takeProfit optional number, defaults to none

How far away from the entry should the take profit order be placed. For an asset quoted in USD (eg XBTUSD), takeProfit can be given in the following ways:-

Example Meaning (assuming asset priced in USD)
takeProfit=none No take profit order will be placed.
takeProfit=50 Take profit order will be placed at an offset of $50 from the current price.
takeProfit=1% Take profit order will be placed at an offset that is calculated as a percentage of the current price. If the price is $10,000, and offset is 1%, then 1% of 10,000 is 100. An offset of $100 will be used.
takeProfit=@950 Take profit order will be placed at a specific price. The @ at the start of the value indicates this should be treated as an absolute price of $950, regardless of what the current price is.
takeProfit=e50 With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
takeProfit=e1% With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
takeProfitTrail optional number, one of none, an offset number. defaults to none

Determines how the take profit order will behave. Set this value to none to use a basic limit order for your take profit. However, if this is set to an offset value, then the take profit will behave like a trailing take profit order. A trailing take profit order waits, without any order visible, until the price reaches the trigger point (determined by the takeProfit setting), at which point a trailing stop market order will be placed, that will trail the price. For an asset quoted in USD (eg XBTUSD), entry can be given in the following ways:-

Example Meaning (assuming asset priced in USD)
takeProfitTrail=none The take profit order will not trail the price.
takeProfitTrail=50 The take profit order will trail by an offset of $50 after its trigger price is reached.
takeProfitTrail=1% The take profit order will trail by an offset that is calculated as a percentage of the current price. If the price is $10,000, and offset is 1%, then 1% of 10,000 is 100. An offset of $100 will be used.
takeProfitTrail=@950 The take profit order will trail by the distance to a specific price at the time of the trigger.
takeProfitTrail=e50 With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
takeProfitTrail=e1% With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
stopLoss optional number, defaults to none

How far away from the entry should the stop loss order be placed. For an asset quoted in USD (eg XBTUSD), stopLoss can be given in the following ways:-

Example Meaning (assuming asset priced in USD)
stopLoss=none No Stop Loss order will be placed.
stopLoss=50 Stop Loss order will be placed at an offset of $50 from the current price.
stopLoss=1% Stop Loss order will be placed at an offset that is calculated as a percentage of the current price. If the price is $10,000, and offset is 1%, then 1% of 10,000 is 100. An offset of $100 will be used.
stopLoss=@950 Stop Loss order will be placed at a specific price. The @ at the start of the value indicates this should be treated as an absolute price of $950, regardless of what the current price is.
stopLoss=e50 With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
stopLoss=e1% With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
stopLossTrail optional boolean, one of true, false. defaults to false

Should the stop loss order be a trailing stop loss. set this to true to enable trailing.

timeLimit optional time string,

The amount of time to leave the order running before giving up. If the time limit is reached, the command stops and any unfilled quantity will be left unfilled.
If this argument is missing, there will be no time limit and the order will run until it is fully filled or cancelled.

Duration How long will it wait?
timeLimit=10 10 seconds
timeLimit=10s Also 10 seconds
timeLimit=10m 10 minutes
timeLimit=10h 10 hours
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.

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

True to run this command in the background.

If true, this command won't wait and the bot will move on to the next command in the list immediately. When all the other commands are complete, this command will continue to work in the background until it is complete too. This is the recommended way of using this command.

If false, then the command will wait for the stop order to be filled before the next command is executed.

Example: Market entry with SL and TP

myApiKeys(XBTUSD) {
    managed(side=buy, amount=1000, entry=market, stopLoss=1%, takeProfit=3%)
}

This example will place a buy order for 1000 contracts, using a market order. As soon as the market order has been filled, it will place a stop loss order 1% below the current price to sell the 1000 contracts again, and a take profit order 3% above the price. When one of these 2 orders is hit, the other will be cancelled and the command will end.

Example: Limit entry with a trailing SL and no take profit

myApiKeys(XBTUSD) {
    managed(side=buy, amount=1000, entry=10, stopLoss=1%, stopLossTrail=true, takeProfit=none)
}

Similar to the first example, only this time the entry will be attempted with a limit order $10 below the current price. If the order is filled (not guaranteed - it's a normal limit order), a stop loss will be placed like before, but this time it will trail the price each time it pushes up to a new high. There will be no take profit order.

Example: Limit entry with 2 take profits

myApiKeys(XBTUSD) {
    managed(side=buy, amount=500, entry=10, stopLoss=1%, takeProfit=2%)
    managed(side=buy, amount=500, entry=10, stopLoss=1%, takeProfit=5%)
}

This time we split our 1000 contracts into 2 orders, 500 contracts in each one. This will both place limit order $10 orders below the current price, like before. Once those fill, we'll place 2 stop loss orders, both 1% below the price. There will be 2 take profit orders as well. One at 2% and one at 5%.

Example: Limit entry, aiming for a final position size of 1000 contracts

myApiKeys(XBTUSD) {
    managed(position=1000, entry=10, stopLoss=1%, takeProfit=none)
}

For this example, we will assume the inital position size was 500 contracts, before this order.

The order asks for a target position size of 1000 contracts. We have 500 already, so it would need to buy another 500 contracts in the entry order. The entry order will use a limit order, set $10 below the current price.

If the entry fills, it will be time to place the stop loss (we asked for no take profit, so only a stop loss will be placed). As we used position to enter this order, the stop loss will try and close the position completely. This means the stop loss will be to sell 1000 contracts. The stop loss will be placed 1% below the current price when the entry fills.

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.