from nsepy import get_history
import datetime as date
import pdb
import talib
watchlist = ['ACC', 'VEDL']
for name in watchlist:
df = get_history(symbol=name, start=date(2020,1,1), end=date.today())
if df.empty:
continue
df['MA50'] = talib.MA(df['Close'], timeperiod=50, matype=0)
df['MA200'] = talib.MA(df['Close'], timeperiod=200, matype=0)
MA50 = df.iloc[-1]['MA50']
MA200 = df.iloc[-1]['MA200']
if (MA50 > MA200):
print(name, 'BUY')
elif (MA50 < MA200):
Print(name, 'SELL')
else:
print(name, 'IGNORE')
1 Answer
Regarding:
import datetime as date
This imports the datetime module, renaming it date in the current context.
However, that module has a date class inside it, with a constructor for a date, which is probably what you wanted. Ditto for the today() member function.
Both of these are part of the datetime.date class, not part of the datetime module (renamed as date).
It's likely you want to do this instead:
from datetime import date
This will bring the date class into you current context, rather than the datetime module renamed as date.
import datetime as dt. Its also popular to just import the classes you want, likefrom datetime import date(that's how the module's doc does it).