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=1%)

    # We can just use a normal limit order as our take profit...
    limit(side=sell, amount=100, offset=3%, 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.

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.

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%);

Even better 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.

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


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.


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.