Even after using GPT, I couldn’t quite figure out the exact reason why my code isn’t working as expected.
Here’s what I’m trying to achieve:
For Weekly Signals: My goal is to receive buy and sell signals immediately after the closing value of each Friday. I want these signals to be generated right after the market closes at the end of the week, so I can act on them without any delay.
For Monthly Signals: Similarly, I want to receive buy and sell signals right after the closing value of each month. These signals should be triggered immediately after the market closes on the last trading day of the month.
However, despite my efforts, the code doesn’t seem to be functioning correctly. Instead of getting the signals promptly after the weekly or monthly close, there appears to be a delay. I’m not sure why this is happening, and I haven’t been able to pinpoint the exact issue.
The aim is to have the signals reflect the most recent data as soon as it’s available, but the current implementation is falling short of that.
The code below is for monthly and weekly data
for stock in t2_list:
# Download stock data for both intervals
stock_data_weekly = download_weekly_data(stock, start_date, end_date)
stock_data_weekly['Open'] = stock_data_weekly['Open'].round(2)
stock_data_weekly['Close'] = stock_data_weekly['Close'].round(2)
stock_data_weekly['High'] = stock_data_weekly['High'].round(2)
stock_data_weekly['Low'] = stock_data_weekly['Low'].round(2)
stock_data_monthly = download_monthly_data(stock, start_date, end_date)
stock_data_monthly['Open'] = stock_data_monthly['Open'].round(2)
stock_data_monthly['Close'] = stock_data_monthly['Close'].round(2)
stock_data_monthly['High'] = stock_data_monthly['High'].round(2)
stock_data_monthly['Low'] = stock_data_monthly['Low'].round(2)
print(stock, len(stock_data_weekly), len(stock_data_monthly))
sleep(2)
if len(stock_data_weekly) >= 71:
#stock_data_weekly = calculate_macd(stock_data_weekly)
stock_data_weekly = calculate_macd_dema(stock_data_weekly)
#print(stock_data_weekly.iloc[:, -8:])
stock_data_weekly = check_crossovers(stock_data_weekly)
# tradingview_data_weekly = format_tradingview_output(stock_data_weekly)
for i, r in stock_data_weekly.iterrows():
sent_date = i + timedelta(days = 6)
date_str = sent_date.strftime('%Y-%m-%d') # Adjust format as needed
previous_friday = datetime.now() - timedelta(days = dates_diff[datetime.now().weekday()] + 1 )
pre_previous_friday =previous_friday - timedelta(days = 7 )
if r['Crossover'] != 'Neutral' and i>pre_previous_friday and i<previous_friday:
#pass
print('weekly', date_str, stock, r['Crossover'])
webhook_discord(webhook_url_weekly, date_str, r['Crossover'], stock, r['Crosspoint'])
sleep(5)
if len(stock_data_monthly) >= 71:
stock_data_monthly = calculate_macd_dema(stock_data_monthly)
stock_data_monthly = check_crossovers(stock_data_monthly)
for i , r in stock_data_monthly.iterrows():
stock_month = i.month
curr_month = datetime.now().month
if curr_month == 1:
acc_month = 12
else:
acc_month = curr_month-1
sent_date = i + timedelta(days = 33)
date_str = sent_date.strftime('%Y-%m') # Adjust format as needed
if r['Crossover'] != 'Neutral' and stock_month==acc_month:
print('monthly', date_str, stock, r['Crossover'])
webhook_discord(webhook_url_monthly, date_str, r['Crossover'], stock, r['Crosspoint'])
#pass
sleep(5)
if __name__ == "__main__":
main()
I hired a python developer but he couldn’t figure it out also tried gpt but couldn’t find the proper solution.
The code below is for MAcd dema.
def calculate_macd_dema(df, short_window=12, long_window=26, signal_window=9):
# Calculate Short-term DEMA
def ema(series, span):
ta_ema = ta.ema(series, span)
return ta_ema
def dema(series, span):
ema1 = ema(series, span)
ema2 = ema(ema1, span)
return 2 * ema1 - ema2
# Parameters
sma = 12
lma = 26
tsp = 9
# Calculate DEMAs
df['DEMAfast'] = dema(df['Close'], sma)
df['DEMAslow'] = dema(df['Close'], lma)
# Calculate MACD Line and Signal Line
df['MACD_DEMA'] = df['DEMAfast'] - df['DEMAslow']
#print(df['MACD_DEMA'])
df['Signal_Line'] = dema(df['MACD_DEMA'], tsp)
df['MACDZeroLag'] = df['MACD_DEMA'] - df['Signal_Line']
return df
# Check for MACD and Signal Line crossovers
def check_crossovers(df):
# crossovers = []
# for i in range(1, len(df)):
# if df['MACD_DEMA'].iloc[i] > df['Signal_Line'].iloc[i] and df['MACD_DEMA'].iloc[i - 1] <= df['Signal_Line'].iloc[i - 1]:
# crosspoint = df['Close'].iloc[i]
# crossovers.append((df.index[i], "Buy", crosspoint))
# elif df['MACD_DEMA'].iloc[i] < df['Signal_Line'].iloc[i] and df['MACD_DEMA'].iloc[i - 1] >= df['Signal_Line'].iloc[i - 1]:
# crosspoint = df['Close'].iloc[i]
# crossovers.append((df.index[i], "Sell", crosspoint))
# else:
# crossovers.append((df.index[i], "Neutral", None)) # None when there's no crosspoint
df_macd_dema_1 = df['MACD_DEMA']
df_macd_dema_2 = df['MACD_DEMA'].shift(1)
df_signal_1 = df['Signal_Line']
df_signal_2 = df['Signal_Line'].shift(1)
df['Crossover'] = np.where((df_macd_dema_1 > df_signal_1) & (df_macd_dema_2 <= df_signal_2), 'Buy',
np.where((df_macd_dema_1 < df_signal_1) & (df_macd_dema_2 >= df_signal_2), 'Sell', 'Neutral'))
df['Crosspoint'] = np.where( (df['Crossover']=='Buy') | (df['Crossover']=='Sell'), df['Close'], None)
return df
Overall i guess the key point is in the upper section that i am missing.
2