SIDEBAR

BASIC code for phase calculation


by John F. Ehlers

This BASIC code finds the real part of the phasor (the horizontal component) and the imaginary part of the phasor (the vertical component) by summing the product of the price and the two sinusoids over one full cycle of the dominant cycle.

The code is reproduced below to allow our Web site browsers to easily copy and paste this information to other applications.

For the complete text of this article and sidebar, please refer to the November 1996 issue of Technical Analysis of STOCKS & COMMODITIES magazine.





Pi=3.1415926
TwoPi=2*Pi
For I=FirstRecord to LastRecord
    RealPart = 0
    ImagPart = 0
    For J = 0 To DominantCycle(I) - 1
        If I > DominantCycle(I) Then Weight = Close(I - J)
        RealPart = RealPart + Cos(TwoPi * J / DominantCycle(I)) * Weight
        ImagPart = ImagPart + Sin(TwoPi * J / DominantCycle(I)) * Weight
    Next
    If Abs(RealPart) > .001 Then
        Phase(I) = Atn(ImagPart / RealPart)
    Else
        Phase(I) = Pi / 2 * Sgn(ImagPart)
    End If
    If RealPart < 0 Then Phase(I) = Phase(I) + Pi
    Phase(I) = Phase(I) + Pi / 2
    If Phase(I) < 0 Then Phase(I) = Phase(I) + TwoPi
    If Phase(I) > TwoPi Then Phase(I) = Phase(I) - TwoPi
    Phase(I) = 180 * Phase(I) / Pi
Next
Excerpted from an article originally published in the November 1996 issue of Technical Analysis of STOCKS & COMMODITIES magazine. 
© Copyright 1996, Technical Analysis, Inc. All rights reserved.

Back to Ehlers Abstract or return to November Contents