In [46]:

```
# install yahoo finance
# !pip3 install yfinance
```

In [47]:

```
import datetime
import pandas as pd
import matplotlib.pyplot as plt
# yahoo API
import yfinance as yf
```

In [48]:

```
ticker = 'FB' # META Platforms
start_time = datetime.datetime(2017, 10, 1)
#end_time = datetime.datetime(2019, 1, 20)
end_time = datetime.datetime.now().date().isoformat() # today
```

In [49]:

```
df = yf.download(ticker, start=start_time, end=end_time)
# use numerical integer index instead of date
df = df.reset_index()
```

[*********************100%***********************] 1 of 1 completed

Let's just plot the regular stock time series data so we have an ide what we are working with.

In [50]:

```
plt.figure(figsize=(15,7))
plt.title('Price chart (Adj Close) ' + ticker)
plt.plot(df['Adj Close'])
plt.show()
```

In [51]:

```
df.head(3)
```

Out[51]:

Date | Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|---|

0 | 2017-10-02 | 171.389999 | 171.869995 | 168.750000 | 169.470001 | 169.470001 | 13392300 |

1 | 2017-10-03 | 169.300003 | 170.729996 | 169.199997 | 169.960007 | 169.960007 | 8190300 |

2 | 2017-10-04 | 169.830002 | 170.669998 | 168.289993 | 168.419998 | 168.419998 | 12127900 |

We will create new time shifted column with Close price in the same dataframe.

In [52]:

```
time_shift = 100 # number of datapoints that will be shifted
```

In [53]:

```
df['Shifted Close'] = df['Close'].shift(time_shift) # move this by n time datapoints
```

In [54]:

```
df.head(3) # quickly check the extended dataframe
```

Out[54]:

Date | Open | High | Low | Close | Adj Close | Volume | Shifted Close | |
---|---|---|---|---|---|---|---|---|

0 | 2017-10-02 | 171.389999 | 171.869995 | 168.750000 | 169.470001 | 169.470001 | 13392300 | NaN |

1 | 2017-10-03 | 169.300003 | 170.729996 | 169.199997 | 169.960007 | 169.960007 | 8190300 | NaN |

2 | 2017-10-04 | 169.830002 | 170.669998 | 168.289993 | 168.419998 | 168.419998 | 12127900 | NaN |

Visualize both regular and time shifted time series for whole historical date range.

In [55]:

```
plt.figure(figsize=(15,7))
plt.title('Price chart (Close) ' + ticker)
plt.plot(df['Date'], df['Close'], label="non-shifted Close", alpha=0.99, linewidth=1) # last "day/period" for regular price,
plt.plot(df['Date'], df['Shifted Close'], label="shifted Close", alpha=0.5, linewidth=0.5) # just limits shifted lagging price ahead
# for the shifted time series, the time axis is the same, so we will keep it the same as the original time series
# and since the time shifted series is "one period" ahead, then we have to plot whole period before our period
plt.legend(loc='upper left')
plt.show()
```

In [56]:

```
plt.figure(figsize=(15,7))
plt.title('Price chart (Close) ' + ticker)
plt.plot(df['Date'][-time_shift:],
df['Close'][-time_shift:],
label="non-shifted Close",
alpha=0.99, linewidth=1) # last "day/period" for regular price
plt.plot(df['Date'][-time_shift:],
df['Shifted Close'][-time_shift:],
label="shifted Close",
alpha=0.5, linewidth=0.5) # limits shifted lagging price ahead
# for the shifted time series, the time axis is the same, so we will keep it the same as the original time series
# and since the time shifted series is one period ahead, then we have to plot whole period before our period
plt.legend(loc='upper left')
plt.show()
```

From the above plot we can clearly see trend differences for the past period/study time interval.

In [56]:

```
```

In [56]:

```
```