attach(lll)
library(dtw)
## Loading required package: proxy
## 
## Attaching package: 'proxy'
## 
## The following objects are masked from 'package:stats':
## 
##     as.dist, dist
## 
## Loaded dtw v1.17-1. See ?dtw for help, citation("dtw") for use in publication.
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
par(mfrow = c(3,2))

srch <- 226502
srch2 <- srch + 288
trade <- srch + 96

plot(Close[srch:srch2], xlab=Time[srch], ylab="Original", type="l", lwd=2)
abline(h=Close[trade], col="gray")
plot(RSI[srch:srch2], xlab=Time[srch], ylab="Original", xlim=c(0,300), ylim=c(0,100), type="l", lwd=2, col="lightgreen")

i <- 0

while(i <= 260000){
  i <- i + 1
  n <- i + 96
  H4 <- i + 288
  if(dtw(RSI[i:n], RSI[226502:226598], step=asymmetric, open.end=TRUE, open.begin=TRUE)$distance < 300){
    plot(Close[i:H4], xlab=Time[i], ylab=X[i], type="l")    
    abline(h=Close[i + 96], col="blue")
    print(dtw(Close[i:n], Close[226502:226598])$distance)
    if(Close[i + 96] < (max(Close[n:H4]) + min(Close[i:H4]))/2){
      abline(h=Close[i + 96], col="red")
    }
    plot(RSI[i:H4], xlab=Time[i], ylab=X[i], xlim=c(0,300), ylim=c(0,100), type="l")
    par(new=T)
    plot(RSI[226502:226790], xlab=Time[i], ylab=X[i], xlim=c(0,300), ylim=c(0,100), type="l", col="lightgreen")
    i <- i + 15
  }     
}
## [1] 4055.654
## [1] 4495.424

## [1] 4643.927
## [1] 4645.648
## [1] 4779.194

## [1] 4843.745
## [1] 4785.012
## [1] 4768.259

## [1] 4845.042
## [1] 4540.085
## [1] 4645.474

## [1] 4665.43
## [1] 4684.267
## [1] 4053.529

## [1] 3785.719
## [1] 3786.605
## [1] 4564.34

## [1] 4563.4
## [1] 4565.085
## [1] 4443.655

## [1] 3796.632
## [1] 3544.606
## [1] 2812.994

## [1] 2536.35
## [1] 1662.999
## [1] 1525.054

## [1] 1129.836
## [1] 689.416
## [1] 548.183

## [1] 546.657
## [1] 547.872
## [1] 144.439

## [1] 306.35
## [1] 984.398
## [1] 935.019

## [1] 922.631
## [1] 902.221
## [1] 309.712

## [1] 1069.437
## [1] 438.602
## [1] 835.475

## [1] 848.995
## [1] 765.273
## [1] 237.657

## [1] 433.964
## [1] 381.008
## [1] 381.369

## [1] 13.096
## [1] 20.029
## [1] 12.653

## [1] 1.935
## [1] 0.455
## [1] 0.044

## [1] 0.234
## [1] 981.7
## [1] 3226.293