/
OrderMatchD.py
executable file
·172 lines (141 loc) · 4.44 KB
/
OrderMatchD.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
import sys, time, os, atexit
from modules.daemon import Daemon
import peewee
from peewee import *
import datetime
import orderd.logger
from orderd.logger import log
from coindb.coindb import *
from orderd.order_book import OrderBook
# All bitcoin values are in Satoshi i.e. divide by 100.000.000 to get the amount in BTC
# Each Litecoin is subdivided into 100,000,000 smaller units, defined by eight decimal places
#db = MySQLDatabase(DBDB,host=DBHOST, user=DBUSER,passwd=DBPWD)
class OrderDaemon(Daemon):
def run(self):
# set up db
dname = 'btcltc_ML'
SRC_CRY = 'BTC'
TRG_CRY = 'LTC'
db.init(dname,host='192.168.1.22', user='jack',passwd='hammer')
try:
db.connect()
except: ### access denied
log.error('db %s access denied' % dname)
exit()
else:
log.info('Connect DB: %s' % dname)
#################################################
## TODO
## - get orders
## - instanziate an Orders objects, which takes the db object
## and has the methods
## creates a shallow copy via [:]
## show() ... returns the current order table in pretty format
## may take an argument for various display formatting, i.e 'sql', 'screen'
## match() does a round of matching
## may return a list of modified orders as a list of addresses
## OR
## iterative starting at top
## checking for next order with status <300/200 = active or partially filled
## try match && match
##
while True:
######################
# get active orders from db
#db.set_autocommit(False)
db.set_autocommit(True)
TIMESTAMP = '2013-10-16 00:00:00'
#TIMESTAMP = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
active_orders = (Order
.select(
Order.send_to_address,
Order.order_type,
Order.source,
Order.target,
Order.amount,
Order.amount_settled,
Order.amount_ask,
Order.created,
Order.price_ask,
Order.status)
.where(
(Order.source == SRC_CRY) | (Order.source == TRG_CRY)
& (Order.status >= Order.STATUS_ACTIVE)
& (Order.status < Order.STATUS_FILLED)
& (TIMESTAMP < Order.valid_until))
.order_by(Order.price_ask.asc(),Order.created.desc())
.for_update(True) ## maybe add nowait?
)
#active_orders.execute()
######################
# set canceled orders to topay
pay_canceled_orders = (Order
.update(
status=Order.STATUS_TOPAY)
.where(
(Order.status == Order.STATUS_CANCELED)
)
)
###############################################
## set canceled order to TOPAY
no_canceled = pay_canceled_orders.execute()
log.debug('paying canceled order: ' + str(no_canceled))
###############################################
## get the order book and init class
ob = OrderBook(active_orders)
###############################################
# discover price
# if the orderbook matching has been down this will be an open price scenario
ob.discover_price()
#log.debug('showing the orderbook')
#ob.show_order_book('screen')
ob.settle_orderbook()
#___________________________________
# looping using a generator
#ob.loop_orders()
#c = ob.get_next()
#print next(c).send_to_address
#print next(c).send_to_address
sys.exit('stop')
pass
#____TODO____
# before writing check if there is a new cancel status
###############################################
## DB LOCKING TESTS
#log.debug('created query')
#time.sleep(20)
#log.debug('executing')
#active_orders.execute()
#time.sleep(20)
#log.debug('committing')
#db.commit()
#log.debug('after commi')
#time.sleep(100000)
###############################################
# testing updateing data in a tuple/row
#x = ob.update_status('999')
#print x
#ob.show_order_book('raw')
log.debug('done settleing')
time.sleep(10)
#####################################################
if __name__ == "__main__":
orderd.logger.set_logging(DEBUG)
daemon = OrderDaemon('/home/markus/order_daemon.pid')
if len(sys.argv) == 3 and 'debug' == sys.argv[2]:
orderd.logger.set_logging(True)
log.debug('Running in debug mode ...')
daemon.run()
if 'start' == sys.argv[1]:
log.info('Starting ...')
daemon.start()
elif 'stop' == sys.argv[1]:
log.info('Stopping ...')
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print "usage: %s start|stop|restart [debug]" % sys.argv[0]
sys.exit(2)
sys.exit(0)