TradingView
TradingView is a social network of 30 million traders and investors using the world's best charts and tools to spot trading opportunities across global markets.
TradingView chart with Trend Following MOMO strategy on the chart.

Alert Message JSON

The TradersPost webhook system lets you easily integrate alerts from platforms like TradingView with TradersPost. Webhooks can receive JSON like the following.
1
{
2
"ticker": "TSLA",
3
"action": "buy",
4
"price": "420.69"
5
}
Copied!
This JSON data structure is very important and it must be 100% correct otherwise TradersPost will not be able to parse the data out of the JSON in order to use the information correctly.
You can dynamically send the values in the above JSON using TradingView variables. You can read more about TradingView variables here.

Enter Long

1
{
2
"ticker": "{{ticker}}",
3
"action": "buy",
4
"price": "{{close}}"
5
}
Copied!

Exit Long

1
{
2
"ticker": "{{ticker}}",
3
"action": "exit",
4
"price": "{{close}}"
5
}
Copied!

Enter Short

1
{
2
"ticker": "{{ticker}}",
3
"action": "sell",
4
"price": "{{close}}"
5
}
Copied!

Exit Short

1
{
2
"ticker": "{{ticker}}",
3
"action": "exit",
4
"price": "{{close}}"
5
}
Copied!
The values inside of the {{ and }} will be dynamically replaced by TradingView when the alert triggers.
When you create an alert in TradingView, you only need to enter the above JSON in the Message field and the URL of your webhook in the Webhook URL field. Here is a screenshot of a simple example that would send a signal to buy SQ when the price crosses above 241.94.
TradingView plain manually configured alert webhook.
This is a simple example to demonstrate the basics of how you can integrate TradingView alerts with TradersPost but the same principals apply if you are doing something more advanced with a Pine Script indicator. Continue reading to learn how you can integrate your Pine Script indicators and alerts with TradersPost.

Pine Script Studies

Here is a simple trend following momentum based indicator called MOMO that was created by Matt DeLong from RealLifeTrading.com. It uses the EMA8 and EMA21 and the signal is when those two values cross each other.
The message parameter of the alertcondition() function is pre-populated with the JSON alert message required by TradersPost.
1
//@version=4
2
//author = https://www.tradingview.com/u/MattDeLong/
3
4
study("Trend Following MOMO", overlay=true)
5
6
ema8 = ema(close, 8)
7
ema21 = ema(close, 21)
8
9
isUptrend = ema8 >= ema21
10
isDowntrend = ema8 <= ema21
11
trendChanging = cross(ema8, ema21)
12
13
tradersPostBuy = trendChanging and isUptrend
14
tradersPostSell = trendChanging and isDowntrend
15
16
plotshape(tradersPostBuy, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, text='Buy')
17
plotshape(tradersPostSell, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, text='Sell')
18
19
aa = plot(ema8, linewidth=3, color=color.green, editable=true, title="ema8")
20
bb = plot(ema21,linewidth=3, color=color.red, editable=true, title="ema21")
21
fill(aa, bb, color=isUptrend ? color.green : color.red)
22
23
alertcondition(tradersPostBuy, title="TradersPost Buy Alert", message="{\"ticker\": \"{{ticker}}\", \"action\": \"buy\", \"price\": {{close}}}")
24
alertcondition(tradersPostSell, title="TradersPost Sell Alert", message="{\"ticker\": \"{{ticker}}\", \"action\": \"sell\", \"price\": {{close}}}")
Copied!
Once you have the Trend Following MOMO indicator in your TradingView you can add it to your charts and it will look like the following with visual buy and sell indicators overlayed on top of your chart.
MOMO Trend Following Indicator
Next, you can create alerts for the buy and sell signals and send them to TradersPost via webhooks. Follow these steps to setup the alerts as webhooks.
  1. 1.
    Open the Alerts pane in TradingView
  2. 2.
    Click Create Alert at the top right of the Alerts pane
  3. 3.
    Select Trend Following MOMO from the Condition dropdown
  4. 4.
    Select TradersPost Buy Alert or TradersPost Sell Alert under the Condition dropdown.
  5. 5.
    Click Once Per Bar Close next to Options
  6. 6.
    Paste your TradersPost webhook URL in the Webhook URL field.
  7. 7.
    Give your alert a name like HD MOMO 1D Buy
Here is what your alert should look like.
MOMO Trend Following Indicator Alert Webhook
Notice how the Message field in the alert was automatically filled in with the JSON that is required for TradersPost? This is made available to us by these two lines of code in the Pine Script.
1
alertcondition(tradersPostBuy, title="TradersPost Buy Alert", message="{\"ticker\": \"{{ticker}}\", \"action\": \"buy\", \"price\": {{close}}}")
2
alertcondition(tradersPostSell, title="TradersPost Sell Alert", message="{\"ticker\": \"{{ticker}}\", \"action\": \"sell\", \"price\": {{close}}}")
Copied!

Pine Script Strategies

There are several different ways that you can build strategies in TradingView from studies that are purely visual indicators to strategies that are back testable.

Custom Strategies

Here is an example where the JSON for the alert message is defined in the Pine Script strategy itself and passed to the alert_message parameter in functions like strategy.entry().
1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2
// © TradersPostInc
3
4
//@version=5
5
strategy('TradersPost Example MOMO Strategy', overlay=true)
6
7
ema8 = ta.ema(close, 8)
8
ema21 = ta.ema(close, 21)
9
10
isUptrend = ema8 >= ema21
11
isDowntrend = ema8 <= ema21
12
trendChanging = ta.cross(ema8, ema21)
13
14
tradersPostBuy = trendChanging and isUptrend
15
tradersPostSell = trendChanging and isDowntrend
16
17
buyAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "buy", "price": ' + str.tostring(close) + '}'
18
sellAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "sell", "price": ' + str.tostring(close) + '}'
19
20
if tradersPostBuy
21
strategy.entry("Long", strategy.long, alert_message = buyAlertMessage)
22
23
if tradersPostSell
24
strategy.close("Long", when = open[0], alert_message = sellAlertMessage)
Copied!
Then when you are setting up the alert for your strategy you can put the following code in the Alert Message text area.
1
{{strategy.order.alert_message}}
Copied!

Complex Strategy

Here is a much more complex version of the above strategy with many more features implemented. It has the following additional features implemented.
  • Both long and short trades
  • Long and short take profit and stop losses
  • Configure EMA length
  • Configure backtest time window
1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2
// © TradersPostInc
3
4
//@version=5
5
strategy('TradersPost Example MOMO Strategy', overlay=true, default_qty_value=100, initial_capital=100000, default_qty_type=strategy.percent_of_equity, pyramiding=0)
6
7
startTime = input.time(defval = timestamp('01 Jan 2021 00:00 +0000'), title = 'Start Time', group = 'Date Range')
8
endTime = input.time(defval = timestamp('31 Dec 2023 23:59 +0000'), title = 'End Time', group = 'Date Range')
9
timeCondition = time >= startTime and time < endTime
10
timeConditionEnd = timeCondition[1] and not timeCondition
11
12
fastEmaLength = input.int(defval = 8, title = 'Fast EMA Length')
13
slowEmaLength = input.int(defval = 21, title = 'Slow EMA Length')
14
sides = input.string(defval = 'Both', title = 'Sides', options = ['Long', 'Short', 'Both', 'None'])
15
16
fastEma = ta.ema(close, fastEmaLength)
17
slowEma = ta.ema(close, slowEmaLength)
18
19
isUptrend = fastEma >= slowEma
20
isDowntrend = fastEma <= slowEma
21
trendChanging = ta.cross(fastEma, slowEma)
22
23
ema105 = request.security(syminfo.tickerid, '30', ta.ema(close, 105)[1], barmerge.gaps_off, barmerge.lookahead_on)
24
ema205 = request.security(syminfo.tickerid, '30', ta.ema(close, 20)[1], barmerge.gaps_off, barmerge.lookahead_on)
25
plot(ema105, linewidth=4, color=color.new(color.purple, 0), editable=true)
26
plot(ema205, linewidth=2, color=color.new(color.purple, 0), editable=true)
27
28
aa = plot(fastEma, linewidth=3, color=color.new(color.green, 0), editable=true)
29
bb = plot(slowEma, linewidth=3, color=color.new(color.red, 0), editable=true)
30
fill(aa, bb, color=isUptrend ? color.green : color.red, transp=90)
31
32
tradersPostBuy = trendChanging and isUptrend and timeCondition
33
tradersPostSell = trendChanging and isDowntrend and timeCondition
34
35
pips = syminfo.pointvalue / syminfo.mintick
36
37
percentOrPipsInput = input.string('Percent', title='Percent or Pips', options=['Percent', 'Pips'])
38
39
stopLossLongInput = input.float(defval=0, step=0.01, title='Stop Loss Long', minval=0)
40
stopLossShortInput = input.float(defval=0, step=0.01, title='Stop Loss Short', minval=0)
41
42
takeProfitLongInput = input.float(defval=0, step=0.01, title='Target Profit Long', minval=0)
43
takeProfitShortInput = input.float(defval=0, step=0.01, title='Target Profit Short', minval=0)
44
45
stopLossPriceLong = ta.valuewhen(tradersPostBuy, close, 0) * (stopLossLongInput / 100) * pips
46
stopLossPriceShort = ta.valuewhen(tradersPostSell, close, 0) * (stopLossShortInput / 100) * pips
47
48
takeProfitPriceLong = ta.valuewhen(tradersPostBuy, close, 0) * (takeProfitLongInput / 100) * pips
49
takeProfitPriceShort = ta.valuewhen(tradersPostSell, close, 0) * (takeProfitShortInput / 100) * pips
50
51
takeProfitALong = takeProfitLongInput > 0 ? takeProfitLongInput : na
52
takeProfitBLong = takeProfitPriceLong > 0 ? takeProfitPriceLong : na
53
54
takeProfitAShort = takeProfitShortInput > 0 ? takeProfitShortInput : na
55
takeProfitBShort = takeProfitPriceShort > 0 ? takeProfitPriceShort : na
56
57
stopLossALong = stopLossLongInput > 0 ? stopLossLongInput : na
58
stopLossBLong = stopLossPriceLong > 0 ? stopLossPriceLong : na
59
60
stopLossAShort = stopLossShortInput > 0 ? stopLossShortInput : na
61
stopLossBShort = stopLossPriceShort > 0 ? stopLossPriceShort : na
62
63
takeProfitLong = percentOrPipsInput == 'Pips' ? takeProfitALong : takeProfitBLong
64
stopLossLong = percentOrPipsInput == 'Pips' ? stopLossALong : stopLossBLong
65
takeProfitShort = percentOrPipsInput == 'Pips' ? takeProfitAShort : takeProfitBShort
66
stopLossShort = percentOrPipsInput == 'Pips' ? stopLossAShort : stopLossBShort
67
68
buyAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "buy", "price": ' + str.tostring(close) + '}'
69
sellAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "sell", "price": ' + str.tostring(close) + '}'
70
71
exitLongAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "exit", "price": ' + str.tostring(close) + '}'
72
exitShortAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "exit", "price": ' + str.tostring(close) + '}'
73
74
if (sides != "None")
75
if tradersPostBuy
76
strategy.entry('Long', strategy.long, when = sides != 'Short', alert_message = buyAlertMessage)
77
strategy.close('Short', when = sides == "Short" and timeCondition, alert_message = exitShortAlertMessage)
78
79
if tradersPostSell
80
strategy.entry('Short', strategy.short, when = sides != 'Long', alert_message = sellAlertMessage)
81
strategy.close('Long', when = sides == 'Long', alert_message = exitLongAlertMessage)
82
83
exitAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "exit"}'
84
85
strategy.exit('Exit Long', from_entry = "Long", profit = takeProfitLong, loss = stopLossLong, alert_message = exitAlertMessage)
86
strategy.exit('Exit Short', from_entry = "Short", profit = takeProfitShort, loss = stopLossShort, alert_message = exitAlertMessage)
87
88
strategy.close_all(when = timeConditionEnd)
Copied!
If you have an idea for a strategy and you need help with implementing the strategy in Pine Script, TradersPost can help you! Learn more about our custom strategy development team.

Shared Strategies

Sometimes you may want to hook up a strategy to TradersPost that was built by someone else and you do not have the ability to modify the Pine Script. You can easily send alerts from existing strategies and send the alerts as webhooks to TradersPost.
Just send the following JSON in the alert message to TradersPost.
1
{
2
"ticker": "{{ticker}}",
3
"action": "{{strategy.order.action}}",
4
"price": "{{close}}"
5
}
Copied!
The {{strategy.order.action}} code will be dynamically replaced with a value of buy or sell when the strategy triggers an order.

Pine Script Repainting

When writing Pine Script strategies and indicators it is important to ensure your code does not suffer from repainting. Read the notes in this section to learn about repainting and how to avoid it.
From PineCoders:
What do we mean by repainting?
Repainting is used to describe three different things, in what we’ve seen in TV members comments on indicators:
  • An indicator showing results that change during the realtime bar, whether the script is using the security() function or not, e.g., a Buy signal that goes on and then off, or a plot that changes values.
  • An indicator that uses future data not yet available on historical bars.
  • An indicator that uses a negative offset= parameter when plotting in order to plot information on past bars. The repainting types we will be discussing here are the first two types, as the third one is intentional—sometimes even intentionally misleading when unscrupulous script writers want their strategy to look better than it is.
Read more about repainting on the PineCoders TradingView profile.

Other Resources

If you are interested automating your TradingView strategies, give TradersPost a try and Register your free account today! If you have any questions, join our Community or email us at [email protected].
Last modified 30d ago