API quick start

Setting up your first trading bot with the Trality Code Editor using basic python is simple! Several built-in functions as well as the numpy library (more to be added soon) are at your disposal.

Let's draw up a sketch of our first trading but that will perform a simple crossing moving average strategy:

@schedule(interval="5m", symbol="BTCUSDT")
def handler(state, data):
sma_long = data.sma(40).last
sma_short = data.sma(20).last
has_position = has_open_position(data.symbol, truncated=True)
if sma_short > sma_long and not has_position:
print("Buying {}".format(data.symbol))
create_order(symbol=data.symbol,amount=0.001)
elif sma_short < sma_long and has_position:
print("Selling {}".format(data.symbol))
close_position(data.symbol)

Parts of a simple trading bot

Let's break down the bot above line-by-line

@schedule(interval="5m", symbol="BTCUSDT")

What you are seeing in the first line is our Schedule decorator. You can annotate any handler function in your code with this decorator to make it run in a specific interval, for a specified set of symbols. In this example, the function will be called every 5 minutes and the handler function will receive data for the symbol or trading pair BTCUSDT - where BTC is the base asset and USDT is the quoted asset.

def handler(state, data):

The specified handler function that you annotate will receive two arguments: state and data. While the State object can be used to store any variables you like between different handlers, the Data object contains your requested information on the symbol BTCUSDT. This object has many built-in functions, such as the more than 100 technical analysis indicators that can be computed directly from the data object.

sma_long = data.sma(40).last
sma_short = data.sma(20).last

As you can see, you can directly compute financial indicators from the data object. In this case we are computing the Simple Moving Average with two different periods (40 and 20 candles) and then selecting the last value.

has_position = has_open_position(data.symbol, truncated=True)

In our strategy we want to avoid having more than 1 open position for our symbol BTCUSDT. We use one of the many Utility functions to retrieve whether we currently have an open position. The truncated parameter assures that very small positions (e.g. Dust) is not considered as an open position.

if sma_short > sma_long and not has_position:
print("Buying {}".format(data.symbol))
create_order(symbol=data.symbol,amount=0.001)
elif sma_short < sma_long and has_position:
print("Selling {}".format(data.symbol))
close_position(data.symbol)

This is the basic logic of the trading bot. Note, that we are comparing both the moving averages and the has_position flag to decide whether to buy or sell. A buy order for an amount of 0.001 BTC is signaled if the shorter SMA is larger than the longer, however only if no open position already exists. This opened position is closed again in case the shorter SMA has a smaller value than the longer one.

The create_order function places a request on the respective exchange to buy 0.001 BTC. The close_position function on the other hand closes an already existing BTC position should it exist.

Note, that for create_order an amount > 0 indicates a buy order, whereas an amount < 0 refers to a sell order!

That's it! After this quick introduction you should be able to create your own basic trading bots. Wasn't that difficult, right? However, to be able to use all the features provided by the Trality API, it is highly recommended to keep studying this documentation.