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.
🛠 Script Structure
Section titled “🛠 Script Structure”setup()→ Used for configuration and initialization.
Can be sync or async (async only needed if you useawait).update()→ Runs continuously and handles trading logic.
Must be async because order placement functions (e.g.,order.buy()) useawait.
🔄 Execution Flow
Section titled “🔄 Execution Flow”graph TD A[Bot Starts] --> B["Run setup()"] B --> C["Wait for trigger (candle/tick)"] C --> D["Run update()"] D --> C
1. Configuring Your Bot
Section titled “1. Configuring Your Bot”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!")}2. Adding Indicators
Section titled “2. Adding Indicators”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)}3. Placing Orders
Section titled “3. Placing Orders”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(); }}Complete Example: Basic RSI Strategy
Section titled “Complete Example: Basic RSI Strategy”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(); }}⚠️ Important Notes
Section titled “⚠️ Important Notes”-
setup()can be sync if noawaitis used. -
update()must always be async (orders requireawait). -
Always begin in simulation (
BacktestorPaper) before trading live:log("Order Mode:", trader.orderMode) // "simulated" or "live" -
Indicators need warm-up: Skip trading if
ind.get()returnsnull.
🚀 Next Steps
Section titled “🚀 Next Steps”- Read the Script API Reference for all available functions.