Skip to content

Market One-Shot

Open a market order with an automatic pip-based SL/TP bracket โ€” the simplest orchestrator.


What it does

  1. Sizes the lot โ€” fixed sp.Lots, or risk-based from rp.RiskPercent over rp.SLPips via CalcLotByRisk.
  2. Opens a market BUY via MT4Sugar.BuyMarket.
  3. Attaches a bracket โ€” SL/TP as pip distances with ModifySLTPByPips.
  4. Manages for ~5s (auto-breakeven, then gated trailing โ€” both from the preset).
  5. Closes the position and returns the ticket.

Use it when you simply want a protected market entry in one call.


Signature

func RunMarketOneShot(ctx context.Context, sugar *mt4.MT4Sugar, sp StrategyPreset, rp RiskPreset) (int32, error)
Parameter Type Description
ctx context.Context Cancellation / timeout.
sugar *mt4.MT4Sugar A connected high-level client (drives entry, bracket, manage, close).
sp StrategyPreset Symbol, Lots (optional), Magic, Comment.
rp RiskPreset RiskPercent, SLPips, TPPips, optional trailing/breakeven.

Returns: the order ticket (int32) and an error.


Example

sugar := mt4.NewMT4Sugar(mt4.NewMT4Service(acc)).SetDefaults(777, 20, "demo")

ticket, err := strategies.RunMarketOneShot(ctx, sugar,
    strategies.MarketEURUSD(), // WHAT: market EURUSD, magic 777
    strategies.Balanced())     // HOW: 1% risk, SL 20 / TP 40, trail 15, BE 10/+2
if err != nil { return err }
fmt.Println("one-shot ticket:", ticket)

If sp.Lots is nil, sizing comes from rp.RiskPercent + rp.SLPips via CalcLotByRisk.


๐Ÿ“š See also