Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Sample program - place a buy order using the Dhan API

To place a buy order using the Dhan API, you typically need to follow these steps:



1. **Authenticate**:
   Authenticate yourself with the Dhan API by providing your API key or other authentication credentials.

2. **Fetch Instrument Details**:
   Retrieve the details of the instrument you want to trade, such as its symbol, exchange, and other necessary parameters.

3. **Place Buy Order**:
   Use the appropriate endpoint to place a buy order. You'll need to specify parameters such as the instrument symbol, quantity, order type (market or limit), price (if applicable), and other relevant details.

4. **Handle Response**:
   Handle the response from the API to determine if the order was successfully placed. The response may include information such as the order ID, execution status, and any error messages.

Here's a basic example of how you might implement this in Python using the `requests` library:

```python

import requests


# Replace 'YOUR_API_KEY' with your actual API key

API_KEY = 'YOUR_API_KEY'


# Base URL for Dhan API

BASE_URL = 'https://api.dhan.co/v1/'


# Authentication headers

headers = {

    'Authorization': f'Bearer {API_KEY}',

    'Content-Type': 'application/json'

}


# Fetch instrument details (replace 'instrument_symbol' with the symbol of the instrument you want to trade)

instrument_symbol = 'YOUR_INSTRUMENT_SYMBOL'

instrument_details_url = f'{BASE_URL}/instruments/{instrument_symbol}'

response = requests.get(instrument_details_url, headers=headers)

instrument_details = response.json()


# Place buy order

buy_order_payload = {

    'symbol': instrument_symbol,

    'quantity': 10,  # Specify the quantity you want to buy

    'order_type': 'market',  # Market order

    # Add other parameters as needed (e.g., price, validity, etc.)

}

place_order_url = f'{BASE_URL}/orders'

response = requests.post(place_order_url, json=buy_order_payload, headers=headers)

order_response = response.json()


# Handle response

if response.status_code == 200:

    print('Buy order placed successfully:')

    print(order_response)

else:

    print('Failed to place buy order:')

    print(order_response)

```

Please note that this is a basic example and may need to be adjusted based on the specific requirements of the Dhan API and your trading strategy. Additionally, ensure that you have appropriate permissions and account balance before placing buy orders through the API.

caa March 26 2024 200 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?