長崎良美

Why my order being closed immediately?

Programming


Hi I sent a market order with 1% stop loss condition like below. 
def on_marketdatafeed(self, md, ab):
    order = AlgoAPIUtil.OrderObject(
        instrument = md.instrument,
        trailingstop = 0.01,
        openclose = 'open', 
        buysell = 1,    #1=buy, -1=sell
        ordertype = 0,  #0=market, 1=limit, 2=stop
        volume = 0.01
    )
    self.evt.sendOrder(order)
    

From the console log, I see that it can successfully open an order. However, the order with the same trade ID is immediately closed at the same timestamp which lead to an immediate loss. Anyone know what is the problem?  

 
Ultraman
it is possible to encounter this if the market is illiquid. 
For a buy order, PL will be calculated as PL = (market bid - open price)

For below example, suppose we send a buy order at time 0, it will be executed at ask price (i.e. 100.5). 
Thus, 
  • At time 0, our immediate PL will be (100-100.5) = -0.5 (or -0.5%)
  • At time 1, PL = 100.1 - 100.5 = -0.4 (or -0.4%)
  • At time 2, PL = 100.7 - 100.5 = 0.2 (or +0.2%)

time bid ask
0 100 100.5
1 100.1 100.4
2 100.7 101.7


 
Ultraman
Original Posted by - Ultraman: it is possible to encounter this if the market is illiquid. 
For a buy order, PL will be calculated as PL = (market bid - open price)

For below example, suppose we send a buy order at time 0, it will be executed at ask price (i.e. 100.5). 
Thus, 
  • At time 0, our immediate PL will be (100-100.5) = -0.5 (or -0.5%)
  • At time 1, PL = 100.1 - 100.5 = -0.4 (or -0.4%)
  • At time 2, PL = 100.7 - 100.5 = 0.2 (or +0.2%)

time bid ask
0 100 100.5
1 100.1 100.4
2 100.7 101.7


Now suppose the market is not liquid enough. 

For example, the current bid and ask price are $100 and $101.5 respectively. 
Suppose we send a buy market order with 1% stop loss condition. 
Then our order will be executed at $101.5. However, our immediate PL will be 100-101.5 = -1.5 (or -1.5%), and therefore trigger the stop loss immediately.