Thinkscript - An Introductory Guide - AlgoTrading101 Blog (2024)

What is Thinkscript?

Thinkscript is a programming language for the Thinkorswim trading platform that allows its users to backtest strategies and build tools such as watchlists, indicators, and more.

Thinkorswim is the trading platform for the broker TD Ameritrade.

Link: https://tlc.thinkorswim.com/center/reference/thinkScript

Can I trade automatically using Thinkscript?

As of 7th March 2022, no.

How to get started with Thinkscript?

To get started with Thinkscript, you will first need to download the app. In order to do this, go over to the following link and select your OS. Then click “install thinkorswim” and follow the installation instructions.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (1)

While you wait for the program to download and install, go over to the following link and in the upper-right corner click the green “Open New Account” sign.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (2)

After that, be sure to go over the sign-up steps in order to properly create an account. When done, open your thinkorswim app and log in with your account credentials. Then you will need to accept the ToS.

Note: when logging in you can choose if you want to use Live Trading or Paper Money.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (3)

Thinkscript main layout

On the main toolbar that is situated on top of your interface, you can find the main windows. Each of the windows has its own (sub)toolbars. Let’s go over what each of the main windows represents:

  • Monitor – tracks your trading activity and includes trading data like orders, balances, trading account status, statements, and more.
  • Trade – contains interfaces that categorize the tradable assets, i.e. Forex, Futures, Pairs, and more.
  • Analyze – has a plethora of analysis models that you can run in real, “what if” and backtesting scenarios.
  • Scan – this serves as a filter with which you can hone in on your assets of interest.
  • MarketWatch – provides you with useful market data
  • Charts – provides a graphical interface for real-time data
  • Tools – has several custom thinkorswim features.

To start scripting go to the Charts window, select a symbol from the symbol table, click “Studies” in the upper right corner, select “Edit Studies” and then click “Create”.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (4)

If this looks overwhelming to you, you are not the only one and by the end of this article, you will understand the basic components and available features of this program.

How to use Thinkscript fundamentals?

Thinkscript fundamentals can be seen as your basic building blocks. For example, in the opened script screen you can change the green “close” into an “open” to see the change in your graph.

Have in mind that you need to press “OK” in the down right corner of your script interface to create the script. Then click apply and you will see a blue line that appeared on your chart.

To go back into the editing mode, find your study in the left “Studies” panel. If you didn’t name it, the name should be “NewStudy0”. To open the script right-click on its name and press “Edit”

How to use Thinkscript Declarations?

Thinkscript declarations need to be initialized with the declare command. For example, let us use the upper declaration to create a price oscillator for our closing BTC values.

We will to this by having the upper declaration place a weighted moving average plot on our main BTC chart.

declare upper;input price = close;input length = 9;plot AvgWtd = wma(price, length);
Thinkscript - An Introductory Guide - AlgoTrading101 Blog (5)

P.S. if you click apply in the scripting mode you can see a glimpse of your main chart but to see it in its full glory you’ll need to close the scripting interface.

How to use Thinkscript functions?

In order to use Thinkscript functions you will need to navigate to their respective drop menu that is found on the right side of your scripting station. When there you can easily explore the available functions.

For an example, let us create an exponential moving average on our BTC data:

input additionalBars = 0;plot ExpAvg = EMA2(close, additionalBars, 0.2);

I will also add an implied volatility graph that will appear in the lower section on the main Graph window:

declare lower;plot ImpliedVolatility = imp_volatility();
Thinkscript - An Introductory Guide - AlgoTrading101 Blog (6)

How to use Thinkscript Constants?

To use Thinkscript constants all you need to do is to define them prior to their use in your scripts. To define a constant you can use the def command that is followed up by your custom constant name.

For our BTC chart, we will add a constant that will aggregate the candles by month and print out the closing prices for monthly periods.

def agg = AggregationPeriod.MONTH; plot data = close(period = agg);
Thinkscript - An Introductory Guide - AlgoTrading101 Blog (7)

How to set Alerts with Thinkscript?

To set an alert with Thinkscript you will need to use the Alert() command inside of which you specify the condition, text, alert type, and sound. There are different alert types that you can set and they are the following:

  • Alert.BAR – triggers only once per bar
  • Alert.ONCE – triggers only once after adding the study
  • Alert.TICK – triggers on each alert tick

The sounds that are available for you alerts are chimes, bells, dings, rings, or soundless.

Now, let’s create an alert that will notify us when the asset reaches the closing price of $300. We’ll want the alert to display a message, alert us on each tick and do a ding sound.

Alert(open == 300, "Open price reached $300!", alert.TICK, Sound.Ding);

How to use Thinkscript patterns?

In order to use Thinkscript patterns, you will need to access your Charts window and in the upper right corner, you will find a “Patterns” button. Be sure to click it and when there you can press “Select patterns”.

I’ll select a Classic Flag pattern that is a formation that appears as a small channel after a steep trend. It will indicate an opposite direction of the said trend.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (8)

How to add simulated trades in Thinkscript?

To add a simulated trade in Thinkscript, you will go to the Analyze window and then select the “Add Simulated Trades” button. Then select the asset you would want to trade. I’ll go for the Google stock.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (9)

To simulate a trade just click on the Bid to sell, or to the Ask to buy, and underneath your orders will appear where you will be able to set up the number of shares.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (10)

To step up the game, you can click on a specific time period and change the calls by adding different strike values, custom quotes, instrument details, and much more.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (11)

How to place orders with Thinkscript?

To place an order with Thinkscript, all you need to do is to go over to the Trade window where you can select your asset and then buy or sell it by clicking the Bid and/or Ask buttons.

As you have clearly noticed, the interface looks the same as in our previous simulated trading demonstration. This was done by the developers to not induce confusion in the users.

To place an order via code you will need to use the following function:

AddOrder(type, condition, price, tradeSize, tickColor, arrowColor, name); 

How to create your own strategy with Thinkscript?

Now that we have grasped the basics behind Thinkscript, we are ready to create a simple trading strategy. For this, we will use the Dropbox asset. We will want to buy when the asset crosses above the 20 SMA and vice versa.

Each time the asset crosses the 20 SMA we will be notified by an alert. The buy orders will be displayed in green while the sell order will be displayed in red. We will only want to buy 20 shares of the stock upon each cross.

input price = close;input length = 20;def AVG = Average(price, length);Alert(AVG < price, "The closing price is bellow the 20 SMA: "+price, alert.TICK, Sound.Ding);Alert(AVG > price, "The closing price is above the 20 SMA: "+price, alert.TICK, Sound.Ding);AddOrder(OrderType.BUY_AUTO, price crosses above avg, open[-1], 20, Color.GREEN, Color.GREEN, “Buy”);AddOrder(OrderType.SELL_AUTO, price crosses below avg, open[-1], 20, Color.RED, Color.RED, “Sell”);

Now that the strategy is done, we can apply it and check for its performance by right-clicking the signal and selecting “Show report”.

Thinkscript - An Introductory Guide - AlgoTrading101 Blog (2024)
Top Articles
NBA 2K24 Best Power Forward (PF) Builds For Current And Next-Gen
Best Power Forward Builds in NBA 2K24 (Season 5)
Pixel Speedrun Unblocked 76
Palm Coast Permits Online
It may surround a charged particle Crossword Clue
Soap2Day Autoplay
Hawkeye 2021 123Movies
Where's The Nearest Wendy's
Planets Visible Tonight Virginia
Our Facility
Winterset Rants And Raves
Houses and Apartments For Rent in Maastricht
Theresa Alone Gofundme
Walgreens San Pedro And Hildebrand
The best TV and film to watch this week - A Very Royal Scandal to Tulsa King
Craigslist Portland Oregon Motorcycles
91 East Freeway Accident Today 2022
*Price Lowered! This weekend ONLY* 2006 VTX1300R, windshield & hard bags, low mi - motorcycles/scooters - by owner -...
eHerkenning (eID) | KPN Zakelijk
Craigslist Pearl Ms
Craigs List Tallahassee
Dark Entreaty Ffxiv
Jordan Poyer Wiki
Sandals Travel Agent Login
Parkeren Emmen | Reserveren vanaf €9,25 per dag | Q-Park
Kirk Franklin Mother Debra Jones Age
Craigslist Fort Smith Ar Personals
Restored Republic
Log in or sign up to view
Lininii
Star News Mugshots
Does Circle K Sell Elf Bars
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Acuity Eye Group - La Quinta Photos
Craigslist Dallastx
Golden Tickets
Adecco Check Stubs
What Are Digital Kitchens & How Can They Work for Foodservice
Crystal Mcbooty
3496 W Little League Dr San Bernardino Ca 92407
Has any non-Muslim here who read the Quran and unironically ENJOYED it?
Craigslist Tulsa Ok Farm And Garden
Puretalkusa.com/Amac
Isabella Duan Ahn Stanford
Craigslist Central Il
Guided Practice Activities 5B-1 Answers
✨ Flysheet for Alpha Wall Tent, Guy Ropes, D-Ring, Metal Runner & Stakes Included for Hunting, Family Camping & Outdoor Activities (12'x14', PE) — 🛍️ The Retail Market
Mynord
Zipformsonline Plus Login
Samsung 9C8
Mcoc Black Panther
Food and Water Safety During Power Outages and Floods
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 5728

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.