One can create a column in a DataFrame based on the values of existing columns using np.where(). Lots of parentheses required otherwise type errors.
import numpy as np
import pandas as pd
import talib.abstract as ta
df = pd.read_csv('backtest_data.csv', index_col=0, parse_dates=True)
df['atr'] = ta.ATR(df.high, df.low, df.close, timeperiod=20)
macd = ta.MACD(df)
df['macd'] = macd.macd
df['macd_signal'] = macd.macdsignal
df['macd_hist'] = macd.macdhist
df['hist_prev'] = df['macd_hist'].shift(1)
df['entry'] = np.where(((df['macd_hist'] > 0) & (df['hist_prev'] < 0)), 1, 0)
Also note in the code that instead of referring to a previous days value by accessing the previous row one can use shift(1) to create a column with ‘yesterdays’ value in ‘todays’ row.