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%, trigger=last);

    # 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
    stopOrder(side=sell, amount=100%p, offset=e1%, trigger=last);

    # 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.


Move the stop loss up to breakeven at first take profit

This time we'll have 2 take profit orders and stop loss order. The stop loss order will move up to breakeven when the first take profit order is reached.

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
    trailingStop(side=sell, amount=100%p, offset=e1%, trigger=last,
            trailingMethod=stepped, stepSize=1%, maxSteps=1);

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

The difference is a change to the trailing stop order and the addition of trailingMethod=stepped, stepSize=1%, maxSteps=1 to the command. The stop loss order will be initially placed 1% below average entry (offset=e1%) and will then monitor the price for changes.

So what do the new settings do? trailingMethod=stepped tells the bot to only move the order in steps, instead of on every tick of price increase. stepSize=1% determines how big those steps should be - 1% of the price in our example, and maxSteps=1 determines how many times the bot should do that.

Here, we have set the step size to be the same as the initial offset, so the stop loss order will start out 1% below the entry, will move up 1% at a time, and will do this only once. The end result is that it will move up to breakeven when the price has gone up 1% above entry.

In order to get the order to jump up to breakeven, make sure that offset and stepSize are both the same amount. You can adjust these values though, to get different effects. For example, to move the stop up in $10 steps, starting $100 below the price, up to $50 above the entry price, you could use the following settings...

trailingStop(side=sell, amount=100%p, offset=100, trigger=last,
    trailingMethod=stepped, stepSize=10, maxSteps=15);

Notice that it is set to allow the order to move 15 times, at $10 each step, giving a total permitted move of $150. It is well worth exploring the options that these controls give you in controlling when and where your stops adjust.


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

myApiKeys(XBTUSD) {
    # Make sure no other orders are still waiting
    cancel(which=all);

    # 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
    stopOrder(side=sell, amount=100%p, offset=e1%, trigger=last);

    # 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. This set of commands is assuming it fully controls the position size, entry and exit orders, so we start by cancelling any orders still on the exchange that might interfer with this.

Next, 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

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) {
    # Make sure no other orders are still waiting
    cancel(which=all);

    # 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.