Skip to content

Commit

Permalink
Merge pull request #8 from jiak94/account_api
Browse files Browse the repository at this point in the history
Implement Account API
  • Loading branch information
jiak94 authored Jan 19, 2024
2 parents c891d61 + 6587256 commit ea8bb46
Show file tree
Hide file tree
Showing 10 changed files with 1,881 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ if your are using poetry

### Account

:white_square_button: Get User Profile
:white_check_mark: Get User Profile

:white_square_button: Get Balances
:white_check_mark: Get Balances

:white_check_mark: Get Positions

:white_square_button: Get History
:white_check_mark: Get History

:white_square_button: Get Gain/Loss
:white_check_mark: Get Gain/Loss

:white_check_mark: Get Orders

Expand Down
97 changes: 97 additions & 0 deletions asynctradier/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,100 @@ class OptionType(StrEnum):

call = "call"
put = "put"


class Classification(StrEnum):
"""
Enum class representing different classifications.
Possible values:
- individual
- entity
- joint_survivor
- traditional_ira
- roth_ira
- rollover_ira
- sep_ira
"""

individual = "individual"
entity = "entity"
joint_survivor = "joint_survivor"
traditional_ira = "traditional_ira"
roth_ira = "roth_ira"
rollover_ira = "rollover_ira"
sep_ira = "sep_ira"


class AccountStatus(StrEnum):
"""
Represents the status of an account.
Attributes:
open (str): The account is open.
closed (str): The account is closed.
"""

active = "active"
closed = "closed"


class AccountType(StrEnum):
"""
Represents the type of account.
Attributes:
cash (str): The account is a cash account.
margin (str): The account is a margin account.
pdt (str): The account is a pattern day trader account.
"""

cash = "cash"
margin = "margin"
pdt = "pdt"


class EventType(StrEnum):
"""
Represents the type of an event.
Attributes:
trade (str): The event is a trade event.
journal (str): The event is a journal event.
option (str): The event is an option event.
ach (str): The event is an ACH event.
wire (str): The event is a wire event.
dividend (str): The event is a dividend event.
fee (str): The event is a fee event.
tax (str): The event is a tax event.
check (str): The event is a check event.
transfer (str): The event is a transfer event.
adjustment (str): The event is an adjustment event.
interest (str): The event is an interest event.
"""

trade = "trade"
journal = "journal"
option = "option"
ach = "ach"
wire = "wire"
dividend = "dividend"
fee = "fee"
tax = "tax"
check = "check"
transfer = "transfer"
adjustment = "adjustment"
interest = "interest"


class TradeType(StrEnum):
"""
Represents the type of a trade.
Attributes:
buy (str): The trade is a buy trade.
sell (str): The trade is a sell trade.
"""

equity = "equity"
option = "option"
210 changes: 210 additions & 0 deletions asynctradier/common/account_balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
from asynctradier.common import AccountType


class CashAccountBalanceDetails:
"""
Represents the details of a cash account balance.
Attributes:
cash_available (float): The amount of cash available in the account.
sweep (float): The amount of cash swept from the account.
unsettled_funds (float): The amount of funds that are currently unsettled.
"""

def __init__(self, **kwargs):
self.cash_available = kwargs.get("cash_available", 0.0)
self.sweep = kwargs.get("sweep", 0.0)
self.unsettled_funds = kwargs.get("unsettled_funds", 0.0)

def to_dict(self):
"""
Converts the AccountBalance object to a dictionary.
Returns:
dict: A dictionary representation of the AccountBalance object.
"""
return {
"cash_available": self.cash_available,
"sweep": self.sweep,
"unsettled_funds": self.unsettled_funds,
}

def __str__(self):
return f"CashAccountBalanceDetails(capacity={self.cash_available}, sweep={self.sweep}, unsettled_funds={self.unsettled_funds})"


class MarginAccountBalanceDetails:
"""
Represents the details of a margin account balance.
Attributes:
fed_call (float): The federal call amount.
maintenance_call (float): The maintenance call amount.
option_buying_power (float): The buying power for options.
stock_buying_power (float): The buying power for stocks.
stock_short_value (float): The value of shorted stocks.
sweep (float): The sweep amount.
"""

def __init__(self, **kwargs):
self.fed_call = kwargs.get("fed_call", 0.0)
self.maintenance_call = kwargs.get("maintenance_call", 0.0)
self.option_buying_power = kwargs.get("option_buying_power", 0.0)
self.stock_buying_power = kwargs.get("stock_buying_power", 0.0)
self.stock_short_value = kwargs.get("stock_short_value", 0.0)
self.sweep = kwargs.get("sweep", 0.0)

def to_dict(self):
"""
Converts the AccountBalance object to a dictionary.
Returns:
dict: A dictionary representation of the AccountBalance object.
"""
return {
"fed_call": self.fed_call,
"maintenance_call": self.maintenance_call,
"option_buying_power": self.option_buying_power,
"stock_buying_power": self.stock_buying_power,
"stock_short_value": self.stock_short_value,
"sweep": self.sweep,
}

def __str__(self):
return f"MarginAccountBalanceDetails(fed_call={self.fed_call}, maintenance_call={self.maintenance_call}, option_buying_power={self.option_buying_power}, stock_buying_power={self.stock_buying_power}, stock_short_value={self.stock_short_value}, sweep={self.sweep})"


class PDTAccountBalanceDetails:
"""
Represents the account balance details for a Pattern Day Trader (PDT).
Attributes:
fed_call (float): The amount of the Federal Call.
maintenance_call (float): The amount of the Maintenance Call.
option_buying_power (float): The buying power for options trading.
stock_buying_power (float): The buying power for stock trading.
stock_short_value (float): The value of shorted stocks.
"""

def __init__(self, **kwargs):
self.fed_call = kwargs.get("fed_call", 0.0)
self.maintenance_call = kwargs.get("maintenance_call", 0.0)
self.option_buying_power = kwargs.get("option_buying_power", 0.0)
self.stock_buying_power = kwargs.get("stock_buying_power", 0.0)
self.stock_short_value = kwargs.get("stock_short_value", 0.0)

def to_dict(self):
"""
Converts the AccountBalance object to a dictionary.
Returns:
dict: A dictionary representation of the AccountBalance object.
"""
return {
"fed_call": self.fed_call,
"maintenance_call": self.maintenance_call,
"option_buying_power": self.option_buying_power,
"stock_buying_power": self.stock_buying_power,
"stock_short_value": self.stock_short_value,
}

def __str__(self):
return f"PDTAccountBalanceDetails(fed_call={self.fed_call}, maintenance_call={self.maintenance_call}, option_buying_power={self.option_buying_power}, stock_buying_power={self.stock_buying_power}, stock_short_value={self.stock_short_value})"


class AccountBalance:
"""
Represents the balance of an account.
Attributes:
option_short_value (float): The short value of options in the account.
total_equity (float): The total equity of the account.
account_number (str): The account number.
account_type (AccountType): The type of the account.
close_pl (float): The close profit/loss of the account.
current_requirement (float): The current requirement of the account.
equity (float): The equity of the account.
long_market_value (float): The long market value of the account.
market_value (float): The market value of the account.
open_pl (float): The open profit/loss of the account.
option_long_value (float): The long value of options in the account.
option_requirement (float): The option requirement of the account.
pending_orders_count (int): The count of pending orders in the account.
short_market_value (float): The short market value of the account.
stock_long_value (float): The long value of stocks in the account.
total_cash (float): The total cash in the account.
uncleared_funds (float): The uncleared funds in the account.
pending_cash (float): The pending cash in the account.
cash (CashAccountBalanceDetails): The details of the cash account balance (if account type is cash).
margin (MarginAccountBalanceDetails): The details of the margin account balance (if account type is margin).
pdt (PDTAccountBalanceDetails): The details of the PDT account balance (if account type is pdt).
"""

def __init__(self, **kwargs):
self.option_short_value = kwargs.get("option_short_value")
self.total_equity = kwargs.get("total_equity")
self.account_number = kwargs.get("account_number")
self.account_type = (
AccountType(kwargs.get("account_type"))
if kwargs.get("account_type")
else None
)
self.close_pl = kwargs.get("close_pl")
self.current_requirement = kwargs.get("current_requirement")
self.equity = kwargs.get("equity")
self.long_market_value = kwargs.get("long_market_value")
self.market_value = kwargs.get("market_value")
self.open_pl = kwargs.get("open_pl")
self.option_long_value = kwargs.get("option_long_value")
self.option_requirement = kwargs.get("option_requirement")
self.pending_orders_count = kwargs.get("pending_orders_count")
self.short_market_value = kwargs.get("short_market_value")
self.stock_long_value = kwargs.get("stock_long_value")
self.total_cash = kwargs.get("total_cash")
self.uncleared_funds = kwargs.get("uncleared_funds")
self.pending_cash = kwargs.get("pending_cash")

self.cash = (
CashAccountBalanceDetails(**kwargs.get("cash"))
if kwargs.get("cash")
else None
)
self.margin = (
MarginAccountBalanceDetails(**kwargs.get("margin"))
if kwargs.get("margin")
else None
)
self.pdt = (
PDTAccountBalanceDetails(**kwargs.get("pdt")) if kwargs.get("pdt") else None
)

def to_dict(self):
"""
Converts the AccountBalance object to a dictionary.
Returns:
dict: A dictionary representation of the AccountBalance object.
"""
return {
"option_short_value": self.option_short_value,
"total_equity": self.total_equity,
"account_number": self.account_number,
"account_type": self.account_type,
"close_pl": self.close_pl,
"current_requirement": self.current_requirement,
"equity": self.equity,
"long_market_value": self.long_market_value,
"market_value": self.market_value,
"open_pl": self.open_pl,
"option_long_value": self.option_long_value,
"option_requirement": self.option_requirement,
"pending_orders_count": self.pending_orders_count,
"short_market_value": self.short_market_value,
"stock_long_value": self.stock_long_value,
"total_cash": self.total_cash,
"uncleared_funds": self.uncleared_funds,
"pending_cash": self.pending_cash,
"cash": self.cash.to_dict() if self.cash else None,
"margin": self.margin.to_dict() if self.margin else None,
"pdt": self.pdt.to_dict() if self.pdt else None,
}
60 changes: 60 additions & 0 deletions asynctradier/common/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from asynctradier.common import EventType, TradeType


class Event:
"""
Represents an event.
Attributes:
amount (float): The amount of the event.
date (str): The date of the event.
type (EventType): The type of the event.
description (str): The description of the event.
commision (float): The commission of the event.
price (float): The price of the event.
quantity (float): The quantity of the event.
symbol (str): The symbol of the event.
trade_type (TradeType): The type of trade.
"""

def __init__(self, **kwargs):
self.amount = float(kwargs.get("amount")) if kwargs.get("amount") else 0.0
self.date = kwargs.get("date")
self.type = EventType(kwargs.get("type")) if kwargs.get("type") else None

detail = kwargs.get(self.type.value, {})

self.description = detail.get("description")
self.commision = (
float(detail.get("commision")) if detail.get("commision") else 0.0
)
self.price = float(detail.get("price")) if detail.get("price") else 0.0
self.quantity = float(detail.get("quantity")) if detail.get("quantity") else 0.0
self.symbol = detail.get("symbol")
self.trade_type = (
TradeType(detail.get("trade_type").lower())
if detail.get("trade_type")
else None
)

def to_dict(self):
"""
Converts the Event object to a dictionary.
Returns:
dict: A dictionary representation of the Event object.
"""
return {
"amount": self.amount,
"date": self.date,
"type": self.type.value,
"description": self.description,
"commision": self.commision,
"price": self.price,
"symbol": self.symbol,
"trade_type": self.trade_type.value,
"quantity": self.quantity,
}

def __str__(self):
return f"Event(amount={self.amount}, date={self.date}, type={self.type}, description={self.description}, commision={self.commision}, price={self.price}, symbol={self.symbol}, trade_type={self.trade_type})"
Loading

0 comments on commit ea8bb46

Please sign in to comment.