v1.0.2

Trade API & Order Routing

Kafal exposes order execution methods through the trade.* namespace. The execution engine manages pending order queues, position sizes, and risk constraints automatically.

Market Orders

Market orders execute on the subsequent bar open or current tick depending on execution mode. Signals can be passed as boolean time-series expressions or scalar booleans.

  • trade.long(signal, size=1.0): Opens or increases a long position when signal is true.
  • trade.short(signal, size=1.0): Opens or increases a short position when signal is true.
  • trade.close(signal=None): Closes the current position. If no signal is provided, closes immediately on the active bar.
// Simple Market Order Signals
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

// Market entry signals pass boolean Series directly
trade.long(crossover(fast, slow), size=1.0)
trade.short(crossunder(fast, slow), size=1.0)

// Close active position on exit signal
trade.close(crossunder(fast, slow))

Pending Orders (Limit & Stop)

Pending orders remain in the working queue until price condition triggers a fill, the order expires, or a cancellation rule is invoked.

  • trade.limit_long(price, size=1.0, ...)
  • trade.limit_short(price, size=1.0, ...)
  • trade.stop_long(price, size=1.0, ...)
  • trade.stop_short(price, size=1.0, ...)
// Limit and Stop Orders
lower_band = ta.vwap(high, low, close, volume) - 2.0 * ta.stdev(close, 20)
upper_band = ta.vwap(high, low, close, volume) + 2.0 * ta.stdev(close, 20)

// Place limit buy order at lower band with 5-bar expiry
trade.limit_long(lower_band, size=1.0, tif="GTC", expire_after_bars=5)

// Place breakout stop order above upper band
trade.stop_long(upper_band, size=1.0, tif="GTC")

Order Execution Parameters

tif (Time-In-Force)

Supports "GTC" (Good-Til-Canceled), "IOC" (Immediate-Or-Cancel), and "FOK" (Fill-Or-Kill).

reduce_only

When set to true, the order can only reduce or close an active position. If placing the order would increase exposure or flip direction, the engine rejects it with a reduce_only_violation.

expire_after_bars

Specifies the exact number of closed bars an order remains active. If unfilled after the specified duration, the order status transitions to expired.

group_id

Assigns a shared identifier to multiple orders. When one order in the group fills, working exit orders sharing the same group_id are automatically canceled (OCO behavior).

Bracket Orders

trade.bracket_long and trade.bracket_short simplify bracket management by atomically creating the entry order along with paired Take Profit (limit) and Stop Loss (stop) exit orders linked via a shared OCO group_id.

// Automated Bracket Orders (Entry + Take Profit + Stop Loss)
entry_price = close
tp_target   = close + 10.0
sl_target   = close - 5.0

// Automatically places limit entry, short limit TP, and short stop SL
// TP and SL legs are assigned matching group_id for One-Cancels-the-Other (OCO) behavior
trade.bracket_long(entry_price, tp_target, sl_target, size=1.0, group_id="brk_01")