quan-digital / ftx

Library for connecting to the FTX API.
MIT License
164 stars 59 forks source link

Take Profit and Trailing Stop not working. #7

Open zombiegriff opened 3 years ago

zombiegriff commented 3 years ago

""" To send a Stop Market order, set type='stop' and supply a trigger_price To send a Stop Limit order, also supply a limit_price To send a Take Profit Market order, set type='trailing_stop' and supply a trigger_price To send a Trailing Stop order, set type='trailing_stop' and supply a trail_value """

This is commented in the api.py. Every way i try to post a take profit or trailing stop or even a stop (as a take_profit value) i receive an error, usually saying the trigger price is too high, which is not possible when putting a take profit on a long position.

Has anyone else had similar issues with placing orders?

Appreciate any suggestions. Thanks

0xstochastic commented 3 years ago

I can place stop-loss orders but get the error you describe when trying to place-take profit orders.

zombiegriff commented 3 years ago

yeah, ive tried every which way i could think of. this link on ftx website https://github.com/ftexchange/ftx. has the same mistake in the comment above, so it seems to be an error made by who ever wrote that for ftx. ill post this on there as well. see if any one from there has had this problem..

zombiegriff commented 3 years ago

let you know if anyone gets back to me

zombiegriff commented 3 years ago

Is the return function in api.py missing the parameters the rest request needs for trailing stop and take profit types.. below?

POST /conditional_orders Stop { "market": "XRP-PERP", "side": "sell", "triggerPrice": 0.306525, "size": 31431.0, "type": "stop", "reduceOnly": false, } Trailing stop { "market": "XRP-PERP", "side": "sell", "trailValue": -0.05, "size": 31431.0, "type": "trailingStop", "reduceOnly": false, } Take profit { "market": "XRP-PERP", "side": "buy", "triggerPrice": 0.367895, "size": 31431.0, "type": "takeProfit", "reduceOnly": false, }

aleexv6 commented 3 years ago

Hey ! I'm having the same issue, do you have any news about this take profit error ?

zombiegriff commented 3 years ago

yes... the code is wrong on this rep. and ftx own rep. i found this in one of ftx pull requests. for some reason nobody has changed the code though. stop markets orders take up to 2 minutes to register on ftx website for me, is this the same for you?

type should be "'type': type" and trail value is missing.

return self._post('conditional_orders', {'market': market, 'side': side, 'triggerPrice': trigger_price, 'size': size, 'reduceOnly': reduce_only, 'type': 'stop', 'cancelLimitOnTrigger': cancel, 'orderPrice': limit_price}) 'size': size, 'reduceOnly': reduce_only, 'type': type, 'cancelLimitOnTrigger': cancel, 'orderPrice': limit_price, 'trailValue': trail_value})

aleexv6 commented 3 years ago

Well, I spoke with the creator of ftx.py about this issue, he told me that he fixed it. Can't test right now but maybe you can !

zombiegriff commented 3 years ago

I tried take profit and trailing stop today at 18.00 roughly. see below. for take profit i got an error of trigger price too high but trigger price needs to be higher than entry for long position. on the trail stop i got an error of must specify trigger. but no trigger is needed for trailing stop... this is using v1.0.2 written by thomgabriel on pycharm.

api_conn.api_client().place_conditional_order(market=pair, side='sell', type='take_profit', trigger_price=entry + (entry * 0.005), size=size, reduce_only=True)

got below error

raise Exception(data['error']) Exception: Trigger price too high

then trailing_stop (with percentage value)

api_conn.api_client().place_conditional_order(market=pair, side='sell', type='trailing_stop', trail_value=entry + (entry * 0.005), size=size, reduce_only=True)

error

raise Exception(data['error']) Exception: Must specify triggerPrice

zombiegriff commented 3 years ago

Do i need to update the library on pycharm? it says 1.0.2 is the latest version

aleexv6 commented 3 years ago

Well you might need to update the library indeed

zombiegriff commented 3 years ago

ok... how? the latest version available via pycharm is 1.0.2. maybe quandigital needs to update the version they have released on pycharm?

honestly i dont need this anymore. i worked round it.. more trying to get it fixed for others at this point.

let me know when the pycharm library has been updated and ill test it

zombiegriff commented 3 years ago

both trailing stop and take profit work now. but i had to copy api.py file to the pycharm external libraries.

zombiegriff commented 3 years ago

sorry one thing i had to add to it was 'trailValue' : trail_value into the return self.post(f'conditional_orders'....) on line 157

Aanish97 commented 3 years ago
@authentication_required
    def place_conditional_order(
            self, market: str, side: str, size: float, type: str = 'stop',
            limit_price: float = None, reduce_only: bool = False, cancel: bool = True,
            trigger_price: float = None, trail_value: float = None
    ) -> dict:
        """
        To send a Stop Market order, set type='stop' and supply a trigger_price
        To send a Stop Limit order, also supply a limit_price
        To send a Take Profit Market order, set type='trailing_stop' and supply a trigger_price
        To send a Trailing Stop order, set type='trailing_stop' and supply a trail_value
        """
        assert type in ('stop', 'take_profit', 'trailing_stop')
        assert type not in ('stop', 'take_profit') or trigger_price is not None, \
            'Need trigger prices for stop losses and take profits'
        assert type not in ('trailing_stop',) or (trigger_price is None and trail_value is not None), \
            'Trailing stops need a trail value and cannot take a trigger price'

        return self._post(f'conditional_orders',
                          {'market': market, 'side': side, 'triggerPrice': trigger_price,
                           'size': size, 'reduceOnly': reduce_only, 'type': 'stop',
                           'cancelLimitOnTrigger': cancel, 'orderPrice': limit_price})

if you look closely the developer of the API actually hardcoded stop orders after checking the type, I realize type is a reserved word in python and it might have been by mistake but common on, this mistake looks like that of a noob.

zombiegriff commented 3 years ago

Lol. I don't think it's anything to do with being a noob. Its written badly purposefully. I've actually rewrote this code. I will upload it and put a link to it in a few days

daymos commented 3 years ago

I modified the original library as well, and can now place take_profit order. However once in a while it still get the 'Too High'/'Too low' trigger price error. There is no documentation on the FTX api page about these error. Do you guy have any clue? I might write to support in the end. But seriously this is sucks.

zombiegriff commented 3 years ago

Not had that problem with take profit. I did with the trailing stop. But that's because I was putting a positive value in for a long trailing stop. I was missing the - operator.

All can suggest is check how you work out the take profit figure. Make sure the calculation is correct.

daymos commented 3 years ago

ok thanks, I'll look into that.

BCusack commented 2 years ago

return self._post('conditional_orders', {'market': market, 'side': side, 'triggerPrice': trigger_price, 'size': size, 'reduceOnly': reduce_only, 'type': type, 'cancelLimitOnTrigger': cancel, 'orderPrice': limit_price,'trailValue':trail_value}) Just added 'trailValue':trail_value to the end of the post

fahadfadi commented 2 years ago

Am I understanding this wrong? To send a Take Profit Market order, set type='trailing_stop' and supply a trigger_price shouldn't it be set type = 'take_profit" ??

fahadfadi commented 2 years ago

Also, the orderPrice argument for take profit as limit order was missing, i have sent a pull request now.

zombiegriff commented 2 years ago

https://github.com/zombiegriff/unofficial_ftx_api

its not finished but it works where i need it too. wrote the conditional orders differently. separate functions for place_order, place_stop_order, place_trailing_stop_order, place_take_profit_order

fahadfadi commented 2 years ago

Nice, I will take a look at it. If I may ask, how are you working around this issue as I realized either of TP/SL orders will be left out in the order book as one gets filled?

totseGO commented 2 years ago

Hi everyone, I'm having an issue with the SL and TP functions of the FTX API.

My goal is to create both a SL and TP that will be active once I open a position. for example I place a buy order at 10, when that order gets filled it opens a SL and TP.

The issue is that one of the two orders will be on the wrong side for a stop limit, for example price is 10, I wanna buy if the price breaks 11 and in this case place a SL at 10 and TP at 12. Though as of now I can place the first order as a stop buy with triggerprice at 11, I can place the TP limit at 12 with a trigger at 11 also but I can't for the SL. Either I have a trigger price at 11 and thus it will immediatly close the position or I can have a stop-loss limit with price at 10 and trigger at 11 but then, since the limit sell price will be below the price it will immediatly fill the order too.

Any idea how I could solve this ? Thanks for everyone taking the time to think about it !

totseGO commented 2 years ago

Also @fahadfadi you can use the 'reduceOnly': True so that if you have no position outstanding it will cancel the orders

EliorSala commented 2 years ago

Has anyone already been able to fix the problem with "trigger price is too high"?