Files
daytrade/dev.py

70 lines
2.4 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.

from datetime import datetime, timedelta
import talib
from loguru import logger
from gdrive.repository import upload_to_drive
from gsheets import repository as gsheets
from providers.provider import FinDataRetriever
from providers.sources.yfin import YFinanceProvider
from charting.indicators import generate_macd_with_rsi_chart
from gappsscript.repository import update_apps_script
ticker = "BTC-USD"
retriever = FinDataRetriever(provider=YFinanceProvider(), ticker=ticker)
ss_system = gsheets.get_sheet(sheet_name="system_crypto")
_, 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 = "/Users/tcudikel/Dev/worldmodel/daytrade/daytrade/localdata/charts/chart.html"
function_path = "/Users/tcudikel/Dev/worldmodel/daytrade/daytrade/localdata/gappsscript/show_macdrsi_chart.js"
generate_macd_with_rsi_chart(data=df, filename=chart_path)
file_id = upload_to_drive('charts.html', chart_path)
update_apps_script(function_path=function_path, file_id=file_id)
print("ok")
# update: write to sheets
"""
for indicator, columns_indic in zip(indicators, columns_indics):
gsheets.update_historical_indicator(sheet=ss_system, indicator=indicator, data=df[columns_indic])
"""
## web-based charting
"""
df.reset_index(inplace=True)
generate_macd_with_obv_chart(data=df)
"""
print()