The purpose of this code is to:
- create a dummy data set.
- Then turn it into a data frame
- Calculate the peaks and make it a column in the data frame
- Calculate the troughs and make it a column in the data frame
- Filling the “nan” values with “hold”
- Replace all the float values with the word “buy”
The problem is with last step is that it is never worked, but there is no error, it is just print the dataframe just like before this couple of lines.
Here is the code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelextrema
list1 = np.random.randint(0,30,(25,2))
df = pd.DataFrame(list1, columns=['a', 'b'])
df['minimum']= df.b[(df.b.shift(1) > df.b) & (df.b.shift(-1) > df.b)]
df['maximum'] = df.b[(df.b.shift(1) < df.b) & (df.b.shift(-1) < df.b)]
plt.scatter(df.index, df['minimum'], c='g')
plt.scatter(df.index, df['maximum'], c='r')
df.b.plot(figsize=(15,5))
df['minimum'].fillna('hold', inplace = True)
for x in df['minimum']:
if type(x) =='float':
df['minimum'].replace(x, 'buy', inplace = True)
print('df')
if type(x) == np.float: