header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

How to write simple indicators using TradingView

Read this article in 12 Minutes
0 basis, complete simple indicator writing in 5 minutes.

In the context of global investment frenzy, everyone is looking for better and more convenient investment tools.

 

TradingView, an investor community and charting website known to almost all professional investors. The site, which was founded in 2011, has reached 1 million monthly active users five years after its establishment.

 

TradingView has a large amount of data, including global securities markets, commodities, foreign exchange, etc. At the same time, its products keep up with market trends. Just when U.S. retail investors responded to Wall Street Bets' call to buy silver, TradingView urgently launched the historical price trends of silver and other commodities for more than a century.

 

Of course, what is most familiar to users in the cryptocurrency industry is the small components embedded in Tradingview in major trading platforms. Users can directly view the market through the widget.

 

Tradingview is not limited to viewing the market, it launched a self-designed scripting language "Pine Script" earlier, And allows users to create custom indicators and run on the Tradingview server.

 

In this article, Rhythm BlockBeats will teach you to start from 0 and write some simple indicators in 5 minutes.

 

Coinbase Premium Indicator

 

Previously, Rhythm BlockBeats once published Bitcoin fell, and whether the downtime Coinbase is manipulating Bitcoin currency market? " article detailed that when the price of Bitcoin fell in recent times, the price of Bitcoin on the Coinbase platform was lower than that of other platforms. And used TradingView to make a Coinbase premium chart for display, but this function requires the purchase of a TradingView member, and it is not intuitive enough.

 

The code released by Weibo user "Hedge Machine" can visually display the Coinbase premium situation, and Rhythm BlockBeats will use this as an example , 5 minutes to understand and learn to write simple code.

 

The source of the code and pictures is Weibo user "Hedge Machine"

 

Code:

 

study("CB-BN basis", overlay = false)< /p>

 

//Uses candle closes for difference so best accuracy on lower timeframs

premium = security("COINBASE:BTCUSD", timeframe.period, close) - security("BINANCE:BTCUSDT", timeframe.period, close)

 

< p line="ckoQ">premiumPercent = (premium/close)*100

 

plot(premiumPercent, color = iff( premium >= 0, color.blue, color.red))

plot(0, color = color.black)

  ;

How to write indicators

 

The main interface of TradingView can be divided into three areas, the uppermost market display area, the middle indicator area and the writing code area (this area will not be displayed if not written).

 

TradingView interface

 

The first line, the first line Marking "//" on the second line indicates a comment, which is convenient for users to understand, and the computer will not compile it.

 

Fifth line:study("Coinbase -Binance", overlay = false)

 

Coinbase-Binance is represented here as the name of the indicator in the TradingView interface. overlay is to confirm where the indicator needs to be added, false means to add to the indicator area, and true means to add to the market display area.

 

Eighth line:premium = security ("COINBASE:BTCUSD", timeframe.period, close) - security("BINANCE:BTCUSDT", timeframe.period, close)

 

security("COINBASE:BTCUSD", timeframe.period, close) means to select the BTC/USD trading pair on the Coinbase trading platform, and use the closing price as the data source.

 

If the opening price of BTC/USD on Bitstamp is selected as the data source, then it can be written as security("BITSTAMP:BTCUSD ", timeframe.period, open)

 

open: opening Price

high: the highest price

low: lowest price

close: closing price

 

The tenth line:premiumPercent = (premium/close)*100

 

This code is displayed as the value after the premium rate is expanded by one hundred times, The latter close represents the closing price of tokens in the current market display area.

 

Of course, if this is changed to premiumPercent = (premium/security("COINBASE:BTCUSD", timeframe.period, close)) would be more user-friendly. After the change, regardless of whether the user switches the market to any investment product, it will be expressed as the premium rate between the Coinbase Bitcoin price and the Binance Bitcoin price, that is, "(Coinabse Bitcoin price-Binance Bitcoin price)/Coinbase Bitcoin price" .

 

The eleventh line:plot( premiumPercent, color = iff(premium >= 0, color.blue, color.red))

 

The premium is greater than When or is equal to 0, it is displayed in blue, otherwise it is displayed in red. The color can be changed at will. For example, if you want to display green, you can change "color.red" to "color.green".

 

The twelfth line:plot( 0, color = color.black)

 

0 axis is displayed in black.

 

Compile delivery contract futures premium rate indicator

 

After understanding the writing logic of TradingView indicators, you can write some relatively simple indicators, such as "delivery contract premium rate".

 

Take the calculation of the premium rate of the Binance June delivery contract as an example, the code is:

 

study("Binance premium rate in June", overlay = false)

 

< p line="zeUZ">premium = security("BINANCE:BTCM2021", timeframe.period, close) - security("BINANCE:BTCUSDT", timeframe.period, close)

 

premiumPercent = (premium/security("BINANCE:BTCUSDT", timeframe.period, close))*100

 

plot(premiumPercent, color = iff(premium >= 0, color.blue, color.red))

 < /p>

 

Through the indicators, we can know that, Currently, the Binance June delivery contract has a premium rate of 6.59% relative to the spot price.

 

In addition to these simple indicators, Tradingview also supports more complex script operations, please refer to Pine Script v4 User Manual.



Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit