Skip to content

Quick Start

Welcome to the TradeHub scripting!
This guide walks you through building your first trading bot script step-by-step.

Scripts follow a standard structure with two functions:

  • setup(): Runs once when the bot starts, ideal for configuration and initialization.
  • update(): Runs on every update cycle (per candle or tick), executing your trading logic.

  1. setup() → Used for configuration and initialization.
    Can be sync or async (async only needed if you use await).
  2. update() → Runs continuously and handles trading logic.
    Must be async because order placement functions (e.g., order.buy()) use await.

graph TD
  A[Bot Starts] --> B["Run setup()"]
  B --> C["Wait for trigger (candle/tick)"]
  C --> D["Run update()"]
  D --> C

In setup(), configure your bot using config.set():

async function setup() {
config.set({
stopLoss: 1.5, // Stop loss at -1.5%
takeProfit: 3, // Take profit at +3%
balance: 1000, // Starting quote balance (backtest/paper only)
updateTrigger: "candle", // Run update once per candle close
})
log("Bot configured successfully!")
}

You can register indicators in setup() using ind.add():

async function setup() {
config.set({ stopLoss: 1.5, takeProfit: 3, balance: 1000, updateTrigger: "candle" })
ind.add({ name: 'rsi', type: 'rsi', options: { period: 14 } }) // Add RSI(14)
log("RSI indicator added.")
}

Retrieve indicator values in update() with ind.get():

async function update() {
const rsi = ind.get('rsi')
if (rsi === null) return // Wait until indicator initializes
log("RSI Value:", rsi)
}

Use order.buy() and order.sell() to trade. These are async functions, so they must be awaited:

async function update() {
/* for example */
const rsi = ind.get('rsi')
if (rsi === null) return // Wait until RSI builds enough data
// Example logic:
// - Buy when RSI crosses above 30 (oversold)
// - Sell when RSI crosses below 70 (overbought)
if (ta.crossup("rsi", 30)) {
await order.buy();
} else if (ta.crossdown("rsi", 70)) {
await order.sell();
}
}

Here’s the complete script putting it all together:

async function setup() {
config.set({
stopLoss: 1.5,
takeProfit: 3,
balance: 1000,
updateTrigger: "candle",
})
ind.add({ name: 'rsi', type: 'rsi', options: { period: 14 } })
log("Setup complete: RSI(14) added.")
}
async function update() {
// Example logic:
// - Buy when RSI crosses above 30 (oversold)
// - Sell when RSI crosses below 70 (overbought)
if (ta.crossup("rsi", 30)) {
await order.buy();
} else if (ta.crossdown("rsi", 70)) {
await order.sell();
}
}

  • setup() can be sync if no await is used.

  • update() must always be async (orders require await).

  • Always begin in simulation (Backtest or Paper) before trading live:

    log("Order Mode:", trader.orderMode) // "simulated" or "live"
  • Indicators need warm-up: Skip trading if ind.get() returns null.