Pinescript library

In order for your MT5 terminal to know what to trade, it needs to obtain the appropriate order ticket. Building that string inside of your Pinescript trading strategy can get fidgety. Especially since you’ll need to add it inside each one of your strategy.

That’s where PineTrader’s library comes in.

All you have to do is install this library directly into your TradingView account and simply call it when you want to generate an order ticket.

Install the library

Follow these steps to install the library:

1- Open Your Pinescript editor

2- Click on “Open” -> “New library”

3- Delete any existing code you see in your editor

4- Copy & 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/
// © pinetrader.io
 
//@version=5
 
// @description TODO: Simplify the order ticket generation for Pinetrader.io
library("PineTraderIo")
 
 
// @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 Execution type of the order: "MRKT" or "PENDING"
// @param trade_type Is it a "SPREAD" trade or a "SINGLE" symbol execution?
// @param action Direction of the trade : "BUY" or "SELL"
// @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 CreateOrderTicket (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

5- Click “Save” -> give the library a name

6- Click “Add to chart”

Voila, you are done. Now when you want to access the CreateOrderTicket() method simply import the library:

// Importing the library
import [USERNAME]/[LIBRARY_NAME]/[VERSION] as [VARIABLE_NAME]
// Calling the library
[VARIABLE_NAME].CreateOrderTicket(**pararms)

Don’t have a TradingView Account yet? Get your first month free!