|
@@ -45,6 +45,7 @@ def connect(reconnect=False):
|
|
|
|
|
|
try:
|
|
try:
|
|
connection = db.connect(db_name)
|
|
connection = db.connect(db_name)
|
|
|
|
+ # connection.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')
|
|
|
|
|
|
cursor = connection.cursor()
|
|
cursor = connection.cursor()
|
|
|
|
|
|
@@ -430,7 +431,7 @@ def ownable_name_exists(name):
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
|
|
-def new_stock(name=None):
|
|
|
|
|
|
+def new_stock( timeout=60, name=None):
|
|
connect()
|
|
connect()
|
|
|
|
|
|
while name is None:
|
|
while name is None:
|
|
@@ -466,12 +467,12 @@ def new_stock(name=None):
|
|
ownable_id,
|
|
ownable_id,
|
|
price,
|
|
price,
|
|
amount,
|
|
amount,
|
|
- 60)
|
|
|
|
|
|
+ timeout)
|
|
return name
|
|
return name
|
|
|
|
|
|
|
|
|
|
-def new_stocks(count=1):
|
|
|
|
- return [new_stock() for _ in range(count)]
|
|
|
|
|
|
+def new_stocks(timeout=60,count=1):
|
|
|
|
+ return [new_stock(timeout=timeout) for _ in range(count)]
|
|
|
|
|
|
|
|
|
|
def ownable_id_by_name(ownable_name):
|
|
def ownable_id_by_name(ownable_name):
|
|
@@ -547,7 +548,7 @@ def execute_orders(ownable_id):
|
|
AND seller.ownable_id = ?
|
|
AND seller.ownable_id = ?
|
|
AND (buy_order."limit" IS NULL
|
|
AND (buy_order."limit" IS NULL
|
|
OR sell_order."limit" IS NULL
|
|
OR sell_order."limit" IS NULL
|
|
- OR (sell_order."limit" < buy_order."limit"
|
|
|
|
|
|
+ OR (sell_order."limit" <= buy_order."limit"
|
|
AND NOT sell_order.stop_loss
|
|
AND NOT sell_order.stop_loss
|
|
AND NOT buy_order.stop_loss))
|
|
AND NOT buy_order.stop_loss))
|
|
ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
|
|
ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
|
|
@@ -584,7 +585,7 @@ def execute_orders(ownable_id):
|
|
price = (float(sell_limit) + float(buy_limit)) / 2
|
|
price = (float(sell_limit) + float(buy_limit)) / 2
|
|
|
|
|
|
if price == 0:
|
|
if price == 0:
|
|
- raise AssertionError() # TODO find a solution
|
|
|
|
|
|
+ raise AssertionError()
|
|
|
|
|
|
buyer_money = user_money(buyer_id)
|
|
buyer_money = user_money(buyer_id)
|
|
|
|
|
|
@@ -592,6 +593,10 @@ def execute_orders(ownable_id):
|
|
sell_order_amount - sell_executed_amount,
|
|
sell_order_amount - sell_executed_amount,
|
|
floor(buyer_money / price))
|
|
floor(buyer_money / price))
|
|
|
|
|
|
|
|
+ if amount == 0: # probable because buyer has not enough money
|
|
|
|
+ delete_order(buy_order_id)
|
|
|
|
+ continue
|
|
|
|
+
|
|
buy_order_finished = (buy_order_amount - buy_executed_amount - amount <= 0) or (
|
|
buy_order_finished = (buy_order_amount - buy_executed_amount - amount <= 0) or (
|
|
buyer_money - amount * price < price)
|
|
buyer_money - amount * price < price)
|
|
sell_order_finished = (sell_order_amount - sell_executed_amount - amount <= 0)
|
|
sell_order_finished = (sell_order_amount - sell_executed_amount - amount <= 0)
|
|
@@ -655,9 +660,9 @@ def execute_orders(ownable_id):
|
|
"limit" = NULL
|
|
"limit" = NULL
|
|
WHERE stop_loss IS NOT NULL
|
|
WHERE stop_loss IS NOT NULL
|
|
AND stop_loss
|
|
AND stop_loss
|
|
- AND (buy AND "limit" > ?)
|
|
|
|
- OR (NOT buy AND "limit" < ?)
|
|
|
|
- ''', (price, price,))
|
|
|
|
|
|
+ AND ? IN (SELECT ownable_id FROM ownership WHERE rowid = ownership_id)
|
|
|
|
+ AND ((buy AND "limit" < ?) OR (NOT buy AND "limit" > ?))
|
|
|
|
+ ''', (ownable_id, price, price,))
|
|
|
|
|
|
|
|
|
|
def ownable_id_by_ownership_id(ownership_id):
|
|
def ownable_id_by_ownership_id(ownership_id):
|