Last active
May 18, 2018 21:03
-
-
Save dev4Fun/5e5cc0e4e797f057fc243f2f857b3901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import logging | |
from ccxt import ExchangeError | |
from model.longtrade import LongTrade | |
from model.shorttrade import ShortTrade | |
class TradeExecutor: | |
def __init__(self, exchange, check_timeout: int = 15): | |
self.check_timeout = check_timeout | |
self.exchange = exchange | |
async def execute_trade(self, trade): | |
if isinstance(trade, ShortTrade): | |
await self.execute_short_trade(trade) | |
elif isinstance(trade, LongTrade): | |
await self.execute_long_trade(trade) | |
async def execute_short_trade(self, trade: ShortTrade): | |
sell_price = trade.start_price | |
buy_price = trade.exit_price | |
symbol = trade.exchange_symbol | |
amount = trade.amount | |
order = self.exchange.create_sell_order(symbol, amount, sell_price) | |
logging.info(f'Opened sell order: {amount} of {symbol}. Target sell {sell_price}, buy price {buy_price}') | |
await self._wait_order_complete(order['id']) | |
# post buy order | |
order = self.exchange.create_buy_order(symbol, amount, buy_price) | |
await self._wait_order_complete(order['id']) | |
logging.info(f'Completed short trade: {amount} of {symbol}. Sold at {sell_price} and bought at {buy_price}') | |
async def execute_long_trade(self, trade: LongTrade): | |
buy_price = trade.start_price | |
sell_price = trade.exit_price | |
symbol = trade.exchange_symbol | |
amount = trade.amount | |
order = self.exchange.create_buy_order(symbol, amount, buy_price) | |
logging.info(f'Opened long trade: {amount} of {symbol}. Target buy {buy_price}, sell price {sell_price}') | |
await self._wait_order_complete(order.id) | |
# post sell order | |
order = self.exchange.create_sell_order(symbol, amount, sell_price) | |
await self._wait_order_complete(order.id) | |
logging.info(f'Completed long trade: {amount} of {symbol}. Bought at {buy_price} and sold at {sell_price}') | |
async def _wait_order_complete(self, order_id): | |
status = 'open' | |
while status is 'open': | |
await asyncio.sleep(self.check_timeout) | |
order = self.exchange.fetch_order(order_id) | |
status = order['status'] | |
logging.info(f'Finished order {order_id} with {status} status') | |
# do not proceed further if we canceled order | |
if status == 'canceled': | |
raise ExchangeError('Trade has been canceled') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment