Skip to content

Commit

Permalink
Merge pull request #4 from stephenhky/develop
Browse files Browse the repository at this point in the history
deployed LPPL bubble crash prediction
  • Loading branch information
stephenhky authored Dec 15, 2023
2 parents 32c9a05 + 0a786ed commit 41c48f7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
14 changes: 14 additions & 0 deletions finportbotutil/syminfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 32 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -40,14 +40,17 @@

# 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')

# 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']
Expand All @@ -64,6 +67,7 @@
CMD_NASDAQ_MA = ['nasdaqma']
CMD_DJI_MA = ['djima']
CMD_MAPLOT = ['maplot']
CMD_FITLPPL = ['predictcrash']


# polling flag
Expand Down Expand Up @@ -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'])
Expand Down
3 changes: 3 additions & 0 deletions messagetemplates/crash.txt
Original file line number Diff line number Diff line change
@@ -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.)

0 comments on commit 41c48f7

Please sign in to comment.