Browse Source

Implemented max price, added several stocks

Foppe Hemminga 6 years ago
parent
commit
603208bd39
3 changed files with 22 additions and 6 deletions
  1. 2 1
      main.py
  2. 13 3
      model.py
  3. 7 2
      stocks.py

+ 2 - 1
main.py

@@ -24,7 +24,8 @@ if __name__ == '__main__':
         data_previous = model.get_data(stock_id, timestamp_previous, db)
         data_latest = model.get_data(stock_id, timestamp_latest, db)
         threshold = stock[3]
-        is_drop = model.process_data(data_previous, data_latest, threshold)
+        max_price = stock[2]
+        is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
         current_price = float(data_latest.current_price)
         quantity = data_latest.available_shares - data_previous.available_shares
         stock_name = stock[0]

+ 13 - 3
model.py

@@ -4,6 +4,7 @@ import psycopg2.extras
 def get_timestamp_previous(this_db):
     """
     Retrieve second to last timestamp from the stocks database
+    :param this_db:
     :return timestamp:
     """
     cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
@@ -18,6 +19,7 @@ def get_timestamp_previous(this_db):
 def get_timestamp_latest(this_db):
     """
     Retrieve latest timestamp from the stocks database
+    :param this_db:
     :return timestamp:
     """
     cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
@@ -54,6 +56,13 @@ def put_timestamp_stored(this_timestamp):
 
 
 def get_data(stock_id, this_timestamp, this_db):
+    """
+    Retrieves detailed data from the existing stocks database
+    :param stock_id:
+    :param this_timestamp:
+    :param this_db:
+    :return:
+    """
     cursor = this_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))
@@ -62,17 +71,18 @@ def get_data(stock_id, this_timestamp, this_db):
     return this_data
 
 
-def process_data(this_data_previous, this_data_latest, this_threshold):
+def process_data(this_data_previous, this_data_latest, this_price, this_threshold):
     """
     Calculate if there is enough change to call it a drop
     :param this_data_previous:
     :param this_data_latest:
-    :param this_threshold
+    :param this_price:
+    :param this_threshold:
     :return boolean:
     """
     this_drop = False
     quantity_previous = this_data_previous.available_shares * float(this_data_latest.current_price)
     quantity_latest = this_data_latest.available_shares * float(this_data_latest.current_price)
-    if quantity_latest - quantity_previous > this_threshold * 1e9:
+    if this_data_latest.current_price > this_price or quantity_latest - quantity_previous > this_threshold * 1e9:
         this_drop = True
     return this_drop

+ 7 - 2
stocks.py

@@ -1,7 +1,12 @@
 # stock name, stock number, max price, min(price * drop) in B
 # So this means: stock name, stock # 15 (FHC), max price $335  and a > $10B drop
-fhc = ("FHG", 15, 335, 10)
+fhg = ("FHG", 15, 335, 10)
 yaz = ("YAZ", 8, 60, 10)
+cnc = ("CNC", 10, 530, 10)
+wlt = ("WLT", 30, 550, 10)
+hrg = ("HRG", 22, 350, 10)
+sym = ("SYM", 16, 480, 10)
+
 
 # Put all stocks in this set
-stocks = (fhc, yaz)
+stocks = (fhg, yaz, cnc, wlt, hrg, sym)