CS Technical Analysis
CS Technical Analysis
seventeen
Technical Analysis
case study
OVERVIEW
Option A: The time horizon and a risk rate are set, and we then use various values of X
and Y (within a given bounds) to calculate the overall profit. A maximum profit is found
among these values.
Option B: The time horizon shifts (within a given bound) and the best values of X and Y
are determined for the maximum profit possible for scenarios of buy and sell, buy only,
and sell only.
Let’s now discuss how to make the calculations for determining the overall profit for a
given X and Y value. We perform these calculations in a calculation sheet with several
columns. Note that the formulas for these calculations are only in the rows corresponding
to the time horizon. In a later section, we will further describe the calculation sheet as well
as the procedure that generates the calculations, UpdateCalc.
The first two columns contain historical data from the S&P records, which we use to
calculate the average market price depending on X and Y. The next two columns check if
the investor can buy or sell shares based on the current trial value for X and Y,
respectively. We simply calculate the average market price based on the value of X for
buying and X for selling. We then enter the formulas and copy them for the time horizon
period.
The next column checks whether or not the user currently owns shares. This is a logical
check to ensure that the user does not sell shares that he or she does not own. For the
first period in the time horizon, we perform this check using the initial number of shares
provided by the user in an input form. For the remaining periods, we check the number of
shares owned in the previous period.
Else
Range("Own").Offset(StartCount, 0).Value = "No"
End If
Range("Own").Offset(StartCount + 1, 0).FormulaR1C1 = "=IF(R[-1]C[2] = ""Yes"",
""Yes"", IF(R[-1]C[3]=""Yes"", ""No"", R[-1]C))"
Range("Own").Offset(StartCount + 1, 0).Copy
Range(Range("Own").Offset(StartCount + 2, 0), Range("Own").Offset(EndCount,
0)).PasteSpecial
The next two columns confirm whether or not the user is going to buy or sell shares for the
current period. We check whether or not we are in a scenario in which we are buying or
selling. We then check whether or not the buy or sell check was met and whether or not
we have shares to sell.
Range("Buy").Offset(StartCount, 0).FormulaR1C1 =
"=IF(BuyAllowed=""Yes"",IF(AND(RC[-2] = ""No"", RC[-6]>RC[-5]),
""Yes"", ""No""),""No"")"
Range("Sell").Offset(StartCount, 0).FormulaR1C1 =
"=IF(SellAllowed=""Yes"",IF(AND(RC[-3] = ""Yes"", RC[-7]<RC[-5]),
""Yes"", ""No""),""No"")"
Range(Range("Buy").Offset(StartCount, 0), Range("Sell").Offset(StartCount,
0)).Copy
Range(Range("Buy").Offset(StartCount + 1, 0), Range("Sell").Offset(EndCount,
0)).PasteSpecial
For either action, we must determine the number of shares by assuming that we will either
buy all the shares for which we have funds or sell all the shares that we currently own.
When buying shares, we divide our current capital by the sum of the market price and the
transaction cost for buying.
The next column calculates the cash flow. We begin with a starting capital specified by the
user in an input form. Any time we buy, we spend all of our money. This cost comes from
the market price of the stock and the transaction cost for buying shares. Any time we sell,
we sell all of our shares and therefore gain their market price in value. However, we also
incur a transaction cost for selling.
Range("CashFlow").Offset(EndCount, 0)).PasteSpecial
The overall profit is stored at the top of the sheet. It is equal to our cash flow at the end of
the time horizon.
The formulas provided here are in terms of the cells and variables used in the sheet and
the procedure descriptions below.
CS19.1.2 Input
CS19.1.3 Output
CS17.2 Worksheets
We will use four worksheets in this application: the welcome sheet, a calculation sheet, an
output sheet for Option A, and an output sheet for Option B. The welcome sheet contains
the title and the description of the application as well as some images (see Figure
CS17.1). The “Start” button takes the user to an input form and then an options form. If the
CASE STUDY 17 Technical Analysis 5
user selects Option A, he or she will see the output sheet for Option A; otherwise, the
output sheet for Option B appears.
We determine the output for Option A and Option B by performing calculations on the
“Calculations” sheet (see Figure CS17.2), which the user can view by clicking a “View
Calc” button on either of the output sheets. The user can return to the output sheet by
clicking the “Back” button. An “End” button allows the user to exit the application.
Figure CS17.2(a)
CASE STUDY 17 Technical Analysis 6
Figure CS17.2(b)
The first two columns contain historical data from the S&P records. The dates are
recorded by year and months, with months represented as decimals. The price per share
is included for each date. The next two columns check if the investor can buy or sell
shares based on the current trial value for X and Y, respectively. The next column checks
whether or not the user currently owns shares. As previously mentioned, this is a logical
check to ensure that the user does not sell shares he or she does not own.
Above the next set of columns are several input cells, which the user provided in the initial
input form and in the re-solve options on the output sheets. The “Buy Allowed” and “Sell
Allowed” cells permit the user to vary the scenarios for Option B. The “Buy” and “Sell”
columns confirm whether or not the user wants to buy or sell shares for the current period.
For either action, the number of shares is also determined. Next, we calculate the cash
flow in the next column and store the overall profit at the top of the sheet. Note that the
formulas for these calculations are only in the rows corresponding to the time horizon.
The output sheet for Option A has a worksheet user interface that allows the user to
specify the risk rate and the time horizon (see Figure CS17.3). Bounds for the X and Y
values have been set in the input form already. Based on these bounds, a table is created
to try different combinations of X and Y in the calculations. We record the overall profit for
each trial, the maximum profit amongst these is highlighted, and the number of buys and
sells at this maximum profit value.
If the user changes the risk rate or time horizon, he or she must then click the “Re-solve”
button to re-run the calculations. The “View Calc” button allows the user to view the
calculation sheet and the “End” button to exit the application.
The output sheet for Option B displays the X and Y values that yield a maximum profit for
a shifting time horizon (see Figure CS17.4). These X, Y, and profit values are calculated
for three different scenarios: buying and selling, buying only, and selling only. The time
horizon shifts according to the values provided by the user in a user form for Option B. If
the user wishes to changes these values, he or she can press the “Re-solve” button. The
“View Calc” button allows the user to view the calculation sheet and the “End” button to
exit the application.
CASE STUDY 17 Technical Analysis 7
The next form that the user sees is the options form (see Figure CS17.6), which allows the
user to choose between Option A and Option B. We employ a frame with two mutually
exclusive option buttons.
If the user selects Option A, then the Option A form appears (see Figure CS17.7). This
form receives the initial values for the time horizon, which (along with the risk rate
provided in the input form) will be used to perform the initial calculations for Option A.
To re-solve Option A, the user does not return to any of these forms, but rather
manipulates the controls on the worksheet to change the risk rate and/or the time horizon
(see Figure CS17.8). We employ a spin button linked to a cell to allow the user to change
the risk rate. We include two text boxes and two combo boxes to allow the user to select
new years and/or months for the time horizon.
Figure CS17.8 The user interface controls on the output sheet for Option A.
If the user selects Option B from the option form, the Option B form appears next (see
Figure CS17.9). This form receives input for the shifting time horizon. The length of each
CASE STUDY 17 Technical Analysis 10
trial horizon is set in the “Period of Time per Interval” text box. The step size for the shift in
the starting date is also an input. The starting and ending dates are considered to be
bounds for the trial time horizons. We include combo boxes for the months of the time
horizon bounds. This form appears to the user again when he or she re-solves Option B.
Summary Option A Form Receives the initial time horizon for Option A.
Controls on output Allows the user to change the risk rate and the time horizon for
sheet for Option A resolving Option A.
Receives input for the shifting time horizon for Option B. Also
Option B Form
used in re-solve options for Option B.
CS17.4 Procedures
We will now outline the procedures for this application. We begin with the Main sub
procedure and variable definitions (see Figure CS17.10). The Main sub procedure simply
calls a ClearPrev procedure and displays the input form and options form. The ClearPrev
procedure clears the output sheets’ values and formatting for Option A and Option B. It
also initializes some variables.
The code to receive the input on the input form appears in Figure CS17.11. In this code,
the upper and lower bounds for X and Y are assigned to their corresponding variables.
The other input values are recorded on the calculations sheet. The application makes note
of the initial risk rate on the corresponding user interface of Option A’s output sheet.
CASE STUDY 17 Technical Analysis 11
The code to receive the user’s selection on the options form appears in Figure CS17.12. If
the user selects Option A, then the output sheet for Option A is formatted, a corresponding
Boolean variable is set, the user is taken to the output sheet for Option A, and the Option
CASE STUDY 17 Technical Analysis 12
A form comes into view. Once the user has entered the initial time horizon in the Option A
form, the application calls the SolveA procedure. If the user selects Option B, a
corresponding Boolean variable is set, the user is taken to the output sheet for Option B,
and the application calls the SolveB procedure. In response to the SolveB procedure, the
Option B form appears to receive the time horizon shift input.
The code for the Option A form records the initial time horizon on the user interface of
Option A’s output sheet (see Figure CS17.13). This form is also initialized to reveal the
first month to be selected in the “Month” combo boxes.
The SolveA procedure performs the main calculations for Option A (see Figure CS17.14).
It begins by determining whether or not we are solving Option A. If we are, then we record
any changes made to the user interface controls on the output sheet for Option A. We use
the SolveA procedure to solve Option B and should not record this information in that
case.
We then loop through all of the X,Y combinations to calculate their profit values and
determine the maximum profit scenario. To perform the calculations, we use an
UpdateCalc procedure (see Figure CS17.15), which loops through the rows in the
calculation sheet that correspond to the time horizon specified by the user. In these rows,
we enter all of the necessary formulas for each column in the calculation sheet.
CASE STUDY 17 Technical Analysis 13
For Option B, we use the SolveB procedure shown in Figure CS17.16. This procedure
begins by displaying the Option B form to the user. Since we will display this form every
time Option B is re-solved, we call from the SolveB procedure instead of the options form.
Next, we loop through the various time horizon values, solving for the best X and Y values
for maximum profit each time. For each time horizon value, we examine three scenarios:
buying and selling, only buying, and only selling. To enforce these scenarios, we modify
the “Buy Allowed” and “Sell Allowed” cells on the calculation sheet. We call the SolveA
procedure to find the optimal X, Y, and profit values for each scenario. Again, the SolveA
procedure calls the UpdateCalc procedure to modify the calculation sheet formulas as
needed for the varying X, Y, and time horizon values.
The Option B form code simply records the time horizon bounds, step size, and interval
length (see Figure CS17.17). The form is also initialized to select the first month in the
“Months” combo boxes.
CASE STUDY 17 Technical Analysis 14
We use the navigational procedures for the “End,” “View Calc,” and “Back” buttons (see
Figure CS17.18).
Main Initializes the application and retrieves the initial input from user.
Initializes the variables, clears previous values, and clears
ClearPrev
animation layout formatting.
Summary Input Form code Records the initial input.
Options Form code Determines whether to solve Option A or Option B.
Option A Form code Records initial time horizon for Option A.
Loops through combinations of X and Y to determine maximum
SolveA
profit.
Updates formulas in calculation sheet depending on X, Y, and
UpdateCalc
time horizon.
SolveB Loops through time horizon values for three scenarios.
Option B Form code Records time horizon shift values.
Navigational Used for navigational buttons.
Option A Re-solve
The user can change the risk rate and/or time horizon.
Option
Summary
Option B Re-solve
Allows the user to change the shift in time horizon values.
Option
CASE STUDY 17 Technical Analysis 18
CS17.6 Summary
The “Technical Analysis of Market Timing” problem seeks to determine values for
X and Y and a max profit for buying and selling shares for a certain time horizon.
There are four worksheets in this application: the welcome sheet, a calculation
sheet, an output sheet for Option A, and an output sheet for Option B.
There are four user forms, some controls on the output for the Option A
worksheet, and a few navigational and functional buttons.
There are several procedures for this application that allow the user to enter
input, perform calculations, view results, and re-solve.
There is a re-solve option for Option A in which the and Option B.
CS17.7 Extensions
What other analysis could you add to the output sheets? (Hint: What about charts
and/or graphs?)
What other scenarios could be considered for Option B?
Add error checking procedures to all of the form codes. For example, ensure that
only integer numbers are entered for the step size in the Option B form.
Expand this application to consider more than one stock at a time.