Skip to main content

Filter by % difference between two ledger values with AutoConfig

· 6 min read

Gunbot's AutoConfig supports percentage-based comparisons between two ledger keys using the differenceBigger and differenceSmaller filters. These filters help measure relative change, such as price vs. average buy price or a gap between two indicators stored in the ledger.

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.

Calculating Relative Change with Difference Filters

The differenceBigger and differenceSmaller filters measure how much one ledger value has changed relative to another, expressed as a percentage. The calculation is typically:

Percentage Difference = ((Value of Ledger Key 2 - Value of Ledger Key 1) / abs(Value of Ledger Key 1)) * 100

(Note: Implementations can vary slightly, especially around the use of abs() or division-by-zero handling. Treat this as a percentage change from Key1 to Key2.)

These filters are used in AutoConfig jobs that process existing pairs (for example, manageOverrides).

Filter Types and Configuration

  1. filterType: "differenceBigger"

    • Purpose: Passes if the calculated percentage difference (LedgerKey2 relative to LedgerKey1) is greater than a specified delta percentage.
    • Logic: ((LKV2 - LKV1) / abs(LKV1)) * 100 > deltaPercent
    • Example: Check if the current Bid price (Bid) is more than 2% above the average buy price (ABP).
      "profitMarginCheck": {
      "filterType": "differenceBigger",
      // The first field name's value is Ledger Key 1 (base for comparison)
      "averageBuy": "ABP",
      // The second field name's value is Ledger Key 2 (value being compared)
      "currentBidPrice": "Bid",
      // The third field name's value is the delta percentage
      "minProfitPct": 2
      }
      This passes if ((this.pair.Bid - this.pair.ABP) / abs(this.pair.ABP)) * 100 > 2.0. The keys averageBuy, currentBidPrice, and minProfitPct are descriptive; their values ("ABP", "Bid", 2) are what AutoConfig uses.
  2. filterType: "differenceSmaller"

    • Purpose: Passes if the calculated percentage difference is less than a specified delta percentage.
    • Logic: ((LKV2 - LKV1) / abs(LKV1)) * 100 < deltaPercent
    • Example: Check if the current Bid price (Bid) has dropped by less than 1.5% from the ABP (i.e., it hasn't dropped much, or has even gone up).
      "smallDropOrProfit": {
      "filterType": "differenceSmaller",
      "buyReference": "ABP", // Ledger Key 1
      "priceToCheck": "BID", // Ledger Key 2
      "maxAcceptableDropPct": -1.5 // Delta: Note the negative for a drop
      // This actually checks if Bid is NOT more than 1.5% BELOW ABP
      // So, if Bid is (ABP * 0.984), diff is -1.6%, fails.
      // If Bid is (ABP * 0.99), diff is -1.0%, passes.
      }
      This filter passes if ((this.pair.Bid - this.pair.ABP) / abs(this.pair.ABP)) * 100 < -1.5. A more intuitive way to check if price is "not too far below" a reference is differenceBigger with a negative delta, such as deltaPercent: -1.5.

General Configuration Structure: An AutoConfig job using these filters would look like:

{
"relativeChangeTrader": {
"enabled": true,
"type": "manageOverrides",
"schedule": "*/3 * * * *",
"pairs": { /* ... */ },
"filters": {
"significantProfitRealized": {
"filterType": "differenceBigger",
"ledgerKey1_avgCost": "ABP", // Base value for comparison
"ledgerKey2_currentPrice": "Bid", // Value to check
"delta_TargetPercent": 3.0 // Required percentage difference
}
},
"overrides": { /* Apply overrides if Bid is >3% above ABP */ }
}
}

The keys for the ledger fields and the delta (for example, ledgerKey1_avgCost, delta_TargetPercent) are for readability. AutoConfig interprets the two ledger keys and the delta based on the values you supply.

Common Ledger Keys for Difference Calculation

These filters work best with numerical ledger keys, such as:

  • Bid, Ask, ABP

Use Cases

  1. Profit/Loss Thresholds:

    • differenceBigger with ABP (key1) and Bid (key2) to check if current price is X% above average cost.
    • differenceSmaller with ABP (key1) and Bid (key2) and a negative delta to check if price has dropped more than X% below average cost (potential stop-loss condition).
  2. Indicator Spread/Convergence:

    • If your strategy stores moving average values like ema1 and ema2 in the ledger:
      • Use differenceBigger with ema2 (key1) and ema1 (key2) and a small positive delta (e.g., 0.1%) to confirm a bullish MA cross where the fast MA is clearly above the slow MA.
      • Use differenceSmaller with ema1 (key1) and ema2 (key2) and a small positive delta (e.g., 0.05%) to find MAs that are very close (converging).
  3. Distance from price levels:

    • Check if Bid is differenceBigger by X% from lowBB (if available in ledger) to see if price has bounced significantly off a lower band.

Important Notes

  • Numerical values: Both ledger keys must resolve to numbers for the calculation to be meaningful.
  • Order of keys matters: ((LKV2 - LKV1) / abs(LKV1)) * 100 measures change relative to LKV1. Swapping them flips the sign and meaning.
  • Division by zero: If Ledger Key 1 (LKV1) is zero, the calculation can fail. Keep this in mind for values like ABP that may be zero.
  • Descriptive parameter keys: Readable keys (like baseValueForCalc, comparedValue, percentageThreshold) make AutoConfig JSON easier to maintain.

The differenceBigger and differenceSmaller filters let you apply percentage-based logic to ledger values without writing custom JavaScript for common comparisons.