An unofficial API for Firstrade
Project description
firstrade-api
A reverse-engineered python API to interact with the Firstrade Trading platform.
This is not an official api! This api's functionality may change at any time.
This api provides a means of buying and selling stocks through Firstrade. It uses the Session class from requests to get authorization cookies. The rest is done with reverse engineered requests to Firstrade's API.
In order to use Fractional shares you must accept the agreement on the website before using it in this API.
Contribution
I am new to coding and new to open-source. I would love any help and suggestions!
Disclaimer
I am not a financial advisor and not affiliated with Firstrade in any way. Use this tool at your own risk. I am not responsible for any losses or damages you may incur by using this project. This tool is provided as-is with no warranty.
Setup
Install using pypi:
pip install firstrade
Quikstart
The code below will:
- Login and print account info.
- Get a quote for 'INTC' and print out the information
- Place a dry run market order for 'INTC' on the first account in the
account_numbers
list - Print out the order confirmation
- Contains a cancel order example
- Get an option Dates, Quotes, and Greeks
- Place a dry run option order
from firstrade import account, order, symbols
# Create a session
ft_ss = account.FTSession(username="", password="", email = "", profile_path="")
need_code = ft_ss.login()
if need_code:
code = input("Please enter the pin sent to your email/phone: ")
ft_ss.login_two(code)
# Get account data
ft_accounts = account.FTAccountData(ft_ss)
if len(ft_accounts.account_numbers) < 1:
raise Exception("No accounts found or an error occured exiting...")
# Print ALL account data
print(ft_accounts.all_accounts)
# Print 1st account number.
print(ft_accounts.account_numbers[0])
# Print ALL accounts market values.
print(ft_accounts.account_balances)
# Get quote for INTC
quote = symbols.SymbolQuote(ft_ss, ft_accounts.account_numbers[0], "INTC")
print(f"Symbol: {quote.symbol}")
print(f"Tick: {quote.tick}")
print(f"Exchange: {quote.exchange}")
print(f"Bid: {quote.bid}")
print(f"Ask: {quote.ask}")
print(f"Last: {quote.last}")
print(f"Bid Size: {quote.bid_size}")
print(f"Ask Size: {quote.ask_size}")
print(f"Last Size: {quote.last_size}")
print(f"Bid MMID: {quote.bid_mmid}")
print(f"Ask MMID: {quote.ask_mmid}")
print(f"Last MMID: {quote.last_mmid}")
print(f"Change: {quote.change}")
print(f"High: {quote.high}")
print(f"Low: {quote.low}")
print(f"Change Color: {quote.change_color}")
print(f"Volume: {quote.volume}")
print(f"Quote Time: {quote.quote_time}")
print(f"Last Trade Time: {quote.last_trade_time}")
print(f"Real Time: {quote.realtime}")
print(f"Fractional: {quote.is_fractional}")
print(f"Company Name: {quote.company_name}")
# Get positions and print them out for an account.
positions = ft_accounts.get_positions(account=ft_accounts.account_numbers[1])
print(positions)
for item in positions["items"]:
print(
f"Quantity {item["quantity"]} of security {item["symbol"]} held in account {ft_accounts.account_numbers[1]}"
)
# Get account history (past 200)
history = ft_accounts.get_account_history(account=ft_accounts.account_numbers[0])
for item in history["items"]:
print(f"Transaction: {item["symbol"]} on {item["report_date"]} for {item["amount"]}.")
# Create an order object.
ft_order = order.Order(ft_ss)
# Place dry run order and print out order confirmation data.
order_conf = ft_order.place_order(
ft_accounts.account_numbers[0],
symbol="INTC",
price_type=order.PriceType.LIMIT,
order_type=order.OrderType.BUY,
duration=order.Duration.DAY,
quantity=1,
dry_run=True,
)
print(order_conf)
if "order_id" not in order_conf["result"]:
print("Dry run complete.")
print(order_conf["result"])
else:
print("Order placed successfully.")
print(f"Order ID: {order_conf["result"]["order_id"]}.")
print(f"Order State: {order_conf["result"]["state"]}.")
# Cancel placed order
# cancel = ft_accounts.cancel_order(order_conf['result']["order_id"])
# if cancel["result"]["result"] == "success":
# print("Order cancelled successfully.")
# print(cancel)
# Check orders
recent_orders = ft_accounts.get_orders(ft_accounts.account_numbers[0])
print(recent_orders)
#Get option dates
option_first = symbols.OptionQuote(ft_ss, "INTC")
for item in option_first.option_dates["items"]:
print(f"Expiration Date: {item["exp_date"]} Days Left: {item["day_left"]} Expiration Type: {item["exp_type"]}")
# Get option quote
option_quote = option_first.get_option_quote("INTC", option_first.option_dates["items"][0]["exp_date"])
print(option_quote)
# Get option greeks
option_greeks = option_first.get_greek_options("INTC", option_first.option_dates["items"][0]["exp_date"])
print(option_greeks)
print(f"Placing dry option order for {option_quote["items"][0]["opt_symbol"]} with a price of {option_quote["items"][0]["ask"]}.")
print("Symbol readable ticker 'INTC'")
# Place dry option order
option_order = ft_order.place_option_order(
account=ft_accounts.account_numbers[0],
option_symbol=option_quote["items"][0]["opt_symbol"],
order_type=order.OrderType.BUY_OPTION,
price_type=order.PriceType.MARKET,
duration=order.Duration.DAY,
contracts=1,
dry_run=True,
)
print(option_order)
# Delete cookies
ft_ss.delete_cookies()
You can also find this code in test.py
Implemented Features
- Login (With all 2FA methods now supported!)
- Get Quotes
- Get Account Data
- Place Orders and Receive order confirmation
- Get Currently Held Positions
- Fractional Trading support (thanks to @jiak94)
- Check on placed order status. (thanks to @Cfomodz)
- Cancel placed orders
- Options (Orders, Quotes, Greeks)
- Order History
TO DO
- Test options fully
- Give me some Ideas!
Options
I am very new to options trading and have not fully tested this feature.
Please:
- USE THIS FEATURE LIKE IT IS A ALPHA/BETA
- PUT IN A GITHUB ISSUE IF YOU FIND ANY PROBLEMS
If you would like to support me, you can do so here:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file firstrade-0.0.30.tar.gz
.
File metadata
- Download URL: firstrade-0.0.30.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1beed88f1f11abb6981ad099789d76225976f76cc11d1ab2a0dc9981535fd7d0 |
|
MD5 | d2d5627e63283e39ee4d1e96535658b7 |
|
BLAKE2b-256 | 2e043ecdd514379c842e88e55948e417d5c9ccfc1cf06d4d5e1bd8344521daac |
File details
Details for the file firstrade-0.0.30-py3-none-any.whl
.
File metadata
- Download URL: firstrade-0.0.30-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ba785aa0706dc06806a24ab8d653f57f26af1962b8700fa3931c62b471cec85 |
|
MD5 | e72baa62f7f821391a2dc62a54134dfe |
|
BLAKE2b-256 | 9acf02b8f8a4f1aa9ccdc51b9d9cab1e749f3e6e06c6fe96229f7b9a1089e5dd |