\ btc-e logging bot - /g/pasta 2.4
From rhaps0dy, 5 Years ago, written in Python.
Embed
  1. #!/usr/bin/python
  2. import os.path
  3. import os
  4. import http.client
  5. import encodings.idna
  6. import time
  7. import json
  8.  
  9. # Pairs to log
  10. logPairs = ["btc_usd", "btc_rur", "btc_eur", "ltc_btc", "ltc_usd", "ltc_rur", "ltc_eur", "nmc_btc", "nmc_usd", "nvc_btc",
  11.         "nvc_usd", "usd_rur", "eur_usd", "trc_btc", "ppc_btc", "ppc_usd", "ftc_btc", "xpm_btc"]
  12.  
  13. # Where to store the files
  14. storePrefix = os.environ['HOME']+"/.btcArchive/"
  15. if not os.path.exists(storePrefix):
  16.     os.makedirs(storePrefix)
  17.  
  18. headers = {"Content-type": "application/x-www-form-urlencoded"}
  19. conn = http.client.HTTPSConnection("btc-e.com", strict=False)
  20.  
  21. fileList={}
  22. for exchange in logPairs:
  23.     for action in ["-buy", "-sell"]:
  24.         if not os.path.exists(storePrefix+exchange+action):
  25.             fileList[exchange+action] = open(storePrefix+exchange+action, "w")
  26.         else:
  27.             fileList[exchange+action] = open(storePrefix+exchange+action, "a")
  28.    
  29.  
  30. while 1:
  31.     try:
  32.         for exchange in logPairs:
  33.             conn.request("GET", "/api/2/%s/ticker"%exchange)
  34.             response = conn.getresponse()
  35.             values = json.loads(response.read().decode('ascii'))
  36.             fileList[exchange+"-buy"].write(str(values['ticker']['buy'])+'\n')
  37.             fileList[exchange+"-sell"].write(str(values['ticker']['sell'])+'\n')
  38.             print(response.status, response.reason)
  39.         time.sleep(120.0)
  40.     except KeyboardInterrupt:
  41.         break
  42.     except:
  43.         print("exception occurred", response.status, response.reason)
  44.  
  45. conn.close()
  46.  
  47. for exchange in logPairs:
  48.         fileList[exchange+"-buy"].close()
  49.         fileList[exchange+"-sell"].close()
  50.  
  51. #I'll use the data collected to make a trading bot