Skip to content

Instantly share code, notes, and snippets.

@s-tlm
Last active January 8, 2021 04:16
Show Gist options
  • Select an option

  • Save s-tlm/eaaf916d56c01fc2f6f41fdd072019aa to your computer and use it in GitHub Desktop.

Select an option

Save s-tlm/eaaf916d56c01fc2f6f41fdd072019aa to your computer and use it in GitHub Desktop.
Buy/Sell signal generator
buy = pd.DataFrame(index=closing_prices.index, columns=['Buy']) # an empty data-frame to store buy signals
sell = pd.DataFrame(index=closing_prices.index, columns=['Sell']) # an empty data-frame to store sell signals
for i in range(1, len(closing_prices)): # ignores first value of historical data as MACD will be equal to signal line there
if i == 1:
if MACD['EMA'].iloc[i] > signal_line['EMA'].iloc[i]:
high = 'MACD'
else:
high = 'SIGNAL'
elif MACD['EMA'].iloc[i] > signal_line['EMA'].iloc[i]:
if high == 'SIGNAL': # MACD crossed signal - bottom to top BUY
if MACD['EMA'].iloc[i] < 0:
buy.iloc[i] = closing_prices[i] # BUY
high = 'MACD'
elif MACD['EMA'].iloc[i] < signal_line['EMA'].iloc[i]:
if high == 'MACD': # MACD crossed signal - top to bottom SELL
if MACD['EMA'].iloc[i] > 0:
sell.iloc[i] = closing_prices[i] # SELL
high = 'SIGNAL'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment