add bid ask price

This commit is contained in:
MuslemRahimi 2024-10-08 13:47:57 +02:00
parent 57a18fbf9e
commit bdf7e9ec9a
10 changed files with 37 additions and 59 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ app/*.sh
app/.env* app/.env*
app/ml_models/weights app/ml_models/weights
app/ml_models/training_data app/ml_models/training_data
app/ml_models/__pycache__
app/json/* app/json/*
app/logs/* app/logs/*
fastify/node_modules fastify/node_modules

View File

@ -43,6 +43,16 @@ async def get_pre_post_quote_of_stocks(ticker_list):
else: else:
return {} 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): async def save_quote_as_json(symbol, data):
with open(f"json/quote/{symbol}.json", 'w') as file: with open(f"json/quote/{symbol}.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
@ -58,6 +68,25 @@ async def save_pre_post_quote_as_json(symbol, data):
except Exception as e: except Exception as e:
pass 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(): async def run():
con = sqlite3.connect('stocks.db') con = sqlite3.connect('stocks.db')
etf_con = sqlite3.connect('etf.db') etf_con = sqlite3.connect('etf.db')
@ -65,7 +94,7 @@ async def run():
cursor = con.cursor() cursor = con.cursor()
cursor.execute("PRAGMA journal_mode = wal") 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()] stocks_symbols = [row[0] for row in cursor.fetchall()]
etf_cursor = etf_con.cursor() etf_cursor = etf_con.cursor()
@ -100,7 +129,7 @@ async def run():
total_symbols = stocks_symbols+etf_symbols 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)] 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") delete_files_in_directory("json/pre-post-quote")
for chunk in chunks: for chunk in chunks:
@ -117,7 +146,11 @@ async def run():
symbol = item['symbol'] symbol = item['symbol']
await save_pre_post_quote_as_json(symbol, item) await save_pre_post_quote_as_json(symbol, item)
#print(f"Saved data for {symbol}.") #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: try:
asyncio.run(run()) asyncio.run(run())

View File

@ -61,7 +61,6 @@ const corsMiddleware = (request, reply, done) => {
fastify.addHook("onRequest", corsMiddleware); fastify.addHook("onRequest", corsMiddleware);
//fastify.register(require('./mixpanel/server'), { mixpanel, UAParser }); //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-post/server"), { pb });
fastify.register(require("./get-one-post/server"), { pb }); fastify.register(require("./get-one-post/server"), { pb });
fastify.register(require("./get-portfolio-data/server"), { pb }); fastify.register(require("./get-portfolio-data/server"), { pb });

View File

@ -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();
};