# Automated Trading Examples

### Understanding Amount & Position

There are a lot of powerful ways to express how large an order you'd like to place using either the *amount* setting, or *position* setting. Here we'll explain all the options and what the difference between amount and position is.

#### What is *side* & *amount*?

*Side* determines if the bot will buy or sell.

*Amount* determines how much.

`market(side=buy, amount=100);`

will simply buy 100. It does not consider your current position size, or your leverage. Just buys 100.

#### What is *position*?

*Position* tells the bot what you'd like your position to be. It's the **target** position size.

It will look at your current position size and work out the difference between what it is now, and what you want it to be. It will then buy **or** sell however much is needed to reach your target.

**Note: Because position works out if it needs to buy or sell, and by how much, side and amount will be ignored. The bot will calcuate them both for you.**

On spot exchanges like Coinbase Pro and Bitfinex, this is treated as the amount of the asset you'd like in your balance. For example, on Bitfinex, using the BTCUSD pair, *position* would represent the amount of BTC you'd like in your balance after the order completes.

#### Quick summary of supported units

Unit | Meaning on BitMEX or Deribit |
---|---|

50 | 50 contracts / dollars |

50% | 50% of your current balance, converted to contracts / dollars |

50%% | 50% of your available balance, in contracts / dollars |

50%b | Same as 50% |

50%a | Same as 50%% |

50%p | 50% of your current position size |

Percentages are supported for both `position`

and `amount`

on Derbit and BitMEX. Spot exchanges like Bitfinex and Coinbase don't support percentages for position sizing, but see the notes below for more details...

### Examples

This will be mostly explained using examples. All the examples in this article will make the following assumptions...

- You are trading on BitMEX on the XBTUSD symbol
- You have a balance of 1 BTC, to keep the maths simpler.
- The Bitcoin price is $10,000, also for simplicity
- You have your leverage slider at 10x
- You have an existing open position of 10,000 contracts using up 10% of your margin.
- A reminder that 1 contract is worth 1 dollar.

We'll also be using market orders in all the examples, but these settings work the same in all orders that support *amount* or *position*. Just remember that the calculations are made at the time the order is placed.

With that out of the way, lets start simple.

#### Examples using *amount* and *side*

Again, remember that this examples are going to be based on the assumptions above.

**Buy 100 contracts**

Just buys 100 contracts, and doesn't take anything else into account.

```
myApiKeys(XBTUSD) {
market(side=buy, amount=100);
}
```

**Sell 100 contracts**

Just sell 100 contracts.

```
myApiKeys(XBTUSD) {
market(side=sell, amount=100);
}
```

**Buy 50% of my balance**

Our balance is 1 BTC, which at the current price is worth $10,000. So, 50% of $10,000 is $5,000, so this will buy 5,000 contracts.

```
myApiKeys(XBTUSD) {
# could also use amount=50%b (the b meaning Balance)
market(side=buy, amount=50%);
}
```

**Buy using 50% of my available margin**

In this example, we have to refer to our current leverage - 10x in the example. The bot does not consider your current leverage in these calculations, you will need to be aware of your leverage settings and calculate a suitable size.

To use up 50% of our margin balance at 10x leverage, we need to buy 50 x 10 = 500% of our current balance. Our balance is $10,000, so 500% of 10,000 is 50,000. This order will buy 50,000 contracts, based on the assumptions above.

```
myApiKeys(XBTUSD) {
# could also use amount=500%b (the b meaning Balance)
market(side=buy, amount=500%);
}
```

**Buy 50% of my availabe balance**

As we have an open position, some of our balance is locked up, leaving only $9,000 worth available. 50% of 9,000 is 4,500, so our order will be for 4,500 contracts. Notice we are using `%%`

instead of `%`

to say we'd like to use Available Balance instead of Balance. We could also have used `%a`

, for percentage of available balance.

```
myApiKeys(XBTUSD) {
# could also use amount=50%a (the a meaning Availabel Balance)
market(side=buy, amount=50%%);
}
```

**Sell 50% of my open position size**

By using `%p`

, we can tell the bot to calculate a percentage of our current open position size. In the assumptions above we said we have an open position of 10,000 contracts. 50% of 10,000 is 5,000, so this will sell 5,000 contracts.

```
myApiKeys(XBTUSD) {
market(side=sell, amount=50%p);
}
```

**Double my open position size**

By using `%p`

, we can tell the bot to calculate a percentage of our current open position size. In the assumptions above we said we have an open position of 10,000 contracts. 100% of 10,000 is 10,000, so this will buy 10,000 contracts.

```
myApiKeys(XBTUSD) {
market(side=buy, amount=100%p);
}
```

#### Examples using *position*

A reminder of the assumptions we are using for the calculations below...

- You are trading on BitMEX on the XBTUSD symbol
- You have a balance of 1 BTC, to keep the maths simpler.
- The Bitcoin price is $10,000, also for simplicity
- You have your leverage slider at 10x
- You have an existing open position of 10,000 contracts using up 10% of your margin.
- A reminder that 1 contract is worth 1 dollar.

**Adjust my position to 5,000**

Our starting position was 10,000 contracts (see assumptions above), but we want it to be 5,000. The command below will work out the difference between where we are (10,000 long) and where we want to be (5,000 long). The result is to sell 5,000 contracts.

```
myApiKeys(XBTUSD) {
market(position=5000);
}
```

**Close my open position**

This example will close our current position, regardless of if we are long or short at the time, and regardless of our current position size.

Our starting position was 10,000 contracts, and our target size is 0 contracts. In this example it will sell 10,000 contracts, taking us down to zero.

```
myApiKeys(XBTUSD) {
market(position=0);
}
```

**Adjust my position to 2,000 Short**

Our starting position was 10,000 contracts (see assumptions above), but we want it to be -2,000. In the command below, the bot will work out the difference between where we are now (10,000) and where we want to be (-2,000). As a result, it will need to sell 12,000 contracts.

Remember that whatever our starting position size / direction, we'll always end up with 2,000 contracts short (-2,000) after this command

```
myApiKeys(XBTUSD) {
market(position=-2000);
}
```

**Adjust my position to 50% of my balance**

Our balance is $10,000 (1 btc). 50% of 10,000 is 5,000. As our current position is already 10,000, this will sell 5,000 contracts.

```
myApiKeys(XBTUSD) {
market(position=50%);
}
```

**Adjust my position so that I am short, using 50% of my available margin**

In this example, we have to refer to our current leverage - 10x in the example. The bot does not consider your current leverage in these calculations, you will need to be aware of your leverage settings and calculate a suitable size.

To use up 50% of our margin balance at 10x leverage, we need to buy 50 x 10 = 500% of our current balance. As we want to be short, we'll ask for a position of -500%.

Our balance is $10,000, so -500% of 10,000 is -50,000. As we want to end up with a position of -50,000, the bot also needs to take into account the current position size, which is +10,000 contracts (see assumptions).

To get from where we are now (+10,000) to where we want to be (-50,000), the bot will need to sell 60,000 contracts.

```
myApiKeys(XBTUSD) {
market(position=-500%);
}
```

**Adjust my position to be 80% of its current size**

Our current position is 10,000. 80% of our current position is 8,000. The bot needs to take us from our current position size of 10,000 to our target position size of 8,000, so it will sell 2,000 contracts

```
myApiKeys(XBTUSD) {
market(position=80%p);
}
```

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.