Files
daytrade/update_chart_macdrsi.py
ALIHAN DIKEL d1b8ffd82d fast commit
2024-09-22 15:18:48 +03:00

57 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from datetime import datetime, timedelta
import talib
from loguru import logger
from config import envvar
from charting.indicators import generate_macd_with_rsi_chart
from gappsscript import repository as gappsscript
from gdrive import repository as gdrive
from gsheets import repository as gsheets
from providers.provider import FinDataRetriever
from providers.sources.yfin import YFinanceProvider
logger.info("starting flow: update chart macdrsi")
ticker = "BTC-USD"
retriever = FinDataRetriever(provider=YFinanceProvider(), ticker=ticker)
ss_system = gsheets.get_sheet(sheet_name="system_crypto")
gsheets.update_date_column(sheet=ss_system, num_days=int(envvar.HISTORICAL_MAX_DATE))
_, date_range = gsheets.get_date_range(sheet=ss_system)
cell_start = ss_system.acell(date_range[1])
cell_end = ss_system.acell(date_range[0])
data_diff = cell_start.row - cell_end.row
end_date = datetime.strptime(cell_end.value, '%Y-%m-%d')
## DIRTYFIX:
# historical ile current arası boşluk, 1 gün daha geriden indic date başlatıyor
end_date += timedelta(days=1)
##
start_date = end_date - timedelta(days=data_diff)
df = retriever.get_historical(start=start_date, end=end_date)
df.drop(columns=['Dividends', 'Stock Splits'], inplace=True)
## to technical anlaysis calculations here
df['MACD'], df['MACD_Signal'], df['MACD_Hist'] = talib.MACD(df['Close'])
df['RSI'] = talib.RSI(df['Close'], timeperiod=14)
#df['OBV'] = talib.OBV(df['Close'], df['Volume'])
####
# parse: Get only technical indicator related column names, convert them to lowercase, and concatenate the prefix 'btcusd_'
ticker_header = retriever.provider.convert_ticker_symbol_to_gsheets_header(ticker)
columns_indics = ["MACD", "MACD_Signal", "MACD_Hist", "RSI"]
indicators = [f'{ticker_header}_{col.lower()}' for col in df.columns if col in columns_indics]
## html-based charting on gsheets
df.reset_index(inplace=True)
#generate_macd_with_rsi_chart(data=df)
chart_path = f"{envvar.PATH_LOCALDATA}/charts/chart.html"
function_path = f"{envvar.PATH_LOCALDATA}/gappsscript/show_macdrsi_chart.js"
generate_macd_with_rsi_chart(data=df, filename=chart_path)
file_id = gdrive.upload_to_drive('charts.html', chart_path)
gappsscript.update_apps_script(function_path=function_path, file_id=file_id)
#print("ok")