-
Notifications
You must be signed in to change notification settings - Fork 20
Indicators
The environment connect to influxdb to fetch candle and then process it at each timestep. The environment is plug with a CandleSet class which manage the candles data (buffer, timeframe, indicators computation, etc...)
We can easily map indicators to the candleSet when configuring an environment. A candleSetPlugin is define as:
{
"label": "sma6h", // name of the indicator (any string)
"opts": { // indicator opts
"name": "sma", // indicator name (cf: src/indicators)
"period": 10, // sma calculate on 10 candles
"key": "close", // sma on which key (open/high/low/close/volume)
"aggTime": "15m" // OPTIONAL: You can specify which candlestick timerange you want to get as parameter
}
}
In the following example we define an environment with 2 indicators sma6h and var1hsma6h.
Be carrefull of the plugin order when using indicator in another indicator. (here first sma then diff)
"env": {
// Watch 2 currency (ETH/USDT, BTC/USDT) be carefull to import data using influx-crypto-watcher
"watchList": [
{"base": "BTC","quote": "USDT","exchange": "binance"}
{"base": "ETH","quote": "USDT","exchange": "binance"}
],
"aggTimes": ['15m', '4h', 1d'],
"warmup": 1500,
"batchSize": 10000,
"bufferSize": 5000,
"backtest": {
"start": "2018-02-15 00:00:00",
"stop": "2018-09-15 00:00:00"
},
"candleSetPlugins": [
{
"label": "sma6h",
"opts": {
"name": "sma",
"period": 360,
"aggTime": "15m", // Will receive candles agg by 15 minutes (00:00, 00:15, 00:30, ...)
"key": "close"
}
},
{
"label": "var1hsma6h",
"opts": {
"name": "diff",
"period": 60,
"key": "indicators.sma6h"
}
}
]
}
You can easily create new indicators in src/indicators (the filename is use as indicator name).
type CandleIndicator = (label: string, opts: any) => CandleSetPlugin;
type CandleSetPlugin = (candles: Candle[], newCandle: Candle) => Promise<{ [name: string]: any }>;
Example:
Create a new file => src/indicators/divide
Then you must export a CandleIndicator.
This indicator is dividing the given key by the given factor
import { Candle } from '../_core/Env/CandleSet';
import { CandleIndicator } from './CandleIndicator';
const divide: CandleIndicator = (label: string, opts: any) => {
// indicators static variables
const scope = {};
// Process function
// This function is called with each new candle
return async (candles: Candle[], newCandle: Candle) => {
return { [label]: newCandle[opts.key] / opts.factor};
};
};
export default divide;
Then you can use it when configuring environment (here divide close by 2 and name it close-divide-by-2)
{
"label": "close-divide-by-2",
"opts": {
"name": "divide",
"key": "close"
"factor": 2,
}
}