Skip to main content

Public client API for the flexible energy trading market GLocalFlex.

Project description

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 Build status codecov Commit activity License

The GLocalFlexTrade client API Documentation provides more information how to use the flxtrd Python package.

The official GLocalFlex Market API Documentation 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.

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

Quickstart Guide

Create virtual environment

python3 -m venv venv

Activate virtual environment

source venv/bin/activate

Install GLocalFlexTrade Python package

pip install flxtrd

Update GLocalFlexTrade Python package to latest version

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

Basic trading client example

"""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

"""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

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")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flxtrd-0.2.9.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flxtrd-0.2.9-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file flxtrd-0.2.9.tar.gz.

File metadata

  • Download URL: flxtrd-0.2.9.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/6.2.0-36-generic

File hashes

Hashes for flxtrd-0.2.9.tar.gz
Algorithm Hash digest
SHA256 5cafdac94a9b37efc42ef69a9b8a4a6cd92259f153e5c869605c9a012baafd53
MD5 df60a421fc245d9df45f9df666b6ea4a
BLAKE2b-256 3011781761d42a589d6b533ec125d42d95516d0af74cf39276997db51083213a

See more details on using hashes here.

File details

Details for the file flxtrd-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: flxtrd-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/6.2.0-36-generic

File hashes

Hashes for flxtrd-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 8e4866e02ff2179f40fb518940369f12d65ed258183e870ab1ee8e28b5f57ab4
MD5 c64fc52f86179b1e784b38985957202f
BLAKE2b-256 58d0b5926731c0b176d84a123c6d5be79173ce2248b76b0dd722a7dc21e0b18b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page