PineScript Library

Overview

The PineTrader library simplifies order ticket generation for your MT5 terminal. Instead of manually building order strings in each trading strategy, you can use our pre-built library functions to generate properly formatted order tickets.

Library Installation

Open PineScript Editor

Launch TradingView and open the PineScript editor

Create New Library

Navigate to “Open” → “New Library”

Clear Editor

Remove any existing code from the editor

Paste Library Code

Copy and paste the following code:

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PineTraderOfficial
 
//@version=6
 
// @description Assembles broker order ticket payloads as JSON strings.
library("OrderTicketBuilder")
 
// Helper: serialize a numeric field if non-zero. Returns empty string otherwise.
fmtNum(string key, float value) =>
    value == 0.0 ? "" : str.format("\"{0}\":{1, number}", key, value)
 
// Helper: serialize a string field as a quoted key/value pair.
fmtStr(string key, string value) =>
    "\"" + key + "\":\"" + value + "\""
 
// @function BuildTicket assembles a JSON order ticket string for downstream execution.
// @param licenseId    License identifier
// @param symbol       Symbol to trade
// @param action       "MRKT" or "PENDING"
// @param orderType    "BUY" or "SELL"
// @param tradeType    "SPREAD" or "SINGLE"
// @param size         (Optional) Trade size
// @param price        (Optional) Price for pending orders
// @param tp           (Optional) Take profit
// @param sl           (Optional) Stop loss
// @param risk         (Optional) Percent risk if size unspecified
// @param trailPrice   (Optional) Trailing-stop trigger price
// @param trailOffset  (Optional) Trailing-stop offset
// @returns JSON order ticket string
export BuildTicket(string licenseId, string symbol, string action, string orderType, string tradeType, float size = 0.0, float price = 0.0, float tp = 0.0, float sl = 0.0, float risk = 0.0, float trailPrice = 0.0, float trailOffset = 0.0) =>
    array<string> parts = array.new<string>()
    array.push(parts, fmtStr("license_id", licenseId))
    array.push(parts, fmtStr("symbol", symbol))
    array.push(parts, fmtStr("action", action))
    array.push(parts, fmtStr("order_type", orderType))
    array.push(parts, fmtStr("trade_type", tradeType))
 
    string sizeField  = fmtNum("size", size)
    string priceField = fmtNum("price", price)
    string tpField    = fmtNum("tp", tp)
    string slField    = fmtNum("sl", sl)
    string riskField  = fmtNum("risk", risk)
    string tpriceField = fmtNum("trail_price", trailPrice)
    string toffField   = fmtNum("trail_offset", trailOffset)
 
    if sizeField != ""
        array.push(parts, sizeField)
    if priceField != ""
        array.push(parts, priceField)
    if tpField != ""
        array.push(parts, tpField)
    if slField != ""
        array.push(parts, slField)
    if riskField != ""
        array.push(parts, riskField)
    if tpriceField != ""
        array.push(parts, tpriceField)
    if toffField != ""
        array.push(parts, toffField)
 
    "{" + array.join(parts, ",") + "}"

Save Library

  1. Click “Save”
  2. Name your library
  3. Click “Add to chart”

Using the Library

To use the BuildTicket() function in your strategies, import the library using:

// Importing the library
import [USERNAME]/[LIBRARY_NAME]/[VERSION] as [VARIABLE_NAME]
 
// Calling the library
[VARIABLE_NAME].BuildTicket(**params)
⚠️

Replace [USERNAME], [LIBRARY_NAME], [VERSION], and [VARIABLE_NAME] with your actual values.

Getting Started

New to TradingView? Get your first month free and start automating your trading strategies today!

Function Parameters

ParameterTypeRequiredDescription
license_idstringYesYour PineTrader license identifier
symbolstringYesTrading symbol (e.g., “EURUSD”)
actionstringYes”MRKT” or “PENDING”
order_typestringYes”BUY” or “SELL”
trade_typestringYes”SPREAD” or “SINGLE”
sizefloatNoTrade size in units
pricefloatNoRequired for pending orders
tpfloatNoTake profit level
slfloatNoStop loss level
riskfloatNoRisk percentage if size not specified
trailPricefloatNoTrailing stop activation price
trailOffsetfloatNoTrailing stop offset