flxtrd


Nameflxtrd JSON
Version 0.2.9 PyPI version JSON
download
home_pagehttps://github.com/glocalflex/GLocalFlexTrade
SummaryPublic client API for the flexible energy trading market GLocalFlex.
upload_time2023-11-15 10:53:57
maintainer
docs_urlNone
authorglocalflex
requires_python>=3.8,<3.12
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
<!-- [![Release](https://img.shields.io/github/v/release/glocalflex/flxtrd)](https://img.shields.io/github/v/release/glocalflex/flxtrd)
[![Build status](https://img.shields.io/github/actions/workflow/status/glocalflex/flxtrd/main.yml?branch=main)](https://github.com/glocalflex/flxtrd/actions/workflows/main.yml?query=branch%3Amain)
[![Commit activity](https://img.shields.io/github/commit-activity/m/glocalflex/flxtrd)](https://img.shields.io/github/commit-activity/m/glocalflex/flxtrd) -->

<!-- [![License](https://img.shields.io/github/license/glocalflex/flxtrd)](https://img.shields.io/github/license/glocalflex/flxtrd) -->




# GLocalFlexTrade Public API

Public client API for the flexible energy trading market GLocalFlex.
The client libary provides standard interface to access the GLocalFlex Market public API.
The client integrates Rest API and AMPQ protocol for communication with the GLocalFlex marketplace.

![Release](https://img.shields.io/github/v/release/glocalflex/glocalflextrade?include_prereleases)
[![Build status](https://img.shields.io/github/actions/workflow/status/glocalflex/GLocalFlexTrade/ci.yml?branch=main)](https://github.com/glocalflex/GLocalFlexTrade/actions/workflows/main.yml?query=branch%3Amain)
[![codecov](https://codecov.io/gh/glocalflex/GLocalFlexTrade/branch/main/graph/badge.svg)](https://codecov.io/gh/glocalflex/GLocalFlexTrade)
[![Commit activity](https://img.shields.io/github/commit-activity/m/glocalflex/GLocalFlexTrade)](https://img.shields.io/github/commit-activity/m/glocalflex/GLocalFlexTrade)
[![License](https://img.shields.io/github/license/glocalflex/GLocalFlexTrade)](https://img.shields.io/github/license/glocalflex/GLocalFlexTrade)


The GLocalFlexTrade client API [Documentation](https://glocalflex.github.io/GLocalFlexTrade/) provides more information how to use the **flxtrd** Python package.

The official **GLocalFlex Market**  [API Documentation](https://www.glocalflexmarket.com/docs/) gives an overview and more details of the public API and energy trading platform.


## Architecture

The basic functionality of the client is to send market orders to the GLocalFlex Market server and receive market responses. The API client is designed to be used in a trading client application.
It is up to the user which additional functionalities are implemented to the client. As an example a trading strategy and energy management logic can be integrated to the client.

``` mermaid
graph LR
    TradingClient --> FlexAPIClient
    TradingClient --> TradingStrategy
    TradingClient --> EnergyManagement
    TradingClient --> CustomPlugins
    FlexAPIClient --> APIProtocols
    FlexAPIClient --> FlexPlugins
```

## Quickstart Guide

Create virtual environment

```sh
python3 -m venv venv
```

Activate virtual environment

```sh
source venv/bin/activate
```

Install GLocalFlexTrade Python package

```sh
pip install flxtrd
```

Update GLocalFlexTrade Python package to latest version

```sh
pip install --upgrade flxtrd
# or
pip install -U flxtrd
```

### Basic trading client example

```py
"""Example usage of the trading client using AMPQ protocol"""
from logging import ERROR, INFO
import sys

from flxtrd import (
    ASK,
    BID,
    FlexAPIClient,
    FlexResource,
    FlexMarket,
    MarketOrder,
    FlexUser,
    log,
    utils,
)


def main() -> None:
    GLOCALFLEX_MARKET_URL = "glocalflexmarket.com"

    user = FlexUser(name="<your_email>",
                    password="<your_password>",
                    access_token="<your_device_access_token>",
                    )

    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)

    # Create a AMPQ client that connects to the message broker
    trading_client = FlexAPIClient(user=user,
                                   market=market
                                   )

    # Define a flexibility resource that will be traded
    # The resource is a 100W power for 60 minutes starting in 5 minutes
    flex_resource = FlexResource(power_w=100,
                                 start_time_epoch_s=utils.utc_timestamp_s() + utils.min_to_s(5),
                                 duration_min=60,
                                 order_expiration_min=50)

    # Create a market ask order to sell flexibility
    market_order = MarketOrder(order_type=ASK,
                               price_eur=100,
                               resource=flex_resource)

    # Send the market order to the message broker
    # The connection to the broker will be initiated automatically
    _, err = trading_client.send_order(market_order=market_order)

    if err: log(ERROR, err); sys.exit(1)

    # Create a market bid order to buy flexibility
    market_order = MarketOrder(order_type=BID,
                               price_eur=100,
                               resource=flex_resource)

    _, err = trading_client.send_order(market_order=market_order)

    if err: log(ERROR, err); sys.exit(1)

    # Check the market responses for closed_deals, price tick messages
    # from the message broker for 60 seconds and exit
    wait_sec = 0
    expected_responses = 3
    log(INFO, f"Waiting for messages from market broker")

    try:
        while wait_sec < 60:
            market_responses = trading_client.check_market_responses()
            if market_responses is not None:
                log(INFO, f"Received {len(market_responses)} responses from market broker")
                # Close the connection to the market message broker
                if len(market_responses) == expected_responses:
                    break

            # Use instead of time.sleep() to allow message broker connection to stay alive
            trading_client.sleep(1)
            wait_sec += 1

    except KeyboardInterrupt:
        log(INFO, "Keyboard interrupt received. Closing connection to the market broker")
    finally:
        trading_client.disconnect()


if __name__ == "__main__":
    main()

```


### Basic REST API client example

```py
"""Example usage of the REST API client"""
from logging import ERROR, INFO
from pprint import pformat
import time
import sys

from flxtrd import (
    ASK,
    BID,
    FlexBroker,
    FlexAPIClient,
    FlexResource,
    FlexMarket,
    MarketOrder,
    FlexUser,
    log,
    utils,
)


def main() -> None:
    GLOCALFLEX_MARKET_URL = "glocalflexmarket.com"

    user = FlexUser(name="<your_email>",
                    password="<your_password>",
                    access_token="<your_device_access_token>"
                    )


    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)

    # Create a AMPQ client that connects to the message broker
    trading_client = FlexAPIClient(user=user,
                                   market=market
                                   )

    # Send a request to the GLocalFlex with REST API
    response, err = trading_client.make_request(method="POST",
                                                endpoint="/users/login",
                                                data={"email": user.name, "password": user.password},
                                                )
    if err:
        log(ERROR, err)

    log(INFO, pformat(response.request_response.json()))
    log(INFO, response.request_response.status_code)


if __name__ == "__main__":
    main()

```


## Listen to market ticker messages
Example usage of the trading client to just listen to market ticker messages

```py

from logging import ERROR, INFO

from flxtrd import (
    FlexAPIClient,
    FlexMarket,
    FlexUser,
    log,

)

def main() -> None:
    GLOCALFLEX_MARKET_URL = "glocalflexmarket.com"

    user = FlexUser(name="<your_email>",
                    password="<your_password>",
                    access_token="<your_device_access_token>"
                    )
    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)

    # Create a AMPQ client that connects to the message broker
    trading_client = FlexAPIClient(user=user,
                                   market=market,
                                   )

    
    trading_client.connect()

    # Check the market responses for closed_deals, price tick messages
    # from the message broker for 60 seconds and exit
    wait_sec = 0
    expected_responses = 1
    log(INFO, f"Waiting for ticker messages from marketplace")


    while True:
        log(INFO, f"Waited {wait_sec} seconds")
        market_responses = trading_client.check_market_responses()
        if market_responses is not None:
            log(INFO, f"Received {len(market_responses)} responses from market broker")
            # Close the connection to the market message broker
            if len(market_responses) == expected_responses:
                break
            
        trading_client.sleep(1)
        wait_sec += 1
        
    trading_client.disconnect()


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        log(INFO, "Keyboard interrupt received. Closing connection to the market broker")
```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/glocalflex/GLocalFlexTrade",
    "name": "flxtrd",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<3.12",
    "maintainer_email": "",
    "keywords": "",
    "author": "glocalflex",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/30/11/781761d42a589d6b533ec125d42d95516d0af74cf39276997db51083213a/flxtrd-0.2.9.tar.gz",
    "platform": null,
    "description": "\n<!-- [![Release](https://img.shields.io/github/v/release/glocalflex/flxtrd)](https://img.shields.io/github/v/release/glocalflex/flxtrd)\n[![Build status](https://img.shields.io/github/actions/workflow/status/glocalflex/flxtrd/main.yml?branch=main)](https://github.com/glocalflex/flxtrd/actions/workflows/main.yml?query=branch%3Amain)\n[![Commit activity](https://img.shields.io/github/commit-activity/m/glocalflex/flxtrd)](https://img.shields.io/github/commit-activity/m/glocalflex/flxtrd) -->\n\n<!-- [![License](https://img.shields.io/github/license/glocalflex/flxtrd)](https://img.shields.io/github/license/glocalflex/flxtrd) -->\n\n\n\n\n# GLocalFlexTrade Public API\n\nPublic client API for the flexible energy trading market GLocalFlex.\nThe client libary provides standard interface to access the GLocalFlex Market public API.\nThe client integrates Rest API and AMPQ protocol for communication with the GLocalFlex marketplace.\n\n![Release](https://img.shields.io/github/v/release/glocalflex/glocalflextrade?include_prereleases)\n[![Build status](https://img.shields.io/github/actions/workflow/status/glocalflex/GLocalFlexTrade/ci.yml?branch=main)](https://github.com/glocalflex/GLocalFlexTrade/actions/workflows/main.yml?query=branch%3Amain)\n[![codecov](https://codecov.io/gh/glocalflex/GLocalFlexTrade/branch/main/graph/badge.svg)](https://codecov.io/gh/glocalflex/GLocalFlexTrade)\n[![Commit activity](https://img.shields.io/github/commit-activity/m/glocalflex/GLocalFlexTrade)](https://img.shields.io/github/commit-activity/m/glocalflex/GLocalFlexTrade)\n[![License](https://img.shields.io/github/license/glocalflex/GLocalFlexTrade)](https://img.shields.io/github/license/glocalflex/GLocalFlexTrade)\n\n\nThe GLocalFlexTrade client API [Documentation](https://glocalflex.github.io/GLocalFlexTrade/) provides more information how to use the **flxtrd** Python package.\n\nThe official **GLocalFlex Market**  [API Documentation](https://www.glocalflexmarket.com/docs/) gives an overview and more details of the public API and energy trading platform.\n\n\n## Architecture\n\nThe basic functionality of the client is to send market orders to the GLocalFlex Market server and receive market responses. The API client is designed to be used in a trading client application.\nIt is up to the user which additional functionalities are implemented to the client. As an example a trading strategy and energy management logic can be integrated to the client.\n\n``` mermaid\ngraph LR\n    TradingClient --> FlexAPIClient\n    TradingClient --> TradingStrategy\n    TradingClient --> EnergyManagement\n    TradingClient --> CustomPlugins\n    FlexAPIClient --> APIProtocols\n    FlexAPIClient --> FlexPlugins\n```\n\n## Quickstart Guide\n\nCreate virtual environment\n\n```sh\npython3 -m venv venv\n```\n\nActivate virtual environment\n\n```sh\nsource venv/bin/activate\n```\n\nInstall GLocalFlexTrade Python package\n\n```sh\npip install flxtrd\n```\n\nUpdate GLocalFlexTrade Python package to latest version\n\n```sh\npip install --upgrade flxtrd\n# or\npip install -U flxtrd\n```\n\n### Basic trading client example\n\n```py\n\"\"\"Example usage of the trading client using AMPQ protocol\"\"\"\nfrom logging import ERROR, INFO\nimport sys\n\nfrom flxtrd import (\n    ASK,\n    BID,\n    FlexAPIClient,\n    FlexResource,\n    FlexMarket,\n    MarketOrder,\n    FlexUser,\n    log,\n    utils,\n)\n\n\ndef main() -> None:\n    GLOCALFLEX_MARKET_URL = \"glocalflexmarket.com\"\n\n    user = FlexUser(name=\"<your_email>\",\n                    password=\"<your_password>\",\n                    access_token=\"<your_device_access_token>\",\n                    )\n\n    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)\n\n    # Create a AMPQ client that connects to the message broker\n    trading_client = FlexAPIClient(user=user,\n                                   market=market\n                                   )\n\n    # Define a flexibility resource that will be traded\n    # The resource is a 100W power for 60 minutes starting in 5 minutes\n    flex_resource = FlexResource(power_w=100,\n                                 start_time_epoch_s=utils.utc_timestamp_s() + utils.min_to_s(5),\n                                 duration_min=60,\n                                 order_expiration_min=50)\n\n    # Create a market ask order to sell flexibility\n    market_order = MarketOrder(order_type=ASK,\n                               price_eur=100,\n                               resource=flex_resource)\n\n    # Send the market order to the message broker\n    # The connection to the broker will be initiated automatically\n    _, err = trading_client.send_order(market_order=market_order)\n\n    if err: log(ERROR, err); sys.exit(1)\n\n    # Create a market bid order to buy flexibility\n    market_order = MarketOrder(order_type=BID,\n                               price_eur=100,\n                               resource=flex_resource)\n\n    _, err = trading_client.send_order(market_order=market_order)\n\n    if err: log(ERROR, err); sys.exit(1)\n\n    # Check the market responses for closed_deals, price tick messages\n    # from the message broker for 60 seconds and exit\n    wait_sec = 0\n    expected_responses = 3\n    log(INFO, f\"Waiting for messages from market broker\")\n\n    try:\n        while wait_sec < 60:\n            market_responses = trading_client.check_market_responses()\n            if market_responses is not None:\n                log(INFO, f\"Received {len(market_responses)} responses from market broker\")\n                # Close the connection to the market message broker\n                if len(market_responses) == expected_responses:\n                    break\n\n            # Use instead of time.sleep() to allow message broker connection to stay alive\n            trading_client.sleep(1)\n            wait_sec += 1\n\n    except KeyboardInterrupt:\n        log(INFO, \"Keyboard interrupt received. Closing connection to the market broker\")\n    finally:\n        trading_client.disconnect()\n\n\nif __name__ == \"__main__\":\n    main()\n\n```\n\n\n### Basic REST API client example\n\n```py\n\"\"\"Example usage of the REST API client\"\"\"\nfrom logging import ERROR, INFO\nfrom pprint import pformat\nimport time\nimport sys\n\nfrom flxtrd import (\n    ASK,\n    BID,\n    FlexBroker,\n    FlexAPIClient,\n    FlexResource,\n    FlexMarket,\n    MarketOrder,\n    FlexUser,\n    log,\n    utils,\n)\n\n\ndef main() -> None:\n    GLOCALFLEX_MARKET_URL = \"glocalflexmarket.com\"\n\n    user = FlexUser(name=\"<your_email>\",\n                    password=\"<your_password>\",\n                    access_token=\"<your_device_access_token>\"\n                    )\n\n\n    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)\n\n    # Create a AMPQ client that connects to the message broker\n    trading_client = FlexAPIClient(user=user,\n                                   market=market\n                                   )\n\n    # Send a request to the GLocalFlex with REST API\n    response, err = trading_client.make_request(method=\"POST\",\n                                                endpoint=\"/users/login\",\n                                                data={\"email\": user.name, \"password\": user.password},\n                                                )\n    if err:\n        log(ERROR, err)\n\n    log(INFO, pformat(response.request_response.json()))\n    log(INFO, response.request_response.status_code)\n\n\nif __name__ == \"__main__\":\n    main()\n\n```\n\n\n## Listen to market ticker messages\nExample usage of the trading client to just listen to market ticker messages\n\n```py\n\nfrom logging import ERROR, INFO\n\nfrom flxtrd import (\n    FlexAPIClient,\n    FlexMarket,\n    FlexUser,\n    log,\n\n)\n\ndef main() -> None:\n    GLOCALFLEX_MARKET_URL = \"glocalflexmarket.com\"\n\n    user = FlexUser(name=\"<your_email>\",\n                    password=\"<your_password>\",\n                    access_token=\"<your_device_access_token>\"\n                    )\n    market = FlexMarket(market_url=GLOCALFLEX_MARKET_URL)\n\n    # Create a AMPQ client that connects to the message broker\n    trading_client = FlexAPIClient(user=user,\n                                   market=market,\n                                   )\n\n    \n    trading_client.connect()\n\n    # Check the market responses for closed_deals, price tick messages\n    # from the message broker for 60 seconds and exit\n    wait_sec = 0\n    expected_responses = 1\n    log(INFO, f\"Waiting for ticker messages from marketplace\")\n\n\n    while True:\n        log(INFO, f\"Waited {wait_sec} seconds\")\n        market_responses = trading_client.check_market_responses()\n        if market_responses is not None:\n            log(INFO, f\"Received {len(market_responses)} responses from market broker\")\n            # Close the connection to the market message broker\n            if len(market_responses) == expected_responses:\n                break\n            \n        trading_client.sleep(1)\n        wait_sec += 1\n        \n    trading_client.disconnect()\n\n\nif __name__ == \"__main__\":\n    try:\n        main()\n    except KeyboardInterrupt:\n        log(INFO, \"Keyboard interrupt received. Closing connection to the market broker\")\n```",
    "bugtrack_url": null,
    "license": "",
    "summary": "Public client API for the flexible energy trading market GLocalFlex.",
    "version": "0.2.9",
    "project_urls": {
        "Documentation": "https://glocalflex.github.io/GLocalFlexTrade/",
        "Homepage": "https://github.com/glocalflex/GLocalFlexTrade",
        "Repository": "https://github.com/glocalflex/GLocalFlexTrade"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "58d0b5926731c0b176d84a123c6d5be79173ce2248b76b0dd722a7dc21e0b18b",
                "md5": "c64fc52f86179b1e784b38985957202f",
                "sha256": "8e4866e02ff2179f40fb518940369f12d65ed258183e870ab1ee8e28b5f57ab4"
            },
            "downloads": -1,
            "filename": "flxtrd-0.2.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c64fc52f86179b1e784b38985957202f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<3.12",
            "size": 19743,
            "upload_time": "2023-11-15T10:53:54",
            "upload_time_iso_8601": "2023-11-15T10:53:54.010428Z",
            "url": "https://files.pythonhosted.org/packages/58/d0/b5926731c0b176d84a123c6d5be79173ce2248b76b0dd722a7dc21e0b18b/flxtrd-0.2.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3011781761d42a589d6b533ec125d42d95516d0af74cf39276997db51083213a",
                "md5": "df60a421fc245d9df45f9df666b6ea4a",
                "sha256": "5cafdac94a9b37efc42ef69a9b8a4a6cd92259f153e5c869605c9a012baafd53"
            },
            "downloads": -1,
            "filename": "flxtrd-0.2.9.tar.gz",
            "has_sig": false,
            "md5_digest": "df60a421fc245d9df45f9df666b6ea4a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<3.12",
            "size": 18525,
            "upload_time": "2023-11-15T10:53:57",
            "upload_time_iso_8601": "2023-11-15T10:53:57.661678Z",
            "url": "https://files.pythonhosted.org/packages/30/11/781761d42a589d6b533ec125d42d95516d0af74cf39276997db51083213a/flxtrd-0.2.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-15 10:53:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "glocalflex",
    "github_project": "GLocalFlexTrade",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "flxtrd"
}
        
Elapsed time: 0.54019s