Skip to main content

What Are Custom Trading Strategies in Gunbot?

Are you a developer who enjoys coding trading strategies but doesn't want to build an entire trading bot from the ground up? Gunbot's custom trading strategies offer the perfect solution. You can write your own JavaScript code for both spot and futures trading, leveraging Gunbot's robust infrastructure without worrying about exchange connectivity or data handling complexities.

Take your trading strategies to the next level with Gunbot's custom features. Take your trading strategies to the next level with Gunbot's custom features. This example uses many of the custom charting options.

With Gunbot handling the heavy lifting, you can focus entirely on building and tuning the perfect strategy. Let's explore how custom trading strategies can empower you to bring your trading ideas to life.

Why Use Custom Strategies?

If you're keen on developing trading algorithms but don't want to reinvent the wheel by coding a full-fledged bot, custom strategies in Gunbot are your ideal playground. You can implement your unique trading logic and take advantage of Gunbot's features, including:

  • Built-in exchange connectivity: Skip the hassle of coding API integrations yourself.
  • Comprehensive market data and indicators: Access real-time data across multiple timeframes without extra effort.
  • Trading methods: Use pre-built methods for executing trades easily and reliably.

Key Features of Custom Strategies

Seamless Integration with Gunbot's Tools

With custom strategies, you can tap into Gunbot's extensive range of trading methods and indicators. This means you can:

  • Combine Multiple Indicators: Build sophisticated strategies by integrating various technical indicators.
  • Access Real-Time Market Data: Make informed decisions using realtime data across as many timeframes as you need.
  • Simplify Complex Calculations: Easily obtain tricky data points like unit costs including fees for spot assets, saving you from manual calculations.

For more details on the methods and modules available, check out Methods and Modules.

Flexible Execution Cycle

Your custom strategies run as asynchronous (async) functions, executing once per 'cycle' for each trading pair. Here's what that means:

  • Independent Pair Execution: Each trading pair triggers the strategy independently, giving you granular control over each asset.
  • Cross-Pair Access: While each strategy runs per pair, you can access data and place orders on other pairs if needed.
  • Responsive to Market Changes: Design strategies that react swiftly to market fluctuations on a per-cycle basis.
  • Time-Sensitive Logic: Implement trading logic that relies on specific timing or conditions.

Write in Plain JavaScript

No need to learn a new scripting language or deal with proprietary syntax. You can write your strategies in plain JavaScript, making it easy to:

  • Iterate Quickly: Modify and test your code rapidly to adapt to changing market conditions.
  • Integrate External Data: Fetch and utilize data from external APIs to enhance your strategy.

Enhanced Charting Capabilities

By default, trades from your custom strategies are visualized on Gunbot's integrated TradingView charts. You can extend this functionality by:

  • Adding Dynamic Chart Elements: Place custom lines, boxes, icons, and more to visualize key aspects of your strategy.
  • Displaying Custom Stats: Show the metrics that matter most to you in the chart sidebar.
  • Leaving Timescale Marks: Annotate specific bars on the chart with notes or signals.

Learn how to visualize strategy targets in Visualize Strategy Targets and display custom stats in Display Custom Stats.

How to Build your own Crypto Trading Bot

Setting Up Your Environment

  • Strategy File Location: Save your custom strategy files in the ./customStrategies directory within your Gunbot installation.
  • Programming Language: Use plain JavaScript, so you don't have to spend time learning new syntax or languages.

Understanding the gb Object

The gb object is your interface to Gunbot's functionality within your custom strategy. It provides access to:

  • Market Data: Fetch current prices, volumes, and other essential market information.
  • Indicators: Use technical indicators like RSI, MACD, Bollinger Bands, and more.
  • Trade Execution Methods: Execute buy and sell orders with ease.
  • Persistent Storage: Store data across cycles without hassle.

For a comprehensive list of market data available, see Market Data. To understand how to use persistent storage, refer to Persistent Storage.

Writing Your First Custom Strategy

Each custom strategy runs as an async function executed per cycle for a trading pair. Here's a simple example to get you started:

// Define 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);
}

In this snippet, the strategy buys when the RSI is below 30 and sells when it's above 70, using most of your available balance. It's a straightforward example, but you can make your strategies as complex as you like.

For more code examples to inspire you, visit Code Examples.

Technical Considerations

Safety and Reliability

  • Test Thoroughly: Custom strategies don't come with built-in safeguards. Always test your code in a simulated environment before going live.
  • Error Handling: Implement proper error handling and logging to make troubleshooting easier.

Node.js Compatibility

  • Version Support: Ensure your strategies are compatible with Node.js v14.4.0, which is what Gunbot uses.

Effective Logging

  • Use Console Logging: Utilize console.log() to output messages in the Gunbot console. This is invaluable for debugging and monitoring your strategy's performance.

Asynchronous Operations

Since your strategy runs in an async function, you can use await for asynchronous operations like fetching external data:

// Fetch data from a custom API
const response = await fetch('https://api.example.com/data');
const data = await response.json();

// Use the data in your strategy
if (data.signal === 'buy') {
gb.method.buyMarket(gb.data.baseBalance * 0.95, gb.data.pairName);
}

Sending Notifications

Want to get notified when certain events occur? You can send custom notifications to the Gunbot GUI:

gb.method.sendNotification('Custom notification message');

Learn more about this feature in Send GUI Notifications.

Enhancing Your Strategy with Custom Modules

To keep your code organized and reusable, you can create custom JavaScript modules.

Creating and Using Custom Modules

  • Module Location: Place your modules in the user_modules folder within your Gunbot installation.
  • Using Modules: Require your module in your strategy file:
// Require the module
const myModule = gb.method.require(gb.modulesPath + '/myModule.js');

// Use functions from your custom module
myModule.doSomething();

This approach helps you manage complex strategies more effectively.

For more information, see Methods and Modules.

Best Practices

  • Continuous Testing: Regularly test your strategies under different market conditions to ensure they perform as expected.
  • Optimize Performance: Keep an eye on how your strategy behaves and make adjustments as needed for optimal results.
  • Engage with the Community: Share your experiences and learn from others in the Gunbot community to enhance your strategies further.

Conclusion

If you're a developer eager to implement your own trading strategies without the overhead of building a bot from scratch, Gunbot's custom strategies offer the perfect solution. You can focus on what you do best—coding strategies—while Gunbot handles the complexities of trade execution and data management.

Don't let the intricacies of bot development hold you back. Dive into custom strategies with Gunbot and transform your trading ideas into reality.