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.
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:
overridesvariablespairVariablesmaxPairstargetin filter typecustom
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:
| Reference | Content |
|---|---|
this.config | The complete Gunbot config. |
this.pair | All pair data the bot works with, identical to the data available in JSON state files. |
this.pairName | String representing the pair currently being processed. |
this.variables | All AutoConfig variables. |
this.pairVariables | All AutoConfig pair-specific variables. |
this.tickers | All collected ticker snapshots, in case it's run in a job type that works with tickers. |
this.userData | User-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
trueif the bag value is bigger thanMIN_VOLUME_TO_SELL, andfalseotherwise:{
"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)"
}
}