Trying To Solve The Alpaca Markets Entry Puzzle

May 7, 2018
Data Science Puzzle

On reddit I stumbled upon this puzzle on something called alpaca.markets, which aims to be “the first ever comission free algo trading platform”. In order to get early entry you ought to solve this puzzle. While solving this puzzle, alpaca.markets updated their page stating that only U.S. citizens from either CA, NY, NV. Germany isn’t listed, so I won’t solve the puzzle completely. Here’s my crack at it, may someone find this useful.

This is the puzzle to be solved before May 35th.

The first step of course is to obtain and process the data.

library(tidyverse)
library(lubridate)

data <- jsonlite::read_json('https://files.alpaca.markets/puzzle/20180401-100.json')
df <- data.frame(date=data$date %>% unlist %>% as.Date,
                 high=data$high %>% unlist,
                 low=data$low %>% unlist,
                 close=data$close %>% unlist,
                 volume=data$volume %>% unlist)

Let’s have a peak at the data:

head(df)
##         date   high    low  close    volume
## 1 2015-07-10 294.71 269.00 286.67 15892.681
## 2 2015-07-11 299.00 284.50 294.23  8509.598
## 3 2015-07-12 316.52 293.50 312.16 12917.991
## 4 2015-07-13 312.91 281.62 293.08 12936.003
## 5 2015-07-14 297.78 286.25 287.51  7146.061
## 6 2015-07-15 294.11 284.98 285.70  7589.801

The index is extracted by the following python code (the str is just the data at the end of the last line):

>>> str = "14950.468103392264]}"
# -6 because we want the values *between* -9 and -5, excluding the
# -9th and -5th values
>>> str[-9:-6]
'339'

This code is just a somewhat literal translation of what the picture states.

# as extracted by the python code above
idx <- 392

# the moving average of one window simply is the average ...
ma <- TTR::SMA(df$close)
m <- ma[idx]

# for i in ...
is.match <- function(x) {
  return ((abs(TTR::SMA(df$close, x)[idx] - m) / m) >= (sqrt(2) - 1))
}

X <- Filter(is.match, 2:idx-1)
# print(min(X))

We need to find a value for \(X\), such that the expression abs(TTR::SMA(df$close, x)[idx] - m) / m) is greater than sqrt(2) - 1, which is 0.4142136. The maximal value for the expression I could manage to find was for X <- idx:

abs(TTR::SMA(df$close, idx)[idx] - m) / m
## [1] 0.3511453

This must mean that my initial choice of idx is off. Although I checked idx <- 392 (at -8:-5) as well, producing a maximum of 0.3511453. A possible point to start off from would be to check for other meanings of -9:-5 other than the pythonic “count from the end of the array”. There aren’t that many possible solutions, because the index must be smaller than 1000, as this is the number of data points provided by the data.

comments powered by Disqus