Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

I used your signal generator code: def signalgenerator(df): Calculate EMA and MA lines…

  1. User

    I used your signal generator code: def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['Close'].ewm(span=5, adjust=False).mean() df['EMA10'] = df['Close'].ewm(span=10, adjust=False).mean() df['EMA20'] = df['Close'].ewm(span=20, adjust=False).mean() df['EMA50'] = df['Close'].ewm(span=50, adjust=False).mean() df['EMA100'] = df['Close'].ewm(span=100, adjust=False).mean() df['EMA200'] = df['Close'].ewm(span=200, adjust=False).mean() df['MA10'] = df['Close'].rolling(window=10).mean() df['MA20'] = df['Close'].rolling(window=20).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean() # Extract necessary prices from df open_price = df.Open.iloc[-1] close_price = df.Close.iloc[-1] previous_open = df.Open.iloc[-2] previous_close = df.Close.iloc[-2]

    # Calculate the last candlestick
    last_candle = df.iloc[-1]
    current_price = df.Close.iloc[-1]
    
    # Initialize analysis variables
    ema_analysis = []
    candle_analysis = []
    
    if (
        df['EMA5'].iloc[-1] > df['EMA10'].iloc[-1] > df['EMA20'].iloc[-1] > df['EMA50'].iloc[-1] > 
        df['EMA100'].iloc[-1] > df['EMA200'].iloc[-1]
    ):
        ema_analysis.append('buy')
    elif (
        df['EMA5'].iloc[-1] < df['EMA10'].iloc[-1] < df['EMA20'].iloc[-1] < df['EMA50'].iloc[-1] < 
        df['EMA100'].iloc[-1] < df['EMA200'].iloc[-1]
    ):
        ema_analysis.append('sell')
    
    if (
        open_price < close_price and
        previous_open > previous_close and
        close_price > previous_open and
        open_price <= previous_close
    ):
        candle_analysis.append('buy')
    elif (
        open_price > close_price and
        previous_open < previous_close and
        close_price < previous_open and
        open_price >= previous_close
    ):
        candle_analysis.append('sell')
    
    stop_loss_price = current_price * (1 - STOP_LOSS_PERCENTAGE / 100)
    
    if (
        'buy' in ema_analysis and
        'buy' in candle_analysis and
        current_price > df['EMA200'].iloc[-1] and
        current_price > stop_loss_price
    ):
        final_signal = 'buy'
    elif (
        'sell' in ema_analysis and
        'sell' in candle_analysis and
        current_price < df['EMA200'].iloc[-1] and
        current_price < stop_loss_price
    ):
        final_signal = 'sell'
    else:
        final_signal = ''
    
    return final_signal
    

    But it deosn't give me any signals

  2. Assistant

    The code you provided is a signal generator function, but it does not include the necessary input parameters to generate signals. Additionally, the code only checks for specific conditions to generate signals, which may not always occur in the provided dataframe. To generate signals using this function, you need to pass a dataframe that includes the required columns (such as 'Open' and 'Close') and contains enough data points for the calculations to be valid.

    Here's an example of how you can use the signal generator function with a sample dataframe:

    import pandas as pd
    
    # Create a sample dataframe
    data = {
        'Close': [10, 12, 15, 14, 13, 12, 11, 10, 9, 8],
        'Open': [9, 11, 14, 13, 12, 11, 10, 9, 8, 7]
    }
    df = pd.DataFrame(data)
    
    # Call the signal generator function
    signal = signal_generator(df)
    
    print(signal)  # Print the generated signal
    

    Make sure to modify the dataframe and input parameters to suit your specific data and requirements.