investfly

The public Python SDK for building on Investfly.

Investfly lets you develop automated trading strategies and custom indicators in Python, work locally in your preferred IDE, and run the finished code on the Investfly live-trading and backtest infrastructure.

Overview

The SDK provides:

  • typed market-data, portfolio, strategy, and indicator models;
  • base contracts for custom strategies and custom indicators;
  • authenticated REST API clients;
  • the investfly-cli command-line application; and
  • type information and authoring helpers for local IDE completion and mypy checks.

Investfly owns the execution algorithms. Your code declares its universe, callbacks, configuration, and runtime-managed policies using these public contracts.

Install

Python 3.11 or later is required. Create a virtual environment so the SDK and its command-line tools stay isolated from other Python projects:

mkdir investfly-project
cd investfly-project
python3 -m venv venv
source venv/bin/activate
python -m pip install --upgrade investfly-sdk
python -c "import investfly; print(investfly.__version__)"

On Windows PowerShell, activate the environment with .\venv\Scripts\Activate.ps1 instead. The final command prints the installed SDK version and confirms that Python can import it.

Quickstart: copy and check the examples

The installation adds both the investfly Python package and an interactive investfly-cli application. Start it from the project directory:

investfly-cli

Commands are entered at the investfly-cli$ prompt, not appended to the shell command. Display the available commands, copy the bundled examples into ./samples, and leave the CLI:

investfly-cli$ -h
investfly-cli$ copysamples
Samples copied to ./samples directory
investfly-cli$ exit

Type-check the copied strategy and indicator examples from the normal terminal prompt:

python -m mypy --check-untyped-defs samples

This workflow does not require an Investfly account and verifies the complete local authoring setup.

Quickstart: call the Investfly API

Use InvestflyApiClient as the authenticated entry point. The following example prompts for credentials rather than storing them in source code. It requires an Investfly account and network access.

from getpass import getpass

from investfly.api.InvestflyApiClient import InvestflyApiClient

api = InvestflyApiClient()

try:
    username = input("Investfly username: ")
    password = getpass("Investfly password: ")
    api.login(username, password)
    strategies = api.strategyApi.listStrategies()
    for strategy in strategies:
        print(strategy.strategyName)
finally:
    if api.isLoggedIn():
        api.logout()

The grouped endpoint clients are available as api.marketApi, api.portfolioApi, api.strategyApi, and api.indicatorApi.

Quickstart: create a custom strategy

Subclass TradingStrategy, choose the securities to evaluate, and add a market-data callback and/or one or more scheduled callbacks:

from typing import List, Optional

from investfly.models import (
    BarInterval,
    DataType,
    Security,
    SecurityType,
    SecurityUniverseSelector,
    TradeOrder,
    TradingStrategy,
    data_trigger,
)


class MyStrategy(TradingStrategy):
    def getSecurityUniverseSelector(self) -> SecurityUniverseSelector:
        return SecurityUniverseSelector.fromSymbols(
            SecurityType.STOCK,
            ["AAPL", "MSFT"],
        )

    @data_trigger(type=DataType.BARS, barInterval=BarInterval.FIFTEEN_MINUTE)
    def onMarketData(self, updatedSecurities: List[Security]) -> Optional[List[TradeOrder]]:
        # Read market data through self.dataService and request runtime-planned
        # orders through self.services.
        return None

Save the example as MyStrategy.py, then check it locally before uploading:

python -m mypy --check-untyped-defs MyStrategy.py

To upload this file, start investfly-cli and enter these commands at its prompt, replacing the placeholders with your account credentials:

investfly-cli$ login -u YOUR_USERNAME -p YOUR_PASSWORD
investfly-cli$ strategy.create -f MyStrategy.py
investfly-cli$ logout
investfly-cli$ exit

The CLI uses the strategy class name as its Investfly name. Backtest and deploy the uploaded strategy from the Investfly website.

Where to go next

The SDK distribution also includes runnable strategy and indicator examples. For installation or API support, contact support@investfly.com.