HeyTraders Documentation

Position Management Patterns

Partial Take Profit (Scale Out)

Tags: position_management, partial_exit, risk_management, scaling_out Functions: sma, Weight Description: Reduces position to 50% when the first take-profit target is reached, then fully exits on MA breakdown. Implements scaled exit via Weight adjustment. Code:

ticker = context['ticker']
ma20 = sma(close, 20)

# Entry condition
entry_cond = (close > ma20) & (close.shift(1) <= ma20)

# Gain calculation (simplified: compare to 10-bar ago price)
gain = (close - close.shift(10)) / close.shift(10)

# First take profit: 5% gain - reduce to 50% position
tp1 = gain > 0.05

# Final exit: below MA
exit_cond = close < ma20

# Initial full position
emit(entry_cond, entry(ticker, 'LONG', Weight(1.0)))
# Scale out to 50% at TP1
emit(tp1, entry(ticker, 'LONG', Weight(0.5)))
# Full exit
emit(exit_cond, exit_position(ticker))

Time-based Exit

Tags: position_management, time_exit, hold_period, timeout Functions: ts_delay Description: Forces liquidation if no profit is made within 5 candles after entry, or when a specific condition is met. Code:

# Entry Signal
entry_cond = rsi(close, 14) < 30
emit(entry_cond, entry(context["ticker"], "LONG", Weight(1.0)))

# Time Exit Logic
# Use `ts_delay` to check if entry condition was True 5 bars ago
entry_was_5_bars_ago = ts_delay(entry_cond, 5)

# If we entered 5 bars ago AND price hasn"t moved much (Close < Entry Price * 1.01)
# Note: "entry_price" is hard to get in pure vectorized without state loop
# Simplified: Just exit after 5 bars fixed
emit(entry_was_5_bars_ago, exit_position(context["ticker"], label="TIME_EXIT"))