I am learning to explore a trading strategies using Pandas. The "Hello World" of algorithmic trading I hear is the Moving Average Cross Over where a shorter rolling mean goes above or below a longer rolling mean to determine long and short positions, respectively.
universe = sorted(os.listdir(f'{os.getcwd()}/Data'))
for u in range(len(universe)):
universe[u] = universe[u][:-4]
df = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
for s in universe:
df[s] = pd.read_csv(f'Data/{s}.csv', index_col='Date')['Adj Close']
df2[s] = df[s].rolling(50).mean()
df3[s] = df[s].rolling(200).mean()
df4[s] = np.where((df2[s] > df3[s]), 1, 0)
for df4, I'm just having a 1 in the true condition of np.where to represent Long. But how would I go about using another condition np.where((df2[s] < df3[s]), -1, 0) for having -1 represent Short? I want 0 to represent no position.
Also, I may have a condition where it only happens once in the data but hold that signal as a "1" or "-1" until another signal registers it as a 0.
So I suppose I don't really to know how to apply If-else conditions within a Pandas DF.
Your help is greatly appreciated, thank you
to obtain sample data:
ticker = ['STT', 'TROW', 'BIIB', 'ADP', 'COG', 'EMR', 'USB', 'UA', 'AXP', 'WYNN']
for t in ticker:
try:
pdr.DataReader(t, data_source='yahoo', start=datetime(2000,1,1), end=datetime(2020,1,1)).to_csv(f'Data/{t}.csv')
except KeyError:
pass