Skip to main content

What are Custom Trading Strategies?

In Gunbot, custom trading strategies allow users to develop and execute their JavaScript strategy code for both spot and futures trading. These strategies leverage Gunbot's built-in exchange connectivity, trading methods, market data, and indicators, offering a powerful tool to build your own trading bots.

Features of Custom Strategies

Custom strategies in Gunbot are defined by their flexibility and technical integration:

Integration with Built-in Methods

Use Gunbot's extensive range of trading methods and indicators. This integration allows you to:

  • Implement complex trading strategies combining multiple indicators.
  • Access real-time market data for informed decision-making, for as many timeframes as you need.
  • Easily access data like the unit cost including fees, for remaining balances for spot assets. This is data that can be very tricky to calculate, it's readily provided by Gunbot.

Execution Cycle

Custom strategies operate as an asynchronous (async) function, running once per 'cycle' for each trading pair. This means:

  • Each trading pair triggers the strategy independently.
  • While each pairs strategy triggers independently, they can access data for other pairs, and place orders on other pairs as well.
  • Strategies can be designed to react to market changes on a per-cycle basis.
  • You have the flexibility to implement time-sensitive trading logic.

Flexibility and Customization

Custom strategies offer unparalleled flexibility:

  • Write strategies in plain JavaScript without the need for specific Gunbot syntax.
  • Modify and test strategies quickly to adapt to changing market conditions.
  • Integrate additional data sources and custom logic as needed.

Charting included

By default custom trading strategies will have all their trades visualized on the integrated TradingView charts in Gunbot. Extend this with:

  • Dynamically placed custom chart elements, like lines, boxes, icons, etc.
  • Custom stats in the chart sidebar, to show the metrics that matter to your strategy.
  • Timescale marks, to leave notes on the chart, per bar.

Environment for Custom Strategy Code

Understanding the environment in which custom strategies operate is crucial for effective strategy development.

Setting Up the Environment

  • Strategy File Location: Custom strategies should be saved in the ./customStrategies directory within your Gunbot installation.
  • Language: Strategies are written in plain JavaScript, providing a familiar and flexible coding environment.

Understanding the gb Object

The gb object is a central component in custom strategies, providing access to various trading methods and data. Examples of gb object usage include:

  • Fetching current market prices.
  • Accessing trading indicators like RSI, MACD, and Bollinger Bands.
  • Executing buy and sell orders.
  • Provides an object for persistent storage.

Writing Custom Strategies

Each custom strategy runs as an async function, executed per cycle for a trading pair. You only need to write the code that is inside the function and you do not need to return anything.

Here is a basic example:

// set buy and sell conditions
const buyConditions = gb.data.rsi < 30 && !gb.data.gotBag;
const sellConditions = gb.data.rsi > 70 && gb.data.gotBag;

if (buyConditions) {
const buyAmount = gb.data.baseBalance * 0.95;
gb.method.buyMarket(buyAmount, gb.data.pairName);
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
}

Technical Considerations and Limitations

When working with custom strategies, consider these technical aspects:

Ensuring Safety and Reliability

  • Custom strategies do not have inherent safeguards like built-in strategies. Ensure thorough testing and validation.
  • Implement proper error handling and logging to troubleshoot issues.

Node.js Compatibility

  • Ensure your strategies are compatible with Node v14.4.0. This includes using features and syntax supported by this version of Node.js.

Effective Logging

  • Use console.log() for logging within the Gunbot console. This helps in debugging and monitoring strategy performance.

Handling Asynchronous Operations

  • Leverage the async nature of custom strategies for fetching additional data and performing non-blocking operations. Example:
// since the code runs in an async function, you can use the await keyword everywhere
const response = await fetch('https://api.example.com/data');

// do stuff with the response of custom api call

Custom Modules

Xou can enhance your strategies with custom JavaScript modules to modularize and reuse code.

Creating Custom Modules

Using Custom Modules in Strategies

  • Store custom modules in the user_modules folder within your Gunbot installation.
  • Example module (./user_modules/myModule.js):
// require the module
const myModule = gb.method.require(gb.modulesPath + '/myModule.js')

// rest of strategy code

Best Practices

  • Testing: Regularly test your strategies in a simulated environment before deploying them live.
  • Debugging: Use logging and debugging tools to identify and resolve issues quickly.
  • Optimizing: Continuously optimize your strategies based on performance data and market conditions.

Conclusion

Custom strategies in Gunbot provide a powerful and flexible way to automate trading based on your unique preferences and insights. By leveraging the extensive range of features and following best practices, you can develop effective strategies that enhance your trading performance. Experiment, innovate, and share your custom strategies with the community to contribute to the collective knowledge and success of Gunbot users.