Skip to main content

Custom Trading Strategy Code Examples

Each example is a complete JavaScript strategy for one spot pair. On each strategy cycle, the script checks the latest completed candle. It stores the candle timestamp before submitting a market order to limit submissions to one per signal candle.

Backtest an example​

  1. Open Code Editor in Gunbot Live. Paste one example into a new strategy file, set the editor target to Spot, and select Save & Backtest.

  2. In Backtesting, choose Binance and USDT-BTC. Set Initial Balance to 1,000 USDT and Trading Fees to 0.1%.

  3. In the strategy settings, set Main timeframe (PERIOD) to 1 hour. Paste these values into JSON overrides.

    {
    "TRADING_LIMIT": 100,
    "MIN_VOLUME_TO_SELL": 10
    }

    On USDT-BTC, these values set a 100 USDT buy budget and a 10 USDT minimum position value. The script skips a buy when the available USDT balance is at or below the buy budget.

  4. Set a date range, for example January through March 2024, and select Run. Each script waits for enough candle history to calculate its indicators. The longest warm-up needs 85 completed candles. Check the trade ledger for entries and exits.

You can also forward-test on a dedicated spot pair in Simulator mode. Start with an unheld asset and no open orders. On USDT-BTC, the scripts treat a BTC balance worth more than the minimum volume to sell as an open position and can sell that balance.

Code examples for custom strategies​