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