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/
// © Pinetraderio
 
//@version=5
 
// For detailed information about this library please refer to our website: https://pinetrader.io
// You will also be able to find more information in our documentation: https://docs.pinetrader.io/
 
// @description TODO: Simplify the order ticket generation for Pinetrader.io
library("PineTraderOT_V5")
 
// @function CreateOrderTicket: Establishes a order ticket following appropriate guidelines.
// @param license_id Provide your license index
// @param symbol Symbol on which to execute the trade
// @param size Size of the trade, in units 
// @param order_type Direction type of the order: "BUY" or "SELL"
// @param trade_type Is it a "SPREAD" trade or a "SINGLE" symbol execution?
// @param action Execution method of the trade : "MRKT" or "PENDING"
// @param price If the order is pending you must specify the execution price
// @param tp (Optional) Take profit of the order [price, ticks, percent]
// @param sl (Optional) Stop loss of the order [price, ticks, percent]
// @param magicNum (Optional) Identifier of the order, required if trade_type="SPREAD"
// @param risk Percent to risk for the trade, if size not specified
// @param trailPrice (Optional) Price at which trailing stop is starting
// @param trailOffset (Optional) Amount to trail by [percent, ticks]
// @returns Return Order string 
export GenerateOT (string license_id, string symbol, string action,  string order_type, string trade_type, 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 )=>
    string dynamic = ""
    dynamic += "\"license_id\":\"" + license_id + "\","
    dynamic += "\"symbol\":\"" + symbol + "\","
    dynamic += "\"action\":\"" + action + "\","    
    dynamic += "\"order_type\":\"" + order_type + "\","
    dynamic += "\"trade_type\":\"" + trade_type + "\","
    if (size != 0.0) 
        dynamic += str.format("\"size\":{0, number},", (size))
    if price != 0.0 
        dynamic += str.format("\"price\":{0, number},", (price))
    if tp != 0.0 
        dynamic += str.format("\"tp\":{0, number},", (tp))
    if sl != 0.0 
        dynamic += str.format("\"sl\":{0, number},", (sl))
    if risk != 0.0 
        dynamic += str.format("\"risk\":{0, number},", (risk))
    if trailPrice != 0.0 
        dynamic += str.format("\"trail_price\":{0, number},", (trailPrice))
    if trailOffset != 0.0 
        dynamic += str.format("\"trail_offset\":{0, number}", (trailOffset))
    // Remove the trailing comma and add the closing curly brace
    dynamic := "{" + dynamic + "}"
    dynamic := str.replace(dynamic, ",}", "}")
    // Return value
    OrderString = dynamic

Save Library

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

Using the Library

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

// Importing the library
import [USERNAME]/[LIBRARY_NAME]/[VERSION] as [VARIABLE_NAME]
 
// Calling the library
[VARIABLE_NAME].CreateOrderTicket(**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