Skip to content

Instantly share code, notes, and snippets.

@KolevDarko
Created May 3, 2017 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KolevDarko/e4e57825871c89adfaf0bf09eded3b45 to your computer and use it in GitHub Desktop.
Save KolevDarko/e4e57825871c89adfaf0bf09eded3b45 to your computer and use it in GitHub Desktop.
# Full code
import hmac
import time
from hashlib import sha256
import subprocess
import requests
secret_key = '' or input('Enter your secret key: ')
public_key = '' or input('Enter your public key: ')
def create_signature():
ts = str(int(time.time()))
sig = str(ts + "." + public_key).encode()
sign = hmac.new(secret_key.encode(), sig, digestmod=sha256).hexdigest()
signature = "{0}.{1}.{2}".format(ts, public_key, sign)
return signature
def main(exchange, starting_price, amount, currency, limit_type, limit):
"""
:param exchange: The exchange where bitcoin was bought
:param starting_price: The price at which bitcoin was bought
:param amount: The amount of bitcoin
:param currency: The currency bitcoin was bought in
:param limit_type: The type of threshold, percent or amount
:param limit: The value of the threshold
:return: Current profit made
"""
API_ROOT = "https://apiv2.bitcoinaverage.com"
response = requests.get("{api}/exchanges/{exchange}".format(api=API_ROOT, exchange=exchange), headers={'X-Signature': create_signature()})
symbol = 'BTC'+currency
latest_bid = response.json()['symbols'][symbol]['bid']
result = calculate_profits(currency, latest_bid, starting_price, amount, limit_type, limit)
return result
def calc_percent_diff(now, base):
difference = now - base
return difference * 100 / base
def calculate_profits(cc, latest_bid, starting_price, amount, limit_type, limit):
difference = abs((latest_bid - starting_price) * amount)
single_diff = latest_bid - starting_price
if limit_type == 'percent':
percent_difference = calc_percent_diff(latest_bid, starting_price)
if percent_difference >= limit:
return "The bitcoin price has increased by {:.2f} %, you have a profit of {:.2f} {}\n That is {:.2f} {} per coin.".format(percent_difference, difference, cc, abs(single_diff), cc)
elif percent_difference <= -1 * limit:
return "The bitcoin price has decreased by {:.2f} %, you have a loss of {:.2f} {}\n That is {:.2f} {} per coin.".format(abs(percent_difference), difference, cc, abs(single_diff), cc)
else:
if single_diff >= limit:
return "The bitcoin price has increased by {:.2f} {}, you have a profit of {:.2f} {}\n That is {:.2f} {} per coin.".format(single_diff, cc, difference, cc, abs(single_diff), cc)
elif single_diff <= -1 * limit:
return "The bitcoin price has decreased by {:.2f} {}, you have a loss of {:.2f} {}\n That is {:.2f} {} per coin.".format(single_diff, cc, abs(difference), cc, abs(single_diff), cc)
if __name__ == '__main__':
my_exchange = input("Your exchange:")
starting_price = input("Bitcoin starting price:")
amount = input("Bitcoin amount:")
currency = input("Your currency (default USD):")
check_every = input("Check every (seconds):")
threshold = input("Choose to be notified when a certain limit is reached\n1) Percent limit: \n2) Amount limit: \n ")
limit_type = None
limit = 0
if threshold == '1':
limit_type = 'percent'
limit = input("Percent:")
elif threshold == '2':
limit_type = 'amount'
limit = input("Amount:")
if not currency:
currency = 'USD'
notifications = input("Choose desktop notifications: \n0) No\n1) Mac\n2) Linux\n")
while True:
result = main(my_exchange, float(starting_price), float(amount), currency, limit_type, float(limit))
if result:
if notifications == '1':
content = 'display notification "{result}" with title "Bitcoin Profits" '.format(result=result)
subprocess.run(['/usr/bin/osascript', '-e', content])
elif notifications == '2':
subprocess.run(['notify-send', 'Bitcoin Profits', result])
else:
print(result)
break
time.sleep(int(check_every))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment