happydasch / btoandav20

Support for Oanda-V20 API in backtrader
Apache License 2.0
131 stars 52 forks source link

Order replacement for bracket orders #59

Closed happydasch closed 3 years ago

happydasch commented 4 years ago

When creating a bracket order with a tradeid(?) btoandav20 will not recognize the orders but will see the new orders as external orders.

See: https://community.backtrader.com/topic/2967/order-synchronization/16

def create_new_order(self, price, exec_type):
    stopprice = price - (self.order_type_sign * self.max_range_price)
    limitprice = price + (self.order_type_sign * self.interval_price)

    self.tradeid_seq +=1

    orders = None
    if self.order_type == bt.Order.Buy:
        orders = self.buy_bracket(size=self.order_size, price=price, exectype=exec_type, 
                            tradeid=self.tradeid_seq, 
                            stopprice=stopprice, stopexec=bt.Order.Stop, 
                            limitprice=limitprice, limitexec=bt.Order.Limit)
    elif self.order_type == bt.Order.Sell:
        orders = self.sell_bracket(size=self.order_size, price=price, exectype=exec_type, 
                            tradeid=self.tradeid_seq, 
                            stopprice=stopprice, stopexec=bt.Order.Stop, 
                            limitprice=limitprice, limitexec=bt.Order.Limit)

    logger.info("$$$ ORDER $$$ type:{}, size:{}, price:{}, exectype:{}({}), tradeid:{}, stopprice:{}, limitprice:{}".format(
        bt.Order.OrdTypes[self.order_type], self.order_size, price, exec_type, bt.Order.ExecTypes[exec_type], self.tradeid_seq, stopprice, limitprice)
    )
    return orders