Skip to main content

Reset global variables with 'resetVariable' if AutoConfig filters fail

· 5 min read

Gunbot's AutoConfig supports global state via setVariable when filters pass. Use resetVariable to change or revert those globals when the job's main filters do not pass.

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.

setVariable vs. resetVariable

In a typical AutoConfig job that processes pairs:

  • setVariable runs when at least one pair passes the job's filters (or filters2, etc.).
  • resetVariable runs when the job executes but no pairs pass the filters.

For jobs that use a global filter (for example, a custom filter checking this.variables), setVariable applies on true and resetVariable on false.

Configuration Example

This example sets a global strongBuyActive flag when a filter passes, and clears it when the filter fails.

{
"strongSignalDetector": {
"enabled": true,
"type": "manageOverrides", // Type can vary; logic depends on filters
"schedule": "*/5 * * * *",
"pairs": {
"exchange": "binance",
"include": "USDT-BTC" // Used as index
},
"filters": {
"isStrongBuyConditionMet": { // Complex filter to detect strong buy
"filterType": "custom",
"script": "/* ... some logic returning true if strong buy ... */" // Example
}
},
"setVariable": { // Executed if 'isStrongBuyConditionMet' is true for USDT-BTC
"strongBuyActive": true,
"lastStrongBuySignalTime": " new Date().toISOString() "
},
"resetVariable": { // Executed if 'isStrongBuyConditionMet' is false for USDT-BTC
"strongBuyActive": false
// We might not want to clear 'lastStrongBuySignalTime' here,
// or set it to null, depending on desired logic.
}
}
}

In the strongSignalDetector job:

  • If the isStrongBuyConditionMet filter passes for BTC-USDT:
    • The setVariable block executes, setting strongBuyActive to true and updating lastStrongBuySignalTime.
  • If the isStrongBuyConditionMet filter fails for BTC-USDT (meaning no pair passed the filters):
    • The resetVariable block executes, setting strongBuyActive to false.

This keeps strongBuyActive aligned with the latest check.

Dynamic Values in resetVariable

Like setVariable, values in resetVariable can be dynamic JavaScript expressions if the string value begins with a leading space.

"resetVariable": {
"consecutiveFails": " (this.variables.consecutiveFails || 0) + 1 ",
"lastFailReason": " 'Signal not strong enough at ' + new Date().toLocaleTimeString() "
}
  • " (this.variables.consecutiveFails || 0) + 1 ": Increments a counter specific to filter failures.
  • " 'Signal not strong enough at ' + new Date().toLocaleTimeString() ": Sets a descriptive string with a timestamp.

Use Cases for resetVariable

  1. Toggling flags: Set true on pass and false on fail.
  2. Default states: Revert to a known default when conditions are not met.
  3. Clearing temporary data: Reset transient values when the opportunity disappears.
  4. Failure counters: Increment counters when a job fails to find qualifying pairs.
  5. Single-job state management: Keep "on" and "off" logic together.

Important Considerations

  • Trigger condition: resetVariable runs when the job's main filters do not pass. If a job has no filters, setVariable will always run and resetVariable may not.
  • No pairs vs. filter failure: For pair-processing jobs, the trigger is "no pairs pass filters." If pairs.include yields an empty list, the behavior may differ by version.
  • Order of operations: setVariable and resetVariable are mutually exclusive per job run.
  • Clarity: If the set/reset logic grows complex, two separate jobs with inverted filters can be easier to maintain.

resetVariable is a clean way to manage "off" or default global state based on whether a job's filters pass.