Ejemplo n.º 1
0
    def __init__(self, hostname, port, ssl=False, application=""):
        """Create an API context for a specific host/port"""

        # Current username for the context
        self.username = None

        extensions = ""

        if application != "":
            extensions = " ({})".format(application)

        # Context headers to add to every request sent to the server
        self._headers = {
            "Content-Type": "application/json",
            "OANDA-Agent": "v20-python/3.0.5{}".format(
                                extensions
                            )
        }

        # Current authentication token
        self.token = None

        # The list of accounts accessible for the current token
        self.token_account_ids = []

        # The base URL for every request made using the context
        self._base_url = "http{}://{}:{}".format(
            "s" if ssl else "",
            hostname,
            port
        )

        # The session used for communicating with the REST server
        self._session = requests.Session()

        self.account = account.EntitySpec(self)
        self.transaction = transaction.EntitySpec(self)
        self.user = user.EntitySpec(self)
        self.trade = trade.EntitySpec(self)
        self.instrument = instrument.EntitySpec(self)
        self.pricing = pricing.EntitySpec(self)
        self.position = position.EntitySpec(self)
        self.order = order.EntitySpec(self)
Ejemplo n.º 2
0
    def __init__(self,
                 hostname,
                 port=443,
                 ssl=True,
                 application="",
                 token=None,
                 decimal_number_as_float=True,
                 stream_chunk_size=512,
                 stream_timeout=10,
                 datetime_format="RFC3339",
                 poll_timeout=2):
        """
        Create an API context for v20 access

        Args:
            hostname: The hostname of the v20 REST server
            port: The port of the v20 REST server
            ssl: Flag to enable/disable SSL
            application: Optional name of the application using the v20 bindings
            token: The authorization token to use when making requests to the
                v20 server
            decimal_number_as_float: Flag that controls whether the string
                representation of floats received from the server should be
                converted into floats or not
            stream_chunk_size: The size of each chunk to read when processing a
                stream response
            stream_timeout: The timeout to use when making a stream request
                with the v20 REST server
            datetime_format: The format to request when dealing with times
            poll_timeout: The timeout to use when making a polling request with
                the v20 REST server
        """

        #
        # V20 REST server hostname
        #
        self.hostname = hostname

        #
        # V20 REST server port
        #
        self.port = port

        #
        # The format to use when dealing with times
        #
        self.datetime_format = datetime_format

        #
        # Form the value for the OANDA-Agent header
        #
        extensions = ""

        if application != "":
            extensions = " ({})".format(application)

        oanda_agent = "v20-python/3.0.15{}".format(extensions)

        #
        # Context headers to add to every request sent to the server
        #
        self._headers = {
            "Content-Type": "application/json",
            "OANDA-Agent": oanda_agent,
            "Accept-Datetime-Format": self.datetime_format
        }

        #
        # Current authentication token
        #
        self.token = None

        if token is not None:
            self.set_token(token)

        #
        # The base URL for every request made using the context
        #
        self._base_url = "http{}://{}:{}".format("s" if ssl else "", hostname,
                                                 port)

        #
        # The session used for communicating with the REST server
        #
        self._session = requests.Session()

        #
        # Flag that controls whether the string representation of floats
        # received from the server should be converted into floats or not
        #
        self.decimal_number_as_float = decimal_number_as_float

        #
        # The size of each chunk to read when processing a stream
        # response
        #
        self.stream_chunk_size = stream_chunk_size

        #
        # The timeout to use when making a stream request with the
        # v20 REST server
        #
        self.stream_timeout = stream_timeout

        #
        # The timeout to use when making a polling request with the
        # v20 REST server
        #
        self.poll_timeout = poll_timeout

        self.account = account.EntitySpec(self)
        self.order = order.EntitySpec(self)
        self.position = position.EntitySpec(self)
        self.user = user.EntitySpec(self)
        self.transaction = transaction.EntitySpec(self)
        self.pricing = pricing.EntitySpec(self)
        self.primitives = primitives.EntitySpec(self)
        self.trade = trade.EntitySpec(self)
        self.instrument = instrument.EntitySpec(self)
Ejemplo n.º 3
0
        #
        # The timeout to use when making a stream request with the
        # v20 REST server
        #
        self.stream_timeout = stream_timeout

        #
        # The timeout to use when making a polling request with the
        # v20 REST server
        #
        self.poll_timeout = poll_timeout

        self.account = account.EntitySpec(self)
        self.order = order.EntitySpec(self)
        self.position = position.EntitySpec(self)
        self.user = user.EntitySpec(self)
        self.transaction = transaction.EntitySpec(self)
        self.pricing = pricing.EntitySpec(self)
        self.primitives = primitives.EntitySpec(self)
        self.site = site.EntitySpec(self)
        self.trade = trade.EntitySpec(self)
        self.instrument = instrument.EntitySpec(self)


    def set_header(self, key, value):
        """
        Set an HTTP header for all requests to the v20 API using
        this context

        Args:
            key: header key to set