Skip to content

Order

Order

Bases: BaseModel

Source code in alpha_trader/order/__init__.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class Order(BaseModel):
    action: str
    check_result: Union[OrderCheckResult, None] = None
    committed_cash: float
    counter_party: Union[str, None] = None
    counter_party_name: Union[str, None] = None
    creation_date: int
    execution_price: Union[float, None] = None
    execution_volume: Union[float, None] = None
    good_after_date: Union[int, None] = None
    good_till_date: Union[int, None] = None
    hourly_change: Union[int, None] = None
    id: str
    listing: Listing
    next_hourly_change_date: Union[int, None] = None
    number_of_shares: int
    owner: str
    owner_name: str
    price: Union[float, None] = None
    private_counter_party: Union[bool, None] = None
    private_owner: bool
    security_identifier: str
    spread: Union[PriceSpread, None] = None
    type: str
    uncommitted_cash: Union[float, None] = None
    uncommitted_shares: int
    version: Union[int, None] = None
    volume: Union[float, None] = None
    client: Client

    @staticmethod
    def initialize_from_api_response(api_response: Dict, client: Client):
        return Order(
            action=api_response["action"],
            check_result=api_response["checkResult"],
            committed_cash=api_response["committedCash"],
            counter_party=api_response["counterParty"],
            counter_party_name=api_response["counterPartyName"],
            creation_date=api_response["creationDate"],
            execution_price=api_response["executionPrice"],
            execution_volume=api_response["executionVolume"],
            good_after_date=api_response["goodAfterDate"],
            good_till_date=api_response["goodTillDate"],
            hourly_change=api_response["hourlyChange"],
            id=api_response["id"],
            listing=Listing.initialize_from_api_response(
                api_response["listing"], client
            ),
            next_hourly_change_date=api_response["nextHourlyChangeDate"],
            number_of_shares=api_response["numberOfShares"],
            owner=api_response["owner"],
            owner_name=api_response["ownerName"],
            price=api_response["price"],
            private_counter_party=api_response["privateCounterParty"],
            private_owner=api_response["privateOwner"],
            security_identifier=api_response["securityIdentifier"],
            spread=PriceSpread.initialize_from_api_response(
                api_response["spread"], client
            )
            if type(api_response["spread"]) == dict
            else None,
            type=api_response["type"],
            uncommitted_cash=api_response["uncommittedCash"],
            uncommitted_shares=api_response["uncommittedShares"],
            version=api_response["version"],
            client=client,
        )

    def delete(self):
        response = self.client.request("DELETE", f"api/securityorders/{self.id}")

        return response.status_code == 200

    @staticmethod
    def create(
        action: str,
        quantity: int,
        client: Client,
        owner_securities_account_id: str,
        security_identifier: str,
        price: float = None,
        good_after_date: int = None,
        good_till_date: int = None,
        order_type: str = "LIMIT",
        counter_party: str = None,
        hourly_change: float = None,
        check_order_only: bool = False,
    ) -> "Order":
        """Creates a new order.

        Args:
            client: Alpha Trader Client
            action: Security Order Action, either "BUY" or "SELL"
            quantity:  Number of shares to buy or sell
            price: Price
            good_after_date: Valid from date (premium feature)
            good_till_date: Valid until date (premium feature)
            order_type: Security Order Type, either "LIMIT" or "MARKET"
            counter_party: Securities Account ID of the counterparty
            owner_securities_account_id: Securities Account ID of the owner
            security_identifier: Security Identifier
            hourly_change: Hourly change of the order
            check_order_only: Only check the order, do not create it

        Returns:
            Order

        """
        data = {
            "action": action,
            "numberOfShares": quantity,
            "price": price,
            "goodAfterDate": good_after_date,
            "goodTillDate": good_till_date,
            "type": order_type,
            "counterparty": counter_party,
            "owner": owner_securities_account_id,
            "securityIdentifier": security_identifier,
            "checkOrderOnly": check_order_only,
            "hourlyChange": hourly_change,
        }

        response = client.request("POST", "api/securityorders", data=data)
        if response.status_code not in [200, 201]:
            print(response.text)

        return Order.initialize_from_api_response(response.json(), client)

    def update(self):
        response = self.client.request("GET", f"api/securityorders/{self.id}")

        return Order.initialize_from_api_response(response.json(), self.client)

    def __str__(self):
        return (
            f"{self.action} {self.number_of_shares} {self.listing.name} @ {self.price}"
        )

    def __repr__(self):
        return self.__str__()

create(action, quantity, client, owner_securities_account_id, security_identifier, price=None, good_after_date=None, good_till_date=None, order_type='LIMIT', counter_party=None, hourly_change=None, check_order_only=False) staticmethod

Creates a new order.

Parameters:

Name Type Description Default
client Client

Alpha Trader Client

required
action str

Security Order Action, either "BUY" or "SELL"

required
quantity int

Number of shares to buy or sell

required
price float

Price

None
good_after_date int

Valid from date (premium feature)

None
good_till_date int

Valid until date (premium feature)

None
order_type str

Security Order Type, either "LIMIT" or "MARKET"

'LIMIT'
counter_party str

Securities Account ID of the counterparty

None
owner_securities_account_id str

Securities Account ID of the owner

required
security_identifier str

Security Identifier

required
hourly_change float

Hourly change of the order

None
check_order_only bool

Only check the order, do not create it

False

Returns:

Type Description
Order

Order

Source code in alpha_trader/order/__init__.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
@staticmethod
def create(
    action: str,
    quantity: int,
    client: Client,
    owner_securities_account_id: str,
    security_identifier: str,
    price: float = None,
    good_after_date: int = None,
    good_till_date: int = None,
    order_type: str = "LIMIT",
    counter_party: str = None,
    hourly_change: float = None,
    check_order_only: bool = False,
) -> "Order":
    """Creates a new order.

    Args:
        client: Alpha Trader Client
        action: Security Order Action, either "BUY" or "SELL"
        quantity:  Number of shares to buy or sell
        price: Price
        good_after_date: Valid from date (premium feature)
        good_till_date: Valid until date (premium feature)
        order_type: Security Order Type, either "LIMIT" or "MARKET"
        counter_party: Securities Account ID of the counterparty
        owner_securities_account_id: Securities Account ID of the owner
        security_identifier: Security Identifier
        hourly_change: Hourly change of the order
        check_order_only: Only check the order, do not create it

    Returns:
        Order

    """
    data = {
        "action": action,
        "numberOfShares": quantity,
        "price": price,
        "goodAfterDate": good_after_date,
        "goodTillDate": good_till_date,
        "type": order_type,
        "counterparty": counter_party,
        "owner": owner_securities_account_id,
        "securityIdentifier": security_identifier,
        "checkOrderOnly": check_order_only,
        "hourlyChange": hourly_change,
    }

    response = client.request("POST", "api/securityorders", data=data)
    if response.status_code not in [200, 201]:
        print(response.text)

    return Order.initialize_from_api_response(response.json(), client)