Pinescript to ThinkScript Functions (ThinkScript Code)

bizzet

New member
Hello, I just joined this forum, however, I have been browsing for quite awhile and appreciate everyone who has contributed. I have learned lots from everyone. This forum has helped me gain access to many great indicators and has sparked an interest of coding in ThinkOrSwim's ThinkScript.

I was recently trying to convert TradingView code (Pinescript) to ThinkScript, and I ran into a few hiccups.

However, this thread actually gave me a solution.

Basically, using script{} allows you to create functions in Thinkscript. These functions can have parameters, defined as the first inputs of the script. Such as

Code:
script [FUNCTION NAME] {
    input data = 0;
    input data2 = 0;
}

I'm not sure if there is a limit to how many parameters you can have, but 3 works and that's all I needed.
I leave the inputs blank since they will be overwritten by the parameters when you call (use) the function.

The functions also can have outputs, defined as the last plot.
I personally use a variable to calculate the output then use the plot to output the variable
Such as

Code:
script  [FUNCTION NAME] {
    input data = 0;
    input data2 = 0;
    def ret_val = [INSERT CALCULATION HERE] [EXAMPLE: data1 + data2]
    plot return = ret_val;
}

These functions allow you to create custom functions that you can use throughout your code.
You can add parameters to the function() by entering them within the parathesis.
Such as

Code:
yourfunctionname(parameter1, parameter2);
calculateSMA(close, length);

This allows you to output the plot defined in your script{}
This is similar to how Pinescript uses their functions such as nz(), ema(), etc.
Here is a complete example

Code:
#// Define the custom script
script addNumbers {
    input data = 0;
    input data2 = 0;
    def ret_val = data1 + data2;
    plot return = ret_val;
}

#// Use the defined custom script
def addNum = addNumbers(10, 100)
plot x = addNum;


This custom example function will add the numbers 10 and 100 then return the value to the addNum variable.
Finally, it will plot the sum of the two numbers on the plot.


Why this is important is because it actually allows me to use some Pinescript code without having to convert each indicator manually.
This has saved me so much time! Enjoy!

Code:
#//
#//    PineToThink
#//        ~  Functions (scripts) to help use Pinescript within ThinkOrSwim. This allows to use Pinescript code without actually
#//        ~  having convert all the code. Some code will still need to be converted manually. However, this speeds up the conversion
#//        ~  process greatly.
#//
#/////////////////////////////////////////////////////////////////////////////////////////////////////////

#////////////////////////////////////////////////////////////////////////////////////////////////////////
#// Pinescript to Thinkscript Helpers

script iff {
        input data = 0;
        input data2 = 0;
        input data3 = 0;
        def ret_val = if data then data2 else data3;
        plot return = ret_val;
    }

script nz {
    input data = 0;
    input data2 = 0;
    def ret_val = if IsNaN(data) then 0 else data2;
    plot nz = ret_val;
}

script barssince {
    input data = close;
    input bars_total = 0;
    def ret_val = data from bars_total bars ago;
    plot return = ret_val;
}

script valuewhen {
    input data = 0;
    input data2 = 0;
    input data3 = 0;
    def ret_val = GetValue(data, data2, data3);
    plot return = ret_val;
}


script crossover {
    input data = 0;
    input data2 = 0;
    def ret_val = data crosses above data2;
    plot return = ret_val;
}

script ema {
    input data = 0;
    input data2 = 0;
    def ret_val = ExpAverage(data, data2);
    plot return = ret_val;
}

def na = Double.NaN;

There is still room to add more scripts but this is what I needed for my code.
Hopefully this will give you a good starting point to code your own Pinescript functions into ThinkScript.
And hopefully something like this isn't already posted. (Though I haven't found anything similar except for the post I linked earlier)

Happy Coding!
 
Last edited:

bizzet

New member
@bizzet : Thanks for sharing this one. Learnt few details on Python scripts as well. Awesome.
It's not Python. It's ThinkScript. That was just the only code format that was changing the colors for me.
I'll change it just to avoid the confusion.

Although, it is annoying that nobody has developed a color format for the CODE tag on this forum when so much code is shared.

Here's a starting point for anyone ready to tackle it
https://xenforo.com/community/resources/additional-language-for-code-bbcode.5985/
 
Last edited:

chIza=iChis1A?H

Member
VIP
Thanks Bizzet for an update, surely will look into this. Always like to learn some new scripts.

Hello @bizzet : Are you part of private community of ThinkorSwim users ?? if so, how is your experience for few script ??
 

BenTen

Administrative
Staff member
Staff
VIP
@bizzet

Although, it is annoying that nobody has developed a color format for the CODE tag on this forum when so much code is shared.

It was already there since day 1.

qt6PAdg.png
 

BenTen

Administrative
Staff member
Staff
VIP
@bizzet There isn't one, but you can use the Ruby option. That's what some of our members choose for their thinkscript snippet.
 

bizzet

New member
@bizzet There isn't one, but you can use the Ruby option. That's what some of our members choose for their thinkscript snippet.
Yeah, that's why I said someone should work on developing it. Maybe I will if I find some free time.

Other programming languages can be used, as I was doing. But as you can see in this thread, some people think the code is whatever language the code is labeled as.

I was using Python to color the code, but a member thought this code was Python instead of Thinkscript so I don't think it's a very good solution.
Even copying the section of Xenforo code for the Ruby CODE BBCode and relabeling it as ThinkScript could work.
@BenTen
 

Similar threads

Top