hope you are well?
So I have recently started trying to experiment with trading algorithms. I am trying to create an ETF rotation strategy. I am struggling with how to compare the returns between the etfs? (Error at the bottom) Am I making a simple mistake? Can you give me any pointers on what I need to do in order to rank them as I don't particularly want to write hundreds of if statements, is there any shortcut?
Thanks for any advice! (This isn't the complete code)
import pandas as pd
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
yf.pdr_override()
now = dt.datetime.now()
start = dt.datetime(2018,1,1)
emasUsed = [60]
etfs = ['FDN','IBB','IEZ','IGV','IHE','IHF','IHI','ITA','ITB','IYJ','IYT','IYW','IYZ'
,'KBE','KCE','KIE','PBJ','PBS','SMH','VNQ','XLB','XLP','XLU','XOP','XRT']
monthly_return = []
top_etfs = [None,None,None,None,None,None]
total = 0
##for etf in etfs:
## df = pdr.get_data_yahoo(etf, start, now)
## monthly_returns = df['Adj Close'].resample('M').ffill().pct_change()
## #print("ETF: ",etf," Monthly Returns: ",monthly_returns)
## monthly_return.append(str(monthly_returns))
## for i in range(30):
## daily_returns = df['Adj Close'].resample('D').ffill().pct_change()
## cumulative_returns = (daily_returns + 1).comprod()
## print(cumulative_returns)
df = pdr.get_data_yahoo(etfs, start, now)
daily_return = (df['Adj Close'].pct_change())
monthly_return = (df['Adj Close'].resample('M').ffill().pct_change())
print(monthly_return)
print(daily_return)
for i in df.index:
print(daily_return['FDN'])
if daily_return['FDN'] > daily_return['IBB']:
print("FDN is better than IBB")
else:
print("IBB is better than FDN")
if daily_return['FDN'] > daily_return['IBB']: File "C:\Python\Python37\lib\site-packages\pandas\core\generic.py", line 1479, in nonzero f"The truth value of a {type(self).name} is ambiguous. " ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().