Exemplo n.º 1
0
    def create_order(self,
                     instrument,
                     units,
                     sl_distance=None,
                     tsl_distance=None,
                     tp_price=None,
                     comment=None,
                     suppress=False,
                     ret=False):
        ''' Places order with Oanda.

        Parameters
        ==========
        instrument: string
            valid instrument name
        units: int
            number of units of instrument to be bought
            (positive int, eg 'units=50')
            or to be sold (negative int, eg 'units=-100')
        sl_distance: float
            stop loss distance price, mandatory eg in Germany
        tsl_distance: float
            trailing stop loss distance
        tp_price: float
            take profit price to be used for the trade
        comment: str
            string
        '''
        client_ext = ClientExtensions(
            comment=comment) if comment is not None else None
        sl_details = (StopLossDetails(distance=sl_distance,
                                      clientExtensions=client_ext)
                      if sl_distance is not None else None)
        tsl_details = (TrailingStopLossDetails(distance=tsl_distance,
                                               clientExtensions=client_ext)
                       if tsl_distance is not None else None)
        tp_details = (TakeProfitDetails(price=tp_price,
                                        clientExtensions=client_ext)
                      if tp_price is not None else None)

        request = self.ctx.order.market(
            self.account_id,
            instrument=instrument,
            units=units,
            stopLossOnFill=sl_details,
            trailingStopLossOnFill=tsl_details,
            takeProfitOnFill=tp_details,
        )
        try:
            order = request.get('orderFillTransaction')
        except:
            order = request.get('orderCreateTransaction')
        if not suppress:
            print('\n\n', order.dict(), '\n')
        if ret is True:
            return order.dict()
Exemplo n.º 2
0
    def create_order(self, instrument, units, price=None, sl_distance=None,
                     tsl_distance=None, tp_price=None, comment=None,
                     touch=False, suppress=False, ret=False):
        ''' Places order with Oanda.

        Parameters
        ==========
        instrument: string
            valid instrument name
        units: int
            number of units of instrument to be bought
            (positive int, eg 'units=50')
            or to be sold (negative int, eg 'units=-100')
        price: float
            limit order price, touch order price
        sl_distance: float
            stop loss distance price, mandatory eg in Germany
        tsl_distance: float
            trailing stop loss distance
        tp_price: float
            take profit price to be used for the trade
        comment: str
            string
        touch: boolean
            market_if_touched order (requires price to be set)
        suppress: boolean
            whether to suppress print out
        ret: boolean
            whether to return the order object
        '''
        client_ext = ClientExtensions(
            comment=comment) if comment is not None else None
        sl_details = (StopLossDetails(distance=sl_distance,
                                      clientExtensions=client_ext)
                      if sl_distance is not None else None)
        tsl_details = (TrailingStopLossDetails(distance=tsl_distance,
                                               clientExtensions=client_ext)
                       if tsl_distance is not None else None)
        tp_details = (TakeProfitDetails(
            price=tp_price, clientExtensions=client_ext)
            if tp_price is not None else None)
        if price is None:
            request = self.ctx.order.market(
                self.account_id,
                instrument=instrument,
                units=units,
                stopLossOnFill=sl_details,
                trailingStopLossOnFill=tsl_details,
                takeProfitOnFill=tp_details,
            )
        elif touch:
            request = self.ctx.order.market_if_touched(
                self.account_id,
                instrument=instrument,
                price=price,
                units=units,
                stopLossOnFill=sl_details,
                trailingStopLossOnFill=tsl_details,
                takeProfitOnFill=tp_details
            )
        else:
            request = self.ctx.order.limit(
                self.account_id,
                instrument=instrument,
                price=price,
                units=units,
                stopLossOnFill=sl_details,
                trailingStopLossOnFill=tsl_details,
                takeProfitOnFill=tp_details
            )

        # First checking if the order is rejected
        if 'orderRejectTransaction' in request.body:
            order = request.get('orderRejectTransaction')
        elif 'orderFillTransaction' in request.body:
            order = request.get('orderFillTransaction')
        elif 'orderCreateTransaction' in request.body:
            order = request.get('orderCreateTransaction')
        else:
            # This case does not happen.  But keeping this for completeness.
            order = None

        if not suppress and order is not None:
            print('\n\n', order.dict(), '\n')
        if ret is True:
            return order.dict() if order is not None else None
Exemplo n.º 3
0
    def _process_order_paramters(self, **kwargs):
        data = {}
        instrument = None
        pip_unit = None

        if kwargs.get('instrument'):
            instrument = get_symbol(kwargs['instrument'])

            data['instrument'] = instrument

        if kwargs.get('trade_id'):
            data['tradeID'] = str(kwargs['trade_id'])
            trade = self.trades.get(data['tradeID']) or self.get_trade(
                data['tradeID'])
            instrument = trade.instrument

        if instrument:
            pip_unit = pip(instrument)

        if kwargs.get('lots'):
            units = lots_to_units(kwargs['lots'],
                                  kwargs.get('side') or OrderSide.BUY)
            data['units'] = str(units)

        if kwargs.get('type'):
            data['type'] = kwargs['type']

        if kwargs.get('timeInForce'):
            data['timeInForce'] = kwargs['timeInForce'] or TimeInForce.FOK

        if kwargs.get('priceBound'):
            data['priceBound'] = str(kwargs['priceBound'])

        if kwargs.get('price'):
            data['price'] = str(kwargs['price'])

        if kwargs.get('positionFill'):
            data['positionFill'] = kwargs[
                'positionFill'] or OrderPositionFill.DEFAULT

        # The Client Extensions to update for the Order. Do not set, modify, or
        # delete clientExtensions if your account is associated with MT4.
        if kwargs.get('client_id') or kwargs.get('client_tag') or kwargs.get(
                'client_comment'):
            data['clientExtensions'] = ClientExtensions(
                id=kwargs['client_id'],
                tag=kwargs['client_tag'],
                comment=kwargs['client_comment'])

        if kwargs.get('trade_client_id') or kwargs.get(
                'trade_client_tag') or kwargs.get('trade_client_comment'):
            data['tradeClientExtensions'] = ClientExtensions(
                id=kwargs['trade_client_id'],
                tag=kwargs['trade_client_tag'],
                comment=kwargs['trade_client_comment'])

        if kwargs.get('take_profit_price'):
            data['takeProfitOnFill'] = TakeProfitDetails(
                price=str(kwargs['take_profit_price']),
                clientExtensions=data.get('clientExtensions'))

        if kwargs.get('stop_loss_pip') and pip_unit:
            stop_loss_price = pip_unit * Decimal(str(kwargs['stop_loss_pip']))
            data['stopLossOnFill'] = StopLossDetails(
                distance=str(stop_loss_price),
                clientExtensions=data.get('clientExtensions'))

        if kwargs.get('stop_loss_distance'):
            data['stopLossOnFill'] = StopLossDetails(
                distance=str(kwargs['stop_loss_distance']),
                clientExtensions=data.get('clientExtensions'))

        if kwargs.get('trailing_pip'):
            trailing_distance_price = pip_unit * Decimal(
                str(kwargs['trailing_pip']))
            data['trailingStopLossOnFill'] = TrailingStopLossDetails(
                distance=str(trailing_distance_price),
                clientExtensions=data.get('clientExtensions'))

        if kwargs.get('trigger_condition'):
            data['triggerCondition'] = kwargs[
                'trigger_condition'] or OrderTriggerCondition.DEFAULT

        if kwargs.get('gtd_time'):
            # todo confirm gtdTime format
            data['gtdTime'] = str(kwargs['gtd_time'])

        if kwargs.get('client_trade_id'):
            data['clientTradeID'] = kwargs['client_trade_id']

        if kwargs.get('guaranteed'):
            data['guaranteed'] = kwargs['guaranteed']

        if kwargs.get('distance'):
            data['distance'] = kwargs['distance']

        return data