From PineScript to Alerts - 101

scroll down for advanced

PineScript is TradingView’s proprietary scripting language used for coding trading indicators and strategies. Its simplified nature makes it accessible for tech-savvy people, yet coding noobs - after 2-3 weeks of digging into it, they can produce strategies of moderate complexity. The most simple trading strategy we can think of can be written in 7 lines of PineScript, while equivalent in MQL (C++-based native MetaTrader4/5 language) might exceed 100 lines.

Starting coding in PineScript is smooth and easy, no need to install anything:

  1. Open any Chart on TradingView.com

  2. Click “Pine Editor” tab at the bottom of the page

  3. Select any of the scripts from “New” dropdown to paste it in the editor

  4. Click “Add to Chart” to compile and run it. That’s it - you have just became a QUANT (trading algorithm developer).

To be able to create Alerts based on the logic coded inside PineScript (for example on crossover of two Moving Averages), the logic needs to be wrapped into alertcondition() function. For example, to produce “long” alert message, there needs to be a function call like this:

alertcondition(EnterLong, title="Long", message='long')

Variety of open source scripts - both indicators (studies) and strategies - can be found on TradingView. We recommend exploring TradingView’s library and community on your own, then experimenting yourself.

PineScript - Advanced, dynamic variables

Apart from all detailed parameters described in “Alerts Syntax” section, which can - and should - be passed via alertcondition() from PineScript, there are some even more advanced ways of transferring logic for trades execution. It is possible to intercept a value of a variable calculated in runtime and forward it in the alert to MT4/MT5. Passing dynamic variables to alerts was introduced by TradingView in November 2019 and is well described in this blog post: https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/ 

Here is a very brief example of code. It passes “low” value of last candle as Stop-Loss:

alertcondition(EnterLong, title="Long", message='long sl={{low}}')

More complex and complete use-case of dynamic variables in alerts is showcased in this educational script, which presents how to set stop-losses below and above most recent pivot points with surgical precision or take partial profits: https://www.tradingview.com/script/9MJO3AgE-TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING/

PineScript - other resources

These are also worth checking when digging into PineScript: