Skip to main content

Custom Code

Many elements in Gunbot's AutoConfig can be calculated dynamically when a job executes. This capability allows power users to create completely 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 the internal Gunbot data available to your code. It's common to encounter undefined data. You can handle these potential errors by adding a filter that checks if the data exists or, for example, 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 the context in which you are trying to access it. For example, a dynamic pair override value will have access to all pair-specific data (because overrides are set for each pair during a single job run). However, a global variable won't have this access (as global variables are set only once per job run).

Examples of Valid Expressions​

Here are some examples of valid expressions you can use:

  • 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 needs to be minified (all code on a single line) to work, as 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)"
}
}