Skip to main content

Custom Code

Many elements in Gunbot's AutoConfig can be calculated dynamically when a job executes. This enables fully dynamic trading scenarios. Every valid JavaScript expression can be used.

Info

A JavaScript expression is any valid unit of code that resolves to a value. It can include variables, operators, function calls, and literals, which are combined to produce a single result.

You can use calculated values for the following elements in AutoConfig:

  • overrides
  • variables
  • pairVariables
  • maxPairs
  • target in filter type custom

To use an expression instead of a static string or number, enter your expression as a string with a leading blank space:

{
"validExpression": " 1 + 2"
}

When using custom JavaScript expressions, be careful with internal Gunbot data. It is common to encounter undefined data. You can guard against this by adding a filter that checks whether data exists or has a value greater than 0 before using it in an expression.

Use the following references to access internal Gunbot data within your expressions:

ReferenceContent
this.configThe complete Gunbot config.
this.pairAll pair data the bot works with, identical to the data available in JSON state files.
this.pairNameString representing the pair currently being processed.
this.variablesAll AutoConfig variables.
this.pairVariablesAll AutoConfig pair-specific variables.
this.tickersAll collected ticker snapshots, in case it's run in a job type that works with tickers.
this.userDataUser-defined content from the acUserData.json file located in the Gunbot root folder.

The availability of pair-specific data depends on context. For example, a dynamic pair override value can access pair-specific data because overrides are set for each pair during a single job run. A global variable cannot, because global variables are set only once per job run.

Examples of Valid Expressions

Examples of valid expressions:

  • To calculate the bag value in the base currency:

    {
    "myCalculatedOverride": " this.pair.Bid * this.pair.quoteBalance"
    }
  • To return true if the bag value is bigger than MIN_VOLUME_TO_SELL, and false otherwise:

    {
    "myComparisonResult": " (this.pair.Bid * this.pair.quoteBalance) > this.pair.whatstrat.MIN_VOLUME_TO_SELL ? true : false"
    }
  • Using an immediately invoked function expression (IIFE). Note: This example must be minified (all code on a single line) because multi-line strings are not standard in JSON.

    Original multi-line JavaScript for clarity:

    (function doStuff(data) {
    if (data.pair.Bid > 0) {
    return true;
    } else {
    return false;
    }
    })(this)

    Minified version for use in AutoConfig JSON:

    {
    "myMinifiedFunction": " (function doStuff(data){if(data.pair.Bid>0){return true}else{return false}})(this)"
    }

Example of a Custom Filter

{
"Position Bigger Than Zero": {
"type": "custom",
"target": " this.pair.quoteBalance * this.pair.Bid > parseFloat(this.pair.whatstrat.MIN_VOLUME_TO_SELL)"
}
}