-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·127 lines (119 loc) · 4.46 KB
/
bot.py
File metadata and controls
executable file
·127 lines (119 loc) · 4.46 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
from manager import Manager
from stats import Stats
from datetime import datetime, time
import math
import random
import string
import sys
import time
class Bot(Manager):
def __init__(self, real_time=False):
Manager.__init__(self)
self.real_time = real_time
self.balance = 1.0
if self.real_time:
self.balance = self.GetBTCBalance()
self.open_orders = []
def getBTCBalance(self):
return float(self.coinut.balance()["free_margin"])
def getOptionPrices(self):
nearest_expiry = self.GetNearestExpiry()
upper_bound = str(self.GetUpperBound(self.GetCurrentPrice()))
open_bids = self.coinut.orderbook(
self.d_type, self.asset, nearest_expiry, upper_bound, self.put_call
)["bid"]
return (upper_bound, open_bids)
def openOrder(self, expiry, strike, amount, price, buy_sell="SELL"):
data = [{
"put_call": self.put_call,
"expiry_time": int(expiry),
"asset": self.asset,
"strike": str(strike),
"amount": int(amount),
"price": str(price),
"open_close": "OPEN",
"type": "LIMIT",
"deriv_type": self.d_type,
"side": buy_sell
}]
if self.real_time:
return self.coinut.new_orders(data)
else:
self.balance += float(price) * int(amount)
rand_id = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in xrange(36))
return [{
"status": "ORDER_OPEN",
"put_call": self.put_call,
"expiry_time": int(expiry),
"timestamp": time.time(),
"price": str(price),
"open_close": "OPEN",
"deriv_type": self.d_type,
"side": buy_sell,
"amount": int(amount),
"asset": self.asset,
"strike": str(strike),
"type": "LIMIT",
"id": rand_id
}]
def calculateOptionValue(self, current_price):
s = Stats()
pop = s.CalculatePoP(current_price)[0]
return (1.0 - pop) * 0.01
if __name__ == "__main__":
real_time = "-rt" in sys.argv
at = Bot(real_time)
stop = False
while not stop:
try:
print("Sleeping until next round begins...")
at.SleepToExpiry()
current_price = at.GetCurrentPrice()
if len(at.open_orders) > 0:
strike = float(at.open_orders[0][0]["strike"])
if current_price >= strike:
if not real_time:
at.balance -= 0.01
print("LOSE\tBalance => {0}").format(at.balance)
else:
print("WIN\tBalance => {0}").format(at.balance)
at.open_orders.pop(0)
time.sleep(5)
attempts = 3
if at.balance <= 0:
print("Balance depleted")
attempts = 0
stop = True
while attempts > 0:
strike, price_list = at.GetOptionPrices()
if len(price_list) > 0:
if float(price_list[0]["price"]) > at.CalculateOptionValue(current_price):
print("{0} => {1}").format(
current_price,
at.CalculateOptionValue(current_price)
)
at.open_orders.append(
at.OpenOrder(
at.GetNearestExpiry(),
at.GetUpperBound(current_price),
max(1, int(at.balance)),
price_list[0]["price"]
)
)
print("ORDER {0} => {1} @ {2} strike <=").format(
current_price,
price_list[0]["price"],
strike
)
attempts = 0
else:
print("No open bids for {1} strike...").format(strike)
attempts -= 1
if attempts > 0:
time.sleep(10)
except KeyboardInterrupt:
print("Stopping program...")
stop = True
if real_time:
at.balance = at.GetBTCBalance()
print("Final balance => {0} BTC").format(at.balance)