TradingView Integration Tutorial
Build your own Pine Script strategy or indicator and connect it to PineScriptLab
Learn the PineScriptLab automation standards, start from safe Pine Script v6 examples, configure TradingView alerts, and connect those alerts to your bot without replacing the existing Marketplace strategy format.
1. What you are building
Your Pine code decides when a signal exists. PineScriptLab validates the alert and sends the normalized action through the existing bot engine.
- Create or adapt a Pine Script strategy or indicator.
- Make the signal follow the PineScriptLab action and direction standards below.
- Create the matching TradingView alert.
- Copy the private Webhook URL and template from your PineScriptLab bot Setup Guide.
- Run a connection test before allowing trading actions.
2. Pine Script basics you need
Strategy — Use strategy() when TradingView should simulate entries/exits and generate order-fill alerts. PineScriptLab's existing Strategy Message Template reads @{{strategy.order.alert_message}} and @{{strategy.order.action}}.
Indicator — Use indicator() with alertcondition() when you want custom alert conditions without strategy orders. Connect those alerts with the optional tradingview_native_v1 JSON format.
The examples use confirmed bars so the starter logic does not intentionally fire intrabar. Your production logic can be different, but understand the timing implications before automating it.
3. PineScriptLab Automation Standards
These rules are the integration contract your own Pine code and TradingView alerts must respect.
- Event identity —
alert_idandtimestampare required. Duplicate protection uses the pairalert_id + timestamp. - Symbol —
symbolmust resolve to the bot's configured symbol. Common TradingView prefixes and a trailing.Pare normalized before comparison. - Direction and action — Long Entry opens/increases long exposure when allowed. Short Entry is Futures-only. Exit direction must match the active position. Native
sideis optional, but if supplied it must agree with the action. - Scale-In — A repeated same-direction Entry can become Scale-In when the bot is eligible. Keep using the normal Entry signal; do not invent a separate external Scale-In command.
- One-Way vs Reversal — One-Way bots use Entry/Exit. Reversal bots may auto-open the opposite side after a normal Exit; use
close_only/Exit Onlywhen you must close and remain flat. - Security and testing — Never publish a private Webhook URL or webhook ID in reusable Pine source. Connection test first. Do not begin integration testing with a live
open,close, orreverseaction.
4. Strategy starter — Pine Script v6
A long-only EMA example for Spot/Futures One-Way showing where PineScriptLab action messages belong. Replace the EMA conditions with your own strategy logic.
//@version=6
strategy("PSLAB EMA Starter - Long Only", overlay = true, pyramiding = 0)
int fastLength = input.int(9, "Fast EMA", minval = 1)
int slowLength = input.int(21, "Slow EMA", minval = 2)
float fastEma = ta.ema(close, fastLength)
float slowEma = ta.ema(close, slowLength)
bool longEntry = barstate.isconfirmed and ta.crossover(fastEma, slowEma) and strategy.position_size == 0
bool longExit = barstate.isconfirmed and ta.crossunder(fastEma, slowEma) and strategy.position_size > 0
if longEntry
strategy.entry("Long", strategy.long, alert_message = "Action: Long Entry")
// Reversal bot + stay flat: change this alert message to "Action: Long Exit Only".
if longExit
strategy.close("Long", alert_message = "Action: Long Exit")
plot(fastEma, "Fast EMA")
plot(slowEma, "Slow EMA")
Reversal bots — For a signal that should close and remain flat, use Action: Long Exit Only (or Short Exit Only). A normal Exit on a reversal bot can auto-open the opposite side after cooldown.
Connect the strategy alert
- Add the strategy to the chart that matches your bot's exchange/symbol.
- Create a TradingView alert from the strategy and select Order fills only.
- Open PineScriptLab → Trading Bots → your parent bot → Setup Guide.
- Copy the private Webhook URL and the existing Strategy Message Template.
- After changing strategy code or Inputs, recreate the TradingView alert so the alert uses the updated strategy state.
5. Indicator starter — TradingView Native v1
Use indicator alert conditions with explicit Native JSON. This keeps PineScriptLab credentials out of reusable Pine source.
//@version=6
indicator("PSLAB EMA Alert Starter - Long Only", overlay = true)
int fastLength = input.int(9, "Fast EMA", minval = 1)
int slowLength = input.int(21, "Slow EMA", minval = 2)
float fastEma = ta.ema(close, fastLength)
float slowEma = ta.ema(close, slowLength)
bool longEntry = barstate.isconfirmed and ta.crossover(fastEma, slowEma)
bool longExit = barstate.isconfirmed and ta.crossunder(fastEma, slowEma)
alertcondition(longEntry, "PSLAB Long Entry", "PSLAB Long Entry")
alertcondition(longExit, "PSLAB Long Exit", "PSLAB Long Exit")
plot(fastEma, "Fast EMA")
plot(slowEma, "Slow EMA")
Native Long Entry example
{
"format": "tradingview_native_v1",
"alert_id": "native-long-entry-@{{timenow}}",
"timestamp": "@{{timenow}}",
"symbol": "@{{ticker}}",
"action": "open",
"position_type": "long",
"webhook_id": "YOUR_BOT_WEBHOOK_ID"
}
Connect the indicator alerts
- Create separate TradingView alerts for PSLAB Long Entry and PSLAB Long Exit.
- Use Once Per Bar Close for this starter.
- Copy the bot's private Webhook URL and optional TradingView Native Template from Setup Guide.
- Entry:
action = open,position_type = long. - Exit: use
action = closefor Spot/Futures One-Way, oraction = close_onlyfor a Reversal bot when you want to stay flat.
6. Signal reference
Keep the external signal simple. PineScriptLab owns the downstream bot state, Scale-In eligibility, reconciliation, and exchange execution rules.
| Intent | Strategy message | Native action | Direction |
|---|---|---|---|
| Open Long | Action: Long Entry | open | long |
| Open Short (Futures) | Action: Short Entry | open | short |
| Close Long | Action: Long Exit | close | long |
| Close Short | Action: Short Exit | close | short |
| Close and stay flat (Reversal) | Action: Long/Short Exit Only | close_only | active direction |
| Explicit reverse (Reversal) | Action: Long/Short Reverse | reverse | active direction |
Spot bots are long-only. Short and reverse signals are rejected. A same-direction Entry while already positioned can become Scale-In only when the bot is eligible.
7. Test safely before trading
Prove the webhook connection without creating an exchange order. For Postman/manual testing, replace CURRENT_ISO_TIMESTAMP, YOUR_BOT_SYMBOL, and YOUR_BOT_WEBHOOK_ID with fresh values from your own bot. Do not paste private values into public documentation or reusable Pine code.
{
"format": "tradingview_native_v1",
"alert_id": "native-connection-test-001",
"timestamp": "CURRENT_ISO_TIMESTAMP",
"symbol": "YOUR_BOT_SYMBOL",
"action": "test",
"webhook_id": "YOUR_BOT_WEBHOOK_ID"
}
Expected result — A successful connection test confirms the bot is ready to receive alerts. It must not open a position, Scale-In, close a position, or propagate a copy trade.
8. Troubleshooting
Most rejected alerts are contract or bot-state issues, not reasons to bypass validation.
- Webhook not found: copy the current private URL again. Rotating a token invalidates the previous URL immediately.
- Symbol mismatch: use the chart for the same market/symbol as the bot.
- Unsupported action/direction: check Spot vs Futures and One-Way vs Reversal rules.
- Same-direction Entry rejected: verify whether Scale-In is configured and currently eligible.
- Exit rejected: the exit must match an active position and the bot may still be reconciling exchange state.
- Duplicate/replay: send a fresh timestamp and an event identity that reflects the TradingView event being delivered.
Keep these as reference integration starters. When an approved sample arrives, publish it as the official PineScriptLab sample without changing the backwards-compatible webhook contracts established above.
Comments (0)
No comments found