def find_trend(data, period: int):
'''
Inputs: dataframe and an interger
Outputs:
returns True if the trend of the simple moving average over given period is positive, else returns False
'''
sma = data['Close'].rolling(period).mean() # creates a series with the rolling mean
diff = sma - sma.shift(1) # calculates a series of values
greater_than_0 = diff > 0 # creates a series of bools
return diff, greater_than_0
aapl['value'], aapl['trend'] = find_trend(aapl, 4)
# display(aapl)
Date High Low Open Close Volume Adj Close ticker value trend
0 2019-01-31 169.000000 164.559998 166.110001 166.440002 40739600.0 162.852737 aapl NaN False
1 2019-02-01 168.979996 165.929993 166.960007 166.520004 32668100.0 162.931030 aapl NaN False
2 2019-02-04 171.660004 167.279999 167.410004 171.250000 31495500.0 167.559082 aapl NaN False
3 2019-02-05 175.080002 172.350006 172.860001 174.179993 36101600.0 170.425934 aapl NaN False
4 2019-02-06 175.570007 172.850006 174.649994 174.240005 28239600.0 170.484650 aapl 1.950001 True
5 2019-02-07 173.940002 170.339996 172.399994 170.940002 31741700.0 167.255768 aapl 1.105000 True
6 2019-02-08 170.660004 168.419998 168.990005 170.410004 23820000.0 167.452316 aapl -0.209999 False
7 2019-02-11 171.210007 169.250000 171.050003 169.429993 20993400.0 166.489288 aapl -1.187500 False
8 2019-02-12 171.000000 169.699997 170.100006 170.889999 22283500.0 167.923965 aapl -0.837502 False