♥ 0 |
Hi, I like to reassign a variable value in order to convert following pine script to TOS script. I looks like TOS requires if/else condition to reassign the value. However, I don’t know how to do it in this case. The code: a = 0.0 b = 0.0 a := max(close, nz(a[1])) – nz(a[1] – b[1])/length Here, nz(x) function replaces x by 0 if x is nan. I appreciate your help Thanks.
Marked as spam
|
Private answer
This is a great question and will really help a lot of our viewers who are trying to translate indicators between Thinkorswim and TradingView. I have accumulated a great deal of experience translating code between these two platforms fr client. And this question you posted is a very common element that shows up in the custom projects I complete for paying clients. So I took a lot more time with this solution that I usually permit. Because I felt this would be very helpful and I wanted to make sure I covered it in sufficient detail. First up, it is most polite when posting example scripts in a forum, to provide a fully functioning section of code. Meaning that everyone viewing the question should be able to simply copy/past your example code into their code editor and have something that immediately plots something on the chart. Here is your example code, which has been polished up to work as a fully functional chart study on the TradingView platform. I absolutely abhor the use of variable names that
So your example has been further update to make the code easier to read the maintain. PineScript Example:
That is the code in PineScript, which anyone can simply copy paste as a new indicator on TradingView. Now let's get into the solutions. Yes, solutions, as in plural. We have at least three methods which immediately come to mind. As listed below, the first is to use the CompoundValue() function. The second is to use the IsNaN() function. The third is to build a custom script named "nz" which can be used as a drop in replacement, saving a lot of time during the translation process. CompoundValue() and IsNaN() Examples:To save time and space, I have combined the techniquest using these two options within the same chart study script. The code for variableA uses the CompoundValue() function and the code for variableB uses the IsNaN() function.
For reference, the CompoundValue() function is described here: https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue And the IsNaN() function is described here: https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN script {} Example:
The script structure is described here: https://toslc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script The screenshot below shows the various methods applied to charts. Marked as spam
|
Please log in to post questions.