Open slot-29 opened 5 years ago
It is possible, There are 2 different trailing orders, stops and IfTouched orders.
The code below is a stop order. The variable trailPrice should be renamed to offsetValue. If you set the offsetValue to -5. Submit it the the BitMEX exchange. A stop-sell order will appear -5 USD down from the current price. If the price increases the stop order will follow the price up. If the price move down, the order won't move. Once the price moves down enough, the order triggers.
A stop-sell order is a Market Sell. (It stops the loss on a Long position) A stop-buy order is a Market Long.
So if you used a positive offsetValue, this creates a Trailing stop-buy Order (Market Long) and things happen in reverse.
Below the ordType='Stop' you can set that to "'Stop', 'StopLimit', 'MarketIfTouched', 'LimitIfTouched'"
What you want is a limit order. For example if you set "ordType" to LimitIfTouched, Then you are creating a trailing Limit order.
You set offsetValue to -5 it will create a trailing Limit Order, I believe you set the side (Buy/Sell) attribute to choose which side you want your limit order to go in once your order has been triggered.
with trailing limit orders. Sequence of things happen. The trailing order is triggered. A new limit order is submitted automatically by the exchange on your behalf (note this order isn't explicitly a postonly type order). Then your now limit order rests in the book until it is hit or cancelled.
res, http_response = self.bitMEXAuth.Order.Order_new(symbol=symbol, simpleOrderQty=simpleOrderQty,
ordType='Stop',pegPriceType='TrailingStopPeg',
pegOffsetValue=trailPrice,execInst='LastPrice',
timeInForce='GoodTillCancel'
).result() # ,stopPx=stopPx,price=CURR_PRICE execInst='LastPrice'
Trailing orders is one of the most complicated order types to wrap your head around. I don't remember all the details about them and there's always a chance of making a mistake.
If you find the orders are been immediately triggered once submitted. Just reverse the offsetValue or the side. That will reverse the triggers. Changing ordType from Stop to LimitIfTouched also reverses the way triggers work I think. Triggers have to be on the correct side of the current market price.
And of course test in https://testnet.bitmex.com first. more API details: https://testnet.bitmex.com/api/explorer/#!/Order/Order_new
truly your answer is very complete, I will test it and I comment, I am creating an automatic bot that works with heikin ashi .. if you are interested we can develop it together. regards!
I tried this:
buy = self.client.Order.Order_new(symbol="XBTUSD", side="Buy", orderQty=((self.money_to_trade * self.leverage)/5), ordType="LimitIfTouched", pegPriceType="TrailingStopPeg", pegOffsetValue=(-10), execInst="LastPrice", timeInForce="GoodTillCancel").result()
but when I run on the console the following message appears:
'error': {'message': 'Invalid price', 'name': 'HTTPError'}
I believe that if I place: ordType="LimitIfTouched"
Can't find where to place the limit order, but if I place only "limit" it tells me that this type of order is not correct.
Please, if you can help me, I would appreciate it since it is the only order I need to understand
Try the code below
res, http_response = self.client.Order.Order_new(symbol="XBTUSD", side="Buy", orderQty=100, ordType="LimitIfTouched", pegPriceType="TrailingStopPeg",price=8460, pegOffsetValue=(-10), execInst="LastPrice", timeInForce="GoodTillCancel").result()
Only difference is adding the price for the limit order. Your really actually sending 2 orders which makes it unclear and confusing. First it creates a trailing triggerOrder -10 away from market price. When the triggerOrder is triggered. the limit order is sent to the orderbook at the price you specified.
Below is the exact opposite stop order too above.
res, http_response = self.client.Order.Order_new(symbol="XBTUSD", side="Sell", orderQty=100, ordType="StopLimit", pegPriceType="TrailingStopPeg",price=8480, pegOffsetValue=(-10), execInst="LastPrice", timeInForce="GoodTillCancel").result()
Hi, I was seeing your work, and it is not clear to me how to place a trailing limit buy, in case I want to follow the price with a "limit" purchase order, is it possible?