Advanced Execution Patterns
Maker Grid Orders
Tags: execution, maker, limit_order, grid, market_making
Functions: entry with limit_price
Description: Places limit orders above and below the current price to profit from range-bound markets using a grid strategy.
Code:
ticker = context["ticker"]
# Grid Levels relative to current price
current_price = close[-1]
grid_step = 0.01 # 1% step
# Buy grid levels below current price
buy_1 = close < current_price * (1 - grid_step * 1)
buy_2 = close < current_price * (1 - grid_step * 2)
buy_3 = close < current_price * (1 - grid_step * 3)
emit(buy_1, entry(ticker, "LONG", Weight(0.1),
execution='LIMIT', limit_price=current_price * 0.99,
label="GRID_BUY_1"))
emit(buy_2, entry(ticker, "LONG", Weight(0.1),
execution='LIMIT', limit_price=current_price * 0.98,
label="GRID_BUY_2"))
emit(buy_3, entry(ticker, "LONG", Weight(0.1),
execution='LIMIT', limit_price=current_price * 0.97,
label="GRID_BUY_3"))
# Sell grid levels above current price
sell_1 = close > current_price * (1 + grid_step * 1)
sell_2 = close > current_price * (1 + grid_step * 2)
sell_3 = close > current_price * (1 + grid_step * 3)
emit(sell_1, entry(ticker, "SHORT", Weight(0.1),
execution='LIMIT', limit_price=current_price * 1.01,
label="GRID_SELL_1"))
emit(sell_2, entry(ticker, "SHORT", Weight(0.1),
execution='LIMIT', limit_price=current_price * 1.02,
label="GRID_SELL_2"))
emit(sell_3, entry(ticker, "SHORT", Weight(0.1),
execution='LIMIT', limit_price=current_price * 1.03,
label="GRID_SELL_3"))
OCO Bracket Order (TP/SL)
Tags: execution, oco, risk_management, bracket, stop_limit
Functions: emit_oco, entry
Description: Submits take-profit (TP) and stop-loss (SL) orders as an OCO (One Cancels Other) pair at entry. When one fills, the other is automatically cancelled.
Code:
ticker = context["ticker"]
# Entry Signal
long_cond = rsi(close, 14) < 30
# Enter Market with built-in TP/SL (recommended over manual OCO)
emit(long_cond, entry(ticker, "LONG", Weight(1.0),
stop_loss=PercentFromEntry(-0.02), # 2% SL
take_profit=PercentFromEntry(0.05), # 5% TP
label="MAIN_POS"))