Math::Business::SM::Stochastic - Technical Analysis: Stochastic Oscillator


  use Math::Business::SM::Stochastic;

  my $sto = new Math::Business::SM::Stochastic;
     $sto->set_days(5);     # Lane uses 5 in his examples (if any)
     $sto->set_dperiod(30); # Lane
     $sto->set_method( Math::Business::SM::Stochastic::METHOD_LANE );
     $sto->set_xperiod(3); # used only by METHOD_FULL 

  # Lane's version
  my $sto = Math::Business::SM::Stochastic->recommended;

  # Probably more like what you expect (ie, matches up with
  # Yahoo/Google/Ameritrade, etc)

  my $sto_slow = Math::Business::SM::Stochastic->modern_slow;
  my $sto_fast = Math::Business::SM::Stochastic->modern_fast;
  my $sto_full = Math::Business::SM::Stochastic->modern_full;

  # basic usage

  my ($D, $K) = $sto->query;
  my $K = $sto->query; # if you don't care about %D
  print "current stochastic: %K=$K and %D=$D\n";


The Stochastic was designed by R. George C. Lane.

It is difficult to find a good reference on this indicator. Almost every source disagrees with the next and some sources even disagree internally (I'm looking at you Wikipedia).

The stochastic (among other things) purports to indicate an "overbought" situation where the %K is above 80 and "oversold" when below 20.

There are "divergences" and "convergences" to look for too, however. If there is a higher high or a lower low in the %K, this can apparently indicate trend changes. This concept is not easy to pin down explicitly, and so is not described here.


The basic idea is that %K should be a momentum indicator and %D should be a smoothed version of %K. Most sources generally agree that %K(5) should be computed as follows:

  $K = 100 * (close - min(@last_5_low))/(max(@last_5_high)-min(@last_5_low))

%D is more sticky and various sources give various answers. Lane himself seemed to use:

  $D = 100 * max(@last_3_high)/min(@last_3_low)

But most charting sites and the Wikipedia seem to choose an SMA or EMA for smothing on %D and thus produce something like:

  $D = $SMA->query;

The main problem getting this right is that Lane himself hasn't really spelled it out in publication. "Do this, then this, then that." The reason seems to be the lecture circuit. He teaches (taught?) this stuff in classes and seminars and hasn't really published it in the traditional sense.

To a certain extent, we therefore feel free to pick whatever we want for the defaults.


Robby Oliver <>


Paul Miller <>

I am using this software in my own projects... If you find bugs, please please please let me know. There is a mailing list with very light traffic that you might want to join:


Well, this is the StockMonkey project afterall. It seems built a Stochastics module before I did and I hadn't noticed. In the olden days, when I first started, I honestly expected to have a flood of these modules from dozens of authors. It never really happened, and when I got a request for Stochastics ... I didn't even look first. I just wrote one. Fail.

I got an email from EMILY STARCK asking why my module was marked ** UNAUTHORIZED RELEASE **, so I renamed this module and pushed the real Math::Business::Stochastic to my PREREQ_PM. Problem solved.


Copyright © 2013 Paul Miller


This is released under the Artistic License. See perlartistic.


perl(1), Math::Business::StockMonkey, Math::Business::StockMonkey::FAQ, Math::Business::StockMonkey::CookBook