Tradingview strategy vs study markers2

Tradingview made a big step ahead in 2019 by introducing webhooks, which can be used finally to automate Tradingview scripts. Clearly this approach still has its limits due to the missing feedback channel back to the Tradingview script; it’s still submitting orders into a black hole and hope they get executed as intended.

The following steps describe how a Tradingview Pine script can be connected to Alertatron. For details how Alertatron works and how the overall command-set looks like, please take a look here.

I hope that the following steps do explain how to automate a Tradingview script via Alertatron. If you have questions, or want to automate another script, please contact me.

Setting up Alertatron

If you don’t have a login at Alertatron, please create one.

Then create a new set of API keys bby clicking on “Configure API Keys”.

Setup Alertatron 1

In the now appearing dialog select the exchange you want to submit your orders to, a unique name for the key (only characters, no numbers), the “Key” and the “Secret”. Click on “Add API keys and restart service”.

Setup Alertatron 2

Setting up the Tradingview Pine script

Tradingview “study” scripts can be automated by using the built-in function alertcondition. Via the alertcondition message attribute a static/const message can be predefined, which can be used as a preset for the next step in Tradingview to connect the alertcondition to an alert, which is hosted in the Tradingview server-side. Please keep in mind that until today, the message attribute is a const string, that means it can not be assembled dynamically at the runtime of the script!

The example below shows two messages: one for entering a long and one for entering a short position. The tag “#bot” is required to connect the following instruction to a trading bot at Alertatron; Alertatron also supports Telegram messages for instance. The term “BitmexKeyOne(XBTUSD)” defines the name of the keys you have entered at Alertatron, and XBTUSD defines the instrument you want to trade at the exchange. Everything else within the curly brackets is the command set defined by Alertatron, to:

  1. cancel all open orders
  2. create a long/short market order wit the targeted position size of 25
  3. create a OCO profit-target and stop-loss order, when the previous long/short market order has been executed

All these sets could also be separated, and the Tradingview Pine script could manage the profit and stop-loss orders. But, Alertatron provides the mechanism to take care of complex orders, and it can submit these follow-up orders faster w/o any delay, while the executing of the Pine script depends on the used settings. Also, Crypto-APIs, like Bitmex REST API, are notorious for being not accessible when the market is going strongly up or down; that would mean that profit and stop-loss orders will be executed with a delay or not at all.

alertcondition(condEnterLong, title='Enter Long', 
   message='Enter Long 
      #bot BitmexKeyOne(XBTUSD) { 
            stopLoss=2.0%) }')
alertcondition(condEnterShort, title='Enter Short', 
   message='Enter Short 
      #bot BitmexKeyOne(XBTUSD) { 
            stopLoss=2.0%) }')

Enabling the Alert(s) in Tradingview

alertconditions by itself cannot trigger any email or popup or webhook. They need to be manually enabled in the Tradingview environment to generate a real alert.

1. Please select the “study” script for which you want to enable an alert, click on the 3 dots, and select “Add Alert on …”

Tradingview enable alerts 1

2. In the Alert pop-up dialog select

  • how often the alert shall be triggered – recommended setting here is “Once Per Bar” or “Once Per Bar Close”
  • the alert actions; check the box for “Webhook URL” (provided by Alertatron)
  • modify the predefined message with the Alertatron instructions
  • click on “Create”. This creates an alert which is hosted on the Tradingview server-side; the number of alerts someone can create depends on the Tradingview subscription type. The alerts are hosted off you local machine, so you don’t need to keep your PC and browser running!

Tradingview enable alerts 2

Tradingview strategy vs study markers2

Tradingview’s “study” and “strategy” scripts behave quite differently, when backtesting the “strategy” and try to match the behavior of the “study” script.

First thing what is different is the missing support for all functions in a “study” script. This requires that that the internal Tradingview backtest-engine behavior must be reengineered in the “study”, which leads to considerable more lines of script code.

Then, the “strategy” script is executed once at the end of each bar; if that happens the script can go through all conditions which might trigger a new positions and submits the order via strategy.entry function. This order is getting executed at the beginning of the next bar using the close price (if simple market order).

When running the “study”, which mimics the code of the “strategy”, when running with historical data, the script is also only called once at the end of each bar. Same behavior here: the “study” can check for conditions and mark the position entry/exit in chart by using Tradingview’s plot functions for instance. It can also raise alertcondition, which can be hooked up in Tradingview to emails, webhooks and so on. But as demonstrated in the screenshot below, the “study” script plots the marker exactly at the bar where the entry/exit conditions were detected, while the “strategy” shows entry/exit points in the next bar. This can be quite confusing, but please keep in mind this is still the backtest behavior. If dealing with realtime data, the behavior between “strategy” and “study” and be aligned, if using “tick mode” for “strategy” and “study” or “end-of-bar mode” for “strategy” and “study”.

Tradingview strategy vs study markers

Tradingview Status Box

Tradingview’s proprietary Pine script has clearly its limitation, but in script version 4 Tradingview has added a couple of useful features.

One of them is the option to add labels to the chart, which can used to render a status box, which might show important position parameters. The code below shows a simple example for how to add a status box to the last bar, which shows current position (long, short, flat), the current profit and the current price. It renders the box in green if profit is positive, red if negative and blue if neutral.

The status box can also be seen in action here on Tradingview.

posColor =, 75)
negColor =, 75)
dftColor =, 75)
posProfit= (strategy.position_size != 0) ? (close * 100 / strategy.position_avg_price - 100) : 0.0
posDir   = (strategy.position_size  > 0) ? "long" : strategy.position_size < 0 ? "short" : "flat"
posCol   = (posProfit > 0) ? posColor : (posProfit < 0) ? negColor : dftColor

var label lb = na
lb :=, max(high, highest(5)[1]),
   text="Pos: "+ posDir +
      "\nPnL: "+tostring(posProfit, "#.##")+"%" +
      "\nClose: "+tostring(close, "#.##"))