-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (121 loc) · 5.1 KB
/
main.py
File metadata and controls
144 lines (121 loc) · 5.1 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import urllib3
import time
from BTC import BTC
class Order:
def __init__(self, _id, _type, _currency1, _currency2, _amount1, _price):
self.id = _id
self.type = _type # 'buy' or 'sell'
self.currency1 = _currency1
self.currency2 = _currency2
self.amount1 = _amount1
self.price = _price
def display(self):
print("order_id = " + str(
self.id) + ", type = " + self.type + ", market - " + self.currency1 + "_" + self.currency2 + \
", amount1 = " + str(self.amount1) + ", price = " + str(self.price))
class BOT:
def __init__(self):
self.orders = []
self.LastOperationPrice = 1
self.critic_low_to_buy = 0
self.critic_high_to_buy = 0
self.critic_low_to_sell = 0
self.critic_high_to_sell = 0
self.BTC_object = BTC()
self.isNextOperationToBuy = False
self.market = 'ltc_uah'
def getMarketPrice(self):
info = self.BTC_object.get_prices(market=self.market)
if self.isNextOperationToBuy:
return info[self.market]['buy']
else:
return info[self.market]['sell']
def mybalance(self, currency):
info = self.BTC_object.get_balance()
for i in info['accounts']:
if i['currency'] == currency:
return float(i['balance'])
def buy_exists(self):
for i in self.orders:
if i.type == 'buy':
return i
return None
def sell_exists(self):
for i in self.orders:
if i.type == 'sell':
return i
return None
def get_status(self, order_id):
info = self.BTC_object.check_order_status(order_id=order_id)
print(info)
if info['status'] == 'processing':
return True
else:
return False
def tryToBuy(self, percentage_diff, price):
if percentage_diff > self.critic_high_to_buy or percentage_diff < self.critic_low_to_buy:
buy_order = self.buy_exists()
if (buy_order is not None) and (self.get_status(buy_order.id)):
info = self.BTC_object.order_move(order_id=buy_order.id, new_price=price)
if info['status'] is not True:
print("Error :", end=' ')
print(info)
else:
buy_order.price = price
self.LastOperationPrice = price
else:
if buy_order is not None:
self.orders.remove(buy_order)
amount = self.mybalance('LTC') / 2
info = self.BTC_object.buy(amount, price, self.market)
print(info)
if info['status'] is True:
new_order = Order(info['order_id'], 'buy', 'LTC', 'UAH', amount, price * amount)
self.orders.append(new_order)
self.LastOperationPrice = price
else:
print("Error :", end=' ')
print(info)
self.isNextOperationToBuy = False
def tryToSell(self, percentage_diff, price):
if percentage_diff > self.critic_high_to_sell or percentage_diff < self.critic_low_to_sell:
sell_order = self.sell_exists()
if (sell_order is not None) and (self.get_status(sell_order.id)):
info = self.BTC_object.order_move(order_id=sell_order.id, new_price=price)
if info['status'] is not True:
print("Error :", end=' ')
print(info)
else:
sell_order.price = price
self.LastOperationPrice = price
else:
if sell_order is not None:
self.orders.remove(sell_order)
amount = self.mybalance('LTC') / 2
info = self.BTC_object.sell(amount, price, self.market)
print(info)
if info['status'] is True:
new_order = Order(info['order_id'], 'sell', 'LTC', 'UAH', amount, price * amount)
self.orders.append(new_order)
self.LastOperationPrice = price
else:
print("Error :", end=' ')
print(info)
self.isNextOperationToBuy = True
def attemptToMakeTrade(self):
current_price = float(self.getMarketPrice())
percentage_diff = ((current_price - self.LastOperationPrice) / self.LastOperationPrice) * 100
print(percentage_diff)
if self.isNextOperationToBuy:
self.tryToBuy(percentage_diff, current_price)
else:
self.tryToSell(percentage_diff, current_price)
def make_money(self):
while True:
self.attemptToMakeTrade()
time.sleep(30)
if __name__ == '__main__':
# bo wyskakuje nepotribnyj do holery welykyj worning :
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
bot = BOT()
BOT.make_money()