Search code examples
pythonpandasdataframeseriestrading

How to store the minimum value of three different Series in a new Series?


here you are the code:

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

ticker = "EURUSD=X"
df = yf.download(ticker, start='2021-1-1', interval='1h')

df['MA30'] = df.Close.rolling(30).mean()
df['MA60'] = df.Close.rolling(60).mean()
df['MA120'] = df.Close.rolling(120).mean()
df['ADX'] = ta.trend.ADXIndicator(df.High, df.Low, df.Close, window=14).adx()
df = df.dropna()

How can I get the minimum (of every row) among MA30, MA60 and MA120 and store it in a new column which I may call "ma_min"? Thanks


Solution

  • import yfinance as yf
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    pd.set_option('display.max_rows', 500)
    pd.set_option('display.max_columns', 500)
    pd.set_option('display.width', 150)
    
    ticker = "EURUSD=X"
    df = yf.download(ticker, start='2021-1-1',interval='1h')
    df['MA30'] = df['Close'].rolling(window=30).mean()
    df['MA60'] = df['Close'].rolling(window=60).mean()
    df['MA120'] = df['Close'].rolling(window=120).mean()
    
    
    df['ma_min'] = df[['MA30','MA60', 'MA120']].min(axis=1)
    print(df.head(400))