rm(list = ls())
library(keras)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(ggthemes)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tensorflow)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(reticulate)
library(TTR)
library(zoo)
py_discover_config()
## python:         C:/Users/chant/AppData/Local/r-miniconda/envs/r-reticulate/python.exe
## libpython:      C:/Users/chant/AppData/Local/r-miniconda/envs/r-reticulate/python37.dll
## pythonhome:     C:/Users/chant/AppData/Local/r-miniconda/envs/r-reticulate
## version:        3.7.10 (default, Feb 26 2021, 13:06:18) [MSC v.1916 64 bit (AMD64)]
## Architecture:   64bit
## numpy:          C:/Users/chant/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy
## numpy_version:  1.19.5
conda_create("emajor")
## [1] "C:\\Users\\chant\\AppData\\Local\\r-miniconda\\envs\\emajor\\python.exe"
virtualenv_create("myenv")
## virtualenv: myenv
use_virtualenv("myenv")

library(readr)
SPY <-  read.csv("C:\\Users\\chant\\Documents\\SP500.csv")
class(SPY)
## [1] "data.frame"
str(SPY)
## 'data.frame':    2495 obs. of  6 variables:
##  $ Date     : chr  "5/2/2011" "5/3/2011" "5/4/2011" "5/5/2011" ...
##  $ Open     : num  1363 1358 1352 1343 1334 ...
##  $ High     : num  1374 1358 1355 1349 1352 ...
##  $ Low      : num  1355 1346 1338 1325 1332 ...
##  $ Close    : num  1358 1352 1343 1335 1334 ...
##  $ Adj.Close: num  1358 1352 1343 1335 1334 ...
SPY.df <- as.data.frame(coredata(SPY))
Series <- SPY.df [, 6]

as.numeric(Series, differences = 1)
##    [1] 1357.75 1352.00 1343.00 1335.00 1334.50 1342.75 1353.75 1338.75 1347.50
##   [10] 1334.00 1325.50 1325.50 1338.50 1341.75 1327.75 1315.25 1313.50 1316.50
##   [19] 1326.50 1330.00 1344.00 1312.00 1312.50 1296.25 1285.00 1284.75 1277.00
##   [28] 1287.50 1269.25 1271.50 1290.00 1265.50 1269.00 1279.40 1273.75 1288.00
##   [37] 1279.75 1277.00 1264.00 1276.25 1294.50 1304.25 1315.50 1334.75 1336.75
##   [46] 1335.75 1351.75 1341.75 1318.50 1310.75 1312.25 1306.75 1315.00 1300.50
##   [55] 1321.25 1321.25 1342.50 1341.00 1333.50 1326.25 1299.00 1296.75 1288.50
##   [64] 1279.75 1247.25 1254.50 1198.75 1197.75 1111.25 1171.75 1123.50 1168.50
##   [73] 1176.75 1198.50 1192.25 1190.00 1143.50 1124.00 1123.25 1158.50 1172.00
##   [82] 1157.50 1176.00 1208.00 1204.75 1217.75 1201.25 1169.25 1164.50 1199.00
##   [91] 1185.75 1158.00 1163.25 1171.50 1188.50 1210.75 1216.74 1197.75 1196.00
##  [100] 1155.75 1123.50 1129.75 1158.50 1169.50 1148.75 1156.25 1126.00 1086.25
##  [109] 1113.50 1135.00 1157.50 1155.00 1191.00 1189.50 1198.25 1198.00 1219.25
##  [118] 1194.00 1223.00 1206.50 1210.00 1235.25 1247.00 1224.50 1237.50 1282.50
##  [127] 1281.00 1249.25 1224.50 1234.25 1255.75 1251.00 1257.50 1273.25 1225.75
##  [136] 1237.50 1261.50 1252.50 1254.00 1231.00 1214.75 1214.00 1190.75 1182.75
##  [145] 1160.00 1191.00 1196.50 1246.00 1243.50 1243.50 1255.00 1255.00 1264.00
##  [154] 1236.00 1258.75 1235.25 1226.25 1212.50 1218.00 1224.00 1199.00 1236.00
##  [163] 1236.25 1249.00 1260.25 1260.25 1244.50 1257.50 1252.50 1272.00 1273.00
##  [172] 1273.00 1274.25 1275.50 1286.00 1288.25 1291.75 1289.00 1289.25 1302.25
##  [181] 1310.50 1310.75 1311.00 1311.50 1320.25 1315.25 1312.50 1309.00 1308.25
##  [190] 1319.75 1322.75 1339.00 1339.00 1344.75 1347.00 1348.25 1340.50 1349.00
##  [199] 1347.75 1342.25 1354.75 1359.75 1360.00 1356.00 1363.00 1363.25 1367.25
##  [208] 1371.50 1364.50 1374.50 1368.75 1364.50 1342.00 1352.75 1366.50 1372.50
##  [217] 1372.50 1396.25 1394.25 1401.75 1405.75 1404.00 1400.00 1397.50 1389.00
##  [226] 1394.00 1415.00 1406.50 1400.25 1398.25 1403.25 1412.50 1408.75 1393.25
##  [235] 1390.25 1375.00 1357.00 1364.00 1386.00 1365.00 1364.00 1383.50 1378.25
##  [244] 1372.50 1375.25 1362.75 1370.00 1387.25 1396.75 1398.50 1393.50 1400.50
##  [253] 1397.50 1386.00 1362.50 1365.75 1358.50 1351.00 1357.50 1350.00 1334.00
##  [262] 1328.25 1322.50 1301.25 1290.75 1315.75 1314.75 1315.75 1322.50 1315.00
##  [271] 1333.50 1308.50 1309.25 1274.00 1273.00 1285.00 1315.50 1316.75 1328.75
##  [280] 1307.00 1326.75 1315.50 1333.00 1335.75 1341.00 1350.50 1350.75 1318.25
##  [289] 1326.75 1306.50 1315.50 1325.50 1322.50 1356.50 1357.50 1361.50 1351.75
##  [298] 1349.25 1335.50 1336.25 1329.25 1351.75 1347.50 1358.50 1367.25 1372.00
##  [307] 1358.25 1343.75 1329.50 1335.00 1354.75 1382.50 1380.50 1374.50 1370.50
##  [316] 1362.00 1389.00 1390.00 1397.00 1398.25 1400.50 1402.50 1402.50 1401.50
##  [325] 1403.50 1413.00 1415.25 1414.75 1412.50 1412.25 1400.00 1409.75 1408.25
##  [334] 1407.75 1407.25 1397.00 1405.00 1406.00 1403.50 1431.00 1438.25 1426.50
##  [343] 1430.50 1439.50 1457.25 1465.75 1460.75 1459.25 1459.50 1460.50 1468.25
##  [352] 1451.50 1437.25 1427.00 1441.00 1434.25 1437.00 1441.00 1444.75 1455.75
##  [361] 1455.50 1449.75 1436.00 1426.25 1428.50 1421.50 1435.50 1449.25 1457.00
##  [370] 1451.50 1424.00 1430.00 1406.75 1405.25 1408.25 1407.50 1406.75 1423.25
##  [379] 1405.50 1412.00 1425.25 1389.00 1375.25 1375.75 1378.25 1371.00 1353.00
##  [388] 1351.25 1359.75 1382.50 1386.25 1388.25 1403.25 1397.50 1407.00 1415.75
##  [397] 1414.50 1407.00 1405.50 1408.25 1413.00 1416.00 1420.25 1431.50 1427.25
##  [406] 1418.00 1415.00 1432.50 1445.75 1437.25 1445.50 1424.75 1413.50 1410.75
##  [415] 1384.00 1420.00 1457.00 1453.50 1457.75 1455.75 1452.25 1455.75 1467.00
##  [424] 1467.25 1464.25 1465.25 1465.50 1475.75 1479.00 1489.50 1490.25 1491.75
##  [433] 1495.75 1497.00 1505.00 1495.25 1493.25 1506.75 1493.50 1506.00 1506.75
##  [442] 1505.25 1512.50 1513.00 1516.25 1517.25 1518.50 1517.00 1528.00 1507.00
##  [451] 1501.00 1514.50 1487.25 1492.50 1515.75 1513.25 1516.50 1525.75 1537.00
##  [460] 1539.00 1542.75 1549.50 1556.00 1552.50 1556.00 1562.25 1561.25 1546.75
##  [469] 1542.25 1549.00 1539.00 1552.00 1547.00 1557.25 1556.75 1562.75 1556.00
##  [478] 1564.50 1548.50 1554.50 1546.00 1559.25 1563.25 1582.75 1587.75 1582.00
##  [487] 1543.50 1568.75 1546.00 1534.00 1547.50 1556.00 1573.50 1574.00 1581.75
##  [496] 1576.50 1588.25 1592.25 1577.25 1592.25 1608.50 1613.50 1620.50 1628.75
##  [505] 1624.50 1629.50 1630.75 1648.00 1654.25 1648.00 1663.00 1664.50 1665.50
##  [514] 1655.50 1650.00 1650.50 1654.50 1647.00 1653.50 1629.00 1636.25 1631.25
##  [523] 1608.00 1622.75 1638.50 1642.00 1627.00 1610.00 1636.75 1624.25 1639.75
##  [532] 1651.00 1629.50 1590.50 1600.75 1566.25 1581.50 1595.50 1606.50 1599.25
##  [541] 1606.75 1607.25 1627.25 1635.50 1645.50 1648.50 1670.00 1670.25 1677.50
##  [550] 1671.25 1675.50 1680.50 1689.50 1690.25 1688.25 1683.75 1684.00 1686.50
##  [559] 1682.50 1684.75 1680.50 1700.25 1704.00 1702.50 1694.00 1688.25 1693.75
##  [568] 1686.25 1687.00 1690.75 1682.00 1655.75 1651.00 1645.00 1650.50 1636.50
##  [577] 1654.75 1661.50 1654.25 1628.25 1632.25 1636.75 1631.25 1639.00 1653.50
##  [586] 1653.00 1653.50 1669.00 1682.50 1688.75 1685.00 1688.50 1698.00 1705.00
##  [595] 1724.50 1724.25 1726.75 1692.75 1692.50 1685.75 1692.50 1686.50 1674.25
##  [604] 1689.50 1683.00 1669.75 1684.75 1667.75 1650.50 1648.75 1685.00 1699.00
##  [613] 1704.25 1692.00 1713.25 1727.75 1736.50 1738.25 1749.50 1741.75 1748.50
##  [622] 1754.00 1759.00 1767.50 1760.50 1751.00 1754.75 1763.00 1756.50 1765.50
##  [631] 1745.25 1766.00 1767.50 1765.00 1778.75 1787.75 1793.50 1788.75 1785.25
##  [640] 1779.75 1793.75 1801.25 1802.50 1802.00 1804.25 1799.75 1791.50 1791.75
##  [649] 1784.00 1805.00 1809.00 1803.00 1780.75 1775.00 1774.75 1786.50 1779.25
##  [658] 1810.75 1808.50 1810.25 1822.75 1836.50 1836.50 1834.75 1841.00 1826.50
##  [667] 1825.50 1820.75 1830.75 1832.50 1833.00 1837.75 1815.00 1833.00 1841.50
##  [676] 1836.25 1834.25 1838.50 1838.50 1824.25 1782.00 1775.75 1788.25 1771.25
##  [685] 1781.25 1776.50 1732.75 1743.75 1744.00 1766.50 1793.50 1794.75 1813.50
##  [694] 1817.00 1824.25 1835.00 1837.50 1825.50 1836.25 1834.25 1846.00 1846.25
##  [703] 1842.00 1854.00 1857.50 1843.00 1871.50 1872.50 1876.25 1878.00 1877.25
##  [712] 1865.25 1867.75 1846.75 1840.00 1857.75 1870.75 1859.75 1873.75 1881.75
##  [721] 1849.50 1859.25 1842.50 1840.50 1850.50 1864.50 1877.75 1883.00 1883.00
##  [730] 1860.00 1838.00 1845.00 1864.75 1827.00 1811.75 1824.50 1839.50 1852.75
##  [739] 1858.00 1864.50 1874.00 1873.00 1873.00 1860.00 1866.00 1871.75 1878.00
##  [748] 1877.75 1874.50 1875.75 1864.25 1874.25 1872.25 1873.50 1892.75 1894.25
##  [757] 1885.25 1867.25 1874.75 1882.25 1868.00 1885.00 1890.25 1897.00 1909.25
##  [766] 1909.00 1918.00 1921.50 1921.75 1922.00 1925.75 1938.50 1949.25 1950.25
##  [775] 1950.50 1944.00 1930.50 1935.75 1936.50 1941.50 1957.00 1958.50 1968.00
##  [784] 1953.00 1943.25 1949.50 1948.75 1952.00 1952.50 1965.75 1967.75 1971.00
##  [793] 1960.50 1967.25 1957.75 1962.50 1971.00 1968.00 1974.75 1953.50 1971.50
##  [802] 1966.25 1975.00 1980.75 1980.75 1971.50 1973.00 1963.00 1965.00 1924.75
##  [811] 1918.50 1932.00 1913.00 1914.75 1905.25 1923.75 1932.50 1930.50 1944.75
##  [820] 1953.50 1952.50 1967.50 1977.25 1983.25 1989.50 1987.75 1995.00 1998.50
##  [829] 1997.00 1996.75 2001.50 1999.75 1998.75 1997.75 2006.00 2000.50 1989.75
##  [838] 1995.00 1997.00 1984.75 1984.25 1999.50 2001.50 2012.25 2019.25 1986.25
##  [847] 1972.25 1991.00 1961.50 1976.00 1969.50 1965.50 1940.75 1938.50 1960.25
##  [856] 1956.00 1928.00 1961.75 1925.00 1894.25 1865.50 1874.75 1846.75 1850.50
##  [865] 1881.00 1900.00 1938.00 1925.00 1946.00 1959.75 1957.00 1980.00 1972.25
##  [874] 1988.50 2011.50 2011.00 2005.50 2018.75 2028.00 2026.00 2034.00 2036.50
##  [883] 2036.00 2034.00 2038.00 2039.75 2048.50 2047.25 2052.00 2061.75 2067.50
##  [892] 2067.50 2072.25 2050.75 2066.00 2072.50 2072.00 2076.00 2059.50 2057.50
##  [901] 2026.50 2031.00 1997.25 1990.00 1971.50 2014.25 2065.00 2065.00 2072.50
##  [910] 2079.00 2084.25 2085.75 2076.75 2052.50 2046.25 2016.00 1994.50 2019.50
##  [919] 2055.00 2035.25 2022.50 2016.00 2007.50 1989.00 2013.00 2016.75 2026.50
##  [928] 2056.50 2044.00 2053.50 2030.00 1991.50 2018.50 1988.50 2017.00 2042.00
##  [937] 2030.00 2055.00 2053.00 2042.50 2062.25 2065.75 2084.00 2093.50 2096.00
##  [946] 2095.50 2095.25 2107.00 2106.75 2113.75 2110.25 2110.00 2102.75 2114.00
##  [955] 2104.75 2096.50 2099.75 2070.75 2077.75 2042.00 2039.50 2064.00 2049.75
##  [964] 2076.50 2074.50 2100.50 2090.50 2101.25 2094.75 2085.00 2053.75 2048.50
##  [973] 2052.50 2075.50 2060.75 2053.00 2059.50 2073.25 2067.75 2076.00 2085.75
##  [982] 2095.50 2086.50 2091.00 2099.75 2100.75 2075.50 2091.00 2091.00 2100.25
##  [991] 2107.00 2111.75 2104.75 2112.00 2099.00 2079.00 2101.50 2109.25 2084.00
## [1000] 2074.25 2084.25 2108.50 2097.75 2095.00 2094.50 2117.50 2119.00 2126.00
## [1009] 2124.50 2122.50 2128.00 2124.50 2105.00 2121.00 2121.75 2106.00 2109.25
## [1018] 2106.75 2116.00 2099.00 2092.25 2078.25 2080.00 2107.00 2109.25 2093.00
## [1027] 2084.00 2097.50 2097.50 2123.00 2118.50 2112.75 2116.50 2099.50 2094.00
## [1036] 2095.75 2050.50 2054.50 2071.00 2068.75 2064.50 2073.75 2039.25 2041.25
## [1045] 2069.00 2094.50 2102.00 2104.25 2117.00 2118.75 2122.00 2114.50 2108.00
## [1054] 2098.50 2077.50 2064.50 2087.25 2101.50 2103.75 2098.50 2091.00 2083.00
## [1063] 2093.75 2079.50 2073.50 2099.75 2079.75 2084.25 2080.50 2089.50 2099.25
## [1072] 2094.00 2072.75 2025.50 1971.50 1871.25 1872.75 1938.00 1989.25 1989.75
## [1081] 1969.25 1916.00 1947.00 1946.00 1921.75 1965.75 1942.75 1949.50 1960.25
## [1090] 1954.00 1979.75 1998.25 1987.75 1960.00 1963.00 1932.00 1928.50 1918.75
## [1099] 1919.25 1872.00 1874.50 1908.75 1916.75 1943.00 1974.75 1968.50 1987.25
## [1108] 2006.50 2007.50 2011.00 1994.00 1984.00 2019.00 2025.50 2027.50 2020.50
## [1117] 2008.50 2053.00 2066.00 2062.25 2060.50 2084.50 2083.00 2073.75 2095.50
## [1126] 2103.00 2094.75 2094.00 2093.75 2073.00 2078.00 2069.00 2040.50 2018.50
## [1135] 2048.00 2049.00 2079.75 2079.25 2088.75 2084.25 2084.75 2088.00 2090.00
## [1144] 2079.75 2100.00 2081.50 2051.25 2088.50 2081.00 2058.75 2042.00 2049.25
## [1153] 2009.75 2018.50 2045.50 2072.00 2034.25 2032.50 2015.00 2036.00 2053.00
## [1162] 2051.25 2048.75 2072.75 2054.50 2035.50 2009.00 2011.75 1986.00 1933.00
## [1171] 1911.50 1914.25 1925.00 1881.50 1914.50 1875.00 1873.00 1855.00 1861.00
## [1180] 1899.25 1870.25 1896.00 1875.00 1880.75 1930.00 1931.25 1897.50 1908.50
## [1189] 1907.75 1875.25 1852.00 1848.25 1846.75 1824.50 1858.25 1888.75 1922.75
## [1198] 1916.50 1914.50 1936.25 1916.00 1930.25 1950.50 1942.75 1929.50 1978.00
## [1207] 1983.50 1990.50 1995.00 1999.00 1981.00 1989.00 1988.75 2020.00 2018.75
## [1216] 2015.75 2027.00 2039.75 2048.00 2042.75 2042.50 2028.75 2028.50 2028.00
## [1225] 2047.50 2055.25 2051.50 2065.00 2057.50 2038.75 2060.25 2035.00 2040.75
## [1234] 2034.50 2055.75 2076.00 2076.50 2075.00 2086.75 2093.75 2098.00 2082.75
## [1243] 2086.00 2083.25 2088.50 2090.75 2072.50 2059.00 2074.25 2057.00 2047.00
## [1252] 2044.00 2052.75 2054.25 2077.50 2058.00 2058.75 2043.50 2062.75 2043.50
## [1261] 2041.50 2038.75 2050.00 2045.25 2075.00 2087.25 2089.75 2097.25 2095.00
## [1270] 2098.00 2103.75 2097.75 2108.25 2110.25 2118.00 2114.25 2096.25 2078.75
## [1279] 2074.50 2071.75 2079.25 2078.50 2074.25 2080.50 2076.75 2105.75 2018.50
## [1288] 1985.00 2028.50 2066.75 2090.25 2096.25 2082.75 2094.00 2092.00 2120.50
## [1297] 2130.25 2145.75 2146.00 2157.25 2152.75 2160.00 2158.75 2167.50 2158.00
## [1306] 2167.50 2162.25 2163.25 2160.50 2164.75 2168.25 2164.50 2152.75 2157.00
## [1315] 2159.25 2176.75 2175.50 2177.50 2172.75 2181.75 2180.25 2186.00 2176.75
## [1324] 2179.75 2183.50 2181.75 2181.50 2185.25 2175.00 2173.50 2168.50 2179.25
## [1333] 2175.25 2169.50 2167.25 2178.00 2184.50 2184.50 2177.50 2122.75 2158.50
## [1342] 2128.75 2120.25 2145.00 2141.75 2133.00 2131.00 2156.25 2168.25 2158.00
## [1351] 2139.75 2152.75 2163.25 2148.50 2160.50 2153.25 2144.75 2153.25 2156.50
## [1360] 2146.50 2134.50 2131.50 2126.25 2127.00 2123.00 2132.00 2138.00 2137.00
## [1369] 2134.75 2144.25 2138.00 2134.00 2123.50 2123.75 2120.00 2103.75 2092.25
## [1378] 2083.50 2080.00 2129.00 2135.50 2160.25 2167.25 2160.50 2179.25 2172.75
## [1387] 2184.25 2180.75 2193.00 2200.25 2200.75 2211.25 2200.75 2203.75 2198.75
## [1396] 2192.00 2192.00 2204.25 2210.00 2236.75 2247.75 2260.00 2256.00 2273.25
## [1405] 2257.00 2263.50 2267.50 2260.00 2266.50 2260.50 2258.75 2260.00 2261.00
## [1414] 2245.25 2245.00 2236.25 2252.50 2264.25 2264.25 2271.50 2265.00 2263.75
## [1423] 2270.50 2263.50 2272.50 2262.75 2266.50 2261.50 2266.00 2262.00 2274.50
## [1432] 2294.00 2294.00 2289.00 2276.00 2274.50 2274.50 2275.50 2291.00 2286.50
## [1441] 2288.50 2290.25 2304.25 2312.75 2326.25 2337.00 2350.50 2345.50 2348.00
## [1450] 2360.00 2361.00 2362.75 2365.00 2368.25 2362.75 2393.50 2382.00 2381.25
## [1459] 2375.50 2366.50 2364.00 2366.25 2371.75 2375.00 2366.50 2383.75 2382.75
## [1468] 2384.50 2370.25 2342.25 2342.50 2340.00 2344.75 2338.50 2351.50 2357.00
## [1477] 2364.50 2359.25 2356.00 2356.50 2346.50 2353.75 2352.25 2352.50 2351.00
## [1486] 2340.75 2327.50 2345.00 2337.25 2333.75 2352.00 2347.50 2370.00 2385.00
## [1495] 2382.25 2386.00 2380.50 2387.00 2385.75 2383.25 2385.50 2397.75 2395.00
## [1504] 2393.25 2395.25 2391.00 2388.75 2398.50 2397.00 2357.50 2363.50 2381.50
## [1513] 2392.75 2398.00 2402.00 2413.50 2413.75 2410.75 2411.00 2429.50 2437.75
## [1522] 2434.50 2430.75 2432.00 2432.50 2430.50 2428.50 2440.00 2437.25 2434.25
## [1531] 2433.75 2447.50 2437.50 2433.50 2431.75 2435.00 2436.00 2420.50 2438.50
## [1540] 2420.00 2421.00 2428.00 2408.50 2422.50 2424.50 2424.50 2440.00 2445.50
## [1549] 2456.00 2458.50 2457.75 2471.50 2471.25 2469.50 2468.50 2474.00 2473.25
## [1558] 2472.00 2470.25 2468.00 2472.25 2473.50 2471.75 2472.00 2477.50 2472.75
## [1567] 2473.00 2437.50 2440.00 2463.50 2463.75 2467.50 2429.50 2426.75 2428.00
## [1576] 2452.75 2441.50 2440.75 2442.50 2443.75 2447.00 2455.75 2470.00 2474.25
## [1585] 2459.75 2465.50 2466.50 2462.50 2487.50 2496.00 2496.50 2496.50 2496.00
## [1594] 2502.75 2504.75 2505.25 2501.00 2499.50 2497.00 2495.50 2504.50 2507.75
## [1603] 2516.00 2526.25 2532.75 2536.25 2550.00 2545.00 2543.75 2548.50 2553.00
## [1612] 2549.50 2552.75 2556.25 2557.00 2560.00 2560.50 2574.00 2563.50 2567.25
## [1621] 2558.50 2561.50 2578.50 2568.25 2572.75 2574.75 2576.75 2582.75 2588.75
## [1630] 2586.75 2591.00 2584.00 2579.50 2582.00 2578.00 2565.00 2585.00 2576.25
## [1639] 2582.00 2596.25 2594.50 2601.75 2626.00 2625.00 2648.00 2644.00 2638.25
## [1648] 2628.25 2629.25 2639.50 2651.00 2661.50 2665.00 2666.50 2653.25 2666.75
## [1657] 2694.50 2684.00 2681.50 2687.75 2686.00 2687.00 2685.50 2685.75 2676.00
## [1666] 2693.00 2711.00 2723.75 2742.50 2746.75 2752.25 2750.50 2769.50 2788.75
## [1675] 2782.50 2803.75 2796.25 2811.00 2835.25 2839.50 2841.00 2841.25 2874.50
## [1684] 2853.50 2824.50 2825.75 2822.50 2756.75 2607.75 2694.25 2668.25 2593.50
## [1693] 2619.00 2655.25 2661.75 2697.00 2734.00 2735.00 2714.00 2698.75 2711.50
## [1702] 2748.75 2784.50 2747.50 2714.50 2678.25 2690.25 2718.50 2724.00 2723.25
## [1711] 2739.25 2784.00 2784.00 2768.50 2749.75 2751.50 2752.75 2722.75 2723.50
## [1720] 2718.25 2643.25 2597.75 2659.50 2615.75 2607.50 2643.00 2575.00 2613.25
## [1729] 2647.00 2661.75 2605.75 2619.00 2655.00 2641.00 2664.00 2657.25 2681.75
## [1738] 2706.50 2709.75 2693.00 2671.50 2671.25 2635.25 2644.50 2674.50 2671.50
## [1747] 2647.00 2652.25 2627.50 2631.75 2663.00 2670.00 2670.25 2696.00 2718.75
## [1756] 2729.50 2731.00 2709.00 2723.00 2718.75 2713.00 2733.00 2726.00 2730.75
## [1765] 2727.50 2718.25 2692.25 2724.50 2705.50 2733.75 2745.50 2751.50 2772.25
## [1774] 2772.25 2778.75 2783.00 2784.00 2774.25 2783.25 2776.50 2779.75 2766.25
## [1783] 2772.00 2752.50 2759.50 2722.25 2728.50 2705.00 2719.50 2721.50 2727.25
## [1792] 2738.50 2763.00 2787.50 2796.75 2774.00 2798.50 2803.25 2796.50 2811.25
## [1801] 2816.00 2805.25 2800.75 2812.00 2821.00 2841.25 2842.50 2817.50 2803.25
## [1810] 2817.00 2810.75 2828.50 2839.50 2850.00 2859.75 2855.25 2853.75 2836.75
## [1819] 2825.50 2841.00 2821.25 2844.75 2852.25 2858.50 2861.75 2861.25 2858.00
## [1828] 2876.75 2898.50 2899.25 2914.75 2902.00 2902.00 2898.25 2888.25 2879.00
## [1837] 2874.75 2880.25 2889.75 2888.50 2905.25 2906.25 2891.00 2906.50 2910.00
## [1846] 2934.00 2943.00 2925.50 2921.25 2911.50 2920.00 2919.00 2930.00 2928.50
## [1855] 2931.50 2907.75 2894.00 2893.75 2888.25 2781.00 2745.50 2768.50 2749.00
## [1864] 2817.75 2816.25 2772.25 2767.50 2756.50 2746.25 2664.25 2688.25 2669.50
## [1873] 2643.50 2685.25 2711.00 2738.00 2724.25 2739.50 2759.00 2816.50 2808.75
## [1882] 2779.00 2727.75 2727.50 2698.50 2734.50 2743.00 2696.25 2640.00 2649.00
## [1891] 2670.00 2683.50 2741.50 2744.25 2758.25 2790.75 2701.75 2691.00 2636.00
## [1900] 2643.00 2641.25 2652.50 2645.50 2602.00 2552.50 2535.75 2503.25 2485.75
## [1909] 2467.75 2471.00 2495.00 2486.00 2505.25 2511.00 2447.75 2531.25 2550.50
## [1918] 2572.50 2582.50 2594.00 2595.00 2580.50 2605.50 2613.25 2635.25 2671.50
## [1927] 2632.00 2638.25 2634.00 2663.50 2642.00 2640.25 2682.50 2704.50 2704.25
## [1936] 2721.25 2731.00 2729.50 2704.00 2706.25 2708.25 2744.75 2749.50 2743.50
## [1945] 2777.00 2778.75 2787.00 2774.25 2791.25 2797.00 2791.50 2795.00 2784.75
## [1954] 2805.00 2791.50 2791.50 2771.50 2750.00 2747.00 2784.00 2792.00 2814.50
## [1963] 2807.25 2811.89 2840.50 2836.50 2827.00 2862.50 2810.75 2807.00 2823.00
## [1972] 2810.50 2821.00 2837.75 2870.50 2867.00 2879.75 2882.75 2896.00 2898.25
## [1981] 2882.50 2894.50 2891.75 2912.50 2909.50 2911.50 2900.50 2910.00 2912.50
## [1990] 2938.00 2930.75 2926.25 2941.50 2943.00 2948.50 2923.00 2917.50 2947.50
## [1999] 2932.50 2890.75 2887.25 2872.75 2887.00 2807.00 2839.25 2855.00 2878.50
## [2008] 2862.00 2844.00 2866.00 2857.50 2819.50 2831.75 2805.00 2780.00 2790.50
## [2017] 2752.50 2749.50 2805.00 2827.75 2845.75 2875.00 2889.25 2887.00 2881.00
## [2026] 2894.50 2890.50 2892.00 2921.75 2929.00 2955.75 2953.90 2952.00 2922.00
## [2035] 2918.00 2931.00 2944.25 2967.75 2979.50 2990.50 2978.50 2982.00 2997.50
## [2044] 3004.00 3015.50 3017.25 3007.00 2985.00 2997.50 2977.00 2989.00 3008.00
## [2053] 3021.50 3006.50 3024.50 3021.75 3012.25 2982.25 2952.00 2932.50 2830.00
## [2062] 2876.00 2880.50 2940.00 2919.75 2880.25 2932.00 2840.75 2848.50 2891.50
## [2071] 2923.75 2898.25 2929.25 2922.25 2855.50 2883.25 2865.50 2889.75 2926.75
## [2080] 2924.75 2906.00 2938.50 2972.00 2980.75 2978.25 2978.50 3002.00 3011.75
## [2089] 3006.50 2999.00 3005.50 3006.25 3006.00 3013.61 2997.00 2970.25 2986.25
## [2098] 2980.50 2963.75 2978.50 2937.75 2880.50 2911.75 2951.00 2937.50 2892.50
## [2107] 2919.00 2941.00 2970.75 2965.50 2997.75 2991.50 2998.00 2988.25 3006.50
## [2116] 2994.50 3005.75 3004.25 3020.25 3036.25 3035.75 3047.75 3035.75 3063.25
## [2125] 3075.75 3072.00 3075.50 3086.00 3090.50 3087.50 3092.00 3095.50 3097.00
## [2134] 3118.25 3121.75 3118.50 3109.00 3104.00 3111.50 3132.75 3143.75 3153.75
## [2143] 3114.25 3091.00 3111.00 3117.75 3146.00 3134.50 3136.00 3143.00 3168.00
## [2152] 3172.00 3194.25 3192.00 3194.75 3207.00 3231.02 3227.25 3244.50 3237.50
## [2161] 3223.50 3231.00 3259.00 3235.50 3243.50 3235.25 3260.25 3276.00 3264.75
## [2170] 3289.75 3288.00 3293.75 3316.50 3325.00 3319.50 3319.75 3326.00 3293.50
## [2179] 3239.50 3278.25 3272.50 3289.75 3224.00 3245.50 3299.50 3335.00 3345.25
## [2188] 3325.50 3353.00 3357.50 3380.50 3377.50 3381.00 3369.25 3387.25 3369.25
## [2197] 3339.25 3226.25 3132.50 3110.25 2957.00 2951.00 3065.00 2997.00 3114.75
## [2206] 3015.50 2964.00 2747.75 2865.75 2740.25 2469.00 2696.00 2416.25 2495.50
## [2215] 2414.00 2403.25 2437.98 2220.50 2438.00 2467.00 2608.00 2524.00 2611.25
## [2224] 2569.75 2448.00 2516.50 2482.75 2644.50 2642.00 2735.00 2779.75 2759.25
## [2233] 2843.00 2775.00 2787.50 2870.00 2806.50 2732.00 2788.50 2780.75 2829.50
## [2242] 2869.00 2867.25 2941.00 2902.50 2821.75 2825.25 2858.25 2833.50 2880.00
## [2251] 2928.50 2922.75 2852.50 2813.00 2847.00 2846.50 2948.00 2918.75 2968.50
## [2260] 2937.00 2953.00 2994.50 3035.50 3038.00 3042.00 3054.00 3077.00 3117.75
## [2269] 3110.50 3186.75 3227.50 3205.50 3186.00 3010.25 3034.75 3073.00 3128.75
## [2278] 3118.00 3109.00 3161.26 3110.75 3118.50 3049.00 3070.75 3007.00 3047.75
## [2287] 3090.25 3103.00 3129.00 3172.00 3136.50 3163.50 3141.00 3178.50 3148.25
## [2296] 3183.50 3219.50 3194.50 3214.00 3245.25 3251.25 3265.50 3227.50 3204.00
## [2305] 3232.25 3213.00 3252.50 3248.75 3263.50 3288.50 3300.00 3316.00 3344.25
## [2314] 3344.75 3352.75 3330.00 3370.00 3367.75 3361.50 3379.75 3387.00 3372.75
## [2323] 3380.75 3392.50 3427.50 3443.00 3480.25 3485.25 3504.50 3499.00 3527.00
## [2332] 3579.25 3461.50 3417.50 3335.50 3400.25 3340.50 3333.75 3382.50 3405.25
## [2341] 3389.50 3361.50 3353.60 3275.00 3299.25 3231.25 3238.00 3287.25 3346.00
## [2350] 3333.75 3352.00 3367.75 3339.25 3393.00 3353.25 3406.75 3437.50 3473.25
## [2359] 3532.75 3504.75 3481.00 3475.50 3462.25 3422.75 3432.25 3432.50 3449.25
## [2368] 3451.75 3393.50 3383.00 3263.50 3302.25 3264.75 3300.50 3361.50 3435.00
## [2377] 3504.75 3500.75 3544.00 3541.00 3568.00 3532.50 3582.00 3623.00 3606.75
## [2386] 3565.00 3580.00 3554.25 3576.00 3632.75 3627.25 3623.25 3660.50 3667.25
## [2395] 3664.50 3698.00 3690.75 3702.00 3672.50 3668.50 3661.25 3646.50 3694.50
## [2404] 3700.75 3721.25 3726.25 3685.75 3677.25 3681.50 3727.50 3720.00 3724.25
## [2413] 3748.75 3692.25 3718.25 3740.50 3795.50 3817.50 3792.00 3794.50 3803.75
## [2422] 3791.25 3762.25 3790.50 3845.00 3846.00 3834.25 3848.50 3842.50 3744.25
## [2431] 3779.25 3705.25 3765.75 3818.25 3823.50 3864.50 3880.25 3908.00 3905.50
## [2440] 3903.00 3912.00 3931.00 3927.75 3928.00 3909.50 3903.00 3873.50 3878.00
## [2449] 3922.50 3828.00 3809.25 3898.75 3867.50 3816.75 3765.50 3839.00 3819.25
## [2458] 3873.25 3896.50 3936.75 3942.25 3967.50 3962.50 3974.00 3916.50 3912.62
## [2467] 3930.00 3899.75 3880.75 3900.50 3964.75 3959.00 3947.75 3967.50 4010.00
## [2476] 4067.75 4064.00 4070.00 4089.00 4119.50 4120.25 4132.75 4118.00 4162.50
## [2485] 4176.25 4155.50 4126.50 4164.75 4127.75 4171.50 4179.50 4179.00 4176.25
## [2494] 4203.50 4174.50
diffed = diff(as.numeric(Series, differences = 1))
head(diffed)
## [1] -5.75 -9.00 -8.00 -0.50  8.25 11.00
lag_transform <- function(x, k= 1){
  
  lagged =  c(rep(NA, k), x[1:(length(x)-k)])
  DF = as.data.frame(cbind(lagged, x))
  colnames(DF) <- c( paste0('x-', k), 'x')
  DF[is.na(DF)] <- 0
  return(DF)
}
supervised = lag_transform(diffed, 1)
head(supervised)
##     x-1     x
## 1  0.00 -5.75
## 2 -5.75 -9.00
## 3 -9.00 -8.00
## 4 -8.00 -0.50
## 5 -0.50  8.25
## 6  8.25 11.00
#=============================================
# Split dataset into training and testing sets
#=============================================
N = nrow(supervised)
n = round(N *0.7, digits = 0)
train = supervised[1:n, ]
test  = supervised[(n+1):N,  ]

#=================================
# Normalize the data
#=================================
## scale data
scale_data = function(train, test, feature_range = c(0, 1)) {
  x = train
  fr_min = feature_range[1]
  fr_max = feature_range[2]
  std_train = ((x - min(x) ) / (max(x) - min(x)  ))
  std_test  = ((test - min(x) ) / (max(x) - min(x)  ))
  
  scaled_train = std_train *(fr_max -fr_min) + fr_min
  scaled_test = std_test *(fr_max -fr_min) + fr_min
  
  return( list(scaled_train = as.vector(scaled_train), scaled_test = as.vector(scaled_test) ,scaler= c(min =min(x), max = max(x))) )
  
}


Scaled = scale_data(train, test, c(-1, 1))

y_train = Scaled$scaled_train[, 2]
x_train = Scaled$scaled_train[, 1]

y_test = Scaled$scaled_test[, 2]
x_test = Scaled$scaled_test[, 1]

## inverse-transform
invert_scaling = function(scaled, scaler, feature_range = c(0, 1)){
  min = scaler[1]
  max = scaler[2]
  t = length(scaled)
  mins = feature_range[1]
  maxs = feature_range[2]
  inverted_dfs = numeric(t)
  
  for( i in 1:t){
    X = (scaled[i]- mins)/(maxs - mins)
    rawValues = X *(max - min) + min
    inverted_dfs[i] <- rawValues
  }
  return(inverted_dfs)
}

# Modeling
# Reshape the input to 3-dim
dim(x_train) <- c(length(x_train), 1, 1)

# specify required arguments
X_shape2 = dim(x_train)[2]
X_shape3 = dim(x_train)[3]
batch_size = 1                # must be a common factor of both the train and test samples
units = 1                     # can adjust this, in model tuninig phase

#=========================================================================================

model <- keras_model_sequential() 
## Warning: Python 'C:/Users/chant/Documents/.virtualenvs/myenv/Scripts/
## python.exe' was requested but 'C:/Users/chant/AppData/Local/r-miniconda/envs/r-
## reticulate/python.exe' was loaded instead (see reticulate::py_config() for more
## information)
model%>%
  layer_lstm(units, batch_input_shape = c(batch_size, X_shape2, X_shape3), stateful= TRUE)%>%
  layer_dense(units = 1)

# compile the model
model %>% compile(
  loss = 'mean_squared_error',
  optimizer = optimizer_adam( lr = 0.02, decay = 1e-6 ),  
  metrics = c('accuracy')
)
# Model summary
summary(model)
## Model: "sequential"
## ________________________________________________________________________________
## Layer (type)                        Output Shape                    Param #     
## ================================================================================
## lstm (LSTM)                         (1, 1)                          12          
## ________________________________________________________________________________
## dense (Dense)                       (1, 1)                          2           
## ================================================================================
## Total params: 14
## Trainable params: 14
## Non-trainable params: 0
## ________________________________________________________________________________
# Fit the model
Epochs = 50   
for(i in 1:Epochs ){
  model %>% fit(x_train, y_train, epochs=1, batch_size=batch_size, verbose=1, shuffle=FALSE)
  model %>% reset_states()
}

# Make predictions
L = length(x_test)
scaler = Scaled$scaler
predictions = numeric(L)
i = 1
for(i in 1:L){
  X = x_test[i]
  dim(X) = c(1,1,1)
  yhat = model %>% predict(X, batch_size=batch_size)
  # invert scaling
  yhat = invert_scaling(yhat, scaler,  c(-1, 1))
  # invert differencing
  class(yhat)
  yhat  = yhat + as.numeric(Series[(n+i)])
  # store
  predictions[i] <- yhat
}


list1 <- rep(NA,1746)
list2 <- c(list1, predictions)
data_plot1 <- as.data.frame(Series)
data_plot2 <- as.data.frame(list2)
ggplot(data_plot1,aes(x=seq_along(Series), y=Series)) + 
  geom_line(color='#56B4E9') +
  geom_line(data = data_plot2,aes(x=seq_along(list2), y=list2),color='red') +
  theme_grey(base_size = 14) +
  ggtitle("Prediction") +
  labs(x = "time index", y = "stock price")
## Warning: Removed 1746 row(s) containing missing values (geom_path).