PineScript to Thinkscript reassgning variable values nz()


Category:
0
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
b := min(close, nz(b[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
Posted by (Questions: 1, Answers: 1)
Asked on August 4, 2024 1:04 pm
34 views
1
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

  1. Are single characters
  2. Do not fully described their purpose or function

So your example has been further update to make the code easier to read the maintain.

PineScript Example:

//@version=5
indicator("Q&A Forum", overlay = false)
length = input.int(14, "Length", minval = 1)
variableA = 0.0
variableB = 0.0
variableA := math.max(close, nz(variableA[1])) - nz(variableA[1] - variableB[1]) / length
variableB := math.min(close, nz(variableB[1])) + nz(variableA[1] - variableB[1]) / length
plot(variableA, "Var A", color = color.aqua)
plot(variableB, "Var B", color = color.red)

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.

declare lower;
input length = 14;
rec variableA;
rec variableB;
variableA = Max(close, CompoundValue(1, variableA[1], 0)) - (CompoundValue(1, variableA[1], 0) - variableB[1]) / length;
variableB = Min(close, if !IsNaN(variableB[1]) then variableB[1] else 0) + ((if !IsNaN(variableA[1]) then variableA else 0) - variableB[1]) / length;
plot testOne = variableA;
plot testTwo = variableB;

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:

declare lower;
script nz {
input data = 0;
plot nonZero = if IsNaN(data) then 0 else data;
}
input length = 14;
rec variableA;
rec variableB;
variableA = Max(close, nz(variableA[1])) - nz(variableA[1] - variableB[1]) / length;
variableB = Min(close, nz(variableB[1])) + nz(variableA[1] - variableB[1]) / length;
plot testOne = variableA;
plot testTwo = variableB;

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.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4108)
Answered on August 4, 2024 1:30 pm
0
Thank you very much Pete.
( at August 5, 2024 4:22 pm)