PINE LIBRARY

Dhan_lib

107
Library "Dhan_lib"
Overview
Dhan_lib is a Pine Script v6 library designed to help traders automate trading orders via TradingView alerts and webhook integration with the Dhan broker API.

This library generates JSON-formatted alert messages for the following instruments.

Equity (Intraday and Delivery)
Options (CE and PE Buy and Sell)
Futures (Buy and Sell)

These alert strings can be directly used inside TradingView alerts to place live orders through an external webhook setup.

🔹 Supported Instruments

Equity
Intraday Buy and Sell
Delivery Buy and Sell

Options
Call (CE) Buy and Sell
Put (PE) Buy and Sell
ATM, ITM, and OTM strike selection
Intraday and Carry Forward

Futures
Buy and Sell
Intraday and Carry Forward

🔹 Key Features

✅ Pine Script v6 compatible
✅ Clean and reusable library functions
✅ Automatic ATM, ITM, and OTM strike calculation
✅ Expiry date handled via string format YYYY-MM-DD
✅ Fully webhook-ready JSON alert structure
✅ Supports multi-leg order format
✅ Designed for TradingView to Dhan automation

🔹 How to Use

Import the library in your strategy or indicator.

import Shivam_Mandrai/Dhan_lib/1

Call the required function.

order_msg = buy_CE_option("YOUR_SECRET_KEY", "NIFTY", 1)

Use the returned string as the alert message.

alert(order_msg, alert.freq_once_per_bar)

Connect TradingView alerts to your Dhan webhook receiver.

---

🔹 Important Notes

Strike prices are calculated dynamically based on the current chart price (close).
Futures symbols use TradingView continuous contract format such as NIFTY1!.
Quantity refers to the number of lots, not the lot size.
Expiry date must be provided in YYYY-MM-DD format.

⚠️ DISCLAIMER (PLEASE READ CAREFULLY)

This library is provided strictly for educational and automation purposes only.

I am not a SEBI-registered advisor.
I do not guarantee any profit or accuracy of orders.
I am not responsible for any financial loss, missed trades, execution errors, or broker-side issues.
Trading in stocks, options, and futures involves significant risk.

Automated trading can fail due to internet issues, broker API downtime, incorrect webhook configuration, slippage, or market volatility.

👉 Use this library entirely at your own risk.
👉 Always test thoroughly using paper trading or simulation before deploying with real capital.

If you want, I can also:

* Shrink this further for TradingView character limits
* Convert it into a single-paragraph version
* Localize it for Indian retail traders




buy_stock_intraday(secret_key, symbol, qty, exchange)
  to buy the stock Intraday
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "TATASTEEL".
    qty (int): int quantity for the order eg-> 1.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

sell_stock_intraday(secret_key, symbol, qty, exchange)
  to sell the stock Intraday
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "TATASTEEL".
    qty (int): int quantity for the order eg-> 1.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

buy_stock_delivery(secret_key, symbol, qty, exchange)
  to buy the stock delivery
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "TATASTEEL".
    qty (int): int quantity for the order eg-> 1.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

sell_stock_delivery(secret_key, symbol, qty, exchange)
  to sell the stock delivery
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "TATASTEEL".
    qty (int): int quantity for the order eg-> 1.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

buy_CE_option(secret_key, symbol, lots, expiry_date, intraday, strike_price_base, ITM_points, OTM_points, exchange)
  to buy CE option
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Index / Stock symbol eg-> "NIFTY", "BANKNIFTY".
    lots (int): int Number of lots eg-> 1.
    expiry_date (string): string Option expiry date in YYYY-MM-DD format eg-> "2026-01-20".
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    strike_price_base (float): float Strike price step size eg-> 50, 100 (default is 100).
    ITM_points (float): float Points below CMP to select ITM strike eg-> 100 (default is 0).
    OTM_points (float): float Points above CMP to select OTM strike eg-> 100 (default is 0).
    exchange (string): string Trading Exchange eg-> "NSE" (default is NSE).
  Returns: order string.

buy_PE_option(secret_key, symbol, lots, expiry_date, intraday, strike_price_base, ITM_points, OTM_points, exchange)
  to buy PE option
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Index / Stock symbol eg-> "NIFTY", "BANKNIFTY".
    lots (int): int Number of lots eg-> 1.
    expiry_date (string): string Option expiry date in YYYY-MM-DD format eg-> "2026-01-20".
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    strike_price_base (float): float Strike price step size eg-> 50, 100 (default is 100).
    ITM_points (float): float Points below CMP to select ITM strike eg-> 100 (default is 0).
    OTM_points (float): float Points above CMP to select OTM strike eg-> 100 (default is 0).
    exchange (string): string Trading Exchange eg-> "NSE" (default is NSE).
  Returns: order string.

sell_CE_option(secret_key, symbol, lots, expiry_date, intraday, strike_price_base, ITM_points, OTM_points, exchange)
  to Sell CE option
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Index / Stock symbol eg-> "NIFTY", "BANKNIFTY".
    lots (int): int Number of lots eg-> 1.
    expiry_date (string): string Option expiry date in YYYY-MM-DD format eg-> "2026-01-20".
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    strike_price_base (float): float Strike price step size eg-> 50, 100 (default is 100).
    ITM_points (float): float Points below CMP to select ITM strike eg-> 100 (default is 0).
    OTM_points (float): float Points above CMP to select OTM strike eg-> 100 (default is 0).
    exchange (string): string Trading Exchange eg-> "NSE" (default is NSE).
  Returns: order string.

sell_PE_option(secret_key, symbol, lots, expiry_date, intraday, strike_price_base, ITM_points, OTM_points, exchange)
  to sell PE option
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Index / Stock symbol eg-> "NIFTY", "BANKNIFTY".
    lots (int): int Number of lots eg-> 1.
    expiry_date (string): string Option expiry date in YYYY-MM-DD format eg-> "2026-01-20".
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    strike_price_base (float): float Strike price step size eg-> 50, 100 (default is 100).
    ITM_points (float): float Points below CMP to select ITM strike eg-> 100 (default is 0).
    OTM_points (float): float Points above CMP to select OTM strike eg-> 100 (default is 0).
    exchange (string): string Trading Exchange eg-> "NSE" (default is NSE).
  Returns: order string.

buy_future(secret_key, symbol, lot, intraday, exchange)
  to buy the Future
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "NIFTY".
    lot (int): int quantity for the order eg-> 1.
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

sell_future(secret_key, symbol, lot, intraday, exchange)
  to sell the Future
  Parameters:
    secret_key (string): string Secret Key of the Dhan Account eg-> "S1HgS".
    symbol (string): string Stock symbol eg-> "NIFTY".
    lot (int): int quantity for the order eg-> 1.
    intraday (bool): bool Set true for intraday order, set false for delivery order eg-> true.
    exchange (string): string Trading Exchange eg-> "NSE".
  Returns: order string.

Wyłączenie odpowiedzialności

Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.