forked from 30ankitbansal/trading_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstamp.py
executable file
·105 lines (95 loc) · 3.9 KB
/
bitstamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import hashlib
import hmac
import json
import datetime
import logging
import threading
import time
import datetime
import calendar
import requests
import os.path
dir = os.path.dirname(os.path.abspath(__file__)) + "/"
class Bitstamp(object):
def __init__(self, key=None, secret=None, client_id=None, coins=None):
self.logger = logging.getLogger("BITSTAMP")
self.BASE_URL = "https://www.bitstamp.net/api/v2/"
self.handler = logging.FileHandler(dir+'logs/bitstamp.log')
self.is_continuous = False
self.key = key
self.secret = secret
self.client_id = client_id
self._init_logger()
self.coins = coins
def _init_logger(self):
self.logger.setLevel(logging.INFO)
self.handler.setLevel(logging.INFO)
self.logger.addHandler(self.handler)
def _format_log(self, string, level):
return "{} {}: {}".format(level, datetime.datetime.now(), string)
def max_bid_price_bitstamp(self):
max_bid_price_bitstamp = {}
price_bitstamp = {}
response_log = ''
for coin in self.coins:
found = False
while not found:
try:
response = requests.get(self.BASE_URL + 'ticker/' + str(coin) + 'USD',
headers={'User-Agent': 'Mozilla/5.0'}).text
response = json.loads(response)
max_bid_price_bitstamp[str(coin)] = response['bid']
price_bitstamp[str(coin)] = response['last']
found = True
response_log = response_log + response
except Exception as e:
pass
self.logger.info(self._format_log(response_log, "INFO"))
return max_bid_price_bitstamp, price_bitstamp
def max_bid_amount(self, coin):
response = requests.get(self.BASE_URL + 'order_book/' + str(coin).lower() + 'usd/')
response = json.loads(response.text)['bids']
MaxBidAmount = response[0][1]
for data in response:
if data[1] > MaxBidAmount:
MaxBidAmount = data[1]
self.logger.info(self._format_log(response, "INFO"))
return MaxBidAmount
def send_bets(self, **params):
if self.key and self.secret:
nonce = str(int(time.time() * 1e6))
message = nonce + self.client_id + self.key
signature = hmac.new(
self.secret.encode('utf-8'), msg=message.encode('utf-8'), digestmod=hashlib.sha256)
signature = signature.hexdigest().upper()
params.update({
'key': self.key, 'signature': signature, 'nonce': nonce
})
url = self.BASE_URL + params['side'] + "/" + params['coin'] + 'usd/'
r = requests.post(url, data=params)
response = json.loads(r.text)
self.logger.info(self._format_log(response, "INFO"))
return response
else:
return "KEY AND SECRET NEEDED FOR BETTING"
def get_balance(self, coin):
params = {}
if self.key and self.secret:
nonce = str(int(time.time() * 1e6))
message = nonce + self.client_id + self.key
signature = hmac.new(
self.secret.encode('utf-8'), msg=message.encode('utf-8'), digestmod=hashlib.sha256)
signature = signature.hexdigest().upper()
params.update({
'key': self.key, 'signature': signature, 'nonce': nonce
})
url = self.BASE_URL + 'balance/' + coin + 'usd/'
r = requests.post(url=url, data=params)
response = json.loads(r.text)
balance_key = coin + '_available'
balance = response[balance_key]
self.logger.info(self._format_log(response, "INFO"))
return balance
else:
return "KEY AND SECRET NEEDED FOR BETTING"