How to write your own Trading Indicators with PineScript in TradingView?
Engineer and a man who loves Code - Life - Trade. #reactjs #react-native #vuejs #nodejs #PHP #AWS
TradingView?
TradingView is a platform where people discover investment ideas and do discussions, sharings, and learn from numerous participants in the market through the platform.
We can get enough data and browser-based charts without any installations or complex setups. Data provided is powerful enough for advanced chartists let alone for beginners.
PineScript?
Pine script, a useful programming language created by TradingView itself. It was designed to be lightweight and convenient for objectives like calculating data, plotting lines, backtesting trading strategies, and creating custom indicators.
It’s pretty sure that we would have the same content as the fresh start, like below:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © username//@version=4 study("My Script") plot(close) Lets’s give our custom script a title: //@version=4 study(title="My Indicators", shorttitle="My Indicators", overlay=true)
Daily prices data made easy:
"We are referring to the daily chart as our reference."
Open price
// Daily Open Prices tdo = security(syminfo.tickerid, 'D', open) // today ydo = security(syminfo.tickerid, 'D', open[1]) // yesterday pdo = security(syminfo.tickerid, 'D', open[2]) // 2 days before odo = security(syminfo.tickerid, 'D', open[3]) // 3 days before
Close prices
// Daily Close Prices tdc = security(syminfo.tickerid, 'D', close) ydc = security(syminfo.tickerid, 'D', close[1]) pdc = security(syminfo.tickerid, 'D', close[2]) odc = security(syminfo.tickerid, 'D', close[3])
Highs & Lows
// Daily High Prices tdh = security(syminfo.tickerid, 'D', high) ydh = security(syminfo.tickerid, 'D', high[1]) pdh = security(syminfo.tickerid, 'D', high[2]) odh = security(syminfo.tickerid, 'D', high[3]) // Daily Low Prices tdl = security(syminfo.tickerid, 'D', low) ydl = security(syminfo.tickerid, 'D', low[1]) pdl = security(syminfo.tickerid, 'D', low[2]) odl = security(syminfo.tickerid, 'D', low[3])
Volumes
// Daily Volumes tdv = security(syminfo.tickerid, 'D', volume) ydv = security(syminfo.tickerid, 'D', volume[1]) pdv = security(syminfo.tickerid, 'D', volume[2]) odv = security(syminfo.tickerid, 'D', volume[3])
Questions?
"What about prices / volumes for 4, 5, 6 … days before today?"
"What do we do with the above information?"
Chill. We will be going through this page by page.
As mentioned, Pinescript is designed to be lightweight and convenient for calculations and plottings.
// Daily Open Prices tdo = security(syminfo.tickerid, 'D', open) // today ydo = security(syminfo.tickerid, 'D', open[1]) // yesterday
While plotting the chart, data is loaded based on daily sequence. As time goes, the above calculations remained applicable from the specific day standpoint.
Writing functions in Pinescript
To return true / false value
TodayHasLargerVolumeComparedYesterday()=> tdv > ydv? true:false
It’s self explanatory where the function will return true if today's volume is higher than yesterday's volume and, vice versa.
Bearish Engulfing
A green candle followed by a red one. And the red candle body had fully covered the green, sometimes extensively.
PineScript function
BearishEngulfingCandle()=> (ydc > ydo and tdo > tdc)? (tdo > ydc and tdc < ydo):false
Bullish Engulfing
A red candle followed by a green one. And the green candle body had fully covered the green, sometimes extensively.
PineScript function
BullishEngulfingCandle()=> (ydo > ydc and tdc > tdo)? (tdc > ydo and tdo < ydc):false We can still assign the functions to variables TurningBearish=BearishEngulfingCandle() TurningBullish=BullishEngulfingCandle()
Then we use the built-in plotchar feature in PineScript
plotchar(TurningBearish, color=color.red, size=size.small)
And the result as below:
Have preferred Icon? No worry. Let’s do this and observe the result!
plotchar(TurningBearish, char='
And the final script is as short as:
//@version=4 study(title="My Indicators", shorttitle="My Indicators", overlay=true)// Daily Price tdo = security(syminfo.tickerid, 'D', open) //today ydo = security(syminfo.tickerid, 'D', open[1]) //yesterday tdc = security(syminfo.tickerid, 'D', close) //today ydc = security(syminfo.tickerid, 'D', close[1]) //yesterdayBearishEngulfingCandle()=> (ydc > ydo and tdo > tdc)? (tdo > ydc and tdc < ydo):falseBullishEngulfingCandle()=> (ydo > ydc and tdc > tdo)? (tdc > ydo and tdo < ydc):falseTurningBearish=BearishEngulfingCandle() TurningBullish=BullishEngulfingCandle()plotchar(TurningBearish, char='
Conclusion:
We have gone through the quick introduction to TradingView and PineScript from the very first section of the article and also run a quick redirection to focus on the chart section in the platform.
Secondly, there are easily understood examples of getting prices and volumes data as well as writing simple functions in PineScript.
Thirdly, we have run through the concept explanations for BullishEngulfing and BearishEngulfing patterns of the candlesticks and finally derive the pattern from some simple snippets.
Finally, we have also gone through the built-in “plotchar” function of Tradingview and successfully replaced the default icons with our preferred ones.
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2021 Sean Pang