Chan Yu Sing

Close position with market order

Programming


I see the error "[2020-01-01 23:25:00] EvtSendOrder Error: the sendOrder attribute parm 'openclose' does not support value 'close' under position base environment."  in the console. I am trying to close my Long order but failed.

Here is my code:

def test_sendOrder(self, lastprice, buysell, openclose, trade_id=None, volume=0.01):

        if openclose == 'open':
            order = AlgoAPIUtil.OrderObject()
            order.instrument = self.myinstrument
            order.ordertype = 0
            order.buysell = buysell
            order.volume = float(volume)
            order.orderRef = str(trade_id or 1)
            order.openclose = 'open'
            if buysell == 1:
                order.stopLossLevel = lastprice * 0.98
            elif buysell == -1:
                order.stopLossLevel = lastprice * 1.02
            self.last_lot_size = volume
            self.evt.sendOrder(order)
            
        elif openclose == 'close':
            order = AlgoAPIUtil.OrderObject()
            order.tradeID = trade_id
            order.openclose = 'close'
            self.evt.sendOrder(order)



 
Kelvin Yeung
For position based environment, you need to "open" an opposite trade in order to unwind the position.