Documentation
¶
Overview ¶
Package protect protects the open positions of an agent: its breakeven and its trailing stop (see ui.ProtectionSettings), applied on every price the bot processes (see xtrader.CandleHook), and the ones the XTrader AI arms on a position, its trigger kept. The stop loss at the broker is its state: it only tightens it, through the stop guard of the order controller, which the strategies and the language model share (see order.Controller. TightenProtection), the most protective level winning, so that a restart goes on from it; the best price since the entry, its triggers, is rebuilt from the candles. A move never removes the stop loss: refused or lost at the broker, it is kept, and tried again after a while.
Index ¶
- Constants
- type Broker
- type Manager
- func (m *Manager) Apply(settings *ui.ProtectionSettings, arms []ui.ProtectionArm)
- func (m *Manager) ApplyDailyStops(stops []ui.DailyStop)
- func (m *Manager) Arm(request ui.SupervisionProtectRequest) ui.SupervisionProtectResponse
- func (m *Manager) DailyStops(request ui.SupervisionDailyStopsRequest) ui.SupervisionDailyStopsResponse
- func (m *Manager) Guarded(string) bool
- func (m *Manager) OnCandle(candle model.Candle)
- func (m *Manager) Protection(pair string) *ui.PositionProtection
- func (m *Manager) Protections() []ui.PositionProtection
- func (m *Manager) Synchronous(now func() time.Time)
- func (m *Manager) Wait()
- type Market
Constants ¶
const (
// ATRPeriod is the period of the ATR of the distances, Wilder's.
ATRPeriod = 14
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker interface {
PositionStop(pair string) (order.Position, model.Order, bool)
TightenProtection(leg model.Order, level float64) (model.Order, error)
SetPositionStop(pair string, side model.SideType, level float64) ([]model.Order, error)
Tick(pair string) float64
}
Broker is the order controller as the protection moves the stop losses through it, see order.Controller.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager protects the open positions of the pairs of an agent, see the package.
func Attach ¶
func Attach(ctx context.Context, bot *xtrader.XTrader, market Market, timeframe string, live bool, agent *remote.Agent, chart *ui.Chart) *Manager
Attach protects the positions of bot, a bot of agent trading on timeframe: on every price it processes (see xtrader.CandleHook), the stop guard of its order controller on while the protection runs, the settings and the arms the controller tells, shown on chart (nil for none) and armed there by the supervisor. Live, the positions are named by their identifier on the MT5 account (live true); on the paper wallet, by their entry in this run. The candles and the costs come from market: the broker live, the paper wallet in paper. Nothing without agent (a run apart from the controller), nil returned.
func New ¶
New returns the protection of the positions of an agent trading on timeframe through broker, its candles from market; refPrefix names its positions: mt5 for the ones of an MT5 account (by their identifier), paper and its run for the ones of the paper wallet (by their entry).
func (*Manager) Apply ¶
func (m *Manager) Apply(settings *ui.ProtectionSettings, arms []ui.ProtectionArm)
Apply sets the settings of the protection, nil when off, and the arms of the XTrader AI in force, as the controller sends them (see agentapi.Protection).
func (*Manager) ApplyDailyStops ¶
ApplyDailyStops sets the stop losses the daily loss limits of the controller ask of the open positions, all of them (none: no stop of the limits), see ui.DailyStop: applied on the next prices, only tightening.
func (*Manager) Arm ¶
func (m *Manager) Arm(request ui.SupervisionProtectRequest) ui.SupervisionProtectResponse
Arm arms a feature on the open position of request, by the XTrader AI, see ui.SupervisionProtectRequest: refused unless its feature is allowed to the supervisor and the position is the one held on its pair; applied at once when its trigger is reached, the answer of the broker awaited; else armed, applied once it is.
func (*Manager) DailyStops ¶
func (m *Manager) DailyStops(request ui.SupervisionDailyStopsRequest) ui.SupervisionDailyStopsResponse
DailyStops sets the stop losses the daily loss limits ask of the open positions (see ApplyDailyStops) and applies each at once, the answer of the broker awaited, see ui.SupervisionDailyStopsResponse: applied, the stop loss confirmed (as tight already, the one it is at); failed or unknown, the stop loss kept; armed, a move in flight or no price yet, applied on the next prices; refused, without a position on its pair, on another side, or with the price past its level.
func (*Manager) Guarded ¶
Guarded reports whether the stop guard of the order controller is on: the protection runs, or may be armed, or the daily loss limits ask a stop loss, see order.Controller.SetStopGuard. It reads an atomic flag only.
func (*Manager) OnCandle ¶
OnCandle protects the position of the pair of candle at its price, a candle in progress or closed, see xtrader.CandleHook: the move, when one is due, sent apart, so that the candles never wait for the broker.
func (*Manager) Protection ¶
func (m *Manager) Protection(pair string) *ui.PositionProtection
Protection returns the protection of the position of pair as it stands, nil without one, see ui.PositionProtection.
func (*Manager) Protections ¶
func (m *Manager) Protections() []ui.PositionProtection
Protections returns the protection of the positions held, by pair.
func (*Manager) Synchronous ¶
Synchronous makes m protect on the clock of a backtest: now read from now (the candle being processed), the candles of a position loaded and its moves applied before OnCandle returns, so that the backtest is the same whatever the speed of the machine.
type Market ¶
type Market interface {
CandlesByLimit(ctx context.Context, pair, period string, limit int) ([]model.Candle, error)
CandlesByPeriod(ctx context.Context, pair, period string, start, end time.Time) ([]model.Candle, error)
}
Market is the exchange of the candles, for the ATR and the best price of a position adopted; and of the costs and the limits of the levels, when it tells them (service.PositionCostReader, service.ProtectionLimiter).