Ejemplo n.º 1
0
    def get_current_data(self, currency_pair, candle_types,
                         time_frame_granularity):
        # Create the Oanda API context
        api_context = v20.Context(Config.get_host_name(),
                                  Config.get_port(),
                                  Config.get_ssl(),
                                  application="sample_code",
                                  token=Config.get_api_token(),
                                  datetime_format=Config.get_date_format())

        # Create the key word arguments for the API
        kwargs = {}
        kwargs['granularity'] = time_frame_granularity
        kwargs['alignmentTimezone'] = Config.get_time_zone()
        kwargs['count'] = 1

        for candle_type in candle_types:
            if candle_type == 'bid':
                kwargs['price'] = 'B' + kwargs.get('price', '')

            elif candle_type == 'ask':
                kwargs['price'] = 'A' + kwargs.get('price', '')

            elif candle_type == 'mid':
                kwargs['price'] = 'M' + kwargs.get('price', '')

        # Use the Oanda API context as well as the key word arguments to get the historical currency data
        response = api_context.instrument.candles(currency_pair, **kwargs)

        # If the API call was unsucessful, return null for the candles data as well as the response error message
        if response.status != 200:
            return None, str(response) + '\n' + str(response.body)

        # Otherwise, return the candles data and null for the error message
        return response.get("candles", 200), None
Ejemplo n.º 2
0
    def get_historical_data(self, currency_pair, candle_types,
                            time_frame_granularity, from_time, to_time):
        # Check the parameters
        valid_params, error_message = self._check_historical_data_parameters(
            currency_pair, candle_types, time_frame_granularity, from_time,
            to_time)

        # If the parameters aren't valid, return null for the candles data as well as the error message
        if not valid_params:
            return None, error_message

        # Create the Oanda API context
        api_context = v20.Context(Config.get_host_name(),
                                  Config.get_port(),
                                  Config.get_ssl(),
                                  application="sample_code",
                                  token=Config.get_api_token(),
                                  datetime_format=Config.get_date_format())

        # Create the key word arguments for the API
        kwargs = {}
        kwargs['granularity'] = time_frame_granularity
        kwargs['fromTime'] = api_context.datetime_to_str(
            datetime.strptime(from_time, '%Y-%m-%d %H:%M:%S'))
        kwargs['toTime'] = api_context.datetime_to_str(
            datetime.strptime(to_time, '%Y-%m-%d %H:%M:%S'))
        kwargs['alignmentTimezone'] = Config.get_time_zone()

        for candle_type in candle_types:
            if candle_type == 'bid':
                kwargs['price'] = 'B' + kwargs.get('price', '')

            elif candle_type == 'ask':
                kwargs['price'] = 'A' + kwargs.get('price', '')

            elif candle_type == 'mid':
                kwargs['price'] = 'M' + kwargs.get('price', '')

        # Use the Oanda API context as well as the key word arguments to get the historical currency data
        response = api_context.instrument.candles(currency_pair, **kwargs)

        # If the API call was unsucessful, return null for the candles data as well as the response error message
        if response.status != 200:
            return None, str(response) + '\n' + str(response.body)

        # Otherwise, return the candles data and null for the error message
        return response.get("candles", 200), None