Automated Trading Examples

Order, Stop Loss and Take Profit

Another common situtation is that you'd like to open a position, and at the same time set a stop loss and a take profit.

Basic Example

In this example we'll open a small position of 100 contracts on BitMEX, and position a stop loss 1% lower and a take profit 3% higher...

myApiKeys(XBTUSD) {
    # Buy 100 contracts NOW
    market(side=buy, amount=100);

    # Add a stop loss
    stopMarket(side=sell, amount=100, offset=e1%);

    # We can just use a normal limit order as our take profit...
    limit(side=sell, amount=100, offset=e3%, reduceOnly=true);
}

Notice that the take profit limit order is using the 'reduce only' feature, which ensures this order can only be used to close your long, instead of accidently opening a new short position.

Entry using percentage of account, and split my Take Profit

Lets add to this example. This time we'll use a percentage of our balance on entry, and close the position at 2 different profit levels...

myApiKeys(XBTUSD) {
    # Buy the equivalent of 90% of my balance
    market(side=buy, amount=90%);

    # Add a stop loss to close 100% of our open position
    # Place the stop 1% away from our average entry
    stopMarket(side=sell, amount=100%p, offset=e1%);

    # 2 take profit orders, using 50% of our open position each
    limit(side=sell, amount=50%p, offset=e1.5%, reduceOnly=true);
    limit(side=sell, amount=50%p, offset=e2.3%, reduceOnly=true);
}

So first we open a position. This will use 90% of your balance at 1x (use 5 x 90 = 450% to use 90% of you margin at 5x).

Once the position is open, place a stop market order to close it if the price falls 1% below entry.

Also, place 2 limit orders, each closing half of the open position (50%p - the p means 'position') at offsets of 1.5% and 2.3% from average entry (the e means relative to average Entry).

We have an article that explains more about the different ways to express position sizes.

The same again, but using a limit order for entry if possible...

myApiKeys(XBTUSD) {
    # Buy the equivalent of 90% of my balance
    aggressive(position=90%, slippageLimit=5, timeLimit=3m);
    market(position=90%);

    # Add a stop loss to close 100% of our open position
    # Place the stop 1% away from our average entry
    stopMarket(side=sell, amount=100%p, offset=e1%);

    # 2 take profit orders, using 50% of our open position each
    limit(side=sell, amount=50%p, offset=e1.5%, reduceOnly=true);
    limit(side=sell, amount=50%p, offset=e2.3%, reduceOnly=true);
}

The only different here is the way we manage the entry. First off, we try to use an aggressive order, which uses a moving limit order to try and get filled as a maker, potentially saving fees. We defined slippage limits and time limits for this to be filled. If it does not fill, any unfilled portion of the order will be completed using a market order. See Understanding Amount and Position to learn how using position instead of side and amount helps in this situation (TLDR - both orders had a target size of 90% of balance, so the market order only needs to fill in whatever the aggressive order could not fill).

An Alternative approach

There is another command that will place both the stop loss and take profit for you, and then watch them both until one of them is filled. As soon as either one is filled, then it will cancel the other one for you, cleaning up after itself.

You should only use this option for very simple cases though. It is less flexibile than the above approach, but is a bit simpler to read.

myApiKeys(XBTUSD) {
    # Buy 100 contracts NOW
    market(side=buy, amount=100);

    # wait for a profit or a loss
    stopOrTakeProfit(side=sell, amount=100, sl=1%, tp=3%);
}

Another alternative

The managed() order provides all 3 parts of the process (entry, stop loss and take profit), in a single command that takes care or a lot of the details for you.

For entry, you can choose from market orders, limit order, or aggressive orders.

Stop loss and take profit orders can be either trailing or fixed.

The managed() order also supports all the timing related tasks (like waiting for limit orders to be filled). Consult the documentation to see all the options and features it supports.

Again, only use this if you need a very simple setup similar to the first example. If you want to anything more complex, then we always recommend using the underlying commands to set things up as you need.

myApiKeys(XBTUSD) {
    # Place the entry order, wait for it to fill,
    # then place stop loss and take profit orders
    managed(side=buy, amount=100, entry=aggressive, stopLoss=1%, takeProfit=3%);
}

Learn more about the commands used here...

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.