-3

I am trying to make and indicator to use in my quantstrat backtest but can't seem to figure out how to write it. Basically I want to compare the High and Low from 2 days ago to the H&L from 3 days ago. If day 2's H&L is lower than day 3 I'll make that as a buy signal if not I'll make it a short signal. After that I want to make another indicator to do the same but bring it forward one period so is yesterdays H&L lower than the H&L from 2 days ago and so on.

Originally I was using the donichan channel but realized this wont work as it will hold the same values until one of them makes a new high or low.

I know this is such a simple problem and I'm over thinking it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cameron Giles
  • 72
  • 1
  • 8
  • Would you show readers your current best shot at it? It's worth noting we get lots of people who claim to have tried something, but when one digs deeper we often find them haven't tried anything at all. I am not saying that applies to you - just that it is good to differentiate questions from posts of that kind. – halfer Nov 20 '17 at 16:13
  • @Cameron What you have written doesn't make sense. how can day2's "high and low" be lower than day 3. You could mean anything by that. you might mean H(3) < H(2) and L(2) > L(3) or something else. It sounds like what you want to do will be straightforward, but you haven't been specific enough. Define your problem more clearly.... – FXQuantTrader Nov 20 '17 at 19:35
  • @halfer I am trying to put my code in but even when I indent 4 spaces the code and save edits the code keeps bunching together into a paragraph that is basically unreadable. Am I doing something wrong? I have used this site a few times but have never had this problem. I am really sorry I don't want it to appear like I am wasting anyones time. I really appreciate the help. – Cameron Giles Nov 21 '17 at 00:40
  • @FXQuantTrader sorry I didn't explain it clearly. Basically I want to test if both the high and low of day -2 and less than the high and low of day -3. So similar to what you wrote: H(-2) – Cameron Giles Nov 21 '17 at 00:43
  • @Cameron: to get a formatted block to appear in Markdown, just ensure you have two things correct: (1) there are four spaces at the start of each line, and (2) there is a blank line before the code block. For convenience, to do the indent, you can paste your code in, select it, and click the code `{}` button in the toolbar. – halfer Nov 21 '17 at 00:52
  • Also see this search: [how to format code on stack overflow](https://duckduckgo.com/?q=how+to+format+code+on+stack+overflow&ia=web). – halfer Nov 21 '17 at 00:52

2 Answers2

1

If I understand your question correctly, I would suggest making two new variables, using the lag function on dplyr for example:

library(dplyr) 
df <- mutate(df, two.days.ago = lag(high.and.low, -2),
                 three.days.ago = lag(high.and.low, -3))


df <- mutate(df, buy.short = ifelse(two.days.ago < three.days.ago, 
                                   "buy", "short"))
Pete
  • 600
  • 1
  • 6
  • 16
  • That looks like it may be what I am looking for, I have never used dplyr before so I will try that now and let you know. Thank you for the help. – Cameron Giles Nov 21 '17 at 00:44
0

I managed to figure it out using @Pete's suggestion of using dylpr lag function. This was my end result:

Rule3 <- function(lagH3,lagL3,lagH2,lagL2)
{ifelse(lagH3 & lagL3>lagH2 & lagL2,1,0)} 
add.indicator(strategy=strategyname,name="lag",
          arguments=list(x=quote(mktdata$High),n=3L),label="lagH3")
add.indicator(strategy=strategyname,name="lag",
          arguments=list(x=quote(mktdata$Low),n=3L),label="lagL3")
 add.indicator(strategy=strategyname,name="lag",
          arguments=list(x=quote(mktdata$High),n=2L),label="lagH2")
add.indicator(strategy=strategyname,name="lag",
          arguments=list(x=quote(mktdata$Low),n=2L),label="lagL2")

add.indicator(strategyname, name="Rule3", arguments=list(lagH3=quote(mktdata$High.lagH3),
lagL3=quote(mktdata$Low.lagL3),Close=quote(mktdata$Close),lagH2=quote(mktdata$High.lagH2),
lagL2=quote(mktdata$Low.lagL2)), label="Rule3Signal")
halfer
  • 19,824
  • 17
  • 99
  • 186
Cameron Giles
  • 72
  • 1
  • 8