Archive for February, 2009

Thinkscript Tip: Dealing With Added Bars On Charts

February 27, 2009

My Formatted Pivot Points Thinkscript was designed to dynamically hide faraway pivot levels based on where the price is currently. If you are down under S1, you don’t want to be looking at R3 all day long. The only problem was that whenever the current bar close would tick above or below the local pivot level, the respective hidden lines would appear and disappear on my chart, making the screen flash like something from YTMND. Easy fix, make the hiding depend on “close[1]” instead of “close”! Not so fast…

If you add extra bars to the right hand side of the chart (which I like to do), then “close[1]” IS NOT the prior bar! Neither is “close” the current bar. “Close” actually refers to the furthest right “bar” on the chart. If you added 5 bars, then “close” is the bar 5 periods into the future. Since this value doesn’t exsist yet, it is assigned the value N/A, and the value returned by calls to the “close” function are the last good value for close, which is the current bar closing price. Most of the time, this is fine since all you care about is the current bar anyway. But if you add bars, and then want to reference bars in the recent past, you won’t get the values you think you will. If I added 5 bars to the right, to get the bar one previous to the current one, I’d have to reference “close[6]”. But you can get around this by using this code:

#
# Define lastclose = last completed bar close:
#
rec lastclose=if isnan(close[-1]) then lastclose[1] else close;

Then you use “lastclose” in your code instead of “close”, and you will get the right values no matter how many extra bars you have on the right. I updated the Formatted Pivot Points code with this change, so no more seizures. You’re welcome.

Some Updates

February 23, 2009

I made some updates to VWAPALOOZA and to the Volatility-Based Trailing Stop. Check them out if you’re interested.

VWAPALOOZA

February 19, 2009

UPDATE 23 Feb 2009: Fixed an error in the formatting. Added “Volume Increment”. Changed Incremental and Rolling VWAP’s to calculate for all days on chart, not just current day.

I asked for some indicator requests on Twitter. The most common response was for VWAP, or Volume Weighted Average Price. Think or Swim has a built-in VWAP indicator, but it is a black box. I like my boxes to have source code. Honestly, I’m not quite sure what the built-in “VWAP” indicator is even plotting. In my testing I couldn’t figure it out. It seems to do different things when plotted on daily vs. intraday charts. There is another built-in indicator called “CumulativeVWAP” that is more useful that I’ll discuss below.

There are many ways to define a VWAP. The “standard” way is to multiply each trade by the corresponding trade volume, sum them all, then divide by the total volume traded, like so (from the wikipedia article):

VWAP Formula

This is typically done over a 1 day timeframe.

To remain true to this definition, we would have to calculate VWAP from the tape itself, using time and sales data. Think or Swim does not (yet) support this. The smallest duration we have to work with is the 133 tick chart. So if you define Pj = (bar high + bar low)/2 and use the formula above for a given day, you should get a plot of the VWAP as it was calculated during the day at each corresponding time. As the day starts out, VWAP responds quickly to price changes, but by the end of the day the volume of each bar becomes small compared to total volume. It’s basically an oscillation that gets more damped as the day goes on (barring extreme changes in price or volume). The final value of VWAP on this tick chart at the close should be near to the actual value as calculated from the individual trades. When I plot this VWAP value (in yellow) along with the value of the built-in “CumulativeVWAP” indicator (in black) with timeframe set to “DAY” (hiding the “upper band” and “lower band”), the two curves lie on top of each other:

vwapvscumvwap

Looks good. However, when you move to a larger timeframe, say 30 minute bars, then my Thinkscript (now gray line) loses resolution compared to the “CumulativeVWAP” indicator (now white dots). The “CumulativeVWAP” still shows the same values as in the fine scale 133 tick chart:

vwapvscumvwap_30mins

This leads me to believe that the “CumulativeVWAP” indicator is calculated from time and sales data server-side at ToS. So if you just want a standard VWAP indicator, use “CumulativeVWAP” instead of calculating it yourself.

Now, I’ve seen others (including Richard at Move the Markets) use a type of rolling VWAP. To me, the idea would be that intraday traders near the end of the day can be far removed from the action at the open. They probably have already closed positions from the early morning and are likely to be trading from a different perspective. Calculating a rolling VWAP would let you find a more recent volume-weighted consensus of value. So I wrote a simple “Rolling VWAP” indicator that looks back N number of bars. In the limit as N goes to the total number of bars so far in a day, my “Rolling VWAP” value approaches the “CumulativeVWAP” value (remembering that my calculation loses resolution as your chart timeframe gets larger).

BUT THAT’S NOT ALL!! Order now and you’ll receive an “Incremental VWAP” calculation at no extra charge! The “Incremental VWAP” calculates VWAP over a given period (say N bars). It then waits for N more bars to go by, and then calculates a new VWAP for that most recent N bar period. Here’s all three of my VWAPs– Full Day (cyan dash), Rolling (solid yellow) and Incremental (purple dash)–along with the built-in “CumulativeVWAP” (solid gray) on a 1 min chart of ES from today:

vwap_pro

UPDATE 23 Feb 2009: I also added the option to specify the number of shares as an increment for VWAP rather than just number of bars.

There are many different ways to use VWAP, and hopefully one of these will help you in your trading. The “Incremental VWAP” contains code that uses a counter variable that goes from 1 to N and then resets to 1, so look at the source if that interests you.

Here is the code: (more…)

Think Desktop Trouble

February 18, 2009

I’ve been having a lot of trouble with errors in Think Desktop lately, having to do with XML and UTF encoding and Java and such. So I haven’t been able to work with code much lately. Sorry for the dearth of stuff. I will be working on some VWAP, MACD Histogram Divergence and tutorials on using trading strategies on the charts soon, if I can get Think Desktop to cooperate.

Another Great Thinkscript Resource

February 14, 2009

I just found another blog that is dedicated to Thinkscripts. Thinkscripter publishes custom Thinkscript indicators as well. His code looks good, and I’m already learning a thing or two from his scripts. Make sure to check it out!