Skip to content

Instantly share code, notes, and snippets.

@byzantime
Created November 12, 2014 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byzantime/2bcacc490ec96ed483a5 to your computer and use it in GitHub Desktop.
Save byzantime/2bcacc490ec96ed483a5 to your computer and use it in GitHub Desktop.
Sell all available bitcoin if price doubles in 7 days
# if bitcoin price on Bitstamp doubles within a 7-day timeframe, sell
from datetime import datetime as dt
from datetime import timedelta
import hashlib
import hmac
import json
from time import sleep
import requests
URL = 'https://www.bitstamp.net/api/'
seven_day_history = []
CLIENT_ID = 0
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
def get_nonce():
return int((dt.utcnow() - dt(1970, 1, 1)).total_seconds())
def make_request(path=None, data=None):
try:
r = requests.post(URL+path, data)
if r.status_code == requests.codes.ok:
return json.loads(r.text)
else:
raise Exception('HTTP error: {}'.format(r.status_code))
except Exception as e:
print e
def make_private_request(path=None, parameters=None):
nonce = get_nonce()
message = '{}{}{}'.format(nonce, CLIENT_ID, API_KEY)
signature = hmac.new(
API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest().upper()
data = {'key': API_KEY, 'signature': signature, 'nonce': nonce}
if parameters: data.update(parameters)
return make_request(path=path, data=data)
while 1:
ticker = make_request(path='ticker/')
now = dt.utcnow()
# append time stamp and price of bitcoin to list
seven_day_history.append((now, float(ticker['bid'])))
# remove items older than 7 days
for i in seven_day_history:
if i[0] < now-timedelta(days=7):
seven_day_history.remove(i)
d1_price = seven_day_history[0][1]
d7_price = seven_day_history[-1][1]
# sell all available bitcoin if price doubled in last 7 days
if d7_price >= d1_price*2:
balance = make_private_request(path='balance/')
xbt_available = balance['btc_available']
# place limit order at price of highest bid
sell = make_private_request(path='sell/',
parameters={'amount': xbt_available,
'price': d7_price})
print '{}: Placed order id {} to sell {} XBT at {} each.'.format(
sell['datetime'], sell['id'], sell['amount'], sell['price'])
# sleep for 12 hours
sleep(43200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment