The following dataframe called df_merged
, is a snippet of a larger dataframe with 30+ commoditites like oil, gold, silver, etc.
Date CRUDE_OIL CRUDE_OIL_SMA200 GOLD GOLD_SMA200 SILVER SILVER_SMA200
0 2021-04-26 61.91 48.415 1779.199951 1853.216498 26.211 25.269005
1 2021-04-27 62.939999 48.5252 1778 1853.028998 26.412001 25.30566
2 2021-04-28 63.860001 48.6464 1773.199951 1852.898998 26.080999 25.341655
3 2021-04-29 65.010002 48.7687 1768.099976 1852.748498 26.052999 25.377005
4 2021-04-30 63.580002 48.8861 1767.300049 1852.529998 25.853001 25.407725
How can I implement a way to compare the regular commodity price with the SMA200 equivalent in an IF statement?
My current setup includes the below if statement for 30+ columns but I believe this can be done in a function.
comm_name = []
comm_averages = []
if (df_merged.CRUDE_OIL.tail(1) > df_merged.CRUDE_OIL_SMA200.tail(1)).any():
print("CRUDE_OIL above 200SMA")
comm_name.append("CRUDE_OIL")
comm_averages.append(1)
else:
print("CRUDE_OIL under 200SMA")
comm_name.append("CRUDE_OIL")
comm_averages.append(0)
if (df_merged.GOLD.tail(1) > df_merged.GOLD_SMA200.tail(1)).any():
print("GOLD above 200SMA")
comm_name.append("GOLD")
comm_averages.append(1)
else:
print("GOLD under 200SMA")
comm_name.append("GOLD")
comm_averages.append(0)
if (df_merged.SILVER.tail(1) > df_merged.SILVER_SMA200.tail(1)).any():
print("SILVER above 200SMA")
comm_name.append("SILVER")
comm_averages.append(1)
else:
print("SILVER under 200SMA")
comm_name.append("SILVER")
comm_averages.append(0)
comm_signals = pd.DataFrame(
{'Name': comm_name,
'Signal': comm_averages
})
comm_signals
output of comm_signals:
Name Signal
0 CRUDE_OIL 1
1 GOLD 0
2 SILVER 1
I looked through this SO thread but couldn't figure out how to implement: Find column whose name contains a specific string
I guess the goal is a function like this:
comm_name = []
comm_averages = []
def comm_compare(df):
if (df_merged["X"].tail(1) > df_merged["X" + "_SMA200"].tail(1)).any():
print(X + "above 200SMA")
comm_name.append(X)
comm_averages.append(1)
else:
print(X + "under 200SMA")
comm_name.append(X)
comm_averages.append(0)
comm_signals = pd.DataFrame(
{'Name': comm_name,
'Signal': comm_averages
})
print(comm_signals)
Name Signal
0 CRUDE_OIL 1
1 GOLD 0
2 SILVER 1