gateio / gateapi-python

243 stars 91 forks source link

How to keep the same total price for each next purchase of the same crypto #145

Open akashc-25 opened 1 year ago

akashc-25 commented 1 year ago

Below is not an issue, I need help regarding the problem that I'm facing

I'm using the POST /spot/orders API to create an Order. I'm passing the four things to create the Order.

  1. Amount, 2. Price, 3. Side, 4. Currency

Below is the code snippet

Order(amount="50", price="0.043792", side="buy", currency_pair="DOGE5S_USDT")

amount=50 means, Total trades that the user request for DOGE5S_USDT price=0.043792 means, At what price user want to buy this crypto side=buy means, User wants to buy the crypto currency_pair=DOGE5S_USDT means, User want to trade for this currency

Once I pass all the necessary details in the Order object, I call the POST /spot/orders API to create an order. So this API will send a request to Gate.io to buy the crypto at whatever price the user has been requested (In our case it's 0.043792). Now, Gate.io will do some calculations to find the total price. The formula is (amount price), So if we put value in amount and price (50 0.043792) parameters it will become 2.1896 as a final price. Once the last price touches buying price they will buy the crypto and also sell the crypto at a random price(Provided by the user). Once the first buying and selling cycle is completed, User again wants to buy the same crypto with a different buying price, Code snippet is below

Order(amount="50", price="0.044792", side='buy', currency_pair="DOGE5S_USDT")

Only one change from our previous code snippet which is price, I just change the price value because now the user wants to buy the same crypto at this price. Again Gate.io will do some calculations to find the total price, let's just put our values in the above formula (50 * 0.044792) it will give the final price as 2.2396. Here is the problem, The problem is when we bought the crypto the first time that time the final price was 2.1896, and now for the second buying time our final is 2.2396. I want the same final price for each buying time.

So, How can I achieve these things?

revilwang commented 1 year ago

In short, it seems you want to use a fixed amount of USDT to buy DOGE5S. You can try to set type to market, leave the price field to None, and set the USDT amount to amount field. In this case, the amount is not how much DOGE5S you want to buy. Instead, it means the amount of USDT you would like to exchange. It is documented in API reference manual

image
akashc-25 commented 1 year ago

I got your point, but instead of using type to market, can I do the same thing when type is limit? Because I want to pass the buying price when I create the order, same for when whatever stop-loss user has set I need to sell the crypto at that stop-loss price.

Any suggestion or idea would be great.

revilwang commented 1 year ago

Hmm, if using limit, your price is an absolute value, then I suppose in the case of selling, you can calculate the amount with your value divided by your price, with proper rounding

akashc-25 commented 1 year ago

Can you please provide me with an example? You can use the same value that I mentioned in my question.

akashc-25 commented 1 year ago

@revilwang Can you please provide me an update on my previous query?

akashc-25 commented 1 year ago

Please anyone help.

revilwang commented 1 year ago

Hello @akashc-25 the amount value is always the amount of DOGE5S_USDT you want to sell or buy if you set the type to limit. So in your case, if I understand correctly, the first final price is 50 * 0.043792 = 2.1896, and now the user change the price to 0.044792, if you want to reach the same final price 2.1896, you need to a do a calculation based on user's input price, that is 2.1896 / 0.044792 = 48.88372924 and round the amount to 4 digits 48.8837 (the amount_precision field in https://api.gateio.ws/api/v4/spot/currency_pairs/DOGE5S_USDT). And the order detail should be set like Order(amount="48.8837", price="0.044792", side="buy", currency_pair="DOGE5S_USDT")

akashc-25 commented 1 year ago

Got it. Thanks for the update.