Archive for July, 2009

Variable Lookback Pivot: Using AggregationPeriod To Reference Past Bars in Think or Swim

July 31, 2009

Reader filip asked about using the Aggregation Period constants to grab past bar values. He was specifically interested in making a rolling pivot using the last 4 hours high, low and close. There’s a few building blocks we can use to set this up: declare fullrange, some recursive price series, and some freaky use of “if/then/else” blocks and double.nan constants. What a pain! But here we go:

The obvious way to look back for a high is to use the “highest(data,length)” function. The problem (there’s always a problem) is that the “length” argument has to be a constant. That’s no good for us since we would have to calculate “length” to know how far back to look, and the function would throw an error.

Instead, we start by multiplying the number of milliseconds in our aggregation period of choice by the number of units we want. For the 4 hour example:

3,600,000 milliseconds per hour x 4 hours = 14,400,000 milliseconds lookback

Then, we divide by the number of milliseconds per bar in our chart. We find this with the GetAggregationPeriod() function. For a 1 min chart:

14,400,000 milliseconds / 60,000 milliseconds per bar = 240 bars

With that lookback period we set a new series for highs and lows that is double.nan for all bars before the latest bar – 240, and set a value for all bars after. We make it recursive so that it will keep the highest high and lowest low in the lookback period. We also set the close of the most recent bar as our pivot-closing value, and then we calculate pivots from these levels. With “declare fullrange;” the chart is constantly updated on each new tick, leaving us a continually evolving look a the pivot levels.

Here’s what the 1 min chart looked like this evening with a 4 hour lookback:


Now, here’s the thing: I would normally write the tutorial for those who want to put in their own elbow grease and then put my ready-to-go indicator out as a “donor only” offering. However, I have neglected the blog lately with work pressures and now the vacation I am on, so I am putting this out free to all. Know that I love you. However, feel free to donate anyway. 🙂

The file is called “VariableLookbackPivotSTUDY.ts” and can be found on my Google site under “Released Thinkscript Studies“. Read the script for more details about what it does. I commented it a lot. Leave a comment if you don’t follow any of it and I’ll try to explain.

S&P E-mini Futures (ES) Trades: 7-28-09

July 28, 2009

I took two trades today off the open in ES. Here’s the chart:


I used a 133 tick chart of ES. You can see my Shaded Opening Range Indicator on there (set from 0930 to 0931), as well as my Volatility-Based Trailing Stop. I’ve also got two exponential moving averages: one is a 5 period and the other a 20 period. But instead of using the close in the EMA calculations, I’m using the pivot point of each bar, or (H + L + C) / 3. I got the idea from John Person’s book, Candlestick and Pivot Point Trading Triggers + CD-ROM: Setups for Stock, Forex, and Futures Markets. Finally, you see a study I called PACD (Pivot-based Average Convergence Divergence). Nothing too exciting–just the MACD using hlc3 to compute the EMA’s instead of close, but I changed the inputs to match the EMA’s (5, 20, and an arbitrarily chosen 5 signal line). This is showing if the 5 EMA is above the 20 EMA or vice versa, and could be used in place of the EMA’s eventually. None of this is set in stone, and it is a bit discretionary, but it’s working for me.

The first trade was a short on a pullback inside the 1min OR. The Volatility Stop was pointing short, and the NYSE A-D ratio and Tick were down and going lower. The market was fading the A-D line, but I expected the futures to fall, and 969 was the overnight low, 2 points below my target. Soon after entry, everything switched directions, but I still waited to see if it would go my way. I was within one tick of being stopped out for a bit, and then got a push back lower. I stayed with it, but in hindsight, the Volatility Stop switched directions and the 5/20 hlc3 EMA’s had crossed over (or equivalently, the PACD went above zero). I should have bailed out there for a smaller 1 or 2 tick loss, but I stayed with the original stop and it was hit, -5 ticks. Then I went long on a bounce off the OR high, stop under the OR low. This time the internals were with me, and the PACD and Volatility Stop were as well. I set a profit target for 8 ticks, and was filled. If I had margin for multiple contracts, I could have traded two, sold one at this first target and let the other go with a trailing stop, and I could have had a point or two more, but I’m happy to stop with a net positive day at +3 ticks or 0.75 points. For my personality and where I’m at as a trader, I’d rather be net positive by a fraction every day than be up big some days and down big some others. That will change down the road as I pursue net profit over time as the primary goal, but consistency and confidence is what I want to gain right now, not bags of money. Consistency first, moneybags afterwards.

My strategy lately has been to play for smaller profits, and keep wider stop losses–the exact opposite of the 1:5 Risk:Reward home run trades I have historically tried. I never would hit my targets, and I often got stopped out. So I’m bringing the two closer together, and it’s working. I intend on watching the tape and getting out in a controlled manner (like I should have on the first trade) if things aren’t shaping up as planned. The stop loss is just in case of disaster. I also don’t get greedy and take the money when it’s there at a closer target, which is different than in my past. So far so good!

Range Bar Approximation Indicator for Think or Swim

July 22, 2009

A donor asked if I had some code for Range bars for Think Desktop. Range bars are bars that complete after a certain range has been traveled, and then a new bar starts at the next tick. Unfortunately, ToS doesn’t let us create custom bar types. But that doesn’t stop us from trying to hack together a workaround!

The “solution” I came up with is not a true range bar according to the traditional definition. The simple indicator I made starts with the first bar open. The indicator then checks for if we have moved X points from the open in either the high or low direction. Once we have moved the required amount one way or the other, we plot a new open level. I also keep track of the value of the open of the prior “Range bar”, and if the new “Range bar” open is a downtick, we get a red cloud. Opposite for upticks. This indicator is designed to use the fastest data available, which for now is a 133 tick chart. Here’s what the indicator looked like on ES today with a +/- 1 point range setting:


On my first examination, it seems more useful as a trend direction indicator than a true proxy for range bars. You could sell a pullback into a red zone, or buy a pullback into a green zone, for example. But it’s the best I can do with what we have available in thinkscript right now (as well as with my time schedule X_X).

This one is available to blog donors on my Google site (look for “” in the “Donors Only” folder). For the do-it-yourself types, leave a comment if you have questions and want to build your own, or have any other thoughts.

A Monumental Day: The Eagle and the ES

July 20, 2009


Today was the 40th anniversary of the Apollo 11 landing. What an incredible feat of engineering, daring and national will! Lots of great coverage out there on the web if you are interested. The Eagle has landed! Here’s Neil at Tranquility Base from a camera on the top of the LM:


It was also a monumental day because I took my first trade since January. I played the open on the ES futures. I was watching a 133 tick chart on Think or Swim. My plan was to play a quick 2 point move off the earliest opening range balance, whether long or short. I also wanted to follow the direction of the internals. I wanted to be exposed to the market for as short a time as possible, so I would either have immediate follow-through (within a minute or two) or I would get out.

Here’s what my chart looked like when I made my move:


I saw that we were pausing near the upper end of the initial range (grey lines) and that there was about 2 points to run from there to the 944.75 pre-market highs. I was also watching these charts of the $TICK and the $ADVN-$DECN for confirmation:


You can see that as of 8:32 CT (9:32 ET) we were in the green on both internals and rising. I got long at 942.50, target 944.50, initial stop 941, just under R1 (red line w/dots). Here’s how the trade played out:


Just a quick hesitation and then off we went. As we thrashed near 942.50 for the first bar or three, I was sorely tempted to just bail out to lock in a tick of profit for my first trade back in the markets, for psychological reasons. I sat on my hands and made myself just watch–stick with the plan; I got my immediate follow through, and I was in the green. When we hit 943.75, I thought we would make it to my target, and as I went to move my stop up to breakeven, I was filled for +2 points. Mission accomplished in around two minutes.

I’m a better trader than I give myself credit for, and my years of watching the markets are starting to give me some “tape sense”. While I’m happy for a win (of course), I’m happier that I made myself follow the plan and stick with it, and that I got my feet back in the water at all. I’ve been avoiding it for far too long.

So break out a beverage today in celebration of Neil Armstrong, Buzz Aldrin, Mike Collins, and me. 🙂

Sponsored Indicator: Cumulative Moving Average for Think or Swim

July 17, 2009

An anonymous donor requested a cumulative moving average indicator for Think or Swim that could start at a given date (on a daily chart) or start at a chosen time on a specific day for intraday charts. This donor suggested that I share it with everyone else, so here it is!

Basically, the code line:

CMA = totalSum(price)/barNumber()

can be used to get a quick cumulative average of all values on a chart. Kind of like a VWAP, but it’s not weighted by volume at all–each bar’s price value is weighted equally. But what if you want to start at some place inside the chart? You can use “if” statements to assign a new variable that contains “double.nan” if a bar is before your start date/time, and contains price values after the start conditions are met. Then you “totalsum” that variable. You also have to keep track of the bar number when you start recording data, since the first bar isn’t bar#1 anymore.

The indicator I wrote is smart enough to ignore the start time automatically if you are using a daily chart. Here’s a CMA on a daily chart of ES starting on the low day on Mar 6, 2009:


Here’s what it looks like if you start on July 14 2009 at 1030 ET on a 15 min chart:


I also figured out a way to automatically reset the indicator each day for true intraday traders. If you enable this option, it ignores the start date and only cares about the start time (1030 ET in this example):


This indicator is provided free to all through the generosity of a donor (look for “Cumulative_Mov_AvgSTUDY.ts” over on my Google site). Thank you to all of my donors and customers–you are helping me slowly build to a point where I can develop code full time. For now I’m still very much buried under my day job and a few custom software projects, hence the dearth of updates here. Thanks for reading!