Create Order Fail

Am using from delta_rest_client import DeltaRestClient to place order this is my actual code.

from delta_rest_client import DeltaRestClient

def place_order():
    response = delta_client.place_order(
        product_id=27,
        size=0.1,
        side='buy'
  )

Getting below Error:

HTTPError: 401 HTTP Error: Unauthorized {"error":{"code":"expired_signature","context":{"request_time":1741207371,"server_time":1741206574}},"success":false} for url: https://api.india.delta.exchange/v2/orders

Then i tried working with http request below code.

def generate_signature(secret, message):
    message = bytes(message, 'utf-8')
    secret = bytes(secret, 'utf-8')
    hash = hmac.new(secret, message, hashlib.sha256)
    return hash.hexdigest()
def place_order():
    base_url = 'https://api.india.delta.exchange'
    api_key = ''
    api_secret = ''
    
    method = 'POST'
    timestamp = str(int(time.time()))
    path = '/v2/orders'
    url = f'{base_url}{path}'
    order_payload = {
        "order_type":"limit_order",
        "size":0.1,
        "side":"buy",
        "limit_price":0.005,
        "product_id":27
    }
        
    payload = json.dumps(order_payload)
    print(payload)
    signature_data = method + timestamp + path  + payload
    signature = generate_signatureNew(api_secret, signature_data)
    
    req_headers = {
      'api-key': api_key,
      'timestamp': timestamp,
      'signature': signature,
      'User-Agent': 'rest-client',
      'Content-Type': 'application/json'
    }
    
    response = requests.request(method, url, data=payload, params={}, timeout=(3, 27), headers=req_headers)
    print(response.json())

Getting below Error:

{'error': {'code': 'expired_signature', 'context': {'request_time': 1741208402, 'server_time': 1741207605}}, 'success': False}

Hi @arbaz00789 , Your request is failing with a 401 Unauthorized error due to an expired signature.

The server time (1741206574) and your request time (1741207371) are not in sync, which is causing authentication to fail. Please sync your system clock with an NTP server (Network Time Protocol) to ensure accurate timestamps. You can manually sync Date & Time from the settings in your system. Once your clock is synced, try placing the request again. Let me know if the issue persists!

Thanks this is working now.

Is there any way that i check check my BTCUSD order is place and i can delete those orders and punch a new order.

I need to perform following steps

  1. First i need to check if any btcusd order is placed.
  2. Than get the order details from delta
  3. Than delete this order with these keys : order_id, product_id
  4. Than place new order.

Is there any simpler way to delete current position only with BTCUSD order and create new order.

Hi @arbaz00789 , pls go through Delta Exchange Api.

you can follow a 3 step approach

# fetch open orders and check any btc order is placed or no
open_orders = delta_client.request("GET", "/v2/orders", auth=True)
btc_orders = [order for order in open_orders["result"] if order["product_symbol"] == "BTCUSD"]

# Cancel Existing BTCUSD Orders
for order in btc_orders:
    delta_client.cancel_order(order["product_id"], order["id"])
    print(f"Canceled Order ID: {order['id']}")

#Place a new btc order
new_order = delta_client.place_order(
    product_id=27,  
    size=1, 
    side="buy", 
    order_type=OrderType.LIMIT,
    limit_price="97000"
)
print("New BTCUSD order placed:", new_order)

customise the above code as per your overall code. let me know if you have any other queries