Skip to content

Commit

Permalink
introduce new types and fields in trading_models
Browse files Browse the repository at this point in the history
  • Loading branch information
hzheng committed Jun 13, 2024
1 parent 5e9e824 commit 5a79784
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A Python library for interacting with the Schwab API, providing simple access to

## Prerequisites
1. You need to have a Schwab brokerage account.
2. Become an Individual Developer via the [Schwab Developer Portal](https://beta-developer.schwab.com/).
2. Become an Individual Developer via the [Schwab Developer Portal](https://developer.schwab.com/).
3. Create an individual developer app and wait until its status is "Ready for use".

## Installation
Expand Down
2 changes: 1 addition & 1 deletion pyschwab/trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _convert_order(self, order: Dict[str, Any] | Order) -> Dict[str, Any]:
raise ValueError("Order must be a dictionary or Order object.")
return remove_none_values(order_dict)

def get_transactions(self, start_time: datetime=None, end_time: datetime=None, symbol: str=None, types: TransactionType=TransactionType.TRADE) -> List[Transaction]:
def get_transactions(self, start_time: datetime=None, end_time: datetime=None, symbol: str=None, types: TransactionType=None) -> List[Transaction]:
account_hash = self._get_account_hash()
now = datetime.now()
start = time_to_str(start_time or now - timedelta(days=30))
Expand Down
22 changes: 16 additions & 6 deletions pyschwab/trading_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .types import ActivityType, AssetType, ComplexOrderStrategyType, ExecutionType, MarketSession, OptionActionType, \
OptionAssetType, OrderDuration, OrderInstruction, OrderStatus, OrderStrategyType, OrderType, \
PositionEffect, QuantityType, RequestedDestination
PositionEffect, QuantityType, RequestedDestination, TransactionStatus, TransactionType
from .utils import camel_to_snake, dataclass_to_dict, to_time


Expand Down Expand Up @@ -43,13 +43,15 @@ class Deliverable:
status: str
symbol: str
instrument_id: int
closing_price: float
type: str
closing_price: float = None
type: str = None
description: str = None

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Deliverable':
if data is None:
return None

converted_data = {camel_to_snake(key): value for key, value in data.items()}
converted_data['asset_type'] = OptionAssetType.from_str(converted_data['asset_type'])
return cls(**converted_data)
Expand Down Expand Up @@ -217,6 +219,11 @@ class Balance:
short_market_value: float = 0.0
buying_power_non_marginable_trade: float = 0.0
sma: float = 0.0
cash_available_for_withdrawal: float = 0.0
cash_debit_call_value: float = 0.0
unsettled_cash: float = 0.0
cash_call: float = 0.0
long_non_marginable_market_value: float = 0.0

@classmethod
def from_dict(cls, data: Dict[str, Any]):
Expand Down Expand Up @@ -297,6 +304,7 @@ class OrderActivity:
quantity: int
order_remaining_quantity: int
execution_legs: List[ExecutionLeg]
activity_id: int = None

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'OrderActivity':
Expand Down Expand Up @@ -438,8 +446,8 @@ def from_dict(cls, data: Dict[str, Any]) -> 'User':
class Transaction:
activity_id: int
time: datetime
type: str
status: str
type: TransactionType
status: TransactionStatus
net_amount: float
account_number: str
sub_account: str
Expand All @@ -450,12 +458,14 @@ class Transaction:
trade_date: datetime = None
settlement_date: datetime = None
description: str = None
activity_type: str = None
activity_type: ActivityType = None

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Transaction':
converted_data = {camel_to_snake(key): value for key, value in data.items()}
converted_data['user'] = User.from_dict(converted_data.get('user', None))
converted_data['type'] = TransactionType.from_str(converted_data['type'])
converted_data['status'] = TransactionStatus.from_str(converted_data['status'])
for dt_key in ['time', 'trade_date', 'settlement_date']:
dt = converted_data.get(dt_key, None)
if dt:
Expand Down
10 changes: 10 additions & 0 deletions pyschwab/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ class RequestedDestination(AutoName):
AUTO = auto()


class TransactionStatus(AutoName):
VALID = auto()
INVALID = auto()
PENDING = auto()
UNKNOWN = auto()


class TransactionType(AutoName):
TRADE = auto()
RECEIVE_AND_DELIVER = auto()
Expand Down Expand Up @@ -384,8 +391,11 @@ class OptionStrategy(AutoName):


class ActivityType(AutoName):
ACTIVITY_CORRECTION = auto()
EXECUTION = auto()
ORDER_ACTION = auto()
TRANSFER = auto()
UNKNOWN = auto()


class ExecutionType(AutoName):
Expand Down

0 comments on commit 5a79784

Please sign in to comment.