Tasilab
Home Pricing Docs EN · عربي Sign in Start free →
Home / Guides / First trading bot

Build your first trading bot.

A trading bot is just a loop: watch the price, decide, act. Here's a complete one for the Saudi Exchange in about 30 lines of Python — and because it trades paper, you can run it live with zero risk.

We'll build a moving-average crossover bot on Saudi Aramco (2222): track a short (5-period) and long (20-period) average of the price, buy when the short crosses above the long (a "golden cross"), and sell when it crosses back below (a "death cross"). It runs against your paper portfolio on Tasilab, so no real order is ever placed.

Setup

Terminal
pip install tasilab
export TASILAB_API_KEY="your-api-key"

Don't have a key yet? Sign up free — you get one plus SAR 100,000 in paper capital.

The bot

sma_bot.py
import os
import time
from collections import deque
from tasilab import Tasilab

SYMBOL, SHORT, LONG, QTY, POLL = "2222", 5, 20, 100, 30

tasi = Tasilab(api_key=os.environ["TASILAB_API_KEY"])
prices = deque(maxlen=LONG)

# Are we already holding this symbol?
in_position = any(p["symbol"] == SYMBOL and p["quantity"] > 0
                  for p in tasi.positions())

while True:
    price = float(tasi.get_quote(SYMBOL)["price"])
    prices.append(price)

    if len(prices) < LONG:                       # warm up first
        print(f"collecting… {len(prices)}/{LONG}")
        time.sleep(POLL)
        continue

    short_ma = sum(list(prices)[-SHORT:]) / SHORT
    long_ma  = sum(prices) / LONG

    if short_ma > long_ma and not in_position:
        order = tasi.buy(SYMBOL, quantity=QTY)    # paper market order
        in_position = True
        print("BUY ", price, "->", order["status"])
    elif short_ma < long_ma and in_position:
        order = tasi.sell(SYMBOL, quantity=QTY)
        in_position = False
        print("SELL", price, "->", order["status"])

    time.sleep(POLL)

Run it with python sma_bot.py. It polls the quote every 30 seconds, keeps the last 20 prices, and acts only on a crossover. Every fill applies the real 0.155% commission plus 15% VAT and lands in your paper wallet.

Backtest before you run it. A bot that trades is only as good as the idea behind it. Before you let this loop, test the same rule against history with the backtesting guide — you'll often find the naive crossover loses to buy-and-hold, which is exactly the kind of thing you want to learn on paper, not with money.

Where to take it next

  • Trade the calendar. The bot loops continuously; wrap it to run only during TASI hours (Sunday–Thursday, 10:00–15:00 AST) — check tasi.market_status().
  • Size positions from your available cash (tasi.portfolio()["cash_balance"]) instead of a fixed quantity.
  • Log every run as a Tasilab experiment so you can compare bot versions — see the backtesting guide.
  • Swap the signal. RSI, MACD, Bollinger — the same loop shape works for any rule you can compute from the price stream.

Get your API key

Free account, an API key, and SAR 100,000 in paper capital — then point the bot at it.

Start free →
Tasilab

An API-native paper-trading sandbox for the Saudi Exchange. Trade with discipline.

Product
  • Paper trading
  • Tadawul API
  • Python SDK
  • Documentation
Guides
  • Backtest in Python
  • Historical data
  • First trading bot
Legal
  • Terms of use
  • Privacy
  • Disclaimer

Tasilab is a simulation-only paper-trading environment. No real orders are placed on the Saudi Exchange. Tasilab is not licensed by the Capital Market Authority and does not provide investment advice.

© 2026 Tasilab