Skip to content

MT4Sugar — 7. Trailing & Breakeven

One-shot stop-management primitives — call them on a timer to trail live.

Both read the current quote, compute the desired stop, and only ever tighten it (a move that would loosen the stop does nothing and returns false). Each returns whether the stop moved on this call.


AutoBreakeven(ctx, ticket, triggerPips, plusPips)

func (s *MT4Sugar) AutoBreakeven(ctx context.Context, ticket int32, triggerPips, plusPips float64) (bool, error)

Once the position is at least triggerPips in profit, move the stop to entry ± plusPips (locking a little profit). Never loosens an existing stop.

Example — arm at +10, lock +2:

moved, _ := sugar.AutoBreakeven(ctx, ticket, 10, 2)
if moved { log.Println("stop moved to breakeven+2") }

Example — poll breakeven on a timer:

for range time.Tick(2 * time.Second) {
    if o, _ := sugar.FindTicket(ctx, ticket); o == nil { break } // closed
    sugar.AutoBreakeven(ctx, ticket, 10, 2)
}

TrailStop(ctx, ticket, distancePips)

func (s *MT4Sugar) TrailStop(ctx context.Context, ticket int32, distancePips float64) (bool, error)

Ratchet the stop to distancePips behind the current price, tightening only.

Example — trail 15 pips on a timer:

for {
    if o, _ := sugar.FindTicket(ctx, ticket); o == nil { break } // closed
    sugar.TrailStop(ctx, ticket, 15)
    time.Sleep(2 * time.Second)
}

Example — breakeven first, then trail:

for range time.Tick(2 * time.Second) {
    if o, _ := sugar.FindTicket(ctx, ticket); o == nil { break }
    sugar.AutoBreakeven(ctx, ticket, 10, 2)
    sugar.TrailStop(ctx, ticket, 15)
}

Both return (false, nil) when the stop shouldn't move yet — that's normal, not an error. A missing order returns an error.


See also: 6. Order Management · 8. Watchers & Waiters