Skip to main content

A package that replicates an ETFs by buying the underlying assets

Project description

ReplicateETF

Disclaimer:

There can be significant tax, financial, and legal consequences related to this repository and its use. Signficant losses can occur. Trading equities is always dangerous and should be approached with extreme caution. By using this repository, you accept all liability or consequences related to the code provided here.

Context:

This repository allows users to purchase the underlying stocks of an ETF.

This repository uses Alpaca API, but the code can be reused for other trading platform like IEX. To directly use the package defined here, please setup an Alpaca API account.

Export Alpaca API Keys and Base URL

This example showcases exporting the basic API keys needed to execute some of the available functions. The BASE_URL defined below corresponds to the paper trading for testing algorithms. There is an alternative BASE_URL for live trading.

Example:

export APCA_API_KEY_ID=<api key>
export APCA_API_SECRET_KEY=<secret key>
export APCA_API_BASE_URL=https://paper-api.alpaca.markets

Here is some more documentation from Alpaca on how to setup API Keys.

Download the Package:

pip install -i https://test.pypi.org/simple/ replicateETF

Import package:

from replicateETF.scrapeETF import ETFHandler

Example use:

from replicateETF.scrapeETF import ETFHandler
from alpaca_trade_api.rest import REST

myObj = ETFHandler()
symbol = "VOO"
etfAssetDict = myObj.getETFTable(symbol)

# identify the mimimum amount of purchasing power to build ETF
minimumDollars = myObj.getMimimumDollars(symbol)

# determine whether available cash enough to build ETF
if myObj.getAvailableCash() < minimumDollars:
    print ("Insufficient fund to build ETF")
else:
    investmentAmount = -1
    while investmentAmount < minimumDollars:
        investmentAmount = float(input(f"Based on the ETF, please enter a value greater than {minimumDollars} to invest: "))
    #build Alpaca client
    api = REST()
    for key in etfAssetDict.keys():
        equity = etfAssetDict[key]
        percent = equity['percent']/100
        # TODO - should be a separate function to enable retry logic
        try:
            orderResponse=api.submit_order(symbol=key, 
                            notional=investmentAmount*percent, 
                            side="buy",
                            type="market")
            print(orderResponse)
        except Exception as e:
            print(e)

This example case is listed as test.py in the repository.

Install dependencies (these should automatically be download but just in case):

  • beautifulsoup4>=4.11.1
  • alpaca-trade-api>=2.0.0
  • selenium>=4.1.3

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

replicateETF-0.0.16.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

replicateETF-0.0.16-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file replicateETF-0.0.16.tar.gz.

File metadata

  • Download URL: replicateETF-0.0.16.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for replicateETF-0.0.16.tar.gz
Algorithm Hash digest
SHA256 e23b1f1ea59e4cd98bfef3e3cb17b551166fc301113a829efb17e7337314f9cc
MD5 234f591d53af5f0dd06ef01ab4fcad0a
BLAKE2b-256 b98a78f43cef241bf97da206dfad43fd818a422cf894a209c52b45b6cd8be8a9

See more details on using hashes here.

File details

Details for the file replicateETF-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: replicateETF-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for replicateETF-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 63a1f4eb208a21362446e45109d25b0e8b88bc14e2d1a6a28074740bf0c00c9d
MD5 bc96df8abe4a63b39e0e5ae5aadd3e17
BLAKE2b-256 7b8cb877a4ff5d6d2f913993ae0970e4b2057c157b1e68aff1f7ea1f059a0352

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