gateio / gateapi-python

243 stars 91 forks source link

How to work with stop loss effectively with API #146

Open akashc-25 opened 1 year ago

akashc-25 commented 1 year ago

I'm using the POST /spot/orders API to sell the crypto.

Let's say, The stop-loss price is $10. The last price of the crypto was $11.

I'm checking, and when stop-loss >= last price is True (say stop-loss is $10 and last price is $10), at that point I'm calling the POST /spot/orders to sell the crypto, but the moment I'm calling the API, the last price of the crypto went down from $10 to $8, So, in this case, my selling request will be in open status in gate.io, and it will sell when last price again touch $10 but I want to sell the crypto when it's gets to my price for stop-loss price.

I also tried the POST /spot/price_orders trigger API, The condition is also the same as above (stop-loss >= last price), but the problem is that when I set the trigger price to $10 (which is the stop-loss price, and the last price is $9) in this API, I get the error saying that "Trigger. price must < last price." because the last price is less than the stop-loss price in our case.

I also attempted to pass the trigger price as 9$ (last price), but in some cases, the last price dropped from 9 to 8, so gate. IO will raise the same error.

Snippet code as below for trigger API

SpotPriceTriggeredOrder(
         trigger={
              'price': "10", # stop-loss price
              'rule': '<=',
              'expiration': 2592000
          },
          put={
              'type': 'limit',
              'side': "sell",
              'price': "10", # stop-loss price
              'amount': "50",
              "account": "normal",
              "time_in_force": "gtc"
          },
          market="DOGE5S_USDT"
      )

Is there any solution to this problem?

akashc-25 commented 1 year ago

Can anyone help, please?

revilwang commented 1 year ago

What is the problem actually? I think you need to go though your business logic once again. It seems you’re trying to set the stop-loss price even when the market price has already gone down.

akashc-25 commented 1 year ago

Okay, Let me give you one example,

Users want to buy the crypto at $15 Sell the crypto at $18 Stop-loss price is $10 Last price of the crypto is $12

So, When the last price crosses the stop-loss price(Latest last price is $8), So when last price is less than or less than equal to stop loss price I want to sell the crypto immediately but the problem is when I send a request for sell the crypto (I'm using POST /spot/orders to sell the crypto) that request in gate.io will be in open status order because last price is $8 and stop-loss is $10. So once last price again touches $10 then it will sell.

I always want to sell the crypto immediately when the last price is <= stop loss.

revilwang commented 1 year ago

If the current price has already goes down below your stop-loss price, and you want to sell it ASAP, you need to place a plain POST /spot/orders request to sell the order either with a price lower than current price, or sell it using market price. The price-triggered-order will not help here.

akashc-25 commented 1 year ago

It means that if the last price is lower than the stop-loss price, it will sell immediately rather than waiting until it touches the stop-loss price. Is it correct?

Below is the snippet code of the POST /spot/orders API for selling Order(amount="50", price="10", side="sell", currency_pair="DOGE5S_USDT")

akashc-25 commented 1 year ago

When I do trading directly from gate.io, even when it's very fast when the last price touches the stop-loss price, it sells immediately, So may I know, if is there any strategy that gate.io follows?

revilwang commented 1 year ago

I'm sorry I still do not get your point. Take your example above:

Users want to buy the crypto at $15 Sell the crypto at $18 Stop-loss price is $10 Last price of the crypto is $12 I always want to sell the crypto immediately when the last price is <= stop loss.

You want to:

  1. If current price > stop-loss price, then wait until it reaches the stop-loss price to sell
  2. If current price <= stop-loss price, you want to sell it as soon as possible with the current price.

Is that correct? If I'm not mistaken, in the first case, you can create a stop-loss triggered order and wait until the triggered happened. That's how it works, avoid your loss automatically for you.

For the second case, if current price has already gone down, there is no point in creating a stop-loss triggered order. Why not try to create a plain order with current price to make sure the order can be sold as soon as possible?

akashc-25 commented 1 year ago

Yes, You're right. Let's consider the first scenario: Do you mean that I should create the stop-loss trigger order at the time of buying?

Because currently, When current_price = stop-loss, at that I create the stop-loss trigger but I get the error saying "Trigger. price must < last price." because current_price changes frequently so when the trigger order executes at that time possibly trigger price >= last price. So this is what the error saying.

akashc-25 commented 1 year ago

Hello @revilwang,

I've implemented the above things in my code and noticed one thing. I'll go ahead and explain that thing with one example.

Users want to buy the crypto at $15 Sell the crypto at $18 Stop-loss price is $10 Last price of the crypto is $12

I'm creating the buy order, and along with that, I'm creating the stop-loss trigger order.

I want to ask, is your stop loss order been executed when the stop loss gets to 10 or immediately when the crypto is bought?

Below is a code snippet of the trigger order

spot_price_trigger_order = SpotPriceTriggeredOrder(
        trigger={
            'price': "10",
            'rule': '<=',
            'expiration': 2592000
        },
        put={
            'type': 'limit',
            'side': 'sell',
            'price': "10",
            'amount': "50",
            "account": "normal",
            "time_in_force": "gtc"
        },
        market="DOGE5S_USDT"
    )
akashc-25 commented 1 year ago

Hi @revilwang, Can you help, please?

revilwang commented 1 year ago

I'm not sure what you mean, but put defines the order placed by system for you when the trigger is matched. Like in your example, if current price is 12, and once the price goes down below 10, a new sell order is created with amount 50.

akashc-25 commented 1 year ago

Yes, but I'm not sure why a new sell order is created because I'm using trigger orders, and once the triggers execute at whatever price they should sell at that time. 

revilwang commented 1 year ago

I think you misunderstand something here. The price triggered order will place an order with parameters you defined in put once the trigger happens. It only defines an action that should happen in the futures, and it has nothing to do with whatever other orders you've created. What do you expect?

akashc-25 commented 1 year ago

I expect the order to sell at whatever price I've set as the trigger price. 

revilwang commented 1 year ago

No, the order only sells with the price you set in put field.

akashc-25 commented 1 year ago

Okay, So Is there a method to sell the order right away? This means that when I ask to sell something for $10 and the current price is $12 and it has dropped from $12 to $9, I don't want to wait for the current price to reach $10; I just want to sell for $10 right away.

revilwang commented 1 year ago

It depends on your own need, either you can sell it with market price, or you can set your put price to a maybe 5-10% lower price, or you don't use the price triggered order, listen to websocket price updates and create the order by yourself.

akashc-25 commented 1 year ago

Okay

akashc-25 commented 1 year ago

Is there any way to know when my triggered order was executed? I mean any callback or webhook event is there in the trigger order API when the trigger order executes? So if is there any event that occurs based on that I can take action.

revilwang commented 1 year ago

No, there is no obvious way to do so currently. But you can try to use a custom order id with text field in your trigger order and subscribe to websocket spot.orders channel(refer to gatews project) and wait for order updates. Use the text field in your order response to match your trigger.

akashc-25 commented 1 year ago

Okay, Basically what I want to do is that when my trigger order is finished I want to run my code automatically and want to sell the crypto. So will it be possible to do it through you mentioned above?