Skip to main content

Investfly SDK

Project description

About

Python-SDK to work with Investfly API. Investfly

Investfly offers a platform for developing automated stock trading strategies. Users can create trading strategies through an intuitive drag-and-drop interface or by coding in Python.

The Investfly SDK contains the API and tools needed to build and deploy Python-based trading strategies on the Investfly platform.

Although you can edit Python code in our browser-based editor, writing code in a familiar IDE (Pycharm, VSCode) is recommended due to all the benefits that rich IDE offers. This SDK and CLI that comes with it makes it possible to develop trading strategy locally using your favorite IDE and upload it your Investfly account.

Quickstart

SDK Installation and Setup

Investfly-SDK comes with all required Python classes used for strategy development as well as a command line tool (CLI) called investfly-cli

Using a terminal app, run the following commands.

Setup Project and Virtual Environment

mkdir investfly
cd investfly
mkdir venv
python3 -m venv venv
source venv/bin/activate

Install investfly-sdk

pip install investfly-sdk

Install investfly-sdk Create src directory for source files. mkdir investfly/src cd investfly/src

Launch investfly-cli

Install investfly-sdk also adds investfly-cli in your path inside the virtual environment. It can be launched simply by using investfly-cli command in the virtual env.

(venv) user@host src$ investfly-cli 

investfly-cli$ -h
usage: investfly-cli [-h]
                     {login,logout,strategy.list,strategy.copysamples,strategy.create,strategy.download,strategy.update,strategy.backtest.start,strategy.backtest.stop,strategy.backtest.result,exit}
                     ...

positional arguments:
  {login,logout,strategy.list,strategy.copysamples,strategy.create,strategy.download,strategy.update,strategy.backtest.start,strategy.backtest.stop,strategy.backtest.result,exit}
                        Available Commands
    login               Login to Investfly
    logout              Logout from Investfly
    strategy.list       List Python Strategies
    strategy.copysamples
                        Copy Samples from SDK
    strategy.create     Create a new trading strategy
    strategy.download   Download one of your strategy and save it to a file
    strategy.update     Update strategy Python Code
    strategy.backtest.start
                        Start backtest for strategy
    strategy.backtest.stop
                        Stop backtest for strategy
    strategy.backtest.result
                        Get backtest result, waiting if backtest is still
                        running
    exit                Stop and Exit CLI

options:
  -h, --help            show this help message and exit

investfly-cli$ 

Test Installation

You can test the installation by using the CLI to login and logout of Investfly.

investfly-cli$ login -u <YOUR_USERNAME> -p <YOUR_PASSWORD>
Session(username='xxxxxx', clientId='xxxxx-kaj1p3lv', clientToken='b29c9acc-330a-4821-9187-282d827e3e91')

investfly-cli$ logout

Trading Strategy Development

Investfly-SDK comes with a starter strategy template and many sample strategies to help you get started quickly.

Copy Samples

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

Create New Strategy

You can use one of the samples to create a new strategy. Normally, you would make a copy of the sample strategy, edit the copy using your favorite IDE to create a new strategy. But for now, we'll use the unmodified sample

investfly-cli$ login -u <YOUR_USERNAME> -p <YOUR_PASSWORD>
Session(username='xxxxx', clientId='xxxxxx-krfs61aa', clientToken='766fad47-3e1e-4f43-a77a-72a95a395fec')

investfly-cli$ strategy.create -n MySmaCrossOverStrategy -f ./samples/strategies/SmaCrossOverStrategy.py
Created strategy 83

investfly-cli$ strategy.list
{'strategyId': 83, 'strategyName': 'MySmaCrossOverStrategy'}

Note that the path is relative to the directory from which you launched investfly-cli (i.e investfly/src in this case)

Edit and Update Code

Edit and update ./samples/strategies/SmaCrossOverStrategy.py as you like. For testing, change the StandardSymbolsList.SP_100 to StandardSymbolsList.SP_500 inside getSecurityUniverseSelector function.

Always use a type checker such as mypy to make sure that your code does not have error. Note that you invoke mypy tool from inside the virtual environment, and outside investfly-cli (i.e it's not a investfly-cli command)

(venv) user@host src$ python -m mypy samples
Success: no issues found in 11 source files
investfly-cli$ strategy.update --id 83 -f ./samples/strategies/SmaCrossOverStrategy.py
Code Updated

After the code is updated, next step is to backtest the strategy and deploy it live. You can do them by logging into Investfly with a web browser, navigating to the strategy page and invoking corresponding actions.

IDE Editor

The primary reason for publishing this SDK is so that you can use your favorite IDE Editor to write and update Python Code. We recommend using PyCharm community edition: https://www.jetbrains.com/pycharm/download

Using IDE editor will assist with auto-completion and type hints. Additionally, use type checking tools like mypy to check your code before deploying.

When using the IDE, open investfly directory created above as a project with your IDE. Make sure that Python Interpreter is configured to the virtual environment investfly/venv/bin/python created above.

TA-Lib Stubs

TA-Lib is a technical analysis library https://github.com/TA-Lib/ta-lib-python used to compute technical indicators by Investfly. This library can also be used in custom indicators and strategies. However, installing Python ta-lib wrapper requires installing native ta-lib, which is challenging based on the OS you are working with. So investfly-sdk ships with ta-lib stubs, so when you install investfly-sdk, pip does not try to install ta-lib. This means that you can develop your code, but cannot run them locally if you are using ta-lib in your code. This is generally OK, because you will use the CLI to upload your code to Investfly server, where it will be run. If you want also want to run your code locally to test it, then follow the installation method described in the link above and then install ta-lib with the following command

pip install ta-lib==0.4.28

API Docs

API Docs are published at https://www.investfly.com/apidocs/investfly.html

Getting Help

Please email support@investfly.com for any support or bug report

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

investfly_sdk-2.1.tar.gz (108.5 kB view details)

Uploaded Source

Built Distribution

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

investfly_sdk-2.1-py3-none-any.whl (157.7 kB view details)

Uploaded Python 3

File details

Details for the file investfly_sdk-2.1.tar.gz.

File metadata

  • Download URL: investfly_sdk-2.1.tar.gz
  • Upload date:
  • Size: 108.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for investfly_sdk-2.1.tar.gz
Algorithm Hash digest
SHA256 5a9c538346a3828766edd1fc4a30a34107f135bb2ed729c6a70527dfb94e00bf
MD5 0169771dfc51a1ae6b8144e03a8284fd
BLAKE2b-256 0174e365607044195e739a91d5da99207647e263477cc297b6918ff0d76045d5

See more details on using hashes here.

File details

Details for the file investfly_sdk-2.1-py3-none-any.whl.

File metadata

  • Download URL: investfly_sdk-2.1-py3-none-any.whl
  • Upload date:
  • Size: 157.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for investfly_sdk-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f0308b0141cb81dff3c7b839514ef2b6e2b75ff74e6773bff8bf400e56b8330
MD5 a0c61b323fddbbbca968aa7f1e03bc8d
BLAKE2b-256 dee3ad97ec1570caba4596d8a7f4f9606e37e51d90f209dab238e8d42ca2d662

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