Search code examples
rif-statementtrading

Tick test for trade classification


Writing my master thesis, I have downloaded trading data:

  >head(Data1)
  PRICE PREVIOUS
1 100   99
2 98    100
3 100   98
4 100   100
5 100   100
6 101   100      

The goal is to add a column that indicates if a trade was buyer ("buy") or seller ("sell") initiated. The rules are as follows:

  • PRICE < PREVIOUS => Sell
  • PRICE > PREVIOUS => Buy
  • PRICE == PREVIOUS => Prior classification

This is how it should look like

      >head(Data1)
     PRICE PREVIOUS TICK
    1 100   99       Buy
    2 98    100      Sell
    3 100   98       Buy
    4 100   100      Buy
    5 100   100      Buy
    6 101   100      Buy

I have written the following code:

Data1$TICK <- ifelse(Data1$PRICE == Data1$PREVIOUS, yes = shift(Data1[ ,3]), no = ifelse(Data1$PRICE>Data1$PREVIOUS, yes= "Buy", no = "Sell"))

However, when I try to execute the code I get the warning:

Error in [.data.frame(Data1, , 3)` : undefined columns selected

Therefore I have two questions:

  1. Is this code capable of delivering the result I would like to get?
  2. What is the error message referring to?

Solution

  • Here is a simple way just using base.

    data1 <- data.frame(PRICE = c(100,98,100,100,100,101), PREVIOUS = c(99,100,98,100,100,100))
    
    tk <- c("Sell", NA, "Buy")[sign(data1$PRICE-data1$PREVIOUS)+2]
    for(i in 2:length(tk)) if (is.na(tk[i])) tk[i] <- tk[i-1]
    
    data1$TICK <- tk
    print(data1)
    
    #   PRICE PREVIOUS TICK
    # 1   100       99  Buy
    # 2    98      100 Sell
    # 3   100       98  Buy
    # 4   100      100  Buy
    # 5   100      100  Buy
    # 6   101      100  Buy
    

    If you think it is more intuitive, you could have:

    tk <- c("Sell", "Prior", "Buy")[sign(data1$PRICE-data1$PREVIOUS)+2]
    for(i in 2:length(tk)) if (tk[i] == "Prior") tk[i] <- tk[i-1]