model.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import psycopg2.extras
  2. def get_timestamp_previous(this_db):
  3. """
  4. Retrieve second to last timestamp from the stocks database
  5. :param this_db:
  6. :return timestamp:
  7. """
  8. cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  9. query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1 OFFSET 1;"""
  10. cursor.execute(query)
  11. res = cursor.fetchone()
  12. this_timestamp_previous = res.timestamp
  13. cursor.close()
  14. return this_timestamp_previous
  15. def get_timestamp_latest(this_db):
  16. """
  17. Retrieve latest timestamp from the stocks database
  18. :param this_db:
  19. :return timestamp:
  20. """
  21. cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  22. query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1;"""
  23. cursor.execute(query)
  24. res = cursor.fetchone()
  25. this_timestamp_latest = res.timestamp
  26. cursor.close()
  27. return this_timestamp_latest
  28. def get_timestamp_stored():
  29. """
  30. Retrieve the timestamp when the program checked the database
  31. :return timestamp:
  32. """
  33. with open('timestamp.txt', 'r') as f:
  34. this_timestamp_stored_string = f.read()
  35. if this_timestamp_stored_string.replace('.', '', 1).isdigit():
  36. this_timestamp_stored = int(this_timestamp_stored_string)
  37. else:
  38. this_timestamp_stored = 0
  39. return this_timestamp_stored
  40. def put_timestamp_stored(this_timestamp):
  41. """
  42. Update timestamp when the program checked the database
  43. :param this_timestamp:
  44. :return:
  45. """
  46. with open('timestamp.txt', 'w') as f:
  47. f.write("{}".format(this_timestamp))
  48. def get_data(stock_id, this_timestamp, this_db):
  49. """
  50. Retrieves detailed data from the existing stocks database
  51. :param stock_id:
  52. :param this_timestamp:
  53. :param this_db:
  54. :return:
  55. """
  56. cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  57. query = """SELECT current_price, available_shares FROM stocks WHERE stock_id = %s AND timestamp = %s"""
  58. cursor.execute(query, (stock_id, this_timestamp))
  59. this_data = cursor.fetchone()
  60. cursor.close()
  61. return this_data
  62. def process_data(this_data_previous, this_data_latest, this_price, this_threshold):
  63. """
  64. Calculate if there is enough change to call it a drop
  65. :param this_data_previous:
  66. :param this_data_latest:
  67. :param this_price:
  68. :param this_threshold:
  69. :return boolean:
  70. """
  71. this_drop = False
  72. quantity_previous = this_data_previous.available_shares * float(this_data_latest.current_price)
  73. quantity_latest = this_data_latest.available_shares * float(this_data_latest.current_price)
  74. if this_data_latest.current_price < this_price and quantity_latest - quantity_previous > this_threshold * 1e9:
  75. this_drop = True
  76. return this_drop