2023-03-24

Introduction

My sister recently started earning money from her part-time job. I taught her a simple trading strategy that uses some technical analysis. The strategy is a Techno-Funda strategy called Darvas Box. It is “Techno-Funda” because it uses both technical (data) analysis and Fundamental analysis. For the fundamental analysis portion, I decided to just use the large companies from the S&P500. This presentation showcases how the various data science tools in R can be used to conduct this analysis.

library(quantmod)

sp500_tickers <- read.csv(url) %>%
  select(Symbol)

for (symbol in sp500_tickers) {
    getSymbols(Symbols = symbol, env = sp500, src = "yahoo", 
    from = as.Date("1973-03-23"), to = Sys.Date())
}

Darvas Box

So now I will give a basic explanation of Darvas Box. The goal when trading stocks is to buy low and sell high, and the trick is to figure out when this happens.

In the 1950s, A trader called Nicolas Darvas noticed a trend in stock market movements where if the movement in a stock was strong enough you predict its actions for the next few days and then take advantage of it. He would draw what are now called “Darvas Boxes” around a stocks price, and if the stock surged out of the box then he knew that it would continue going up for a while so it would be a good time to buy.

The key goal in this project is to identify and draw a Darvas Box.

Darvas Box Rules

So how do you know if you have a Darvas Box? These are the rules that I eventually settled on:

  1. Find a new 52-Week High, this is a possible upper-bound for the box

  2. Check if the next 3-days are consistently lower than this upper-bound

  3. Now find the lower-bound of the box which can be identified as a day where the next 3-days are consistently higher

  4. Wait till the stock cross the upper-bound and buy, additionally the candlestick on the last day must be large and the volume should be high as well

Data Characteristics

Earlier I showed you how the data was downloaded. Now lets take a look at an individual stock. Here is the last few days of stock data for AAPL:

tail(AAPL, 6)
stock_data <- na.omit(AAPL)
##             Date   Open   High    Low  Close Adj.Close   Volume
## 10656 2023-03-20 155.07 157.82 154.15 157.40    157.40 73641400
## 10657 2023-03-21 157.32 159.40 156.54 159.28    159.28 73938300
## 10658 2023-03-22 159.30 162.14 157.81 157.83    157.83 75701800
## 10659 2023-03-23 158.83 161.55 157.68 158.93    158.93 67622100
## 10660 2023-03-24 158.86 160.34 157.85 160.25    160.25 59196500
## 10661 2023-03-27 159.94 160.77 157.87 158.28    158.28 52322000

Graphing the Closing Data

First we can graph the Closing Data using a Line Graph so that we can see the price of the stock over time.

Narrowing Down the Range

Now we can narrow down the range to see the trend.

Graphing the Volumes

By graphing the volumes we can see the interest in the stock over time.

Graphing the Moving Averages

\[\text{SMA}_n = \frac{\sum_{i=t-n+1}^t y_i}{n}\]

Graphing the Candlesticks

The candlesticks graph is the most complex one and aggregates the information from the previous graphs and also shows us the high, open, close, and low of our stock as we prepare to find an entry point.

Drawing the Darvas Box

Now that we have conducted the data analysis and visualization we can draw our Darvas Box and identify the entry point.

Conclusion

So that is how you can analyze a stock using the strategy Darvas Box and R. An interesting thing I learned was that this was way rarer in actuality then I previously thought.

Thank you for listening to my presentation.