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.
Last updated
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.
Last updated
The TradersPost webhook system lets you easily integrate alerts from platforms like TradingView with TradersPost. Webhooks can receive JSON like the following.
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.
Using Pine Script strategies as opposed to indicators or studies is the best way to build automated trading strategies that work well with systems like TradersPost.
The Pine Script code of a strategy will have a line at the top of the code which calls the function strategy()
If you build your Pine Script as a strategy, you only need one alert per ticker, and you can use the following JSON for the alert.
TradingView will replace the {{...}}
values dynamically with a real value when the alert gets sent to TradersPost.
{{ticker}} - the ticker the alert is setup on
{{strategy.order.action}} - buy or sell
{{strategy.market_position}} - long, short or flat. This value tells TradersPost what the state of the position should be after executing the
{{strategy.order.contracts}} - the quantity of the order executed
{{close}} - the current price at the time the alert was triggered
You can alternatively use a Pine Script indicator instead of a strategy to send signals to TradersPost.
The Pine Script of an indicator will have a line at the top of the code which calls the function indicator()
With an indicator, you have to setup separate buy and sell alerts per ticker. Your code should have multiple calls to the alertcondition()
function.
Then you can copy and paste the following JSON snippets in to the alert message in TradingView.
You can also use the sentiment field to exit a bullish position without entering a bearish position on the other side. When you send sentiment=flat, it will always exit the full quantity of the open position.
You can also use the sentiment field to exit a bearish position without entering a bullish position on the other side.
When you send sentiment=flat, it will always exit the full quantity of the open position.
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.
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.
To make it easier to write strategies with the alert messages automatically constructed in valid JSON, we have written the WebhookMessage Library that allows you to pass along your parameters and it will build the JSON for you.
You can add the library to your favorites and then import the library into your Pine Script strategies and indicators.
There are several different ways that you can build strategies in TradingView from studies that are purely visual indicators to strategies that can be backtested.
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.
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()
.
Then when you are setting up the alert for your strategy you can put the following code in the Alert Message
text area in TradingView.
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
It is important when building your Pine Script strategy to understand the differences between building a strategy for a backtest and building a strategy for live automated execution. It is possible to build a strategy that works well in a backtest, but cannot be automated. Here is an example.
If you program your strategy to swap sides from long to short or short to long, you need to program the swap from one side to the other in one order instead of two.
Here is an example where it is done in two orders. When you do this, it will execute fine in the backtest, but this results in 2 alerts being sent to TradersPost at the exact same time and it is not possible to know which one to execute first.
Here is how you can do the same thing in one order instead of two:
This will swap from one side to the other in one order and will send one signal to TradersPost with all of the quantity required to both close the current open position and swap to the other side in one order.
For this type of setup to work when you are sending a quantity in your signal and using the TradingView strategy JSON, make sure you check "Use signal quantity" and "Subtract exit quantity from signal quantity" in your TradersPost strategy subscription settings. If you are long 5 shares of SPY and you want to swap to the short side with 5 shares of SPY then TradingView will send a quantity of 10 in that scenario and TradersPost can subtract the quantity of the open position from the signal quantity so you get 5 to exit the long position and 5 to enter short on the other side.
It is common for users to want to program in logic for a specific window of time that their strategy trades during. It is important that this logic exists in your strategy so that you are backtesting your strategy with those time based rules and your forward testing execution matches the same rules from your backtest.
Here is an example Pine Script strategy that demonstrates how you can implement something like this.
Now you have some new settings you can use to control when your strategy is allowed to trade and when it should exit.
You can find a more complex example in the 8-55 EMA Crossover NQ Futures Strategy in our GitHub repository.
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.
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.
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.
Open the Alerts pane in TradingView
Click Create Alert at the top right of the Alerts pane
Select Trend Following MOMO from the Condition dropdown
Select TradersPost Buy Alert or TradersPost Sell Alert under the Condition dropdown.
Click Once Per Bar Close next to Options
Paste your TradersPost webhook URL in the Webhook URL field.
Give your alert a name like HD MOMO 1D Buy
Here is what your alert should look like.
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.
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.
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 support@traderspost.io.
TradingView alert webhooks can be delayed by anywhere from 2-3 seconds to upwards of 60 or more. TradingView considers a 25 to 45 second delay to be normal.