What's New

TOS Strategies: Targets and Stops

Truth be told, I have a love-hate relationship with TOS strategies. If you’ve ever tried to code a strategy exit with either a price target or a stop loss you have undoubtedly felt this frustration. The core of the problem was that exit strategies did not know the price at which the long or short entry was executed. To work around this you had to code the entire entry into the exit and even then that did not necessarily work properly.

History aside, I was working to create stop type exit strategies for one of our members, when I discovered the following (recent?) function addition to thinkScript: entryPrice(). Like mana from heaven, the TOS programmers heard our prayers. As you might have already guessed, this is the solution to the aforementioned problem. With relative ease you can create long and short exits with price targets and stop-losses. I’ve slammed a few together here to illustrate the point. Below is an image of today’s /ES session with a custom strategy of mine and the example stop losses set to target = 3.0 and stop-loss = 1.5 (Strategy Report Here).

Strategy Stops

# TS_Stop_LX
# http://www.thinkscripter.com
# Last update 02 Sep 2010

declare LONG_EXIT;

input stop = 2.5;
input target = 3.0;

def entry = entryPrice();
def exit = if (high >= (entry + target) or low <= (entry - stop)) then 1 else 0;
def exitPrice = if (high >= (entry + target)) then (entry + target) else (entry - stop);

addOrder(exit, exitPrice);
setColor(color.white);
# TS_Stop_SX
# http://www.thinkscripter.com
# Last update 02 Sep 2010

declare SHORT_EXIT;

input stop = 2.5;
input target = 3.0;

def entry = entryPrice();
def exit = if (low <= (entry - target) or high >= (entry + stop)) then 1 else 0;
def exitPrice = if (low <= (entry - target)) then (entry - target) else (entry + stop);

addOrder(exit, exitPrice);
setColor(color.white);
Posted in Tutorial | 1 Comment

Fold – Basic Syntax

It seems as though there is a fair bit of confusion regarding the new thinkScript looping capability implemented with the fold function. At first glance, the syntax is a bit unusual but dig a little deeper and there is a classic for loop hiding there – albeit with some constraints. Here’s the syntax taken directly from thinkorswim’s own Resource Center:

def <result> = fold <index> = <start> to <end> with <variable> [ = <init> ] do <expression>;

So, first and foremost, the fold function is used to define the value for a named variable. Hence, the end result of any fold iteration is saved into an immutable variable once the loop is complete. That variable can be either a non-self-referencing variable using the def statement or can refer to prior values of itself when used with the rec statement. The key point here is that you cannot operate on other variables or do anything within the loop. Your loop’s sole function will be to return a value into a variable. This alone heavily constrains what you can do with fold. In the sample code above result will contain the final computed value.

Let’s dive a little deeper into the beast, shall we. Every loop has an index over which it iterates. If you ever programmed in BASIC, you probably coded something like the following

for i = 1 to 100
print i
next i

which result in the numbers 1 to 100 printing to the screen for your immense enjoyment. OK, so I’m dating myself here, but yes I began coding BASIC on my Apple II in the early 80′s and wrote such to impress my less computer savvy classmates. Back to the task at hand. The i variable in this syntax is the index over which this loop iterates or counts. There will be 100 iterations of the loop until the program completes. The functionality in the fold function is identical with the index variable being iterated from the value of start to end in the above sample code.

To recap, we have a variable result to store the final value of the calculation, and we have a loop which will iterate its index variable from start to end which is mathematically start-end times.

Now on to the calculation itself. Internal within the structure of the fold loop is another variable incidentally called variable within the sample code above. This is were the interim value of the loop calculation is held while the loop is iterating. At the conclusion of the loop, the final value of variable is then transferred to result. Beginning to get the idea? The value of variable may be initialized to something using the = init syntax. If you omit this optional code, the value of variable will simply start at 0.

The final piece of the puzzle is the calculation code itself. This is the do expression portion of the code. Within the expression you can perform any one-line thinkScript calculation using the index variable or other variables/numeric values from elsewhere in your thinkScript code. That is, the code you create here should have the syntax of whatever lies on the right side of an equals sign in the classic thinkScript variable definition. Each time through the loop, the expression will be evaluated and the result will be placed into variable awaiting the next iteration. So, if my expression were variable*index then you would be successively multiplying the value of variable by index each iteration through the loop. This might look like this in a BASIC pseudocode example:

for index = 1 to 10
variable = variable*index
next index

Well, that’s all for today. Next time we’ll go through some examples to bring the picture into complete focus. Happy thinkScripting and best of luck in your trading. -Eric

Posted in Tutorial | Leave a comment

ThinkScripter 3.0

Some of you may have noticed that I’ve been conspicuously absent both in my posts here and in the forum of late. Well….I’ve been locked in the ThinkScripter laboratory for nearly eight weeks now working on the facelift for the blog and I’m happy to take the wraps off the new design today. This marks the third major site overhaul since I began in January of 2009.

The first iteration was a boilerplate WordPress.com blog with little or no customization. This is a great way to get started publishing content on the internet. Being the tinkerer that I am, I quickly began running into the limitations of the basic free blog. I moved over to a paid server and cobbled together a few custom theme parts to introduce version two. It served me steadfastly until my desire for just that much more control finally got me rolling a few months ago.

Many of the changes are under the hood, so to speak, and rather than add patches to the previous site architecture I just started with a clean slate and built my own theme. I’ve added a dynamic Indicator page, thinkScript syntax highlighting, and skinned the Forum properly just to name a few of the enhancements. In the process I became much more intimate with web architecture, php, css, wordpress, etc and finally feel like the master of my own domain – no pun intended.

As with all major updates I expect to encounter some bugs along the way and would appreciate any tippers if something is acting wonky.

Lastly, it has been a distinct pleasure and honor to bring this site to you and the thinkScript community at large. Thank you all for your kind remarks, encouragement, and donations along the way. Now, time for a cold one!

Your host,
Eric

Posted in Uncategorized | 6 Comments

Trend

Here’s another indicator by very popular request. It wasn’t until TOS included the new fold function that I was able to properly implement it in thinkScript.

This is a modified Heikin-Ashi trend indicator made popular by John Carter and widely available across the internet for other platforms. First note that these are not Heikin-Ashi candlesticks themselves which are a built in option in TOS. Instead, this is the Heikin-Ashi coloring applied to regular candlesticks with a slight modification. The modification involves rejecting a change in the Heikin-Ashi trend when the bar in question is an inside bar; inside in the Heikin-Ashi sense. The user selects the period to look back for the inside bar comparison. Pictured below is the Trend indicator with blue as an uptrend and red as a downtrend. You have the option to display the rejected inside bars in a highlighted color as shown in the second image. The colors are fully user selectable using global defaults.

Trend

Trend with Inside Bars Highlighted

Posted in Indicator | Tagged , , | 14 Comments

Renko Chart

Using the framework of the previous Three Line Break study, I was able to craft this Renko Chart. At first glance it doesn’t resemble a Renko Chart but upon careful inspection you will see the same information albeit in a different manner. The colored blocks are analogous to those of the Renko chart. Trend change is indicated when the next block begins to form in the alternate color. The user can select either a variable ATR block size or a fixed price size. The paintbars can be colored to match the Renko blocks as shown. The Renko Chart will be added to the Pro Subscriber bundle this weekend.

Renko Chart

Posted in Indicator | Tagged , , , | 4 Comments