Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Plotting Swing Highs and Lows with Dots Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
Putt4Dough
Posted : Monday, July 27, 2015 8:16:31 PM

Registered User
Joined: 7/30/2007
Posts: 1,072

Hi Bruce,

I'm guessing you've already done this as it's a pretty common technique. I'm basically looking to plot dots at swing highs and lows similar to this ...

I was reading Trading Systems and Methods by Perry Kaufman on a rainy day, and pulled some stuff from it to clarify the process. I'm including the description in English, the TradeStation EasyLanguage code, and the Excel formulas ... you probably won't need all three but I figured you might prefer one over the others. 

Here it is in English ...

Constructing a Classic Swing Chart

A classic technique for finding the swing highs and lows uses the following steps. This can be applied to any data frequency, daily, weekly, hourly, or any intraday period; therefore, references to days in the following example could be replaced by weeks or 15-minute bars. 
 
1. Begin by plotting the high to low of the first day in the first column of the chart. At this point, we do not know the direction of this swing bar or the trend of prices.
 
2. Add the second day of data.
   a. If there is a higher high but not a lower low, 
       then continue the first swing higher and ignore the low of the day. 
       We can now call this an upswing.
   b. If there is a lower low but not a higher high, 
       then continue the first swing lower and ignore the high of the day. 
       This is now a downswing.
   c. If there is both a new high and a new low (an outside day), 
       then continue the current high direction and ignore the low. 
      (You may choose the low instead, but this choice must be consistently applied.)
   d. If the high of the second day is less than the previous high of the column and the low is greater than the previous low (an inside day), 
       then ignore that day and go to the next day.
 
3. Once a direction has been established, add the next day’s data.
   a. If the current swing is up, then
       i. First look at the new daily high. 
          If the high is higher than the high in the current column, 
          extend that swing to the new high, ignoring the low. 
          Go to the next day.
       ii. If the new high is lower than the high of the current column then test for a swing reversal. 
           If the current swing high minus the low of the new day is greater than the swing filter value, 
           then move one column to the right and plot the range of the new day high to low. 
           The new swing is down.
   b. If the current swing is down, then
       i. Look first at the new daily low. 
          If the low is lower than the low in the current column, 
          extend that swing down to the new low and ignore the high. 
          Go to the next day.
      ii. If the new low is higher than the low of the current column, 
          then test for a swing reversal. 
          If the high of the new day minus the low of the current column is greater than the swing filter               value, 
          then move one column to the right and plot the range of the new day from high to low. 
          The swing is now up.
 
4. A new price that does not make a new high or low and does not exceed the swing reversal threshold is ignored.
 
This particular method is called a 2-day or 2-period technique because it requires only two periods to identify a swing change. The sensitivity of the swing chart can be reduced by requiring that a swing change only occur after three or more days where prices reverse their direction.
 
Based on William F. Eng, “A Mechanical Trading System,” Technical Analysis of Stocks & Commodities (July 1986). Further information can be found in Eng, The Technical Analysis of Stocks, Options & Futures (Chicago: Probus, 1988).
 
Source: Trading Systems and Methods (5th ed) - Perry Kaufman
 
Here's the TradeStation EasyLanguage code. I don't use TradeStation myself but the code is pretty easy to read ...
 
[LegacyColorValue = true]; 
 
{ TSM Swing:  Plots swing highs and lows
  Copyright 1994-1999, P.J. Kaufman.  All rights reserved. 
 
Plot setup: 
1. Place on same graph window as price
2. Scale same as price
3. In style, set both values to "point" with weight 2
 
Inputs for TSM SWING:
type = 0, normal price
          = 1, 3-month rate
          = 2, bond price at par
swing = price swing in whole % (e.g., 2% = 2.0)  }
 
input: type(0), swing(2.5);
 
vars:  pcswing(0), last(0), curhigh(0), curlow(0), swhigh(0), swlow(0) , highbar(0),
lowbar(0), chighbar(0), clowbar(0), lowp(0), highp(0), xclose(0), xhigh(0),
xlow(0), divide(4), factor(1), par(800);
 
pcswing = swing / 100.;
 
if type = 0 then begin
     xhigh = high;
     xlow = low;
     xclose = close;
     end
else if type = 1 then begin
     xhigh = 100 - low;
     xlow = 100 - high;
     xclose = 100 - close;
     end
else if type = 2 then begin
     xhigh = par / low;
     xlow = par / high;
     xclose = par / close;
end;
 
{ INITIALIZE MOST RECENT HIGH AND LOW }
  if currentbar = 1 then begin
      curhigh = xclose; { current high }
      curlow  = xclose; { current low }
      end;
                
{ SEARCH FOR A NEW HIGH -- favor reversals }
   if last<>1 then begin
   { REVERSE FROM HIGH IF MINIMUM % SWING }
      if xlow < curhigh - curhigh*pcswing then begin
           last = 1; { last high fixed }
           swhigh   = curhigh; { new verified high }
           highbar   = chighbar;
           curlow = xlow; { initialize new lows }
           lowp = xlow; { swing low for plot }
           clowbar = currentbar;
           if type = 0 then
                plot1[currentbar - highbar](high[currentbar - highbar]*1.01,"swinghigh")
           else
                plot3[currentbar - highbar](low[currentbar - highbar]*.99,"swinglow");
           end
      else begin
           if xhigh > curhigh then begin
              curhigh = xhigh;          { new current high }
              chighbar = currentbar;
              end;
      end;
   end;
 
{ SEARCH FOR A NEW LOW - favor reversal }
   if last <> -1 then begin
{ REVERSAL FROM LOW IF MINIMUM % SWING }
      if xhigh > curlow + curlow*pcswing then begin
           last = -1;
           swlow   = curlow;
           lowbar   = clowbar;
           curhigh = xhigh;          { initialize current high }
           highp = xhigh;               { swing high for plot }
           chighbar = currentbar;
           if type = 0 then 
                plot2[currentbar - lowbar](low[currentbar - lowbar]*.99,"swinglow")
           else
                plot4[currentbar - lowbar](high[currentbar - lowbar]*1.01,"swinghigh");
           end
     else begin
          if xlow < curlow then begin
               curlow = xlow;
               clowbar = currentbar;
               end;
          end;
    end;

And here's how to implement finding swing highs and lows using Excel ...

Set up your columns with the following data:

A  Date
B  High price
C  Low price
D  Closing price
 
Put the minimum swing percentage value in cell C3 (for example, 5% =.05)
 
Put the following formulas in the corresponding cells. Row 6 initializes the process.
 
E6 =B6 (the current swing high)
F6 =C6 (the current swing low)
G6 =IF(D6>(E6+F6),-1,1)    (where -1 is a downswing and +1 is an upswing)
 
Row 7 is the beginning of the repeated process. Row 7 can be copied down.
 
E7 =IF($G6=-1#AND#b7>E6,B7,IF($G6=1,B7,E6))    (the new current low)
F7 =IF($G6=1#AND#C7<F6,C7,IF($G6=-1,C7,F6))    (the new current high)
G7 =IF($G6=-1,IF(C7<E7 - $C$3*D7,1,$G6),IF(B7>F7+$C$3*D7,-1,$G6))    (test high or low)
H7 =IF($G7=1#AND#G6=-1,E7,H6)    (the new swing high)
I7 =IF($67=-1#AND#G6=1,F7,I6))       (the new swing low)
 
Thanks for your help, Bruce!!
Putt4Dough
Posted : Monday, July 27, 2015 10:49:02 PM

Registered User
Joined: 7/30/2007
Posts: 1,072

Hi Bruce, forgot to mention one thing ...

"This particular method is called a 2-day or 2-period technique because it requires only two periods to identify a swing change. The sensitivity of the swing chart can be reduced by requiring that a swing change only occur after three or more days where prices reverse their direction."

I wanted to start with the basic 2-bar technique, but it would be nice to be able to adjust the sensitivity of swing changes by using three or more bars. Is that do-able?

Thanks again!

 

Bruce_L
Posted : Tuesday, July 28, 2015 1:05:21 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

The Personal Criteria Formula Language does not have variables which could be used to store the "current column". It might be possible to calculate what this value should be at each bar, but the resulting PCF would probably be too long and slow to be practical or post in the forums.

There is a technique for identifying swing/pivot highs/lows however which uses much simpler Condition Formulas. It just tests for a high with x number of lower highs prior to it and y number of lower highs after it or a low with x number of higher lows prior to it and y number of higher lows after it.

Frequently x and y are the same, but they do not have to be.

So a high with 5 prior lower highs "x" and 2 subsequent lower highs "y" could be identified with the following Condition Formula.

MAXH2 < H2 AND H2 > MAXH5.3

The only number in the formula which is not either x or y is the 3 at the end and this will always be y + 1.

If you want to plot a dot at the value of the high at the point where it is identified (so 2 bars after the actual high), you could use the following Indicator Formula as the Formula in a Custom PCF Indicator with the Plot Style set to Dot.

H2 / ABS(MAXH2 < H2 AND H2 > MAXH5.3)

Note that we could have use a * instead of / since multiplying and dividing by 1 produce the same value. We used a / however because dividing by 0 returns an error while multiplying by zero returns 0. This means we avoid having a string of dots plotted at 0 along the bottom of the price pane.

Another example checking for 3 prior higher lows "x" and 1 subsequent high low "y" could be identified with the following Condition Formula.

MINL1 > L1 AND L1 < MINL3.2

Note that MINL1 = L, so this could be shortened to the following.

L > L1 AND L1 < MINL3.2

So the Formula we would need to use to plot this at the point where it is identified would be as follows.

L1 / ABS(L > L1 AND L1 < MINL3.2)

Now these formulas plot the dots not at the high or low but at the value of the high or low y bars later. There is an advantage to this. You are not getting the illusion that the high or low is being identified earlier than it is and the dot ends up being plotted above or below price instead of at the top or bottom of the price bar.

We can make the dot happen at the bar of the high or low however by adding a 1-period moving average set to the Dot Plot Style with the Offset set to negative y.

If you do this however, you would probably want to set the opacity setting of the original Custom PCF Indicator all the way to the left so it is not visible on the chart.

PCF for Swing Point Highs and Lows



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Putt4Dough
Posted : Tuesday, July 28, 2015 1:08:37 PM

Registered User
Joined: 7/30/2007
Posts: 1,072

Thanks, Bruce !

caution
Posted : Friday, December 4, 2015 11:10:43 AM
Registered User
Joined: 5/5/2010
Posts: 185

Bruce,

Is it possible to translate the obove easylanguage code (swing highs and lows) to realcode?

thanks

Bruce_L
Posted : Friday, December 4, 2015 11:21:37 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

The following topic has RealCode Conditions to identify swing highs and lows.

Swing High and Swing Low Indicators



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
caution
Posted : Friday, December 4, 2015 1:24:09 PM
Registered User
Joined: 5/5/2010
Posts: 185

Thx.

Bruce_L
Posted : Friday, December 4, 2015 2:30:16 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

You're welcome.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.