Skip to content

Commit

Permalink
actualize data types; replace Market; rename Order (#37)
Browse files Browse the repository at this point in the history
* actualize data types; replace Market; rename Order
  • Loading branch information
DBoyara authored Mar 1, 2024
1 parent 5f0742d commit e38d14f
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 86 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,8 @@ async def create_stop_order(transaction_id: int):
),
)
return await client.orders.create_stop_order(payload)


if __name__ == "__main__":
import asyncio

res = asyncio.run(create_order())
print(res)

print(asyncio.run(create_stop_order(1111111111111111111)))

print(asyncio.run(get_orders()))

print(asyncio.run(del_order(res.data.transactionId)))

print(asyncio.run(get_orders()))
```

Больше примеров в examples/

## Authors

Expand Down
33 changes: 9 additions & 24 deletions examples/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
async def create_order():
payload = CreateOrderRequestModel(
clientId=client_id,
securityBoard=BoardType.Futures,
securityCode="SiH3",
buySell=OrderType.Sell,
securityBoard=BoardType.TQBR,
securityCode="ALRS",
buySell=OrderType.Buy,
quantity=1,
price=74920,
price=72.23,
property=PropertyType.PutInQueue,
condition=None,
validateBefore=None,
Expand Down Expand Up @@ -55,20 +55,20 @@ async def del_order(transaction_id: int):
async def create_stop_order(transaction_id: int):
payload = CreateStopOrderRequestModel(
clientId=client_id,
securityBoard=BoardType.Futures,
securityCode="SiH3",
buySell=OrderType.Buy,
securityBoard=BoardType.TQBR,
securityCode="ALRS",
buySell=OrderType.Sell,
linkOrder=transaction_id,
stopLoss=StopLossModel(
activationPrice=74940,
activationPrice=71.80,
marketPrice=True,
quantity=StopQuantity(
value=1,
units=StopQuantityUnits.Lots,
)
),
takeProfit=TakeProfitModel(
activationPrice=74850,
activationPrice=72.30,
marketPrice=True,
quantity=StopQuantity(
value=1,
Expand All @@ -77,18 +77,3 @@ async def create_stop_order(transaction_id: int):
),
)
return await client.orders.create_stop_order(payload)


if __name__ == "__main__":
import asyncio

res = asyncio.run(create_order())
print(res)

print(asyncio.run(create_stop_order(1111111111111111111)))

print(asyncio.run(get_orders()))

print(asyncio.run(del_order(res.data.transactionId)))

print(asyncio.run(get_orders()))
9 changes: 1 addition & 8 deletions examples/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
client = Client(token)


async def get_all_data():
return await client.securities.get_data()


async def get_data_by_code(code: str):
return await client.securities.get_data(code)


if __name__ == "__main__":
import asyncio

print(asyncio.run(get_all_data()))

code_ = "SiZ2"
print(asyncio.run(get_data_by_code(code_)))
print(asyncio.run(get_data_by_code("SBER")))
11 changes: 11 additions & 0 deletions finam_trade_api/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import Optional

from pydantic import BaseModel
Expand All @@ -11,3 +12,13 @@ class BaseErrorModel(BaseModel):

class ErrorBodyModel(BaseModel):
error: BaseErrorModel


class Market(str, Enum):
Stock = "Stock"
Forts = "Forts"
Spbex = "Spbex"
Mma = "Mma"
Ets = "Ets"
Bonds = "Bonds"
Options = "Options"
2 changes: 1 addition & 1 deletion finam_trade_api/order/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
DelStopOrderData,
DelStopOrderRequestModel,
DelStopOrderResponseModel,
OrderResponse,
Order,
OrdersRequestModel,
OrdersResponseData,
OrdersResponseModel,
Expand Down
59 changes: 36 additions & 23 deletions finam_trade_api/order/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from pydantic import BaseModel

from finam_trade_api.models import Market


class OrderType(str, Enum):
Sell = "Sell"
Expand All @@ -21,9 +23,9 @@ class ConditionType(str, Enum):


class PropertyType(str, Enum):
PutInQueue = "PutInQueue"
CancelBalance = "CancelBalance"
ImmOrCancel = "ImmOrCancel"
PutInQueue = "PutInQueue" # неисполненная часть заявки помещается в очередь заявок биржи;
CancelBalance = "CancelBalance" # неисполненная часть заявки снимается с торгов;
ImmOrCancel = "ImmOrCancel" # сделки совершаются в том случае, если заявка может быть удовлетворена полностью


class ValidBeforeType(str, Enum):
Expand Down Expand Up @@ -67,58 +69,67 @@ class ValidBefore(BaseModel):


class OrderStatus(str, Enum):
NONE = "None"
Active = "Active"
Cancelled = "Cancelled"
Matched = "Matched"


class OrderMarket(str, Enum):
Stock = "Stock"
Forts = "Forts"
Spbex = "Spbex"
Mma = "Mma"
Ets = "Ets"
Bonds = "Bonds"
Options = "Options"


class OrderResponse(BaseModel):
class Order(BaseModel):
orderNo: int
transactionId: int
securityCode: Optional[str] = None
clientId: Optional[str] = None
status: Optional[OrderStatus] = None
status: OrderStatus
buySell: OrderType
createdAt: Optional[str] = None
acceptedAt: Optional[str] = None
price: float = 0
price: float = 0 # В последующих версиях API поле для рыночных заявок будет равно null, а не 0.
quantity: int
balance: int
message: Optional[str] = None
currency: Optional[str] = None
condition: Optional[Condition] = None
validBefore: Optional[ValidBefore] = None
securityBoard: Optional[str] = None
market: Optional[OrderMarket] = None
market: Market


class OrdersResponseData(BaseModel):
clientId: str
orders: List[OrderResponse]
orders: List[Order]


class OrdersResponseModel(BaseModel):
data: OrdersResponseData


class StopOrder(BaseModel):
stopOrderId: int
stopId: int
securityCode: str
securityBoard: BoardType
market: Market
clientId: str
buySell: OrderType
expirationDate: Optional[str] = None
linkOrder: int
validBefore: Optional[ValidBefore] = None
status: OrderStatus
message: Optional[str] = None
orderNo: int
tradeNo: int
acceptedAt: Optional[str] = None
canceledAt: Optional[str] = None
currency: Optional[str] = None
takeProfitExtremum: float
takeProfitLevel: float
stopLoss: Optional["StopLossModel"] = None
takeProfit: Optional["TakeProfitModel"] = None


class StopOrdersResponseData(BaseModel):
clientId: str
stopOrders: List[StopOrder]
stops: List[StopOrder]


class StopOrdersResponseModel(BaseModel):
Expand All @@ -142,7 +153,7 @@ class CreateOrderRequestModel(BaseModel):
buySell: OrderType
quantity: int
useCredit: bool = False
price: Optional[float] # цена заявки
price: float = 0 # В последующих версиях API поле для рыночных заявок будет равно null, а не 0.
property: PropertyType
condition: Optional[Condition] = None
validBefore: Optional[ValidBefore] = None
Expand All @@ -155,9 +166,11 @@ class StopQuantity(BaseModel):

class StopLossModel(BaseModel):
activationPrice: float
price: Optional[float] = None
price: float = 0
marketPrice: bool
quantity: StopQuantity
time: int = 0 # Защитное время, сек.
useCredit: bool = False


class StopPrice(BaseModel):
Expand All @@ -171,7 +184,7 @@ class TakeProfitModel(BaseModel):
spreadPrice: Optional[StopPrice] = None
marketPrice: bool
quantity: StopQuantity
time: Optional[int] = None # Защитное время, сек.
time: int = 0 # Защитное время, сек.
useCredit: bool = False


Expand Down
4 changes: 3 additions & 1 deletion finam_trade_api/portfolio/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pydantic import BaseModel

from finam_trade_api.models import Market


class PortfolioRequestOptionalModel(BaseModel):
includeCurrencies: str = "true"
Expand All @@ -23,7 +25,7 @@ class PortfolioContent(BaseModel):

class Position(BaseModel):
securityCode: str
market: str
market: Market
balance: int
currentPrice: float
equity: float
Expand Down
24 changes: 13 additions & 11 deletions finam_trade_api/securities/model.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from typing import List
from typing import List, Optional

from pydantic import BaseModel

from finam_trade_api.models import Market


class Security(BaseModel):
code: str
board: str
market: str
decimals: float
lotSize: float
minStep: float
currency: str
shortName: str
properties: float
timeZoneName: str
market: Market
decimals: int
lotSize: int
minStep: int
currency: Optional[str] = None
shortName: Optional[str] = None
properties: int
timeZoneName: Optional[str] = None
bpCost: float
accruedInterest: float
priceSign: str
ticker: str
lotDivider: float
ticker: Optional[str] = None
lotDivider: int


class SecurityData(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "finam-trade-api"
version = "2.2.0"
version = "2.3.0"
description = "Асинхронный REST-клиент для API Finam"
authors = ["DBoyara <[email protected]>"]
license = "GNU GPL v.3.0"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
maintainer="DBoyara",
maintainer_email="[email protected]",
packages=find_packages(),
version="2.2.0",
version="2.3.0",
install_requires=["aiohttp >= 3.8.3, < 4.0.0", "pydantic >= 1.10.2, < 2.0.0"],
python_requires=">3.8.0, <4",
license="GNU GPL v.3.0",
Expand Down

0 comments on commit e38d14f

Please sign in to comment.