Skip to main content

Access Gunbot's main config in custom AutoConfig filters via this.config

ยท 6 min read

When writing custom JavaScript filters for Gunbot's AutoConfig, you often need to make decisions based not just on market data, but also on settings from your main Gunbot configuration (config.js). AutoConfig provides a powerful context variable, this.config, which gives your custom scripts read-only access to the entire config.js object structure.

Use the AutoConfig wizard

Nearly every option that follows can be set without editing files by hand.
Click the โ‹ฎ (three-dots) menu โžœ AutoConfig, step through the wizard, and press Save; it will write a correct autoconfig.json for you.

Understanding this.configโ€‹

Inside the JavaScript environment of an AutoConfig custom filter, the this keyword refers to a special context object. One of its most useful properties is config. This this.config object is essentially a live (at the time of filter execution) JavaScript representation of your entire config.js file.

This means your custom filter can inspect:

  • Global bot settings (e.g., this.config.bot.WATCH_MODE, this.config.bot.VERBOSE).
  • Exchange settings (e.g., this.config.exchanges[this.exchangeName].delay).
  • Strategy definitions (e.g., this.config.strategies.stepgrid.GAIN).
  • Specific pair overrides (e.g., this.config.pairs[this.exchangeName][this.pairName].override.GAIN).
  • GUI settings and more.

This allows for highly adaptive filter logic that can respond to the overall configuration of your Gunbot instance.

How to Use this.config in a Scriptโ€‹

You access values from this.config using standard JavaScript object property access (dot notation or bracket notation). The path to the desired setting mirrors its location in your config.js structure.

Consider this AutoConfig job with a custom filter:

{
"adaptivePairManager": {
"enabled": true,
"type": "manageOverrides", // Job to change overrides
"schedule": "*/5 * * * *",
"pairs": {
"exchange": "binance",
"include": "USDT-"
},
"filters": {
"checkGlobalSettingAndPairOverride": {
"filterType": "custom",
"script": "/* Accessing this.config */ \n const jobName = 'adaptivePairManager'; /* Or get dynamically */ \n const currentExchange = this.exchangeName; \n const currentPair = this.pairName; \n \n // Check a global bot setting \n if (this.config.bot.WATCH_MODE === true) { \n console.log('Bot is in WATCH_MODE, filter fails for ' + currentPair); \n return false; // Don't apply overrides if bot is in watch mode \n } \n \n // Check if the pair itself has a specific override \n let currentPairGainOverride = this.config.pairs[currentExchange]?.[currentPair]?.override?.GAIN; \n \n if (typeof currentPairGainOverride === 'number' && currentPairGainOverride < 0.5) { \n console.log('GAIN for ' + currentPair + ' is already very low (' + currentPairGainOverride + '), filter passes to adjust it.'); \n return true; // Pass if GAIN is already set and very low \n } \n \n // Accessing a strategy parameter (assuming pair has a strategy assigned) \n const pairStrategyName = this.config.pairs[currentExchange]?.[currentPair]?.strategy; \n if (pairStrategyName) { \n const strategyStopLimit = this.config.strategies[pairStrategyName]?.STOP_LIMIT; \n if (typeof strategyStopLimit === 'number' && strategyStopLimit < 100) { \n console.log('Strategy ' + pairStrategyName + ' STOP_LIMIT is low for ' + currentPair + ', filter passes.'); \n return true; \n } \n } \n \n return false;"
}
},
"overrides": { // Overrides to apply if the custom filter passes
"GAIN": 1,
"STOP_LIMIT": 200
}
}
}

In the script for checkGlobalSettingAndPairOverride:

  1. this.config.bot.WATCH_MODE === true: It checks if the global WATCH_MODE is enabled. If so, the filter fails, preventing overrides from being applied.
  2. this.config.pairs[currentExchange]?.[currentPair]?.override?.GAIN: It safely accesses the GAIN override specific to the current pair being evaluated. The ?. (optional chaining) prevents errors if parts of the path don't exist.
  3. this.config.strategies[pairStrategyName]?.TRADING_LIMIT: It retrieves the TRADING_LIMIT from the definition of the strategy currently assigned to the pair.

Practical Examples and Use Casesโ€‹

  1. Conditional Logic Based on Global Settings:

    • Only add new pairs if WATCH_MODE is false.
    • Adjust filter sensitivity based on a custom global setting in config.js like this.config.bot.riskLevel = "high".
  2. Interacting with Pair-Specific Overrides:

    • A manageOverrides job could check if a pair's STOP_LIMIT is already very tight before attempting to tighten it further.
    • Filter for pairs whose STOP_LIMIT override (used as an example for a numeric parameter) is below a certain threshold.
  3. Reading Default Strategy Parameters:

    • If a pair has no override for GAIN, your filter could read the default GAIN from its assigned strategy definition: this.config.strategies[strategyName].GAIN.
  4. Checking Exchange-Level Settings:

    • Verify if the target exchange is marked as enabled in config.js (though AutoConfig itself usually handles this at a higher level).
    • const_delay = this.config.exchanges[this.exchangeName].delay;

Important Considerationsโ€‹

  • Read-Only Access: this.config provides a snapshot for reading. Any modifications your script makes to this.config (e.g., this.config.bot.WATCH_MODE = false;) will not persist to your actual config.js file and will only affect the this.config object within the current script's execution scope. To change config.js, an AutoConfig job must successfully complete its intended action (e.g., an addPairs or manageOverrides job type writing to the config).
  • Configuration Structure Awareness: You need to know the structure of your config.js to correctly path to the settings you need. An incorrect path will result in undefined.

Using this.config in your custom AutoConfig filters dramatically expands their capabilities, allowing for intelligent, context-aware automation that can adapt to your overall Gunbot setup and strategy configurations. It enables a deeper integration between AutoConfig's decision-making and your established Gunbot settings.