Our plans and pricing are changing on 1st November. Learn more...

Automated Trading Examples

Pyramiding and Multiple Entries

For some strategies, you might get multiple long signal in a row, or several short signals. There are a few choices of what to do in this situation though.

  1. Just keep adding to your position and hope for the best.
  2. Act on the first signal and ignore the following signals until you get one that flips your position (eg, from long to short).
  3. Keep adding to your position, up to some maximum position size, then ignore everything after that until you get the opposite signal.

We'll ignore option 1, as any basic set of orders will be able to do that. We'll also just focus on the 'Buy' signal. A sell signal would have very similar sets of commands, so we'll keep things simple.

Trade on first signal, Ignoring duplicates

myApiKeys(XBTUSD) {
    # Check if we have a long position, and stop processing if we do

    # We weren't long, so buy 1000 contracts
    market(side=buy, amount=1000);

Imagine we have no position at all at the start. When the first signal arrives, the first command checks our position size to see if we are long or not. As out position size is Zero, we are not long. The command will stop if we are long, and since we aren't long, we can carry on to the next command. The market order will be executed, leaving us long 1000 contracts.

The second time we get the signal, the bot will once again check our position size. This time it is 1000 long. The command will stop us if we are long, so processing of all the later commands is cancelled. The market order is never executed and our position size stays at 1000 long.

This same process will continue to happen for every additional 'Buy' signal that comes in.

Adding to a position, up to a limit

A more common approach would be to keep adding to your position up to a maximum position size. Let's set ourselves a max position size of 10,000 contracts long. We're happy to let our position grow to 10,000 contracts, but if we are already at 10,000, or more, we don't want to be adding any more.

myApiKeys(XBTUSD) {
    # Only continue if our position is less than 10,000 contracts
    continue(if=positionLessThan, value=10000);

    # We're inside our limits, so add a bit more
    market(side=buy, amount=1000);

This time we are only allowing processing to continue if our position is smaller than 10,000. Each time we get a signal, we'll end up adding another 1,000 contracts to our position. Finally, when our position has grown to 10,000 contracts after 10 'Buy' signals in a row, we'll start ignoring any additional signals.

Adding to a position, up to a limit, but close the short if there is one

This time we want to do the same as the previous example, but also close any open short if there is one before we start stacking our buys...

We need to break things up into 2 blocks (they should both be sent in the same alert though). The first one starts with a test that will only allow it to continue if we have an open short position. The second is basically the same as before, adding to the long up to some limit. The only difference is the addition of the wait() command, which will be enough to give the first block a chance to complete if it had anything to do.

myApiKeys(XBTUSD) {
    # This block will only execute if we are short

    # Close the short position

myApiKeys(XBTUSD) {
    # Give the short time to close if there was one

    # Only continue if our position is less than 10,000 contracts
    continue(if=positionLessThan, value=10000);

    # We're inside our limits, so add a bit more
    market(side=buy, amount=1000);

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.


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.