コード例 #1
0
    def try_entry_order(self, pair):
        """ Gets the latest market data and runs the strategy on it.
			If strategy says to buy, it buys. """

        bot = self.bot
        session = self.session
        exchange = self.exchange
        strategy = self.strategy

        symbol = pair.symbol
        print("Checking signal on", symbol)
        df = exchange.getSymbolKlines(symbol,
                                      "5m",
                                      limit=strategy.minimum_period)
        l = len(df) - 1
        strategy.setup(df)

        # if long bot
        buy_signal = strategy.checkBuySignal(l)
        if buy_signal:

            print("BUY! on", symbol)
            desired_price = Decimal(df['close'][l])
            quote_qty = Decimal(bot.starting_balance) * Decimal(
                bot.trade_allocation) / Decimal(100)
            desired_quantity = quote_qty / desired_price
            order = Order(bot_id=bot.id,
                          symbol=symbol,
                          status="NEW",
                          side="BUY",
                          is_entry=True,
                          entry_price=desired_price,
                          original_quantity=desired_quantity,
                          is_test=bot.test_run)

            session.add(order)
            session.commit()

            order_response = dict()
            if bot.test_run:
                order_response = dict(message='success')
            else:
                order_response = exchange.placeLimitOrder(
                    symbol=symbol,
                    price=desired_price,
                    side="BUY",
                    amount=desired_quantity,
                    custom_id=order.id)

            if exchange.isValidResponse(order_response):
                print("SUCCESSFUL ORDER! on", symbol)
                exchange.updateSQLOrderModel(order, order_response, bot)
                pair.current_order_id = order.id
                pair.active = False
                bot.current_balance = bot.current_balance - quote_qty
                session.commit()
            else:
                print("ERROR placing order! on", symbol)
                session.query(Order).filter(Order.id == order.id).delete()
                session.commit()
コード例 #2
0
ファイル: OrderManager.py プロジェクト: licensetocole/pyjuque
def createOrderModel(symbol, test_mode, order_params, order):
    """ Create Order Model and fill only mandatory params. 
    Other params are filled after order is filled. """
    if 'price' not in order_params:
        order_params['price'] = None
    if 'take_profit_price' not in order_params:
        order_params['take_profit_price'] = None
    if 'stop_price' not in order_params:
        order_params['stop_price'] = None
    if 'is_entry' not in order_params:
        order_params['is_entry'] = False
    if order is None:
        position_id = str(uuid4())
        entry_price = None
    elif order is not None:
        position_id = order.position_id
        entry_price = order.entry_price

    new_order_model = Order(
        id=str(uuid4()),
        position_id=position_id,
        bot_id=order_params['bot_id'],
        symbol=symbol,
        price=order_params['price'],
        take_profit_price=order_params['take_profit_price'],
        order_type=order_params['order_type'],
        stop_price=order_params['stop_price'],
        original_quantity=order_params['quantity'],
        side=order_params['side'],
        is_entry=order_params['is_entry'],
        is_test=test_mode,
        entry_price=entry_price,
        last_checked_time=int(round(time.time() * 1000)))
    return new_order_model
コード例 #3
0
    def try_exit_order(self, order):
        """ Checks whether the order has been filled or not. 
			If it has, and it was an entry order, it places the 
			corresponding exit order. If it was an exit order, it 
			resumes trading on that pair. """

        symbol = order.symbol

        bot = self.bot
        session = self.session
        exchange = self.exchange
        strategy = self.strategy
        pair: Pair = bot.getPairWithSymbol(session, symbol)
        order: Order = session.query(Order).get(order.id)
        exit_settings = session.query(ExitSettings).get(bot.exit_settings_id)

        print("Checking", order, "on", symbol)
        if order.is_test:
            self.updateTestOrder(order)
        else:
            exchange_order_info = exchange.getOrder(symbol=symbol,
                                                    order_id=order.id,
                                                    is_custom_id=True)
            if not exchange.isValidResponse(exchange_order_info):
                print("Error checking order, will try again next time")
                return
            exchange.updateSQLOrderModel(order, exchange_order_info, bot)
            session.commit()

        # Now check if order was filled
        if order.status == exchange.ORDER_STATUS_FILLED:
            print("FILLED!")
            if order.is_entry:
                # If this entry order has been filled, place the corresponding exit order

                entry_price = Decimal(order.entry_price) * (
                    Decimal(100) + exit_settings.profit_target) / Decimal(100)
                stop_loss_price = Decimal(
                    order.entry_price) * bot.stop_loss_target / Decimal(100)

                print("Entry order, place exit order! on", symbol)
                new_order_model = Order(
                    bot_id=bot.id,
                    symbol=symbol,
                    status="NEW",
                    side="SELL",
                    is_entry=False,
                    entry_price=entry_price,
                    stop_loss_price=stop_loss_price,
                    original_quantity=order.executed_quantity,
                    is_test=bot.test_run)

                session.add(new_order_model)
                session.commit()

                new_order_response = dict()
                if bot.test_run:
                    new_order_response = dict(message="success")
                else:
                    new_order_response = exchange.placeLimitOrder(
                        symbol=symbol,
                        price=entry_price,
                        side="SELL",
                        amount=order.executed_quantity,
                        custom_id=new_order_model.id)

                if exchange.isValidResponse(new_order_response):
                    exchange.updateSQLOrderModel(new_order_model,
                                                 new_order_response, bot)
                    new_order_model.matched_order_id = order.id
                    order.is_closed = True
                    order.matched_order_id = new_order_model.id
                    pair.active = False
                    pair.current_order_id = new_order_model.id
                    session.commit()
                else:
                    session.query(Order).filter(
                        Order.id == new_order_model.id).delete()
                    session.commit()
            else:
                # If this exit order has been filled, resume trading
                print("Exit order, resume trading! on", symbol)
                order.is_closed = True
                pair.active = True
                pair.current_order_id = None
                bot.current_balance = bot.current_balance + order.entry_price * order.executed_quantity
                session.commit()