I have some code
dates <- as.POSIXct(subset(Ident, coredata.Ident. == "Bull.Engulfing")$Date, format="%Y.%m.%d %H:%M")
for(i in seq_along(dates)){
EnterPrice<-as.vector(Sept17$Open[(dates)])
StopLoss<-as.vector(EnterPrice-0.0050)
TakeProfit<-as.vector(EnterPrice+0.0100)
MinDate<-as.vector(Sept17$Low[(dates)])
MaxDate<-as.vector(Sept17$High[(dates)])
n<-0
m<-0
while (MinDate > StopLoss) {
n<-n+1
MinDate<-Sept17$Low[Sept17[dates,which.i=TRUE]+n]
}
while (MaxDate < TakeProfit) {
m<-m+1
MaxDate<-Sept17$High[Sept17[dates,which.i=TRUE]+m]
}
if (m<n) {
F$Outcome=print("Win")
F$Hours=m
} else {
F$Outcome=print("Lose")
F$Hours=n
}
}
This returns an error of
Error in `[.xts`(Sept17$High, Sept17[dates, which.i = TRUE] + m) :
subscript out of bounds
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Basically what it does is it takes some data called Ident
and computes dates and times for when the Ident
data says Bull.Engulfing
. It then computes EnterPrice, StopLoss, TakeProfit, MinDate
and MaxDate
on each of the dates computed.
For each date/hour I want it to go through the while
loop and work out how many hours (n
) it takes for one of the conditions to not be satisfied in the while loop. I would like it to display a vector of either Loss
if the StopLoss
condition is not satisfied or Profit
if the TakeProfit
condition is not satisfied and I would like to display the number of hours (n
) it takes to achieve the unsatisfied condition.
I have tried above but not sure where to go from there.
So for example, let's say a component of dates
is 2017-01-01 10:00
.
The EnterPrice
might be 1.3100
therefore the StopLoss
would be 1.3050
and TakeProfit
would be 1.3200
.
It would then check every hour after that using the Sept17
data and if the min on that day and hour went less than the StopLoss
it would say Lose
in a vector, if the max on a day and hour went above the TakeProfit
it would say Win
, and it would tell me the number of hours (n
) it would take to get there.
It would then loop through for every other date where a Bull.Engulfing
occurred.
TIA