Foppe Hemminga 6 лет назад
Родитель
Сommit
3f8c2e60e5
3 измененных файлов с 62 добавлено и 9 удалено
  1. 55 6
      data.py
  2. 1 0
      database.py
  3. 6 3
      stocks.py

+ 55 - 6
data.py

@@ -1,20 +1,69 @@
 import database
+import psycopg2.extras
 import stocks
+from dotenv import load_dotenv
+import os
+from pprint import pprint
+from datetime import datetime
+from urllib.parse import urlencode
+# from urllib.request import Request, urlopen
+import requests
 
 
-# Retrieve last and second to last timestamp from the server
+def broadcast(name, timestamp, price, quantity, this_drop):
+    # development only
+    load_dotenv()
+    url = os.environ["BROADCAST_URL"]
+    # Create the string
+    time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
+    print(time)
+    this_drop_decimal = float(this_drop / 1e9)
+    string = "{}: {} dropped {} shares at ${:.2f} for a grand total of ${:.1f}B".format(
+        time, name, quantity, price, this_drop_decimal)
+    json = {'content': string}
+    print(urlencode(json).encode())
+    response = requests.post(url, json=json)
+    if response.status_code in [200, 204]:
+        print("Webhook executed")
+    else:
+        print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
+
+
+# Retrieve last and second to last timestamp from the serverS
 db = database.db
-cursor = db.cursor()
+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)
-one_before_latest_entry_timestamp = cursor.fetchone()[0]
+res = cursor.fetchone()
+timestamp_previous = res.timestamp
 query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1;"""
 cursor.execute(query)
-latest_entry_timestamp = cursor.fetchone()[0]
+res = cursor.fetchone()
+timestamp_latest = res.timestamp
+
+print(timestamp_previous)
+print(timestamp_latest)
+
+for stock in stocks.stocks:
+    query = """SELECT current_price, available_shares FROM stocks WHERE stock_id = %s AND timestamp = %s"""
+    cursor.execute(query, (stock[1], timestamp_latest))
+    data_latest = cursor.fetchone()
+    cursor.execute(query, (stock[1], timestamp_previous))
+    data_previous = cursor.fetchone()
+
+    pprint(data_latest)
+    pprint(data_previous)
+    # Calculations
 
-print(one_before_latest_entry_timestamp)
-print(latest_entry_timestamp)
+    available_latest = int(data_latest.available_shares * data_latest.current_price)
+    available_previous = int(data_previous.available_shares * data_previous.current_price)
+    print(available_latest)
+    print(available_previous)
 
+    drop = available_latest - available_previous
+    # broadcast(stock[0], timestamp_latest, data_latest.current_price, data_latest.available_shares, drop)
+    if drop > stock[3] * 1e9:
+        broadcast(stock[0], timestamp_latest, data_latest.current_price, data_latest.available_shares, drop)
 
 cursor.close()
 db.close()

+ 1 - 0
database.py

@@ -2,6 +2,7 @@ from dotenv import load_dotenv
 import os
 import psycopg2
 
+
 # Develop only
 load_dotenv()
 host = os.getenv("STOCK_DB_HOST")

+ 6 - 3
stocks.py

@@ -1,3 +1,6 @@
-# stock number, max price, min(price * drop) in B
-# So this means stock # 15 (FHC), min price $335  and a $10B drop
-fhc = (15, 335, 10)
+# stock name, stock number, max price, min(price * drop) in B
+# So this means: stock name, stock # 15 (FHC), min price $335  and a $10B drop
+fhc = ("FHC", 15, 335, 10)
+
+# Put all stocks in this set
+stocks = (fhc,)