Exemple #1
0
    def send_stop_loss(self, order: Order) -> Trade:
        path = '/v1/me/sendparentorder'
        resp = self.private.base_post(path=path,
                                      parameters=[{
                                          "product_code": order.product_code,
                                          "condition_type": order.order_type,
                                          "side": order.side,
                                          "trigger_price": order.price,
                                          "size": order.units
                                      }])
        if resp['status_code'] >= 400:
            logger.error(
                f'action=send_stop order content={order.product_code}, {order.order_type}, {order.units}, {order.price}, {order.side}'
            )
            logger.error(
                f'action=send_stop error={resp["response"]["error_message"]}')
            raise ValueError
        else:
            logger.info(f'action=send_stop resp={resp["response"]}')

        order_id = resp['response']['parent_order_acceptance_id']
        side = order.side
        price = order.price
        units = order.units
        trade = Trade(trade_id=order_id, side=side, price=price, units=units)

        return trade
Exemple #2
0
    def send_stop_loss(self, order: Order) -> Trade:
        if order.side == constants.BUY:
            units = order.units
        elif order.side == constants.SELL:
            units = -order.units
        order_data = {
            'order': {
                'type': order.order_type,
                'instrument': order.product_code,
                'units': units,
                'price': order.price
            }
        }
        req = orders.OrderCreate(accountID=self.account_id, data=order_data)
        try:
            resp = self.client.request(req)
            logger.info(f'action=send_order resp={resp}')
        except V20Error as e:
            logger.error(f'action=send_order error={e}')
            raise ValueError
        order_id = resp['orderCreateTransaction']['id']
        price = order.price
        units = order.units
        side = order.side
        trade = Trade(trade_id=order_id, side=side, price=price, units=units)

        return trade
Exemple #3
0
    def trade_close(self, trade_id) -> Trade:
        req = trades.TradeClose(self.account_id, trade_id)
        try:
            resp = self.client.request(req)
            logger.info(f'action=trade_close resp={resp}')
        except V20Error as e:
            logger.error(f'action=trade_close error={e}')
            requests.post(
                settings.WEB_HOOK_URL,
                data=json.dumps({
                    'text': u'cannot close trade',  # 通知内容
                }))
            return Trade(0, constants.BUY, 0, 0)

        trade = Trade(trade_id=trade_id,
                      side=constants.BUY
                      if float(resp['orderFillTransaction']['units']) > 0 else
                      constants.SELL,
                      units=float(resp['orderFillTransaction']['units']),
                      price=float(resp['orderFillTransaction']['price']))
        return trade
Exemple #4
0
    def send_trail_stop(self, order: Order) -> Trade:
        if order.side == constants.BUY:
            units = order.units
        elif order.side == constants.SELL:
            units = -order.units
        trailingStopLossOnFill = TrailingStopLossDetails(distance=order.price)
        ordr = MarketOrderRequest(
            instrument=order.product_code,
            units=units,
            trailingStopLossOnFill=trailingStopLossOnFill.data)
        req = orders.OrderCreate(accountID=self.account_id, data=ordr.data)
        try:
            resp = self.client.request(req)
            logger.info(f'action=send_trail_stop resp={resp}')
        except V20Error as e:
            logger.error(f'action=send_trail_stop error={e}')
            return Trade(trade_id=None, side='BUY', price=0, units=0)
        order_id = int(resp['orderCreateTransaction']['id']) + 1
        price = resp['orderFillTransaction']['price']
        units = units
        side = order.side
        trade = Trade(trade_id=order_id, side=side, price=price, units=units)

        return trade
Exemple #5
0
    def trade_details(self, product_code, order_id) -> Trade:
        path = '/v1/me/getexecutions' + '?product_code=' + product_code + '&child_order_acceptance_id=' + order_id
        resp = self.private.base_get(path=path)
        if resp["status_code"] >= 400 or resp["response"] == []:
            logger.error(f'action=trade_details error')
            raise
        else:
            response = resp["response"][0]
            logger.info(f'action=trade_details resp={response}')

        trade = Trade(trade_id=response['id'],
                      side=response['side'],
                      units=float(response['size']),
                      price=float(response['price']))
        return trade
Exemple #6
0
    def trade_details(self, trade_id) -> Trade:
        req = trades.TradeDetails(self.account_id, trade_id)
        try:
            resp = self.client.request(req)
            logger.info(f'action=trade_details resp={resp}')
        except V20Error as e:
            logger.error(f'action=trade_details error={e}')
            raise

        trade = Trade(
            trade_id=trade_id,
            side=constants.BUY
            if float(resp['trade']['currentUnits']) > 0 else constants.SELL,
            units=float(resp['trade']['currentUnits']),
            price=float(resp['trade']['price']))
        return trade
Exemple #7
0
    def get_open_trade(self) -> list:
        req = trades.OpenTrades(self.account_id)
        try:
            resp = self.client.request(req)
            logger.info(f'action=get_open_trade resp={resp}')
        except V20Error as e:
            logger.error(f'action=get_open_trade error={e}')
            raise

        trades_list = []
        for trade in resp['trades']:
            trades_list.insert(
                0,
                Trade(trade_id=trade['id'],
                      side=constants.BUY
                      if float(trade['currentUnits']) > 0 else constants.SELL,
                      units=float(trade['currentUnits']),
                      price=float(trade['price'])))
        return trades_list
Exemple #8
0
    def get_open_trade(self,
                       product_code=constants.PRODUCT_CODE_FX_BTC_JPY) -> list:
        path = '/v1/me/getpositions' + '?product_code=' + product_code
        resp = self.private.base_get(path=path)
        if resp["status_code"] >= 400:
            logger.error(
                f'action=get_open_trade error={resp["response"]["error_message"]}'
            )
            return None
        else:
            response = resp["response"]
            logger.info(f'action=get_open_trade resp={response}')

        trades_list = []
        for i, trade in enumerate(response):
            trades_list.insert(
                0,
                Trade(trade_id=i,
                      side=trade["side"],
                      units=float(trade['size']),
                      price=float(trade['price'])))
        return trades_list