Skip to content

Trailing Stop Loss Example

Jeff JM Cai edited this page Apr 18, 2021 · 3 revisions

Trailing Stop Loss Example

def equity_sell_trailingstop(symbol, quantity):
    from tda.orders.common import Duration, EquityInstruction,StopPriceLinkType,StopPriceLinkBasis
    from tda.orders.common import OrderStrategyType, OrderType, Session
    from tda.orders.generic import OrderBuilder

    return (OrderBuilder()
            .set_order_type(OrderType.TRAILING_STOP)
            .set_session(Session.NORMAL)
            .set_duration(Duration.DAY)
            .set_stop_price_link_type(StopPriceLinkType.VALUE)
            .set_stop_price_link_basis(StopPriceLinkBasis.LAST)
            .set_order_strategy_type(OrderStrategyType.SINGLE)
            .add_equity_leg(EquityInstruction.SELL, symbol, quantity))


orderTemplate = first_triggers_second(
    equity_buy_limit(stock, quantity, price),
    one_cancels_other(
        equity_sell_limit(stock, quantity, price + 1), # 1 Dollar Profit Mark
        equity_sell_trailingstop(stock, quantity)
            .set_order_type(OrderType.TRAILING_STOP)
            .set_stop_price_offset(.15)) # .15 cent trailing stop 

Note: Your can put any price and Profit and offset for trailing stop loss.