1

With ccxt library I can create orders on phemex. I use the following command:

order = exchange.create_order(symbol, type, side, amount, price, params)

But I want to do a leverage (contract) trade, how to set that? And how to change if the order is a long or short order? And by creating the order I want to give a stop loss and a take profit point. I think I have to do that in params. Do anyone have the syntax for that?

Thank you!

Dani
  • 29
  • 1
  • 8
  • Please refer this: https://github.com/ccxt/ccxt/blob/master/examples/py/bitmex-create-order.py – SreehariGaddam Jan 03 '22 at 18:28
  • I don't get it. How will my order looks like? Let's say I do this: `order = exchange.create_order("BTCUSD", "StopLimit", "Buy", 0.1, params)` with `params = {'stopPx': 38000.0}` How can I set a takeprofit for this order? – Dani Jan 04 '22 at 17:15

3 Answers3

2
while True:
    ask=exchange.fetch_order_book(sym)
    pr=ask['asks'][0][0]
    if exchange.fetch_positions()==[]:
        break
    elif exchange.fetch_positions()[0]['info']['unrealisedRoePcnt']*-100>=sl:
        exchange.create_order(sym, 'limit', 'sell', amount, pr)
    elif exchange.fetch_positions()[0]['info']['unrealisedRoePcnt']*100>=tp:
        exchange.create_order(sym, 'limit', 'sell', amount, pr)
    sleep(1)

this is how u can set tp/sl for your position :)

Ref
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 19:24
1

You need to preset the leverage like that:

exchange.set_leverage(leverage, symbol)

After that you can create a new order with your params:

exchange.create_order(symbol, type , side, amount, params=params)

I had a problem, where my takeprofit and stoploss were too low according to ccxt-api. If you'll get the same issue, you need to multiply takeProfit and stopLoss by 10000, dont ask me why. It's going to look like this:

params = {'takeProfitEp': (x*10000), 'stopLossEp': (z*10000)}

Have a nice day

Saschax
  • 11
  • 2
  • Thank you for your answer. What would type be? Market order? And where can I set weather it is a long or a short order? – Dani Jan 07 '22 at 05:36
  • Yeah, type could be "market", for a market order and side would be either "buy" or "sell" for a long or short position. Both as string value. You can find more details here: https://github.com/ccxt/ccxt/wiki/Manual – Saschax Jan 08 '22 at 11:02
  • Sorry I think I did not write it correct. A short order for me is to set on decreasing prices and not only sell a position. So I can buy and sell a long position, but I can also buy and sell a short position. So in any parameter should be defined weather it is a short or long position. Did you know what I mean? – Dani Jan 08 '22 at 15:13
  • You did not understand how trading works. If you do sell a order, you are betting for a lower price. You can either have a long position or a short position for example: you want to short bitcoin, so your side would be "sell" and phemex would enter a short position with an amount for example of 1. If you are now selling again you get a short position of an amount of 2. If you are going to "buy" you are decrease that position. – Saschax Jan 09 '22 at 13:12
0

This was my solution to this problem

order = self.exchange.create_order(
                symbols_dict[symbol], 'limit', side, amount=amount, price=price, params={'leverage': 10})

            order_stopLoss = self.exchange.create_order(
                symbols_dict[symbol], 'market', self.against_side(side), amount=amount*10, params={'stopLossPrice': stopLoss, 'reduceOnly': True})

            order_takeProfit = self.exchange.create_order(
                symbols_dict[symbol], 'market', self.against_side(side), amount=amount*10*0.2, params={'takeProfitPrice': target, 'reduceOnly': True})
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '23 at 13:31