Skip to content

Commit

Permalink
[BaseStrategy] Allow cancelation of my/all orders of type sell/buy
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Jan 25, 2016
1 parent cdf88dc commit 71282aa
Showing 1 changed file with 55 additions and 23 deletions.
78 changes: 55 additions & 23 deletions scripts/exchange-bots/bot/strategies/basestrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,74 @@ def __init__(self, *args, **kwargs):
self.opened_orders = []
self.restore()

def cancel_all(self) :
def cancel_all(self, side="both") :
onceCanceled = False
curOrders = self.dex.returnOpenOrdersIds()
curOrders = self.dex.returnOpenOrders()
for m in self.settings["markets"]:
for o in curOrders[m]:
try :
self.dex.cancel(o)
onceCanceled = True
except:
print("An error has occured when trying to cancel order %s!" % o)
if m in curOrders:
for o in curOrders[m]:
if o["type"] is side or side is "both":
try :
print("Canceling %s" % o["orderNumber"])
self.dex.cancel(o["orderNumber"])
onceCanceled = True
except:
print("An error has occured when trying to cancel order %s!" % o)
return onceCanceled

def cancel_mine(self) :
curOrders = self.dex.returnOpenOrdersIds()
def cancel_mine(self, side="both") :
curOrders = self.dex.returnOpenOrders()
state = self.getState()
onceCanceled = False
for o in state["orders"]:
for m in self.settings["markets"]:
if o in curOrders[m] :
try :
self.dex.cancel(o)
onceCanceled = True
except:
print("An error has occured when trying to cancel order %s!" % o["orderNumber"])
if o["type"] is side or side is "both":
try :
print("Canceling %s" % o["orderNumber"])
self.dex.cancel(o["orderNumber"])
onceCanceled = True
except:
print("An error has occured when trying to cancel order %s!" % o["orderNumber"])
return onceCanceled

def cancel_this_markets(self) :
orders = self.dex.returnOpenOrders()
onceCanceled = False
for m in self.settings["markets"]:
for order in orders[m]:
for o in orders[m]:
try :
self.dex.cancel(order["orderNumber"])
print("Canceling %s" % o["orderNumber"])
self.dex.cancel(o["orderNumber"])
onceCanceled = True
except:
print("An error has occured when trying to cancel order %s!" % order["orderNumber"])
print("An error has occured when trying to cancel order %s!" % o["orderNumber"])
return onceCanceled

def cancel_all_sell_orders(self):
return self.cancel_all("sell")

def cancel_all_buy_orders(self):
return self.cancel_all("buy")

def cancel_my_sell_orders(self):
return self.cancel_mine("sell")

def cancel_my_buy_orders(self):
return self.cancel_mine("buy")

def cancel_all_bid_orders(self):
return self.cancel_all("buy")

def cancel_all_ask_orders(self):
return self.cancel_all("sell")

def cancel_my_bid_orders(self):
return self.cancel_my_buys()

def cancel_my_ask_orders(self):
return self.cancel_my_sells()

def getState(self):
return self.state

Expand All @@ -72,10 +102,12 @@ def store(self):
for market in self.settings["markets"] :
if market not in myorders:
myorders[market] = []
for orderid in curOrders[market] :
if orderid not in self.opened_orders[market] :
myorders[market].append(orderid)
self.orderPlaced(orderid)
if market in curOrders:
for orderid in curOrders[market] :
if market not in self.opened_orders or \
orderid not in self.opened_orders[market] :
myorders[market].append(orderid)
self.orderPlaced(orderid)

state["orders"] = myorders
with open(self.filename, 'w') as fp:
Expand Down Expand Up @@ -110,7 +142,7 @@ def sell(self, market, price, amount):

def buy(self, market, price, amount):
quote, base = market.split(self.config.market_separator)
print(" - Buying %f %s with %s @%f %s/%s" % (amount, base, quote, price, quote, base))
print(" - Buying %f %s with %s @%f %s/%s" % (amount, quote, base, price, quote, base))
self.dex.buy(market, price, amount)

def place(self) :
Expand Down

0 comments on commit 71282aa

Please sign in to comment.