In this section, we’ll set tangible goals for this exercise:
- Find the best and worst performing stocks. We’ll first evaluate which stock grew (or lost value) the most, relative to its starting price. For this, we’ll need to know the stock price from the start and the end of the time series, and calculate the difference relative to the initial price. In this challenge, you’ll learn how to declare, allocate, and initialize dynamic arrays; calculate the size of an array; and reference individual array elements.
- Identify risky stocks. Some stocks are riskier than others. This can be quantified using stock volatility, which is related to the standard deviation of the stock price. Standard deviation is a statistical measure of how much the values deviate from the average value, and it can be defined over arbitrary time periods. In this challenge, you’ll learn to slice arrays and perform whole-array arithmetic.
- Identify good times to buy and sell. Traders commonly use a technique called the moving-average crossover to decide whether it’s a good time to buy or sell shares of a stock. The moving-average crossover tells us when the stock price crosses the moving average (average over a limited time window) and indicates change in the longer term trend of the stock price, regardless of high-frequency fluctuations. In this challenge, you’ll learn how to search the arrays for specific values and extract elements that meet any criteria that you want.
Before we dive into implementing the solutions to these challenges, let’s get familiar with the data that we’ll work with.
Objectives for this exercise