π’ππ‘οΈ Buy Limit with SL/TP (BuyLimitWithSLTP)ΒΆ
Sugar method: Places pending BUY LIMIT order with Stop Loss and Take Profit set from the start.
API Information:
- Method:
sugar.BuyLimitWithSLTP(symbol, volume, price, sl, tp) - Timeout: 10 seconds
- Returns: Pending order ticket number
π Method SignatureΒΆ
π½ Input / β¬οΈ OutputΒΆ
| Input | Type | Description |
|---|---|---|
symbol |
string |
Trading symbol (e.g., "EURUSD") |
volume |
float64 |
Lot size (e.g., 0.1) |
price |
float64 |
Entry price (must be BELOW current ASK) |
sl |
float64 |
Stop Loss price (0 = no SL) |
tp |
float64 |
Take Profit price (0 = no TP) |
| Output | Type | Description |
|---|---|---|
ticket |
uint64 |
Pending order ticket number |
error |
error |
Error if order rejected |
π¬ Just the EssentialsΒΆ
- What it is: BUY LIMIT pending order with SL/TP - complete setup in one call.
- Why you need it: Set everything (entry, SL, TP) and forget - activates automatically.
- Sanity check: Entry below ASK, SL below entry, TP above entry.
π LogicΒΆ
Current ASK: 1.08500
Entry: 1.08000 (LIMIT - 50 pips below)
SL: 1.07500 (50 pips below entry)
TP: 1.09000 (100 pips above entry)
When price drops to 1.08000 β Order fills with SL/TP already set
π― When to UseΒΆ
β Support level entries - Buy at support with protection
β Pullback trading - Wait for dip, enter with risk management
β Set and forget - Don't need to monitor for fill
β Automated strategies - Complete order setup in advance
π Usage ExamplesΒΆ
1) Basic usage - support with SL/TPΒΆ
symbol := "EURUSD"
currentAsk, _ := sugar.GetAsk(symbol)
entryPrice := 1.08000 // Support level
sl := 1.07500 // 50 pips below entry
tp := 1.09000 // 100 pips above entry
ticket, err := sugar.BuyLimitWithSLTP(symbol, 0.1, entryPrice, sl, tp)
if err != nil {
fmt.Printf("Order failed: %v\n", err)
return
}
fmt.Printf("β
BUY LIMIT with SL/TP placed\n")
fmt.Printf(" Ticket: #%d\n", ticket)
fmt.Printf(" Entry: %.5f (current: %.5f)\n", entryPrice, currentAsk)
fmt.Printf(" SL: %.5f (-50 pips from entry)\n", sl)
fmt.Printf(" TP: %.5f (+100 pips from entry)\n", tp)
2) Calculate SL/TP from entry priceΒΆ
symbol := "EURUSD"
entryPrice := 1.08000
volume := 0.1
info, _ := sugar.GetSymbolInfo(symbol)
// Calculate SL/TP relative to entry
sl := entryPrice - (50 * info.Point) // 50 pips SL
tp := entryPrice + (100 * info.Point) // 100 pips TP
ticket, _ := sugar.BuyLimitWithSLTP(symbol, volume, entryPrice, sl, tp)
fmt.Printf("β
BUY LIMIT with calculated SL/TP\n")
fmt.Printf(" Entry: %.5f\n", entryPrice)
fmt.Printf(" SL: %.5f (50 pips)\n", sl)
fmt.Printf(" TP: %.5f (100 pips)\n", tp)
fmt.Printf(" R:R: 1:2\n")
3) Using CalculateSLTP helperΒΆ
symbol := "EURUSD"
entryPrice := 1.08000
volume := 0.1
slPips := 50.0
tpPips := 100.0
// Calculate SL/TP from entry price
sl, tp, _ := sugar.CalculateSLTP(symbol, "BUY", entryPrice, slPips, tpPips)
ticket, _ := sugar.BuyLimitWithSLTP(symbol, volume, entryPrice, sl, tp)
fmt.Printf("β
BUY LIMIT using helper\n")
fmt.Printf(" Entry: %.5f\n", entryPrice)
fmt.Printf(" SL: %.5f (%.0f pips)\n", sl, slPips)
fmt.Printf(" TP: %.5f (%.0f pips)\n", tp, tpPips)
4) Multiple buy limits at support zonesΒΆ
symbol := "EURUSD"
volume := 0.05
// Three support levels
supports := []struct {
entry float64
sl float64
tp float64
}{
{1.08000, 1.07500, 1.09000}, // Strong support
{1.07500, 1.07000, 1.08500}, // Medium support
{1.07000, 1.06500, 1.08000}, // Weak support
}
fmt.Println("Placing BUY LIMITS at support zones:")
for i, level := range supports {
ticket, err := sugar.BuyLimitWithSLTP(symbol, volume, level.entry, level.sl, level.tp)
if err != nil {
fmt.Printf("Level %d failed: %v\n", i+1, err)
continue
}
fmt.Printf("β
Level %d: Entry %.5f, SL %.5f, TP %.5f - Ticket #%d\n",
i+1, level.entry, level.sl, level.tp, ticket)
}
5) Fibonacci retracement with tight SLΒΆ
symbol := "EURUSD"
// Previous swing
swingHigh := 1.09000
swingLow := 1.08000
range_ := swingHigh - swingLow
// 61.8% Fibonacci retracement
fib618 := swingHigh - (range_ * 0.618)
info, _ := sugar.GetSymbolInfo(symbol)
// Tight SL below Fib level, TP at swing high
sl := fib618 - (30 * info.Point) // 30 pips SL
tp := swingHigh // Target swing high
ticket, _ := sugar.BuyLimitWithSLTP(symbol, 0.1, fib618, sl, tp)
fmt.Printf("β
Fibonacci BUY LIMIT\n")
fmt.Printf(" Entry: %.5f (Fib 61.8%%)\n", fib618)
fmt.Printf(" SL: %.5f (30 pips)\n", sl)
fmt.Printf(" TP: %.5f (swing high)\n", tp)
6) Risk-based position sizing with limit orderΒΆ
symbol := "EURUSD"
entryPrice := 1.08000
riskPercent := 2.0
slPips := 50.0
tpPips := 100.0
// Calculate lot size based on risk
lotSize, _ := sugar.CalculatePositionSize(symbol, riskPercent, slPips)
// Calculate SL/TP from entry
sl, tp, _ := sugar.CalculateSLTP(symbol, "BUY", entryPrice, slPips, tpPips)
ticket, _ := sugar.BuyLimitWithSLTP(symbol, lotSize, entryPrice, sl, tp)
fmt.Printf("β
Risk-managed BUY LIMIT\n")
fmt.Printf(" Entry: %.5f\n", entryPrice)
fmt.Printf(" Risk: %.1f%% of balance\n", riskPercent)
fmt.Printf(" Size: %.2f lots\n", lotSize)
fmt.Printf(" SL: %.0f pips\n", slPips)
fmt.Printf(" TP: %.0f pips\n", tpPips)
fmt.Printf(" Ticket: #%d\n", ticket)
7) Scale in strategy with SL/TPΒΆ
symbol := "EURUSD"
totalVolume := 0.3
entries := 3
volumePerEntry := totalVolume / float64(entries)
info, _ := sugar.GetSymbolInfo(symbol)
fmt.Printf("Scaling in with %d BUY LIMIT orders:\n", entries)
baseEntry := 1.08000
baseSL := 1.07500
baseTP := 1.09000
for i := 0; i < entries; i++ {
// Each entry 20 pips lower
entry := baseEntry - (float64(i) * 20 * info.Point)
sl := baseSL - (float64(i) * 20 * info.Point)
tp := baseTP // Same TP for all
ticket, err := sugar.BuyLimitWithSLTP(symbol, volumePerEntry, entry, sl, tp)
if err != nil {
continue
}
fmt.Printf("Entry %d: %.2f lots at %.5f (SL %.5f, TP %.5f) - #%d\n",
i+1, volumePerEntry, entry, sl, tp, ticket)
}
8) Pending order with validationΒΆ
symbol := "EURUSD"
entryPrice := 1.08000
volume := 0.1
slPips := 50.0
tpPips := 100.0
// Validate entry is below current ASK
currentAsk, _ := sugar.GetAsk(symbol)
if entryPrice >= currentAsk {
fmt.Printf("β Entry %.5f must be below ASK %.5f\n", entryPrice, currentAsk)
return
}
// Check minimum stop level
stopLevel, _ := sugar.GetMinStopLevel(symbol)
info, _ := sugar.GetSymbolInfo(symbol)
minDistance := float64(stopLevel) * info.Point
if (currentAsk - entryPrice) < minDistance {
fmt.Printf("β Entry too close to current price\n")
return
}
// Calculate SL/TP
sl, tp, _ := sugar.CalculateSLTP(symbol, "BUY", entryPrice, slPips, tpPips)
// Validate SL/TP distances
if (entryPrice - sl) < minDistance {
fmt.Printf("β SL too close to entry\n")
return
}
ticket, _ := sugar.BuyLimitWithSLTP(symbol, volume, entryPrice, sl, tp)
fmt.Printf("β
Validated BUY LIMIT placed - Ticket #%d\n", ticket)
9) Weekend gap strategyΒΆ
// Friday close: 1.08500
// Expecting gap down Monday morning
symbol := "EURUSD"
// Set buy limit below Friday close
entryPrice := 1.08000 // 50 pips gap
volume := 0.1
info, _ := sugar.GetSymbolInfo(symbol)
// Tight SL (gap fill expected)
sl := entryPrice - (30 * info.Point)
tp := 1.08500 // Target Friday close
ticket, _ := sugar.BuyLimitWithSLTP(symbol, volume, entryPrice, sl, tp)
fmt.Printf("Weekend gap strategy:\n")
fmt.Printf(" Friday close: 1.08500\n")
fmt.Printf(" Entry: %.5f (gap down)\n", entryPrice)
fmt.Printf(" SL: %.5f (30 pips)\n", sl)
fmt.Printf(" TP: %.5f (gap fill)\n", tp)
10) Complete limit order functionΒΆ
func PlaceBuyLimitWithProtection(
sugar *mt5.MT5Sugar,
symbol string,
entryPrice float64,
riskPercent float64,
slPips float64,
tpPips float64,
) (uint64, error) {
fmt.Println("βββββββββββββββββββββββββββββββββββββββββ")
fmt.Println("β BUY LIMIT WITH SL/TP β")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββ")
// Validate entry price
currentAsk, _ := sugar.GetAsk(symbol)
if entryPrice >= currentAsk {
return 0, fmt.Errorf("entry %.5f must be below ASK %.5f", entryPrice, currentAsk)
}
fmt.Printf("β
Entry: %.5f (%.0f pips below current)\n",
entryPrice, (currentAsk-entryPrice)/0.00001)
// Calculate position size
lotSize, err := sugar.CalculatePositionSize(symbol, riskPercent, slPips)
if err != nil {
return 0, err
}
fmt.Printf("β
Size: %.2f lots (%.1f%% risk)\n", lotSize, riskPercent)
// Calculate SL/TP
sl, tp, _ := sugar.CalculateSLTP(symbol, "BUY", entryPrice, slPips, tpPips)
fmt.Printf("β
SL: %.5f (%.0f pips)\n", sl, slPips)
fmt.Printf("β
TP: %.5f (%.0f pips)\n", tp, tpPips)
// Place order
ticket, err := sugar.BuyLimitWithSLTP(symbol, lotSize, entryPrice, sl, tp)
if err != nil {
return 0, err
}
fmt.Printf("\nπ― BUY LIMIT placed successfully!\n")
fmt.Printf(" Ticket: #%d\n", ticket)
return ticket, nil
}
// Usage:
ticket, _ := PlaceBuyLimitWithProtection(sugar, "EURUSD", 1.08000, 2.0, 50, 100)
π Related MethodsΒΆ
π¬ Other pending orders with SL/TP:
SellLimitWithSLTP()- SELL LIMIT with protectionBuyStopWithSLTP()- BUY STOP with protection
π¬ Market orders with SL/TP:
BuyMarketWithSLTP()- Immediate BUY with protection
π¬ Helper methods:
CalculateSLTP()- Calculate SL/TP from pipsBuyLimit()- Without SL/TP (add manually later)
β οΈ Common PitfallsΒΆ
1) Wrong SL/TP directionΒΆ
// β WRONG - SL must be BELOW entry for BUY
entry := 1.08000
sugar.BuyLimitWithSLTP("EURUSD", 0.1, entry, 1.08500, 1.09000) // SL above!
// β
CORRECT - SL below, TP above
sugar.BuyLimitWithSLTP("EURUSD", 0.1, entry, 1.07500, 1.09000)
2) Calculating SL/TP from current price instead of entryΒΆ
// β WRONG - SL/TP calculated from current price
ask, _ := sugar.GetAsk("EURUSD")
entry := 1.08000
sl := ask - (50 * point) // Wrong! Should be from entry!
// β
CORRECT - calculate from entry price
sl := entry - (50 * point)
tp := entry + (100 * point)
π Pro TipsΒΆ
-
Calculate from entry price - Not current price
-
Validate entry level - Must be below current ASK
-
Check min stop level - Both entry and SL must respect broker limits
-
Use for support trading - Perfect for buying dips
-
Monitor for fills - Check if order was executed
See also: BuyLimit.md, SellLimitWithSLTP.md, CalculateSLTP.md