|
@@ -1,20 +1,69 @@
|
|
|
import database
|
|
import database
|
|
|
|
|
+import psycopg2.extras
|
|
|
import stocks
|
|
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
|
|
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;"""
|
|
query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1 OFFSET 1;"""
|
|
|
cursor.execute(query)
|
|
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;"""
|
|
query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1;"""
|
|
|
cursor.execute(query)
|
|
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()
|
|
cursor.close()
|
|
|
db.close()
|
|
db.close()
|