Coding Indicator in Tradingview from InspirationSo today is rest and research Sunday and I wanted to share with folks how I go about creating new strategies and indicators. Hopefully this can teach traders to fish rather than giving them just the fish (of a buy/sell signal!)
I was inspired by watching a Youtube podcast Desiretotrade by Etienne Crane where I saw on his chart arrows indicating where price closed outside of a Bollinger Band followed by price closing back inside. This made me want to test such a price action phenomenon myself and to do that I needed to code it up.
I'm really a hacker when it comes to programming and Pine Script but I began coding from ZERO knowledge and my tricks, which I share, should hopefully help new Tradingview/Pine Script coders. GOOGLE IS YOUR FRIEND!!! It has all the answers if you just type the correct search :)
Unfortunately, I did not realize that Tradingview cuts off videos at 20 minutes. Oops! I got the meat of the indicator and process of using it all recorded and got cut off as I was cleaning up the code to make it more user friendly. I'll know to keep track of time for the next video!
Please let me know if this was helpful to traders... I get motivated by positive vibes to do more teaching!
Pinescript
Tips on securing your own Pine Script code and sharing10 Pine Commandments
Rule number uno: never share something you don't intend on open-sourcing. It will be archived forever.
2. If you believe ideas are intellectual property and you have a 1 in a million unique idea, tell them that you would like that idea to not be shared.
3. If someone randomly sends you scripts without you prompting, don't send them some of your (valuable) ones because you feel as if there has been rapport or trust built.
4. Never get high on your own supply
The rest of these biggie lyrics don't make any sense for this. Anyways:
When you turn "sharing on" for your chart and add a script from console, anyone can copy the script and see the source code. This is expected behavior but should be known for those who don't like sharing.
When you publish an idea and add a script from console, the same thing happens as above. If you've done this on accident, you have 15 minutes to delete it.
Tip: never work on a private project on a chart that has sharing on. Label chart names with 'Sharing' to help identify them.
If you want to allow trial use, create an invite-only indicator. Do not use closed-source indicators and assume updating them will stop trial usage.
Don't use pastebin lol.
You're not that original, and neither am I. We all use the same sites, read the same articles; mind meld is bound to happen. I used to publish scripts pre-emptively because of this.
When you share your indicator, it can be reverse engineered if it is simple enough/uses well known functions.
Here's a funny example that is slightly related:
Person is accused of stealing script, the accuser is accused of copy/pasting the Pine Legends Chris Moody/LazyBear:
www.trading view.com/script/4IfUS1Jd-SNG/
Accuser actually reverse engineered this script as far as I can tell:
www.trading view.com/script/pvGZhYxm-Canadian-Trader-Dream-Strategy/
New Pine Updates - Line.get_price(), label styles and much moreHello,
This video idea covers some of the big updates added to Pine and TradingView over the last several days. Most importantly, I show you how to use the following new features:
- line.get_price() - a function used to get the price of a line object on the chart at a specific bar value
- label styles - how to use the new styles added into Pine for your labels
I also discuss some other new features such as:
- label tooltips
- the addition of Candlestick patterns as built-in indicators on the platform
- syminfo.basecurrency
- timezone parameter when getting a bar's time
Thank you!
Backtest doesn't show any tradesI have tried fixing my code to backtest my strategy and when I try to backtest it, it tells me that my code couldn't complete any trades. Please help I am in dire need.
Here is the code:
//@version=4
strategy("backtest #001", overlay=true, initial_capital=100000, currency='USD')
//backtest
fromYear = year > 2010
toYear = year < 2020
//inputs
mystoch = stoch(close, 14, 3, 3) >= 85
myema = ema(close, 200) >= ema(close, 200)
mybbw = bbw(close, 20, 2) >= 0.03
//closing inputs
mystochclose= stoch(close, 14, 3, 3) <= 40
myemaclose = ema(close, 200) <= ema(close, 197)
mybbwclose = bbw(close, 20, 2) <= 0.03
//problems
strategy.entry('buy', true, 1, when = (mystoch and myema and mybbw)? 1 : 0)
strategy.close("buy", when = (mystochclose and myemaclose and mybbwclose)? 1 : 0) , qty_percent = 50
Giancapino 5 viewsMy 5 layouts by the most used&updated:
1) www.tradingview.com Gold e-o-d CME OI vs week CFTC OI +Preliminary
2) www.tradingview.com 15' DXY correlation + session bars
3) www.tradingview.com other studies
4) www.tradingview.com COT graphs
5) www.tradingview.com Compare contracts yoy
most of them use scripts I learned to use and can be copied
>this page is not published on tv but can be viewed by anyone sharing its link:
UPDATE:
I decided to make this page public
Anyone can copy the entire layout included the scripts' source to use&modify for himself.
If anyone is interested in howto or share contaact me here or by chat tv/wazap/teleg
A More Efficient Calculation Of The Relative Strength IndexIntroduction
I explained in my post on indicators settings how computation time might affect your strategy. Therefore efficiency is an important factor that must be taken into account when making an indicator. If i'am known for something on tradingview its certainly not from making huge codes nor fancy plots, but for my short and efficient estimations/indicators, the first estimation i made being the least squares moving average using the rolling z-score method, then came the rolling correlation, and finally the recursive bands that allow to make extremely efficient and smooth extremities.
Today i propose a more efficient version of the relative strength index, one of the most popular technical indicators of all times. This post will also briefly introduce the concept of system linearity/additive superposition.
Breaking Down The Relative Strength Index
The relative strength index (rsi) is a technical indicator that is classified as a momentum oscillator. This technical indicator allow the user to know when an asset is overvalued and thus interesting to sell, or under evaluated, thus interesting to buy. However its many properties, such as constant scale (0,100) and stationarity allowed him to find multiple uses.
The calculation of the relative strength index take into account the direction of the price changes, its pretty straightforward. First we calculate the average price absolute changes based on their sign :
UP = close - previous close if close > previous close else 0
DN = previous close - close if close < previous close else 0
Then the relative strength factor is calculated :
RS = RMA(UP,P)/RMA(DN,P)
Where RMA is the Wilder moving average of period P . The relative strength index is then calculated as follows :
RSI = 100 - 100/(1 + RS)
As a reminder, the Wilder moving average is an exponential filter with the form :
y(t) = a*x+(1-a)*y(t-1) where a = 1/length . The smoothing constant being equal to 1/length allow to get a smoother result than the exponential moving average who use a smoothing constant of 2/(length+1).
Simple Efficient Changes
As we can see the calculation is not that complicated, the use of an exponential filter make the indicator extremely efficient. However there is room for improvement. First we can skip the if else or any conditional operator by using the max function.
change = close - previous close
UP = max(change,0)
DN = max(change*-1,0)
This is easy to understand, when the closing price is greater than the previous one the change will be positive, therefore the maximum value between the change and 0 will be the change since change > 0 , values of change lower than 0 mean that the closing price is lower than the previous one, in this case max(change,0) = 0 .
For Dn we do the same except that we reverse the sign of the change, this allow us to get a positive results when the closing price is lower than the previous one, then we reuse the trick with max , and we therefore get the positive price change during a downward price change.
Then come the calculation of the relative strength index : 100 - 100/(1 + RS) . We can simplify it easily, first lets forget about the scale of (0,100) and focus on a scale of (0,1), a simple scaling solution is done using : a/(a+b) , where (a,b) > 0 , we then are sure to get a value in a scale of (0,1), because a+b >= a . We have two elements, UP and DN , we only need to apply the Wilder Moving Average, and we get :
RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
In order to scale it in a range of (0,100) we can simply use :
100*RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
= 100*RMA(max(change,0),P)/(RMA(max(change,0),P) + RMA(max(change*-1,0),P))
And "voila"
Superposition Principle and Linear Filters
A function is said to be linear if : f(a + b) = f(a) + f(b) . If you have studied signal processing a little bit, you must have encountered the term "Linear Filter", its just the same, simply put, a filter is said to be linear if :
filter(a+b) = filter(a) + filter(b)
Simple right ? Lot of filters are linear, the simple moving average is, the wma, lsma, ema...etc. One of most famous non linear filters is the median filter, therefore :
median(a+b) ≠ median(a) + median(b)
When a filter is linear we say that he satisfies the superposition principle. So how can this help us ? Well lets see back our formula :
100*RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
We use the wilder moving average 3 times, however we can take advantage of the superposition principle by using instead :
100*RMA(UP,P)/RMA(UP + DN,P)
Reducing the number of filters used is always great, even if they recursion.
Final Touch
Thanks to the superposition principle we where able to have RMA(UP + DN,P) , which allow us to only average UP and DN togethers instead of separately, however we can see something odd. We have UP + DN , but UP and DN are only the positive changes of their associated price sign, therefore :
UP + DN = abs(change)
Lets use pinescript, we end up with the following estimation :
a = change(close)
rsi = 100*rma(max(a,0),length)/rma(abs(a),length)
compared to a basic calculation :
up = max(x - x , 0)
dn = max(x - x, 0)
rs = rma(up, length) / rma(dn, length)
rsi = 100 - 100 / (1 + rs)
Here is the difference between our estimate and the rsi function of both length = 14 :
with an average value of 0.000000..., those are certainly due to rounding errors.
In a chart we can see that the difference is not significant.
In our orange our estimate, in blue the classic rsi of both length = 14.
Conclusion
In this post i proposed a simplification of the relative strength index indicator calculation. I introduced the concept of linearity, this is a must have when we have to think efficiently. I also highlighted simple tricks using the max function and some basic calculus related to rescaling. I had a lot of fun while simplifying the relative strength index, its an indicator everyone use. I hope this post was enjoyable to read, with the hope that it was useful to you. If this calculation was already proposed please pm me the reference.
You can check my last paper about this calculation if you want more details, the link to my papers is in the signature. Thanks for reading !
Making A Good Indicator DescriptionIntroduction
When posting an indicator a good concept/code isn't the only thing required in order to have a quality indicator, its description is also extremely important. Each person has its own style,
mine is pretty academic, but as long as your description respect certain criterions you are free to do as you wish.
In this post i want to help you make the best of your published indicator by introducing basic concepts in post writing.
Basic Description
An indicator description should at least explain what kind of information your indicator is giving as well as how to interpret/use it. Lets take the Supertrend indicator as example, we'll be making a brief description of it.
The Supertrend indicator is a technical indicator created by the french trader Olivier Seban that aim to detect the current market trend direction. The indicator detect an up-trend when below the market price and a down-trend when above.
This indicator posses two setting, length and mult, higher values of length/mult allow the indicator to detect trends of longer period. This indicator can also be used as trailing stop loss, it can also provide a support level when below the price or a resistance level when above.
Lets breakdown this simple description.
-"The Supertrend indicator is a technical indicator created by the french trader Olivier Seban"
If the indicator is not yours, always credit/mention the original author, this is no option.
-"that aim to detect the current market trend direction"
Aim of the indicator.
-"The indicator detect an up-trending market when below the market price and a down-trend when above."
How to interpret the indicator.
-"This indicator posses two setting, length and mult, higher values of length/mult allow the indicator to detect trends of longer period."
Always try to introduce the indicator settings and explain how they affect the indicator output.
-"This indicator can also be used as trailing stop loss, it can also provide a support level when below the price or a resistance level when above."
Alternative uses of the indicator, try to provide a graphic showing the indicator acting as mentioned, i our case we should show a graphic showing the Supertrend acting as a support/resistance.
Once you write up your indicator description read it back several times (something i wish i could do more often) and always ask yourself if your indicator description fully describe the aim of the indicator. If you like writing you can of course add more elements, although try not to be redundant, this is really important, skip things such as :
"Yesterday i was dinning with a colleague and he asked me how to get zero-lag filters, since then i tried to..."
This is tiring for the reader and so try not including it.
More Complete Description
A more complete description introduce additional elements regarding your indicator such as your motivation behind its publishing, its calculation...etc.
Such description of the Supertrend indicator could look like this :
Detecting the current market price trend has always been a major challenge in technical analysis, systems relying on the cross between a noisy series and a certain level/indicator can produce whipsaws trades. In order to provide a solution to this problem more advanced indicators have been proposed, one of them being the Supertrend indicator developed by the french trader Olivier Seban.
This indicator aim to detect the current market trend direction, the indicator detect an up-trending market when the price is superior to the Supertrend, and a down trending market when the price is inferior to the Supertrend.
The indicator is an iterative calculation, the core element of its calculation involve creating two bands, denoted as upper/lower, calculated as follows :
upper = hl2 + atr(length)*mult
lower = hl2 - atr(length)*mult
where atr is the average true range of period length and hl2 is the median price defined as : (high + low)/2.
Higher values of length/mult allow the indicator to be less sensitive to shorter price variations, this allow to detect trends of longer period.
Apart from the basic indicator usage described above, the Supertrend indicator can also act as a :
- trailing stop loss, in this case if the closing price cross the value of the indicator, the trade will be closed.
- support/resistance level, in this case an upward movements can be expected after the low price cross the Supertrend, and a downward movement when the high price cross the Supertrend indicator.
In conclusion, the Supertrend indicator is an elegant solution when it comes to detecting the current market price trend, its ability to avoid whipsaws trades makes him a good ally for decision making.
Including Graphics
Graphics of your indicators can add more clarity to your description since visual examples are often easier to understand, the goal here is to see how your indicator interact with the market price. Try including readable graphics, this can mean charts without other indicators (except if your indicator can work in conjunction with other indicators).
However keep your main chart (your current chart layout when publishing the indicator) free of any other indicators/elements, it should only contain the price and your indicator, its the first image people we'll see and if it include other indicators then people will have an hard time telling which one is the one you are publishing.
It can be interesting to show your indicators using different settings rather than the one put by default. You can make use of figures such as arrows or crosses in order to highlight the signals made by your indicator.
Additional Advices
Use a bold text when declaring the title of a section, use italic with text that represent code, formulas, variables names.
You can first write your indicator description on a text editor.
If your code include another work from tradingview don't forget to mention it in the description, for example :
this indicator make use of the "name of the indicator" made by "name of the author" that you can find here "tradingview link of the indicator".
In this case make sure you have the consent of the author of the indicator you are using. This is no option, else you can have your indicator taken down.
Don't include bitcoin wallets/donation links in your indicator description, those go on your signature and nowhere else.
Knowing when not to make a description is important, and by that i mean knowing when not to publish, make sure to read the house rules before you are publishing an indicator.
If you are still struggling then you can try to read other indicators description on the net, if you want a deadly serious description then you can learn from the structure of research papers. For example if your indicator is similar to a moving average try reading descriptions of moving average indicators and inspire yourself from them.
Conclusion
Making a good indicator description is essential, the benefits of doing it are enormous, don't forget that scripts take space in servers. If you are publishing a script and that you know that your description is to short and doesn't explain the aim of your indicator then you are just adding more work for the moderators, its not nice.
Also great descriptions allow you to gain trust and credibility in the community, and more importantly it shows that you care about your indicator, i might be harsh but people that does not care about their indicators does not deserve to publish them (however its not case with you nice reader, right ?).
I hope i can help users with this post, its an important subject, if you have doubts about your indicator description try seeking for help in the pinescript chat, you can even contact me, i will be happy to help.
Thanks for reading, and if you plan to use those advices then thanks for caring, love u :kokoro: