Skip to content

Instantly share code, notes, and snippets.

@bretton
Created August 2, 2020 07:53
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 bretton/ea32c0d4fcd05e862a2486a33f0f6962 to your computer and use it in GitHub Desktop.
Save bretton/ea32c0d4fcd05e862a2486a33f0f6962 to your computer and use it in GitHub Desktop.
How many days over $10k since CZ tweet to "slap yourself" for selling below that price?
#!/usr/bin/python3
# coding=utf-8
#
# Dev Log
# how-many-days-over-10k.py
# =======
# 2019-08-02 - https://twitter.com/cz_binance/status/1157075839495761920
# # "Slap yourself,
# # if you sold $BTC under $10,000."
# 2020-07-18 - setup script to pull data from Bitmex UDF and create candles and highlights
# - we want to download price data and highlight how many days bitcoin has closed
# - over $10k
# 2020-08-02 - Anniversary of CZ comment after one year, code put up as gist
#
##########################
import os
import sys
import time
import datetime as dt
import json
import requests
import pandas as df
from bokeh.layouts import column
from bokeh.plotting import figure, show, output_file
# debug to true or false
debug = "false"
showchart = "true"
# we need today to determine yesterday because we need to get OHLC until yesterday
today = dt.date.today()
yesterday = today - dt.timedelta(days=1)
# the Bitmex UDF response with OHLC is limited to 10080 replies and won't accept resolution of 1d, only 60,
# so we'll create epochs and call in the data for each year, appending to the data frame each time
# earliest record for an hourly candle appears to be epoch datetime 1443182400
# this is GMT: Friday, September 25, 2015 12:00:00 PM
# this is adapted from a 5 year script, for this version of the script we're only
# interested in 2019/2020 to check CZ's "slap yourself for selling under $10k"
# 2019
# we only need from start of August for visual summary, so remove start of year
#period2019Start = dt.datetime(2019, 1, 1, 0, 0).strftime('%s')
period2019Start = dt.datetime(2019, 8, 1, 0, 0).strftime('%s')
period2019End = dt.datetime(2019, 12, 31, 11, 59).strftime('%s')
period2019Request = requests.get("https://www.bitmex.com/api/udf/history?symbol=XBTUSD&resolution=60&from="+period2019Start+"&to="+period2019End).json()
myArray2019 = df.DataFrame(period2019Request)
# 2020
period2020Start = dt.datetime(2020, 1, 1, 0, 0).strftime('%s')
# set to yesterday date, or comment that out, and uncomment and edit for manual date
#period2020End = dt.datetime(2020, 8, 2, 11, 59).strftime('%s')
# using today instead of yesterday
#period2020End = yesterday.strftime('%s')
period2020End = today.strftime('%s')
period2020Request = requests.get("https://www.bitmex.com/api/udf/history?symbol=XBTUSD&resolution=60&from="+period2020Start+"&to="+period2020End).json()
myArray2020 = df.DataFrame(period2020Request)
# my array of annual dataframes
myBigArray = [myArray2019, myArray2020]
# we want to concat the dataframes, with ignore_index=True, so we get all rows
myCombinedResult = df.concat(myBigArray, axis=0, join='outer', ignore_index=True, keys=None, levels=None, names=None, verify_integrity=False, copy=True)
# remove the http ok 200 status column
del myCombinedResult['s']
# we can set the column headings
myCombinedResult.columns = ["datetime", "close", "open", "high", "low", "volume"]
myCombinedResult = myCombinedResult[["datetime", "open", "high", "low", "close", "volume"]]
# convert the datetime column into a datetime dtype
myCombinedResult["datetime"] = df.to_datetime(myCombinedResult["datetime"], errors='raise', dayfirst=False, yearfirst=False, utc=None, format=None, exact=True, unit='s', infer_datetime_format=False, origin='unix', cache=True)
# set the datetime to an index
myCombinedResult.set_index(["datetime"], drop=True, append=False, inplace=True, verify_integrity=False)
# resample hourly to daily
myCombinedResult = myCombinedResult.resample('1440Min', axis=0).bfill()
threshold = 10000.0
mySummary = myCombinedResult[myCombinedResult["close"] >= threshold]
if debug == "true":
print(myCombinedResult)
print(mySummary)
if showchart == "true":
# plot results
w = 24*60*60*1000 # 1d in ms
chartTools = "pan,wheel_zoom,box_zoom,reset,save"
p = figure(x_axis_type="datetime", tools=chartTools, plot_width=1400, title = "Bitmex XBTUSD daily close above $10k since CZ 'slap yourself' tweet", output_backend="webgl", sizing_mode="fixed")
p.grid.grid_line_alpha=0.3
# add price line from close
p.line(x = 'datetime', y = 'close', source = myCombinedResult, color = 'black', line_width = 1)
# use a scatter chart to show the summary table of prices above $10k
p.scatter(x = 'datetime', y = 'close', source = mySummary, color = 'red', line_width = 5)
# configure visual properties on a plot's title attribute
p.title.align = "center"
p.title.text_color = "black"
p.title.text_font_size = "30px"
n = figure(x_axis_type="datetime", tools=chartTools, plot_height=100, title = "Bitmex XBTUSD daily trade volumes", output_backend="webgl", sizing_mode="scale_width")
# use a bar chart to show the volume
n.vbar(x = 'datetime', top = 'volume', source = myCombinedResult, width = w, color = "pink")
n.grid.grid_line_alpha=0.3
n.title.align = "center"
n.title.text_color = "black"
n.title.text_font_size = "18px"
# save to file
output_file("candlestick.html", title="Bitcoin daily close above $10k since CZ 'slap yourself' tweet")
# open a browser
show(column(p,n,sizing_mode="scale_height"))
@bretton
Copy link
Author

bretton commented Aug 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment