Calling the Top

Some time mid-2013 I was reading through rpietila's posts on the bitcointalk.org forums and came across the theory that you could "call the top" of a bubble simply by selling if the price doubles in 7 days.

Unfortunately, he's got 4.7k-odd posts now, so the exact post has eluded me so far.

While the old trope "past performance doesn't indicate future results" always applies, there's no harm in being prepared to take advantage of an identified pattern should it manifest again.

For an example from the most recent doubling-in-7-days, see this chart of the Nov 2013 run up to Dec 31st peak, where 7-day price doublings can be seen twice:

recent 7day doubling example Bitcoin charts link

In light of the recent upward price movement (THIS IS GENTLEMEN), the topic seemed apposite, so I whipped up a script to test the theory:

# 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)
        r.raise_for_status()
        return json.loads(r.text)
    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)

View on Github

To use this, you will need a funded Bitstamp account with an API key setup allowing the script to query balance and place sell orders. Suggested improvements would include:

  • increasing the price checking frequency - sleep(21600) would give you a 6hr resolution
  • message logging / email sending
  • replace scheduling by running script as cron job, with celery, etc

Mandatory disclaimer: The code is licensed under the MIT license and does not claim to be fit for any purpose.