improve
This commit is contained in:
		
							
								
								
									
										63
									
								
								bitbank.py
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								bitbank.py
									
									
									
									
									
								
							| @@ -27,12 +27,12 @@ async def init(cli): | |||||||
|   pair = Pair(cli, "btc_jpy") |   pair = Pair(cli, "btc_jpy") | ||||||
|   logic.EMA_Chicken( |   logic.EMA_Chicken( | ||||||
|       logging.getLogger("bitbank/btc/EMA_Chicken"), |       logging.getLogger("bitbank/btc/EMA_Chicken"), | ||||||
|       pair, pair.candlestick_1m, 0.001, 0.01) |       pair, pair.candlestick_1m, 0.001, 0.007) | ||||||
|  |  | ||||||
|   pair = Pair(cli, "eth_jpy") |   pair = Pair(cli, "eth_jpy") | ||||||
|   logic.MM( |   logic.MM( | ||||||
|       logging.getLogger("bitbank/eth/MM"), |       logging.getLogger("bitbank/eth/MM"), | ||||||
|       pair, 0.001, 1) |       pair, 0.01, 1) | ||||||
|   logic.EMA_Chicken( |   logic.EMA_Chicken( | ||||||
|       logging.getLogger("bitbank/eth/EMA_Chicken"), |       logging.getLogger("bitbank/eth/EMA_Chicken"), | ||||||
|       pair, pair.candlestick_1m, 0.01, 0.01) |       pair, pair.candlestick_1m, 0.01, 0.01) | ||||||
| @@ -115,13 +115,22 @@ class Order(util.pair.Order): | |||||||
|     self._id   = id |     self._id   = id | ||||||
|  |  | ||||||
|   async def update(self): |   async def update(self): | ||||||
|     async with _k: |     if self.done: return | ||||||
|       res = await self._cli.get(PRIVATE_API_ENTRYPOINT+"/user/spot/order", params={ |     for i in range(5): | ||||||
|         "pair": self._pair.name, |       async with _k: | ||||||
|         "order_id": self._id, |         res = await self._cli.get(PRIVATE_API_ENTRYPOINT+"/user/spot/order", params={ | ||||||
|       }) |           "pair": self._pair.name, | ||||||
|       res = await res.json() |           "order_id": self._id, | ||||||
|     _check_response(res) |         }) | ||||||
|  |         res = await res.json() | ||||||
|  |       if res["success"] == 1: break | ||||||
|  |  | ||||||
|  |       # sometimes fails without a reason, so it does retry :( | ||||||
|  |       if res["data"]["code"] != 50009 and str(res["data"]["code"]) in ERROR_CODES: | ||||||
|  |         _check_response(res) | ||||||
|  |       await asyncio.sleep(1) | ||||||
|  |     if res["success"] != 1: raise Exception("failed to update") | ||||||
|  |  | ||||||
|     res = res["data"] |     res = res["data"] | ||||||
|     self.amount = float(res["start_amount"]) |     self.amount = float(res["start_amount"]) | ||||||
|     self.remain = float(res["remaining_amount"]) |     self.remain = float(res["remaining_amount"]) | ||||||
| @@ -132,13 +141,35 @@ class Order(util.pair.Order): | |||||||
|       res["status"] == "CANCELED_PARTIALLY_FILLED" |       res["status"] == "CANCELED_PARTIALLY_FILLED" | ||||||
|  |  | ||||||
|   async def cancel(self): |   async def cancel(self): | ||||||
|     async with _k: |     for i in range(5): | ||||||
|       res = await self._cli.post(PRIVATE_API_ENTRYPOINT+"/user/spot/cancel_order", data={ |       async with _k: | ||||||
|         "pair": self._pair.name, |         res = await self._cli.post(PRIVATE_API_ENTRYPOINT+"/user/spot/cancel_order", data={ | ||||||
|         "order_id": self._id, |           "pair": self._pair.name, | ||||||
|       }) |           "order_id": self._id, | ||||||
|       res = await res.json() |         }) | ||||||
|     _check_response(res) |         res = await res.json() | ||||||
|  |       if res["success"] == 1: | ||||||
|  |         res = res["data"] | ||||||
|  |         self.amount = float(res["start_amount"]) | ||||||
|  |         self.remain = float(res["remaining_amount"]) | ||||||
|  |         self.price  = float(res["average_price"]) | ||||||
|  |         self.done   = True | ||||||
|  |         return | ||||||
|  |  | ||||||
|  |       code = res["data"]["code"] | ||||||
|  |       if code == 50027: | ||||||
|  |         await self.update() | ||||||
|  |         return | ||||||
|  |  | ||||||
|  |       # sometimes fails without a reason, so it does retry :( | ||||||
|  |       retry =  \ | ||||||
|  |         code == 50009 or  \ | ||||||
|  |         str(code) not in ERROR_CODES | ||||||
|  |  | ||||||
|  |       if not retry: | ||||||
|  |         _check_response(res) | ||||||
|  |       await asyncio.sleep(1) | ||||||
|  |     raise Exception("failed to cancel") | ||||||
|  |  | ||||||
|  |  | ||||||
| class Ticker(util.ticker.Ticker): | class Ticker(util.ticker.Ticker): | ||||||
|   | |||||||
| @@ -34,7 +34,7 @@ class EMA_Chicken: | |||||||
|           outgo = buy.price * self._lot |           outgo = buy.price * self._lot | ||||||
|           self._total  -= outgo |           self._total  -= outgo | ||||||
|           self._remain += self._lot |           self._remain += self._lot | ||||||
|           self._logger.info(f"<BUY> {self._lot} / {outgo} ({self._total})") |           self._logger.info(f"<BUY> {self._lot} / {outgo} ({self._remain} / {self._total})") | ||||||
|  |  | ||||||
|           limit_price = buy.price*(1+self._limit_rate) |           limit_price = buy.price*(1+self._limit_rate) | ||||||
|           stop_price  = buy.price*(1-self._limit_rate) |           stop_price  = buy.price*(1-self._limit_rate) | ||||||
| @@ -49,7 +49,7 @@ class EMA_Chicken: | |||||||
|               self._total  += income |               self._total  += income | ||||||
|               self._remain -= amount |               self._remain -= amount | ||||||
|               if amount > 0: |               if amount > 0: | ||||||
|                 self._logger.info(f"[WIN] <SELL> {amount} / {income} ({self._total})") |                 self._logger.info(f"[WIN] <SELL> {amount} / {income} ({self._remain} / {self._total})") | ||||||
|               break |               break | ||||||
|  |  | ||||||
|             if self._pair.ticker.price < stop_price: |             if self._pair.ticker.price < stop_price: | ||||||
| @@ -62,5 +62,5 @@ class EMA_Chicken: | |||||||
|               self._total  += income |               self._total  += income | ||||||
|               self._remain -= amount |               self._remain -= amount | ||||||
|               if amount > 0: |               if amount > 0: | ||||||
|                 self._logger.info(f"[LOSE] <SELL> {amount} / {income} ({self._total})") |                 self._logger.info(f"[LOSE] <SELL> {amount} / {income} ({self._remain} / {self._total})") | ||||||
|               break |               break | ||||||
|   | |||||||
| @@ -41,7 +41,7 @@ class Hige: | |||||||
|           self._total  += income |           self._total  += income | ||||||
|           self._sell    = None |           self._sell    = None | ||||||
|           if amount > 0: |           if amount > 0: | ||||||
|             self._logger.info("<SELL> {amount} / {income} ({self._total})") |             self._logger.info(f"<SELL> {amount} / {income} ({self._remain} / {self._total})") | ||||||
|           continue |           continue | ||||||
|         elif self._sell_price != expected_sell_price: |         elif self._sell_price != expected_sell_price: | ||||||
|           await self._sell.cancel() |           await self._sell.cancel() | ||||||
| @@ -60,7 +60,7 @@ class Hige: | |||||||
|           self._remain += amount |           self._remain += amount | ||||||
|           self._buy     = None |           self._buy     = None | ||||||
|           if amount > 0: |           if amount > 0: | ||||||
|             self._logger.info("<BUY> {amount} / {outgo} ({self._total})") |             self._logger.info(f"<BUY> {amount} / {outgo} ({self._remain} / {self._total})") | ||||||
|           continue |           continue | ||||||
|         elif self._buy_price != expected_buy_price: |         elif self._buy_price != expected_buy_price: | ||||||
|           await self._buy.cancel() |           await self._buy.cancel() | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								logic/mm.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								logic/mm.py
									
									
									
									
									
								
							| @@ -48,17 +48,14 @@ class MM: | |||||||
|  |  | ||||||
|         if self._sell.done: |         if self._sell.done: | ||||||
|           amount = self._sell.amount - self._sell.remain |           amount = self._sell.amount - self._sell.remain | ||||||
|  |           self._remain -= amount | ||||||
|           if amount > 0: |           if amount > 0: | ||||||
|             income = amount*self._sell.price |             income = amount*self._sell.price | ||||||
|             self._total += income |             self._total += income | ||||||
|             self._logger.info(f"<SELL> {amount} / {income} ({self._total})") |             self._logger.info(f"<SELL> {amount} / {income} ({self._remain} / {self._total})") | ||||||
|           self._remain -= amount |  | ||||||
|           self._sell = None |           self._sell = None | ||||||
|         elif abs(self._sell_price-(ask-self._epsilon)) >= pair.price_unit: |         elif abs(self._sell_price-(ask-self._epsilon)) >= pair.price_unit: | ||||||
|           try: |           await self._sell.cancel() | ||||||
|             await self._sell.cancel() |  | ||||||
|           except Exception: |  | ||||||
|             pass |  | ||||||
|       elif enough_spread and sell_amount >= pair.order_unit: |       elif enough_spread and sell_amount >= pair.order_unit: | ||||||
|         # order SELL |         # order SELL | ||||||
|         self._sell_price = ask-self._epsilon |         self._sell_price = ask-self._epsilon | ||||||
| @@ -66,7 +63,6 @@ class MM: | |||||||
|  |  | ||||||
|       if self._buy is not None: |       if self._buy is not None: | ||||||
|         # check current BUY order |         # check current BUY order | ||||||
|         await asyncio.sleep(0.5) |  | ||||||
|         await self._buy.update() |         await self._buy.update() | ||||||
|  |  | ||||||
|         # get lowest bid |         # get lowest bid | ||||||
| @@ -75,17 +71,14 @@ class MM: | |||||||
|  |  | ||||||
|         if self._buy.done: |         if self._buy.done: | ||||||
|           amount = self._buy.amount - self._buy.remain |           amount = self._buy.amount - self._buy.remain | ||||||
|  |           self._remain += amount | ||||||
|           if amount > 0: |           if amount > 0: | ||||||
|             outgo = amount*self._buy.price |             outgo = amount*self._buy.price | ||||||
|             self._total -= outgo |             self._total -= outgo | ||||||
|             self._logger.info(f"<BUY> {amount} / {outgo} ({self._total})") |             self._logger.info(f"<BUY> {amount} / {outgo} ({self._remain} / {self._total})") | ||||||
|           self._remain += amount |  | ||||||
|           self._buy = None |           self._buy = None | ||||||
|         elif abs(self._buy_price-(bid+self._epsilon)) >= pair.price_unit: |         elif abs(self._buy_price-(bid+self._epsilon)) >= pair.price_unit: | ||||||
|           try: |           await self._buy.cancel() | ||||||
|             await self._buy.cancel() |  | ||||||
|           except Exception: |  | ||||||
|             pass |  | ||||||
|       elif enough_spread and buy_amount > pair.order_unit: |       elif enough_spread and buy_amount > pair.order_unit: | ||||||
|         # order BUY |         # order BUY | ||||||
|         self._buy_price = bid+self._epsilon |         self._buy_price = bid+self._epsilon | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user