-2

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().

  • 1
    Your question isn't "how to compare and rank ETFs in Python", it's "how to deduce the truth value of a series in python". **Reading** the error that's being thrown is an essential step in programming. – Pranav Hosangadi Jul 31 '20 at 14:57
  • 1
    Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Pranav Hosangadi Jul 31 '20 at 14:57
  • What is an ouput of print(daily_return['FDN'])? – Yuri Ginsburg Jul 31 '20 at 18:49
  • @YuriGinsburg , the output should be something like "2018-1-1 0.012345" – Ben Townsend Aug 01 '20 at 17:37

1 Answers1

0

The error happens because you trying to numerically compare series themselves instead of series members. Note also that the very first data point is [2018-01-01 00:00:00 NaN]

So in your code it should be

for i in df.index[1:]:
    if daily_return['FDN'][i] > daily_return['IBB'][i]:
       ...
Yuri Ginsburg
  • 2,302
  • 2
  • 13
  • 16