diff --git a/.gitignore b/.gitignore index 2e89ef7..3aeec93 100755 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ app/*.sh app/.env* app/ml_models/weights app/ml_models/training_data +app/ml_models/__pycache__ app/json/* app/logs/* fastify/node_modules diff --git a/app/cron_quote.py b/app/cron_quote.py index dc7159f..738bdca 100755 --- a/app/cron_quote.py +++ b/app/cron_quote.py @@ -43,6 +43,16 @@ async def get_pre_post_quote_of_stocks(ticker_list): else: return {} +async def get_bid_ask_quote_of_stocks(ticker_list): + ticker_str = ','.join(ticker_list) + async with aiohttp.ClientSession() as session: + url = f"https://financialmodelingprep.com/api/v4/batch-pre-post-market/{ticker_str}?apikey={api_key}" + async with session.get(url) as response: + if response.status == 200: + return await response.json() + else: + return {} + async def save_quote_as_json(symbol, data): with open(f"json/quote/{symbol}.json", 'w') as file: ujson.dump(data, file) @@ -58,6 +68,25 @@ async def save_pre_post_quote_as_json(symbol, data): except Exception as e: pass +async def save_bid_ask_as_json(symbol, data): + try: + # Read previous close price and load existing quote data + with open(f"json/quote/{symbol}.json", 'r') as file: + quote_data = ujson.load(file) + + # Update quote data with new price, ask, bid, changesPercentage, and timestamp + quote_data.update({ + 'ask': round(data['ask'], 2), # Add ask price + 'bid': round(data['bid'], 2), # Add bid price + }) + + # Save the updated quote data back to the same JSON file + with open(f"json/quote/{symbol}.json", 'w') as file: + ujson.dump(quote_data, file) + except Exception as e: + print(f"An error occurred: {e}") # Print the error for debugging + + async def run(): con = sqlite3.connect('stocks.db') etf_con = sqlite3.connect('etf.db') @@ -65,7 +94,7 @@ async def run(): cursor = con.cursor() cursor.execute("PRAGMA journal_mode = wal") - cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol != ?", ('%5EGSPC',)) + cursor.execute("SELECT DISTINCT symbol FROM stocks") stocks_symbols = [row[0] for row in cursor.fetchall()] etf_cursor = etf_con.cursor() @@ -100,7 +129,7 @@ async def run(): total_symbols = stocks_symbols+etf_symbols - chunk_size = len(total_symbols) // 10 # Divide the list into 10 chunks + chunk_size = len(total_symbols) // 20 # Divide the list into N chunks chunks = [total_symbols[i:i + chunk_size] for i in range(0, len(total_symbols), chunk_size)] delete_files_in_directory("json/pre-post-quote") for chunk in chunks: @@ -117,7 +146,11 @@ async def run(): symbol = item['symbol'] await save_pre_post_quote_as_json(symbol, item) #print(f"Saved data for {symbol}.") - + #Always true + bid_ask_quote = await get_bid_ask_quote_of_stocks(chunk) + for item in bid_ask_quote: + symbol = item['symbol'] + await save_bid_ask_as_json(symbol, item) try: asyncio.run(run()) diff --git a/app/ml_models/__pycache__/backtesting.cpython-310.pyc b/app/ml_models/__pycache__/backtesting.cpython-310.pyc deleted file mode 100755 index 71814c6..0000000 Binary files a/app/ml_models/__pycache__/backtesting.cpython-310.pyc and /dev/null differ diff --git a/app/ml_models/__pycache__/classification.cpython-310.pyc b/app/ml_models/__pycache__/classification.cpython-310.pyc deleted file mode 100644 index 3edd680..0000000 Binary files a/app/ml_models/__pycache__/classification.cpython-310.pyc and /dev/null differ diff --git a/app/ml_models/__pycache__/fundamental_predictor.cpython-310.pyc b/app/ml_models/__pycache__/fundamental_predictor.cpython-310.pyc deleted file mode 100644 index ab98be1..0000000 Binary files a/app/ml_models/__pycache__/fundamental_predictor.cpython-310.pyc and /dev/null differ diff --git a/app/ml_models/__pycache__/prophet.cpython-310.pyc b/app/ml_models/__pycache__/prophet.cpython-310.pyc deleted file mode 100755 index 5b12a2a..0000000 Binary files a/app/ml_models/__pycache__/prophet.cpython-310.pyc and /dev/null differ diff --git a/app/ml_models/__pycache__/prophet_model.cpython-310.pyc b/app/ml_models/__pycache__/prophet_model.cpython-310.pyc deleted file mode 100644 index 5af84d4..0000000 Binary files a/app/ml_models/__pycache__/prophet_model.cpython-310.pyc and /dev/null differ diff --git a/app/ml_models/__pycache__/score_model.cpython-310.pyc b/app/ml_models/__pycache__/score_model.cpython-310.pyc deleted file mode 100644 index 4bca0ec..0000000 Binary files a/app/ml_models/__pycache__/score_model.cpython-310.pyc and /dev/null differ diff --git a/fastify/app.js b/fastify/app.js index e98a2bc..6f553d7 100755 --- a/fastify/app.js +++ b/fastify/app.js @@ -61,7 +61,6 @@ const corsMiddleware = (request, reply, done) => { fastify.addHook("onRequest", corsMiddleware); //fastify.register(require('./mixpanel/server'), { mixpanel, UAParser }); -fastify.register(require("./get-all-comments/server"), { pb }); fastify.register(require("./get-post/server"), { pb }); fastify.register(require("./get-one-post/server"), { pb }); fastify.register(require("./get-portfolio-data/server"), { pb }); diff --git a/fastify/get-all-comments/server.js b/fastify/get-all-comments/server.js deleted file mode 100755 index 1719cf5..0000000 --- a/fastify/get-all-comments/server.js +++ /dev/null @@ -1,55 +0,0 @@ -// Declare a route - -function listToTree(comments, parentProp = "reply") { - // Create id indexed comments dictionary - const commentsDict = {}; - for (let comment of comments) { - commentsDict[comment.id] = { - ...comment, - children: [], - }; - } - - // Build the tree - const tree = []; - for (const comment of comments) { - const parentId = comment[parentProp]; - if (parentId) { - commentsDict[parentId].children.push(commentsDict[comment.id]); - } else { - tree.push(commentsDict[comment.id]); - } - } - - return tree; -} - -module.exports = function (fastify, opts, done) { - - const pb = opts.pb; - - fastify.post('/get-all-comments', async (request, reply) => { - const data = request.body; - const postId = data?.postId - let output; - - try { - const result = await pb.collection("comments").getFullList({ - filter: `post="${postId}"`, - expand: 'user,alreadyVoted(comment)', - fields: "*,expand.user,expand.alreadyVoted(comment).user,expand.alreadyVoted(comment).type", - sort: '-created', - }) - - - output = listToTree(result); - } - catch(e) { - output = []; - } - -+ reply.send({ items: output }) - }); - - done(); -}; \ No newline at end of file