gateio / gateapi-python

247 stars 92 forks source link

About buying & selling with +1 & -1 market price #96

Closed espala closed 2 years ago

espala commented 2 years ago

Hello There,

I am using the following api. With the help of this api, I can create an order for any coin at any price I want.

Sometimes I need to get "last price +1" or "last price +3" quickly. Sometimes I need to sell similarly "last price -1" or "last price -3". How can I do this, if you can help I would be glad.

I'm just developing myself in Python and I'm not very good at it. I will learn in time. Please excuse me, my goal is not to want fish, but to learn to fishing. I'm working on it.

I have reviewed many articles in issues on github, but I could not find clear information. Maybe I didn't understand.

tried to derive it by copying the following example from spot.py. But I was not successful. I'm probably missing a lot of things or doing it wrong.

"https://github.com/gateio/gateapi-python/blob/master/example/spot.py"

from __future__ import print_function
import gate_api
from gate_api.exceptions import ApiException, GateApiException
# Defining the host is optional and defaults to https://api.gateio.ws/api/v4
# See configuration.py for a list of all supported configuration parameters.
# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure APIv4 key authorization
configuration = gate_api.Configuration(
    host = "https://api.gateio.ws/api/v4",
    key = "mykey",
    secret = "mysecret"
)

api_client = gate_api.ApiClient(configuration)
api_instance = gate_api.SpotApi(api_client)

price = '0.869'
orderprice = '0.869'
amount = '800'

order = gate_api.Order(amount=amount, price=orderprice, account='spot', side='buy', currency_pair='SVT_USDT')
#time_in_force='ioc'

try:
    # Create an order
    api_response = api_instance.create_order(order)
    print(api_response)
except GateApiException as ex:
    print("Gate api exception, label: %s, message: %s\n" % (ex.label, ex.message))
except ApiException as e:
    print("Exception when calling SpotApi->create_order: %s\n" % e)
revilwang commented 2 years ago

Try to learn something about decimal library. Common operations like

import decimal

price = '0.869'
order_price = decimal.Decimal(price)

print(order_price + 1)
print(order_price / 2)
print(order_price - decimal.Decimal(0.001))
print(order_price * decimal.Decimal('0.3'))

# turn decimal to string
print(str(order_price))