Skip to content

Commit

Permalink
[Mod] type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
noranhe committed Apr 21, 2022
1 parent 368c23f commit 13772de
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions vnpy_paperaccount/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@


LOCAL_TZ = get_localzone()
APP_NAME: str = "PaperAccount"
GATEWAY_NAME: str = "PAPER"
APP_NAME = "PaperAccount"
GATEWAY_NAME = "PAPER"


class PaperEngine(BaseEngine):
Expand Down Expand Up @@ -98,15 +98,15 @@ def process_tick_event(self, event: Event) -> None:

self.ticks[tick.vt_symbol] = tick

active_orders: dict = self.active_orders.get(tick.vt_symbol, None)
active_orders: Optional[dict] = self.active_orders.get(tick.vt_symbol, None)
if active_orders:
for orderid, order in list(active_orders.items()):
self.cross_order(order, tick)

if not order.is_active():
active_orders.pop(orderid)

quote: QuoteData = self.active_quotes.get(tick.vt_symbol, None)
quote: Optional[QuoteData] = self.active_quotes.get(tick.vt_symbol, None)
if quote:
self.cross_quote(quote, tick)

Expand All @@ -121,17 +121,17 @@ def process_timer_event(self, event: Event) -> None:
self.timer_count = 0

for position in self.positions.values():
contract: ContractData = self.main_engine.get_contract(position.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(position.vt_symbol)
if contract:
self.calculate_pnl(position)
self.put_event(EVENT_POSITION, copy(position))

def calculate_pnl(self, position: PositionData) -> None:
""""""
tick: TickData = self.ticks.get(position.vt_symbol, None)
tick: Optional[TickData] = self.ticks.get(position.vt_symbol, None)

if tick:
contract: ContractData = self.main_engine.get_contract(position.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(position.vt_symbol)

if position.direction == Direction.SHORT:
multiplier: float = -position.volume * contract.size
Expand Down Expand Up @@ -159,7 +159,7 @@ def query_history(self, req: HistoryRequest, gateway_name: str) -> List[BarData]

def send_order(self, req: OrderRequest, gateway_name: str) -> str:
""""""
contract: ContractData = self.main_engine.get_contract(req.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(req.vt_symbol)
if not contract:
self.write_log(f"委托失败,找不到该合约{req.vt_symbol}")
return ""
Expand Down Expand Up @@ -191,7 +191,7 @@ def send_order(self, req: OrderRequest, gateway_name: str) -> str:

# Cross order immediately with last tick data
if self.instant_trade and order.status != Status.REJECTED:
tick: TickData = self.ticks.get(order.vt_symbol, None)
tick: Optional[TickData] = self.ticks.get(order.vt_symbol, None)
if tick:
self.cross_order(order, tick)

Expand All @@ -211,7 +211,7 @@ def cancel_order(self, req: CancelRequest, gateway_name: str) -> None:
self.put_event(EVENT_ORDER, copy(order))

# Free frozen position volume
contract: ContractData = self.main_engine.get_contract(order.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(order.vt_symbol)
if contract.net_position:
return

Expand All @@ -228,7 +228,7 @@ def cancel_order(self, req: CancelRequest, gateway_name: str) -> None:

def send_quote(self, req: QuoteRequest, gateway_name: str) -> str:
""""""
contract: ContractData = self.main_engine.get_contract(req.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(req.vt_symbol)
if not contract:
self.write_log(f"报价失败,找不到该合约{req.vt_symbol}")
return ""
Expand Down Expand Up @@ -259,7 +259,7 @@ def send_quote(self, req: QuoteRequest, gateway_name: str) -> str:

def cancel_quote(self, req: CancelRequest, gateway_name: str) -> None:
""""""
quote: QuoteData = self.active_quotes.get(req.vt_symbol, None)
quote: Optional[QuoteData] = self.active_quotes.get(req.vt_symbol, None)
if not quote:
return

Expand Down Expand Up @@ -313,7 +313,7 @@ def check_order_valid(self, order: OrderData, contract: ContractData) -> Optiona

def cross_order(self, order: OrderData, tick: TickData) -> None:
""""""
contract: ContractData = self.main_engine.get_contract(order.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(order.vt_symbol)

trade_price = 0

Expand Down Expand Up @@ -363,7 +363,7 @@ def cross_order(self, order: OrderData, tick: TickData) -> None:

def cross_quote(self, quote: QuoteData, tick: TickData) -> None:
""""""
contract: ContractData = self.main_engine.get_contract(quote.vt_symbol)
contract: Optional[ContractData] = self.main_engine.get_contract(quote.vt_symbol)

trade_price = 0

Expand Down Expand Up @@ -416,15 +416,15 @@ def update_position(self, trade: TradeData, contract: ContractData) -> None:
if contract.net_position:
position: PositionData = self.get_position(vt_symbol, Direction.NET)

old_volume = position.volume
old_cost = position.volume * position.price
old_volume: float = position.volume
old_cost: float = position.volume * position.price

if trade.direction == Direction.LONG:
pos_change = trade.volume
else:
pos_change = -trade.volume

new_volume = position.volume + pos_change
new_volume: float = position.volume + pos_change

# No position holding, clear price
if not new_volume:
Expand Down

0 comments on commit 13772de

Please sign in to comment.