Managing Trades
Managed vs Unmanaged trading
Managed vs Unmanaged Trading
HaasScript offers two approaches to order execution: managed trading (simple) and unmanaged trading (advanced). Understanding the difference helps you choose the right approach for your strategy.
Managed Trading
Managed trading lets HaasScript handle order execution automatically. You provide signals and HaasScript manages the order lifecycle.
Managed Trading Commands
| Command | Description |
|---|---|
DoLong() / DoBuy()
|
Open long position (spot: buy, margin: long) |
DoShort() / DoSell()
|
Open short position (spot: sell, margin: short) |
DoExitPosition() |
Close current position |
Characteristics
- Simple: Just provide signals and notes
- Automatic: HaasScript manages order execution
- Adaptive: Automatically adapts to market type (spot/margin/leverage)
- Returns void: No order identifier returned
- Less control: Can't set specific order parameters
Basic Examples
Open Long Position
local rsi = RSI(ClosePrices(), 14)
if rsi < 30 then
DoLong("RSI oversold")
end
Open Short Position
local rsi = RSI(ClosePrices(), 14)
if rsi > 70 then
DoShort("RSI overbought")
end
Close Position
if HasLongPosition() then
DoExitPosition("Close long position")
end
Add Notes to Orders
local rsi = RSI(ClosePrices(), 14)
local price = Price()
if rsi < 30 then
local note = "RSI: " .. rsi .. ", Price: " .. price
DoLong(note)
end
Managed Trading Workflow
local rsi = RSI(ClosePrices(), 14)
local position = GetPositionDirection()
-- Entry signals
if position == NoPosition then
if rsi < 30 then
DoLong("RSI oversold entry")
elseif rsi > 70 then
DoShort("RSI overbought entry")
end
end
-- Exit signals
if position == PositionLong and rsi > 60 then
DoExitPosition("Long take profit")
end
if position == PositionShort and rsi < 40 then
DoExitPosition("Short take profit")
end
Unmanaged Trading
Unmanaged trading gives you full control over order execution. You specify all order parameters and manage the order lifecycle.
Unmanaged Trading Commands
| Command | Description |
|---|---|
PlaceGoLongOrder() |
Place go long order |
PlaceGoShortOrder() |
Place go short order |
PlaceExitLongOrder() |
Exit long position |
PlaceExitShortOrder() |
Exit short position |
PlaceBuyOrder() |
Place buy order (spot) |
PlaceSellOrder() |
Place sell order (spot) |
Characteristics
- Full control: Specify price, amount, type, timeout, etc.
- Manual management: You manage order lifecycle
- Order identifier: Returns unique order ID for tracking
- More parameters: price, amount, market, type, note, positionId, timeout, triggerPrice, reduceOnly, hiddenOrder
- Advanced: Requires more code and management
PlaceGoLongOrder() Syntax
local orderId = PlaceGoLongOrder(price, amount, [market], [type], [note], [positionId], [timeout], [triggerPrice], [reduceOnly], [hiddenOrder])
Basic Examples
Simple Limit Order
local price = Price()
local amount = 0.1
local orderId = PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Limit buy order")
Market Order
local currentPrice = Price()
local amount = 0.1
local orderId = PlaceGoLongOrder(currentPrice, amount, PriceMarket(), MarketOrderType, "Market buy order")
Order with Timeout
local price = Price() * 0.99 -- 1% below current
local amount = 0.1
local orderId = PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Buy order", "", 300)
-- Timeout: 300 seconds (5 minutes)
Stop-Limit Order
local triggerPrice = Price() * 1.01 -- Trigger at 1% above
local limitPrice = Price() * 1.015 -- Limit at 1.5% above
local amount = 0.1
local orderId = PlaceGoLongOrder(limitPrice, amount, PriceMarket(), StopLimitOrderType, "Stop-limit buy", "", 600, triggerPrice)
Comparison
| Feature | Managed | Unmanaged |
|---|---|---|
| Simplicity | Simple | Complex |
| Control | Low | High |
| Parameters | Minimal | Full |
| Order ID | No | Yes |
| Timeout | Automatic | Manual |
| Order type | Automatic | Manual |
| Best for | Beginners, simple strategies | Advanced users, complex strategies |
When to Use Each
Use Managed Trading When:
- You're starting with HaasScript
- You want simple, straightforward order execution
- You don't need custom order parameters
- You're okay with automatic order management
- You're building basic strategies or quick prototypes
-- Simple managed trading strategy
local rsi = RSI(ClosePrices(), 14)
if rsi < 30 then
DoLong("Simple entry")
elseif rsi > 70 then
DoExitPosition("Simple exit")
end
Use Unmanaged Trading When:
- You need specific order parameters
- You want custom order timeouts
- You need to track order IDs
- You're building advanced strategies
- You want full control over execution
-- Advanced unmanaged trading strategy
local price = Price()
local amount = 0.1
local rsi = RSI(ClosePrices(), 14)
if rsi < 30 then
-- Place limit order 1% below current price
local limitPrice = price * 0.99
local orderId = PlaceGoLongOrder(limitPrice, amount, CurrentMarket(), "Limit", "RSI oversold", "", 300)
Log("Order placed: " .. orderId)
end
Practical Examples
Example 1: Managed Trading Strategy
local rsi = RSI(ClosePrices(), 14)
local macd = EasyMACD(1, "MACD")
-- Simple entry with multiple indicators
if rsi < 30 and macd == SignalLong then
DoLong("RSI oversold + MACD bullish")
elseif rsi > 70 and macd == SignalShort then
DoShort("RSI overbought + MACD bearish")
end
-- Simple exit
local position = GetPositionDirection()
if position == PositionLong and rsi > 60 then
DoExitPosition("Take profit")
end
if position == PositionShort and rsi < 40 then
DoExitPosition("Take profit")
end
Example 2: Unmanaged Trading Strategy
local price = CurrentPrice().close
local amount = 0.1
local rsi = RSI(ClosePrices(), 14)
-- Entry with custom order parameters
if rsi < 30 then
local limitPrice = price * 0.98 -- 2% below current
local orderId = PlaceGoLongOrder(
limitPrice,
amount,
PriceMarket(),
LimitOrderType,
"RSI oversold - 2% below market",
"",
600, -- 10 minute timeout
0, -- No trigger price
false,-- Not reduce only
false -- Not hidden
)
Log("Limit order placed: " .. orderId)
end
-- Exit with specific parameters
if GetPositionDirection() == PositionLong then
local exitPrice = price * 1.02 -- 2% above current
PlaceExitLongOrder(exitPrice, amount, PriceMarket(), LimitOrderType, "Take profit 2%")
end
Example 3: Order Management with IDs
local rsi = RSI(ClosePrices(), 14)
local price = CurrentPrice().close
local amount = 0.1
-- Store order IDs
local longOrderId = Load("loid", "")
local shortOrderId = Load("soid", "")
-- Place orders and track IDs
if rsi < 30 and longOrderId == "" then
longOrderId = PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Entry long")
Log("Long order ID: " .. longOrderId)
end
if rsi > 70 and shortOrderId == "" then
shortOrderId = PlaceGoShortOrder(price, amount, PriceMarket(), LimitOrderType, "Entry short")
Log("Short order ID: " .. shortOrderId)
end
-- Cancel specific order if needed
if rsi > 40 and longOrderId ~= "" then
CancelOrder(longOrderId)
longOrderId = ""
Log("Cancelled long order")
end
-- Remember to save order IDs!
Save("loid", longOrderId)
Save("soid", shortOrderId)
Order Types
Managed Trading (Automatic)
-- HaasScript uses order type set from the bot settings
DoLong("Automatic order type")
Unmanaged Trading (Manual)
-- Specify order type manually
PlaceGoLongOrder(price, amount, PriceMarket(), MarketOrderType, "Market order") -- Immediate execution
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Limit order") -- Specific price
PlaceGoLongOrder(price, amount, PriceMarket(), StopLimitOrderType, "Stop-limit", "", 0, triggerPrice) -- Trigger then limit
PlaceGoLongOrder(price, amount, PriceMarket(), StopMarketOrderType, "Stop-market", "", 0, triggerPrice) -- Trigger then market
Order Timeout
Managed Trading (Automatic)
-- HaasScript manages order timeout automatically
DoLong("Order timeout is automatic")
Unmanaged Trading (Manual)
-- Set custom timeout (in seconds)
local timeout = 300 -- 5 minutes
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "With timeout", "", timeout)
-- Disable timeout
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "No timeout", "", 0)
Reduce Only
Prevent orders from flipping your position:
-- Reduce only: won't flip position to short
PlaceExitLongOrder(price, amount, PriceMarket(), MarketOrderType, "Reduce only", "", 600, 0, true)
Hidden Orders
Hide orders from the order book:
-- Hidden order: doesn't appear in order book
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Hidden order", "", 600, 0, false, true)
Common Mistakes
Mixing Managed and Unmanaged
HaasScript will throw an error when managed and unmanaged trading commands are mixed in a script.
-- Wrong: Mixing approaches in same strategy
if rsi < 30 then
DoLong("Managed entry")
end
if rsi > 70 then
PlaceExitLongOrder(price, amount, PriceMarket(), LimitOrderType, "Unmanaged exit")
end
-- Correct: Stick to one approach
if rsi < 30 then
DoLong("Managed entry")
end
if rsi > 70 then
DoExitPosition("Managed exit")
end
Not Tracking Order IDs
-- Wrong: Placing orders but not tracking IDs
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Entry")
-- Can't cancel or track this order later
-- Correct: Store order IDs
local orderId = Load("myOrderId", "")
orderId = PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Entry")
Log("Order ID: " .. orderId)
Save("myOrderId", orderId)
Forgetting Market Parameter
-- Wrong: Not specifying market in unmanaged trading
PlaceGoLongOrder(price, amount, "", LimitOrderType, "Order")
-- Uses current market, but better to be explicit
-- Correct: Specify market explicitly
PlaceGoLongOrder(price, amount, PriceMarket(), LimitOrderType, "Order")
Best Practices
Managed Trading Best Practices
- Keep it simple - Use for basic strategies
- Use descriptive notes - Helps with tracking
- Let HaasScript manage - Don't interfere with order lifecycle
- Test thoroughly - Verify signals work as expected
Unmanaged Trading Best Practices
- Track order IDs - Store them for later management
- Set appropriate timeouts - Don't let orders hang forever
- Handle order states - Monitor filled, cancelled, expired orders
- Use descriptive notes - Easier to debug
- Specify market explicitly - Avoid confusion
- Test edge cases - Partial fills, rejections, timeouts
General Best Practices
- Choose one approach - Don't mix managed and unmanaged in same strategy
- Start with managed - Progress to unmanaged as you gain experience
- Document your approach - Comment your code clearly
- Test thoroughly - Backtest and forward test both approaches
Summary
- Managed trading: Simple, automatic, less control
- Unmanaged trading: Complex, manual, full control
- Managed commands: DoLong(), DoShort(), DoExitPosition()
- Unmanaged commands: PlaceGoLongOrder(), PlaceGoShortOrder(), PlaceExitLongOrder(), PlaceExitShortOrder()
- Choose managed for simplicity and beginners
- Choose unmanaged for advanced control and customization
- Don't mix approaches in the same strategy
- Track order IDs in unmanaged trading
- Test thoroughly regardless of approach
Understanding both approaches helps you choose the right tool for your strategy's complexity level.