uniVocity / univocity-trader

open-source trading framework for java, supports backtesting and live trading with exchanges
577 stars 140 forks source link

Support bracket orders #29

Closed jbax closed 4 years ago

jbax commented 4 years ago

It's basically submitting an order at a price with two defined exits (sell if price hits a profit target, or sell at a maximum loss). The opposite when shorting.

As per interactive brokers docs:

https://interactivebrokers.github.io/tws-api/bracket_order.html

jbax commented 4 years ago

Done, now one can use an OrderManager implementation to attach orders on opposite sides of the parent order, creating a bracket. For example:

@Override
public void prepareOrder(SymbolPriceDetails priceDetails, OrderBook book, OrderRequest order, Candle latestCandle) {
            //attach orders to long buys or short sells
    if (order.isBuy() && order.isLong() || order.isSell() && order.isShort()) {
        //attached orders are created with opposite side. If parent order is BUY, the following orders will be SELL orders, and vice versa.
        OrderRequest stopLoss = order.attach(MARKET, -2.0); //sell or cover short at market if price goes down 2%
        OrderRequest takeProfit = order.attach(MARKET, 4.0); //sell or cover short at market if price goes up 4%
    }
}