Posts Tagged ‘swing_low’

Swing High / Swing Low Indicator for Think or Swim

April 29, 2009

Update 2-18-10: v3–Added optional lines for plotting after the swing points:

As part of some work I have been doing, I needed a way to identify swing highs and swing lows. What is a swing high / low? It depends on your definition, but basically a swing high is the highest high looking a few bars back and a few bars forward. A swing low is the lowest low looking a few back and a few forward. The more bars you include in the series, the more significant the swing, but the confirmation comes further from the actual swing point. Say you wanted to define a swing high as this:

A bar high that is higher than the highs of the two bars just before it AND higher than the highs of the two bars just after it.

The thinkscript code might look like this:

Def swinghigh = if high > high[1] and high > high[2] and high > high[-1] and high > high[-2] then 1 else 0;

The code for a swing low is similar. This method is a pain because it requires you to hard code the swing period. If you want to change to a different number of bars, you have to modify the source code. Note that the confirmation of a swing point does not come until 2 bars after the swing high in this case. If you wanted to extend the swing check to 3 bars before and after, you would add the checks for a high > high[3] and high > high [-3]. The resulting swing will be more significant, but the signal comes 3 bars after the fact.

To plot the swing high you could do something like this:

Plot sh = if swinghigh then high else double.nan;

This would paint a dot on all the swing highs, and nothing everywhere else. The code for swing lows is similar. So now you know how to create your own swing high / swing low indicator. Free education; you provide the sweat equity. If you have any questions on making your own, leave a comment and I’ll be glad to help out!

I spent a couple of hours last night figuring out how to make the indicator generalized, meaning that you can input any number of bars to define your swing period, and the code finds the highs and lows for you without modding the indicator source code. This makes it easier to change on the fly for backtesting or responding to market volatility. Here’s what it looks like:


My pain + my brain = your gain! Become a donor and you can download my complete generalized swing point script file, ready to be imported into Think Desktop. This is part of my new “Teach for free, Work for fee” philosophy. If you donate, be sure to email me and let me know what you donated for so I can send it to you! All my files (many for free and some for donors only, including the Swing Points) are at my Google site: