diff --git a/finportbotutil/syminfo.py b/finportbotutil/syminfo.py index 6229977..a2b084a 100644 --- a/finportbotutil/syminfo.py +++ b/finportbotutil/syminfo.py @@ -80,3 +80,17 @@ async def get_ma_plots_info(symbol, startdate, enddate, dayswindow, api_url, tit response = requests.request("GET", api_url, headers=headers, data=payload) return json.loads(response.text) + + +async def fit_lppl(symbol, startdate, enddate, api_url): + payload = json.dumps({ + 'symbol': symbol, + 'startdate': startdate, + 'enddate': enddate + }) + headers = { + 'Content-Type': 'application/json' + } + + response = requests.request("GET", api_url, headers=headers, data=payload) + return json.loads(response.text) diff --git a/main.py b/main.py index e37377e..ac4d615 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import re import os import logging -from datetime import date, datetime +from datetime import date, datetime, timedelta from dateutil.relativedelta import relativedelta import asyncio import urllib @@ -17,7 +17,7 @@ import boto3 from finportbotutil.tipcalc import calculate_tips -from finportbotutil.syminfo import get_symbol_inference, get_symbols_correlation, get_plots_infos, search_symbols, get_ma_plots_info +from finportbotutil.syminfo import get_symbol_inference, get_symbols_correlation, get_plots_infos, search_symbols, get_ma_plots_info, fit_lppl logging.basicConfig(level=logging.INFO) @@ -40,7 +40,7 @@ # Search API search_api_url = os.getenv('SEARCH') -modelloadretry = int(os.getenv('MODELLOADRETRY', 5)) +modelloadretry = int(os.getenv('SEARCHMODELLOADRETRY', 5)) # Add or Modify User ARN addmodifyuser_arn = os.environ.get('ADDUSERARN') @@ -48,6 +48,9 @@ # Symbol Search Wrapper ARN searchwrappwer_arn = os.environ.get('SEARCHWRAPPERARN') +# fit LPPL model URL +fit_lppl_url = os.environ.get('FITLPPL') + # commands CMD_START = ['start'] CMD_HELP = ['help'] @@ -64,6 +67,7 @@ CMD_NASDAQ_MA = ['nasdaqma'] CMD_DJI_MA = ['djima'] CMD_MAPLOT = ['maplot'] +CMD_FITLPPL = ['predictcrash'] # polling flag @@ -483,6 +487,31 @@ def handle_maplot_callback_query(call): logging.error('Unknown error!') print('Unknown error!', file=sys.stderr) +@bot.message_handler(commands=CMD_FITLPPL) +def fit_lppl_bubble_burst(message): + logging.info(message) + print(message) + + stringlists = re.sub('\s+', ' ', message.text).split(' ')[1:] + symbol = '^GSPC' if len(stringlists) < 1 else stringlists[0] + startdate = (datetime.today() - timedelta(days=365)).strftime('%Y-%m-%d') if len(stringlists) < 2 else stringlists[1] + enddate = datetime.today().strftime('%Y-%m-%d') if len(stringlists) < 3 else stringlists[2] + + results = asyncio.run(fit_lppl(symbol, startdate, enddate, fit_lppl_url)) + logging.info(results) + print(results) + + if 'message' in results.keys() and results['message'] == 'Internal server error': + message_text = 'API internal error.' + bot.reply_to(message, message_text) + return {'message': message_text} + else: + message_text = open(os.path.join('messagetemplates', 'crash.txt')).read().format( + crashdate=results['estimated_crash_date'] + ) + bot.reply_to(message, message_text) + return {'message': message_text,} + def lambda_handler(event, context): message = json.loads(event['body']) diff --git a/messagetemplates/crash.txt b/messagetemplates/crash.txt new file mode 100644 index 0000000..39149b1 --- /dev/null +++ b/messagetemplates/crash.txt @@ -0,0 +1,3 @@ +From the model and the information given, the projected crash date is {crashdate}. + +(Disclaimer: prediction can change due to stochasticity and other events. The estimation is by no means accurate.) \ No newline at end of file