Skip to content

Instantly share code, notes, and snippets.

@cderinbogaz
Created June 19, 2018 14:22
Show Gist options
  • Save cderinbogaz/25931135e33513c33144b492f194ecb7 to your computer and use it in GitHub Desktop.
Save cderinbogaz/25931135e33513c33144b492f194ecb7 to your computer and use it in GitHub Desktop.
Delta function inside main.py
def delta(exchange, currency):
try:
exchange.database.begin()
table = exchange.database[currency]
table_length = len(table)
date_1h = datetime.datetime.utcnow()
date_24h = datetime.datetime.utcnow() + datetime.timedelta(-1)
timestamp_1h = exchange.parse8601(str(date_1h)) - 3600000
timestamp_24h = exchange.parse8601(str(date_24h))
result_1h = table.find_one(table.table.columns.time_ms > timestamp_1h)
result_24h = table.find_one(table.table.columns.time_ms > timestamp_24h)
result_now = table.find_one(id=table_length)
closing_now = result_now['close']
# CHECK IF THE VALUES ARE NONE TYPE OR NOT.
if not result_1h:
print("none value for result_1h")
delta_1h = 0
else:
closing_1h = result_1h['close']
delta_1h = ((closing_now - closing_1h) / closing_now) * 100
if not result_24h:
print("none value for result_24h")
delta_24h = 0
else:
closing_24h = result_24h['close']
delta_24h = ((closing_now - closing_24h) / closing_now) * 100
exchange.database.commit()
except Exception as error:
print("Error in Delta function: " + str(error))
return (delta_1h, delta_24h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment