| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import database
- import psycopg2.extras
- def get_timestamp_previous():
- """
- Retrieve second to last timestamp from the stocks database
- :return timestamp:
- """
- db = database.db
- cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
- query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1 OFFSET 1;"""
- cursor.execute(query)
- res = cursor.fetchone()
- this_timestamp_previous = res.timestamp
- cursor.close()
- db.close()
- return this_timestamp_previous
- def get_timestamp_latest():
- """
- Retrieve latest timestamp from the stocks database
- :return timestamp:
- """
- db = database.db
- cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
- query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1;"""
- cursor.execute(query)
- res = cursor.fetchone()
- this_timestamp_latest = res.timestamp
- cursor.close()
- db.close()
- return this_timestamp_latest
- def get_timestamp_stored():
- """
- Retrieve the timestamp when the program checked the database
- :return timestamp:
- """
- with open('npc-drops/timestamp.txt', 'r') as f:
- this_timestamp_stored = f.read()
- return int(this_timestamp_stored)
- def put_timestamp_stored(this_timestamp):
- """
- Update timestamp when the program checked the database
- :param this_timestamp:
- :return:
- """
- with open('npc_drops/timestamp.txt', 'w') as f:
- f.write(this_timestamp)
- def get_data(stock_id, this_timestamp):
- db = database.db
- cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
- query = """SELECT current_price, available_shares FROM stocks WHERE stock_id = %s AND timestamp = %s"""
- cursor.execute(query, (stock_id, this_timestamp))
- this_data = cursor.fetchone()
- cursor.close()
- db.close()
- return this_data
- def process_data(this_data_previous, this_data_latest, this_threshold):
- """
- Checks if there is enough change to call it a drop
- :param this_data_previous:
- :param this_data_latest:
- :param this_threshold
- :return boolean:
- """
- this_drop = False
- quantity_previous = this_data_previous.available_shares * this_data_latest.current_price
- quantity_latest = this_data_latest.available_shares * this_data_latest.current_price
- if quantity_latest - quantity_previous > this_threshold * 1e9:
- this_drop = True
- return this_drop
|