|
11 | 11 | import matplotlib.pyplot as plt
|
12 | 12 | plt.style.use('dark_background')
|
13 | 13 | import datetime
|
14 |
| -import sys |
15 |
| -#The following ensures that only one instance of this program will run. |
16 |
| -try: |
17 |
| - import socket |
18 |
| - s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
19 |
| - ## Create an abstract socket, by prefixing it with null. |
20 |
| - s.bind( '\0postconnect_gateway_notify_lock') |
21 |
| -except socket.error as e: |
22 |
| - error_code = e.args[0] |
23 |
| - error_string = e.args[1] |
24 |
| - print "Process already running (%d:%s ). Exiting" % ( error_code, error_string) |
25 |
| - sys.exit (0) |
26 |
| -# Get historical bitcoin price information from gdax |
27 |
| -client = gdax.PublicClient() |
28 |
| -rates = client.get_product_historic_rates('BTC-USD', granularity=60*60*24) |
29 |
| -# Modify the dates from ISO 8601 to human readable. |
30 |
| -dates = [datetime.date.fromtimestamp(element[0]) for element in rates] |
31 |
| -# Create the pandas plot. |
32 |
| -df = pd.DataFrame(rates, columns = ['date', 'low', 'high', 'open', 'close', 'volume']) |
33 |
| -df['date'] = [datetime.date.fromtimestamp(element[0]) for element in rates] |
34 |
| -df = df.head(30) |
35 |
| -df = df.sort_values('date', ascending = True) |
36 |
| -df.plot('date', 'close', kind='line', use_index=True, title='Bitcoin closing prices by date', rot=90, grid=True, figsize=[6,9]) |
37 |
| -plt.show() |
| 14 | +import subprocess |
| 15 | +import os, fcntl |
| 16 | +def run_once(): |
| 17 | + global fh |
| 18 | + fh=open(os.path.realpath(__file__),'r') |
| 19 | + try: |
| 20 | + fcntl.flock(fh,fcntl.LOCK_EX|fcntl.LOCK_NB) |
| 21 | + except: |
| 22 | + os._exit(0) |
| 23 | +if __name__ == "__main__": |
| 24 | + run_once() |
| 25 | + # Get historical bitcoin price information from gdax |
| 26 | + client = gdax.PublicClient() |
| 27 | + rates = client.get_product_historic_rates('BTC-USD', granularity=60*60*24) |
| 28 | + # Modify the dates from ISO 8601 to human readable. |
| 29 | + dates = [datetime.date.fromtimestamp(element[0]) for element in rates] |
| 30 | + # Create the pandas plot. |
| 31 | + df = pd.DataFrame(rates, columns = ['date', 'low', 'high', 'open', 'close', 'volume']) |
| 32 | + df['date'] = [datetime.date.fromtimestamp(element[0]) for element in rates] |
| 33 | + df = df.head(30) |
| 34 | + df = df.sort_values('date', ascending = True) |
| 35 | + df.plot('date', 'close', kind='line', use_index=True, title='Bitcoin closing prices by date', rot=90, grid=True, figsize=[6,9]) |
| 36 | + plt.show() |
| 37 | + |
0 commit comments