Simple PineConnector Template

This lesson breaks down the source code to a simple PineConnector template script that sends TradingView alerts to MetaTrader so we can automate our strategies.

Check out my other free lessons!

Source Code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading
// @version=5
indicator("PineConnector Simple Example Code", overlay=true)

// Simple strategy inputs
STRATEGY_SL_DISTANCE = input.float(10, "Stop Loss Distance Pips", group="Strategy")
STRATEGY_RISK_REWARD = input.float(1, "Risk:Reward", group="Strategy")

// Simple PineConnector Inputs
PC_ID = input.string("XXX", "License ID", group="PineConnector")
PC_RISK = input.float(1, "Risk Contracts/Equity", group="PineConnector")
PC_LIMIT = input.bool(true, "Use Limit Order", group="PineConnector")
PC_LIMIT_PIPS = input.float(5, "Pip Limit Order Distance", group="PineConnector")

// Get market point -> pip value
GetPipSize() =>
    syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)

// Simple Example Strategy Code (replace with your own!)
bool LongTrade = close > open
bool ShortTrade = close < open
var label PC_DEBUG_LABEL = na

// Trigger Long alerts
if LongTrade
    // Generate PC alert string
    pcBuy       = PC_LIMIT ? "buylimit" : "buy"
    pcPrice     = PC_LIMIT ? ",price=" + str.tostring(PC_LIMIT_PIPS) : ""
    pcRisk      = "risk=" + str.tostring(PC_RISK)
    pcStop      = "sl=" + str.tostring(STRATEGY_SL_DISTANCE)
    pcProfit    = "tp=" + str.tostring(STRATEGY_SL_DISTANCE * STRATEGY_RISK_REWARD)
    pcAlert     = PC_ID + "," + pcBuy + "," + syminfo.ticker + pcPrice + "," + pcRisk + "," + pcStop + "," + pcProfit

    // Debug label
    PC_DEBUG_LABEL := label.new(bar_index, high + (10 * syminfo.mintick), pcAlert, color=color.white)
    label.delete(PC_DEBUG_LABEL[1])

    // Send alert to PC if bar closes bullish (moved barstate.isconfirmed here so that debug label updates in realtime)
    if barstate.isconfirmed
        alert(pcAlert)

// Trigger Short alerts
if ShortTrade
    // Generate PC alert string
    pcSell      = PC_LIMIT ? "selllimit" : "sell"
    pcPrice     = PC_LIMIT ? ",price=" + str.tostring(PC_LIMIT_PIPS) : ""
    pcRisk      = "risk=" + str.tostring(PC_RISK)
    pcStop      = "sl=" + str.tostring(STRATEGY_SL_DISTANCE)
    pcProfit    = "tp=" + str.tostring(STRATEGY_SL_DISTANCE * STRATEGY_RISK_REWARD)
    pcAlert     = PC_ID + "," + pcSell + "," + syminfo.ticker + pcPrice + "," + pcRisk + "," + pcStop + "," + pcProfit

    // Debug label
    PC_DEBUG_LABEL := label.new(bar_index, high + (10 * syminfo.mintick), pcAlert, color=color.white)
    label.delete(PC_DEBUG_LABEL[1])

    // Send alert to PC if bar closes bearish (moved barstate.isconfirmed here so that debug label updates in realtime)
    if barstate.isconfirmed
        alert(pcAlert)

// Plot signals
plotshape(LongTrade, "Buy", shape.triangleup, location.belowbar, color=color.green)
plotshape(ShortTrade, "Sell", shape.triangledown, location.abovebar, color=color.red)