Documentation
¶
Overview ¶
Package entryfilter filters the entries of the strategy of a family agent, whatever the strategy: a decorator of strategy.Strategy (see Wrap) handing the strategy a broker whose entries it asks first (see broker.go), so it behaves the same in a backtest, in paper and live. Its filters, each off by default (see Config and RegisterFlags):
- the regime of the market (A1, regime.go): enter only while it trends, or only while it ranges, measured by Kaufman's efficiency ratio, Wilder's ADX or the Hurst exponent; and no entry while the ATR is at an extreme percentile of its recent values;
- the equity curve (A2, tracker.go): no entry while the equity curve of the theoretical trades of the strategy, every entry it attempts, is under its moving average, until it gets back above it;
- the meta-labeling (A3, agents/metalabel): no entry a model trained on the past entries of the strategy gives too low a probability of reaching its target before its stop; and the samples to train it on recorded;
- the intermarket filter (B8, intermarket.go): buy and sell only as a reference symbol not traded (eg: the dollar index against gold) stands against its EMA.
An entry refused returns order.ErrEntriesBlocked wrapped with the reason of the filter, which the strategies take as a block, as they do the blocks of the bot (it never touches them: BlockEntries and HoldEntries belong to the controller and to the backtest), told once per change of reason and pair. A filter not ready (too few candles, the reference missing or stale) refuses the entries: never taken for a favourable one. The exits always pass.
Nothing reads the future: the filters of an entry decided at the close of a candle read the candles up to it (the ones handed to the strategy), and the candles of the reference closed by then.
Index ¶
Constants ¶
const ( RegimeTrend = "trend" // the market trending: the value of the method at or above the threshold RegimeRange = "range" // the market ranging: below it )
The regimes an entry may be asked to be in (Config.Regime).
const ( MethodER = "er" // Kaufman's efficiency ratio MethodADX = "adx" // Wilder's ADX MethodHurst = "hurst" // the Hurst exponent, by rescaled range )
The methods measuring the regime (Config.RegimeMethod), see regime.go.
const ( IntermarketInverse = "inverse" // buy while the reference is below its EMA, sell while above IntermarketDirect = "direct" // buy while above, sell while below )
The modes of the intermarket filter (Config.IntermarketMode).
const ( DefaultERLen = 10 DefaultERThreshold = 0.30 DefaultADXLen = 14 DefaultADXThreshold = 25 DefaultHurstLen = 100 DefaultHurstThreshold = 0.50 DefaultATRWindow = 100 DefaultEquityMaxBars = 100 DefaultMetaMaxBars = 100 DefaultIntermarketEMA = 50 DefaultIntermarketMode = IntermarketInverse )
The defaults of the filters: the lookback and the threshold of each method of the regime when 0 (er: 10 bars, the period of Kaufman's adaptive average, and 0.30, a threshold commonly read into his ratio, ours; adx: 14 bars, Wilder's period, and 25, the usual reading of his index, Wilder speaking of the ADXR; hurst: 100 returns, ours, and 0.5, the random walk), the window of the ATR percentile, the horizon of the theoretical trades of the equity curve and of the labels of the samples, the EMA of the reference and the mode of the intermarket filter.
Variables ¶
var Flags = []string{flagRegime, flagRegimeMethod, flagRegimeLen, flagRegimeThreshold, flagRegimeMaxATRPct,
flagRegimeATRWindow, flagEquity, flagEquityMaxBars, flagMetaModel, flagMetaThreshold, flagMetaRecord,
flagMetaMaxBars, flagIMSymbol, flagIMMode, flagIMEMA, flagIMPairs}
Flags are the names of the flags of RegisterFlags, eg: for a list of the shared inputs.
Functions ¶
func RegisterFlags ¶
RegisterFlags defines the flags of the entry filter on fs (see Config), each off by default, and returns what builds the Config of them once fs is parsed: its defaults applied, validated, and a flag of a filter set without the filter (eg: -regime-len without -regime) refused rather than ignored.
func Wrap ¶
Wrap returns s filtered as cfg says (see the package), s itself when no filter is on (cfg.Enabled false) nor a gate (cfg.Gate): the agents without a filter trade as they did. With a gate alone, the strategy only has its entries on the assets the gate refuses refused, nothing measured nor read more. It fails on a config that can't filter, or a model of -meta-model that can't be read.
Types ¶
type CandleSource ¶
type CandleSource interface {
CandlesByLimit(ctx context.Context, pair, period string, limit int) ([]model.Candle, error)
}
CandleSource reads the last candles of a symbol, complete ones only, eg: the market data of MT5 (paper), its broker (live), or the feed of a backtest, which answers only the candles closed by its simulated now.
type Config ¶
type Config struct {
Regime string
RegimeMethod string
RegimeLen int
RegimeThreshold float64
RegimeMaxATRPct float64
RegimeATRWindow int
EquityCurve int
EquityCurveMaxBars int
MetaModel string
MetaThreshold float64
MetaRecord string
MetaMaxBars int
IntermarketSymbol string
IntermarketMode string
IntermarketEMA int
IntermarketPairs []string
Agent string
Strategy string
Source string
Run int64
Segment string
RecordFrom time.Time
// Gate, when set, tells the assets the strategy may open positions on now
// (the window of the trading hours open), asked before any filter
Gate *agentapi.PairGate
// Claim, when set, claims an entry the filters let through of the
// controller (the exposure limit of the agents), last, just before it is
// sent: whether it may go, and why not
Claim func(pair string, side model.SideType) (bool, string)
}
Config is the entry filter of an agent, shared by the strategies of every family agent (see RegisterFlags), each filter off at its zero value, see Enabled:
- Regime ("", RegimeTrend, RegimeRange): enter only while the market is in that regime, measured by RegimeMethod over RegimeLen candles, trending at or above RegimeThreshold (above it for the Hurst exponent);
- RegimeMaxATRPct (0 off, up to 100): no entry while the percentile of the ATR of 14 over the RegimeATRWindow candles before is above it, an extreme volatility, with or without Regime;
- EquityCurve (0 off, 2 or more): no entry while the equity curve of the theoretical trades of the strategy (see tracker.go) is under its SMA of its last EquityCurve points, judged once that many trades ended, each trade ended after EquityCurveMaxBars candles at most;
- MetaModel (a file of metalabel.Model): an entry only when the model gives it a probability of its target first at or above MetaThreshold (0: the model's own);
- MetaRecord (a JSON lines file): every entry attempted appended to it as a metalabel.Sample once its label is known, MetaMaxBars candles at most after it (never a filter);
- IntermarketSymbol (a symbol not traded, eg: DXYm): buy and sell only as the reference stands against its EMA of IntermarketEMA candles, in the IntermarketMode, on the pairs of IntermarketPairs (none: all of them).
Agent and Strategy name the agent and its strategy in the samples, Source where they were recorded (metalabel.SourceLive, SourcePaper or SourceBacktest), Run the backtest run of the controller (0 for none) and Segment its segment (metalabel.SegmentFit or SegmentValidation, "" for none), so that the training tells them apart. An entry decided before RecordFrom (zero for none), the warm-up of a backtest, is neither recorded (the run of the segment before covers it, its samples recorded once) nor counted by the equity curve (which starts the period empty, as it starts live).
func (Config) Enabled ¶
Enabled reports whether a filter is on, or the samples recorded: off, the strategy is left as it is (see Wrap).
func (Config) References ¶
References returns the symbols the filter reads but never trades: the one of the intermarket filter, if any.