OpenAlgo

OpenAlgo
Overview
OpenAlgo is a Pine Script v6 helper library for strategies that send their orders to the open-source OpenAlgo order-routing platform through TradingView alerts, typically for Indian markets. It handles the conversions and message formats those strategies repeat: expiry and symbol formats, ATM strikes, the order JSON, custom alert templates, an end-of-day time check and an "all selected filters pass" gate.
This library only builds text and values inside Pine Script. It does not place orders by itself; orders are sent by TradingView alerts that the user configures.
What's inside
Quick start
Pine Script®
Notes
Demo
Add the library to a chart on its own to see a demo that uses every exported function.
Library "OpenAlgo"
Helpers for strategies that route orders to OpenAlgo through TradingView alerts: symbol and expiry
conversion between OpenAlgo (NFO:NIFTY24FEB2623500CE) and TradingView (NSE:NIFTY260224C23500)
formats, ATM strike rounding, the OpenAlgo order JSON, custom alert templates, an end-of-day time
check and an all-selected-filters gate.
tvExpiry(expiry)
Converts an OpenAlgo expiry (DDMMMYY, e.g. "24FEB26") to TradingView's YYMMDD (e.g. "260224").
Parameters:
expiry (string): (series string) Expiry in DDMMMYY form.
Returns: (string) The expiry in YYMMDD form ("00" as the month when it isn't recognised).
exchangeOf(tickerid, dflt)
Exchange prefix of a ticker, e.g. "NSE" for "NSE:NIFTY1!".
Parameters:
tickerid (string): (series string) A ticker with or without an exchange prefix.
dflt (string): (series string) Returned when there is no prefix. Optional. Default is "NSE".
Returns: (string) The exchange.
rootSymbol(tickerid)
Root symbol of a ticker: the exchange prefix and continuous-contract suffixes ("1!", "2!", "!") removed,
e.g. "NIFTY" for "NSE:NIFTY1!".
Parameters:
tickerid (string): (series string) A ticker with or without an exchange prefix.
Returns: (string) The root symbol.
atmStrike(price, interval)
Nearest strike to a price on a strike grid (a remainder of exactly half an interval rounds down).
Parameters:
price (float): (series float) Underlying price.
interval (int): (series int) Strike interval, e.g. 50.
Returns: (float) The strike.
optionSymbol(root, expiry, strike, isCall)
OpenAlgo option symbol, e.g. "NIFTY24FEB2623500CE".
Parameters:
root (string): (series string) Root symbol, e.g. "NIFTY".
expiry (string): (series string) Expiry in DDMMMYY form, e.g. "24FEB26".
strike (float): (series float) Strike (rounded to an integer).
isCall (bool): (series bool) `true` for CE, `false` for PE.
Returns: (string) The option symbol.
orderJson(apikey, strategyName, symbol, action, exch, priceType, price, product, quantity, positionSize)
OpenAlgo `placesmartorder` JSON payload.
Parameters:
apikey (string): (series string) OpenAlgo API key.
strategyName (string): (series string) Strategy name.
symbol (string): (series string) OpenAlgo symbol.
action (string): (series string) "BUY" or "SELL".
exch (string): (series string) OpenAlgo exchange, e.g. "NFO".
priceType (string): (series string) "MARKET" or "LIMIT".
price (string): (series string) Limit price ("0" for market orders).
product (string): (series string) "MIS", "NRML" or "CNC".
quantity (string): (series string) Order quantity, in lots.
positionSize (string): (series string) Target position after the order ("0" = flat, "-N" = short N).
Returns: (string) The JSON payload.
fillTemplate(template, signal, symbol, action, exch, price, quantity, strategyName)
Fills a custom alert template. Placeholders: {{signal}}, {{exchange}}, {{symbol}}, {{action}}, {{price}},
{{quantity}}, {{strategy}}.
Parameters:
template (string): (series string) The template.
signal (string): (series string) Signal name, e.g. "BUY" or "COVER".
symbol (string): (series string) Symbol.
action (string): (series string) Order action.
exch (string): (series string) Exchange.
price (string): (series string) Price.
quantity (string): (series string) Quantity.
strategyName (string): (series string) Strategy name.
Returns: (string) The filled message.
pastTime(hhmm)
Whether the bar's time (in the exchange time zone) is at or past an HHMM time, e.g. for an end-of-day
square-off.
Parameters:
hhmm (int): (series int) Time as HHMM, e.g. 1525.
Returns: (bool) `true` from that minute to the end of the day.
allPass(enabled, passed)
Filter gate: `true` when at least one filter is enabled and every enabled filter passed.
Parameters:
enabled (array<bool>): (array<bool>) Which filters are enabled.
passed (array<bool>): (array<bool>) Which filters passed, in the same order.
Returns: (bool) The gate.
Overview
OpenAlgo is a Pine Script v6 helper library for strategies that send their orders to the open-source OpenAlgo order-routing platform through TradingView alerts, typically for Indian markets. It handles the conversions and message formats those strategies repeat: expiry and symbol formats, ATM strikes, the order JSON, custom alert templates, an end-of-day time check and an "all selected filters pass" gate.
This library only builds text and values inside Pine Script. It does not place orders by itself; orders are sent by TradingView alerts that the user configures.
What's inside
- tvExpiry(expiry): converts an OpenAlgo expiry in DDMMMYY form (24FEB26) to TradingView's YYMMDD form (260224).
- exchangeOf(tickerid, dflt) and rootSymbol(tickerid): split a ticker such as NSE:NIFTY1! into its exchange (NSE) and root symbol (NIFTY), removing continuous-contract suffixes.
- atmStrike(price, interval): nearest strike on a strike grid.
- optionSymbol(root, expiry, strike, isCall): OpenAlgo option symbol, for example NIFTY24FEB2623500CE.
- orderJson(...): the OpenAlgo smart-order JSON payload (API key, strategy, symbol, action, exchange, price type, price, product, quantity, position size).
- fillTemplate(...): fills a custom alert template with the placeholders {{signal}}, {{exchange}}, {{symbol}}, {{action}}, {{price}}, {{quantity}} and {{strategy}}.
- pastTime(hhmm): true from a given exchange-time minute (for example 1525) to the end of the day, for end-of-day square-off logic.
- allPass(enabled, passed): true when at least one filter is enabled and every enabled filter passed.
Quick start
Notes
- Never publish a script with a real API key in its defaults. Keep keys in the script's inputs on your own chart, or in the alert message.
- Alert payloads are sent by TradingView only when the user creates an alert with a webhook; the library has no network access of its own.
Demo
Add the library to a chart on its own to see a demo that uses every exported function.
Library "OpenAlgo"
Helpers for strategies that route orders to OpenAlgo through TradingView alerts: symbol and expiry
conversion between OpenAlgo (NFO:NIFTY24FEB2623500CE) and TradingView (NSE:NIFTY260224C23500)
formats, ATM strike rounding, the OpenAlgo order JSON, custom alert templates, an end-of-day time
check and an all-selected-filters gate.
tvExpiry(expiry)
Converts an OpenAlgo expiry (DDMMMYY, e.g. "24FEB26") to TradingView's YYMMDD (e.g. "260224").
Parameters:
expiry (string): (series string) Expiry in DDMMMYY form.
Returns: (string) The expiry in YYMMDD form ("00" as the month when it isn't recognised).
exchangeOf(tickerid, dflt)
Exchange prefix of a ticker, e.g. "NSE" for "NSE:NIFTY1!".
Parameters:
tickerid (string): (series string) A ticker with or without an exchange prefix.
dflt (string): (series string) Returned when there is no prefix. Optional. Default is "NSE".
Returns: (string) The exchange.
rootSymbol(tickerid)
Root symbol of a ticker: the exchange prefix and continuous-contract suffixes ("1!", "2!", "!") removed,
e.g. "NIFTY" for "NSE:NIFTY1!".
Parameters:
tickerid (string): (series string) A ticker with or without an exchange prefix.
Returns: (string) The root symbol.
atmStrike(price, interval)
Nearest strike to a price on a strike grid (a remainder of exactly half an interval rounds down).
Parameters:
price (float): (series float) Underlying price.
interval (int): (series int) Strike interval, e.g. 50.
Returns: (float) The strike.
optionSymbol(root, expiry, strike, isCall)
OpenAlgo option symbol, e.g. "NIFTY24FEB2623500CE".
Parameters:
root (string): (series string) Root symbol, e.g. "NIFTY".
expiry (string): (series string) Expiry in DDMMMYY form, e.g. "24FEB26".
strike (float): (series float) Strike (rounded to an integer).
isCall (bool): (series bool) `true` for CE, `false` for PE.
Returns: (string) The option symbol.
orderJson(apikey, strategyName, symbol, action, exch, priceType, price, product, quantity, positionSize)
OpenAlgo `placesmartorder` JSON payload.
Parameters:
apikey (string): (series string) OpenAlgo API key.
strategyName (string): (series string) Strategy name.
symbol (string): (series string) OpenAlgo symbol.
action (string): (series string) "BUY" or "SELL".
exch (string): (series string) OpenAlgo exchange, e.g. "NFO".
priceType (string): (series string) "MARKET" or "LIMIT".
price (string): (series string) Limit price ("0" for market orders).
product (string): (series string) "MIS", "NRML" or "CNC".
quantity (string): (series string) Order quantity, in lots.
positionSize (string): (series string) Target position after the order ("0" = flat, "-N" = short N).
Returns: (string) The JSON payload.
fillTemplate(template, signal, symbol, action, exch, price, quantity, strategyName)
Fills a custom alert template. Placeholders: {{signal}}, {{exchange}}, {{symbol}}, {{action}}, {{price}},
{{quantity}}, {{strategy}}.
Parameters:
template (string): (series string) The template.
signal (string): (series string) Signal name, e.g. "BUY" or "COVER".
symbol (string): (series string) Symbol.
action (string): (series string) Order action.
exch (string): (series string) Exchange.
price (string): (series string) Price.
quantity (string): (series string) Quantity.
strategyName (string): (series string) Strategy name.
Returns: (string) The filled message.
pastTime(hhmm)
Whether the bar's time (in the exchange time zone) is at or past an HHMM time, e.g. for an end-of-day
square-off.
Parameters:
hhmm (int): (series int) Time as HHMM, e.g. 1525.
Returns: (bool) `true` from that minute to the end of the day.
allPass(enabled, passed)
Filter gate: `true` when at least one filter is enabled and every enabled filter passed.
Parameters:
enabled (array<bool>): (array<bool>) Which filters are enabled.
passed (array<bool>): (array<bool>) Which filters passed, in the same order.
Returns: (bool) The gate.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.