Skip to content

Commit

Permalink
fix: price set already send order
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvictor committed Jul 19, 2022
1 parent 19c535c commit fff5f5c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sjtrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
trading with shioaji
"""

__version__ = "0.4.3"
__version__ = "0.4.4"

def inject_env():
import os
Expand Down
1 change: 1 addition & 0 deletions sjtrade/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PriceSet:
price: float
quantity: int
price_type: TFTStockPriceType
in_transit_quantity: int = 0
# time: datetime.time
# time_cond: TimeCond

Expand Down
25 changes: 20 additions & 5 deletions sjtrade/trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
)


logger.add("sjtrader.log", rotation="1 days")


class SJTrader:
def __init__(self, api: sj.Shioaji, simulation: bool = False):
self.api = api
Expand Down Expand Up @@ -131,7 +134,7 @@ def place_entry_order(
if not contract:
logger.warning(f"Code: {code} not exist in TW Stock.")
else:
self.positions[code] = Position(
position = self.positions[code] = Position(
contract=contract,
cond=PositionCond(
quantity=pos,
Expand All @@ -143,10 +146,12 @@ def place_entry_order(
)
self.snapshots[code] = Snapshot(price=0.0)
self.api.quote.subscribe(contract, version=QuoteVersion.v1)
for price_set in self.positions[code].cond.entry_price:
for price_set in position.cond.entry_price:
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
continue
price, price_quantity = price_set.price, price_set.quantity
quantity_s = quantity_split(price_quantity, threshold=499)
with self.positions[code].lock:
with position.lock:
for q in quantity_s:
trade = api.place_order(
contract=contract,
Expand All @@ -163,7 +168,9 @@ def place_entry_order(
),
timeout=0,
)
self.positions[code].entry_trades.append(trade)
price_set.in_transit_quantity += q
# position.status.entry_order_in_transit += q
position.entry_trades.append(trade)
logger.info(f"{code} | {trade.order}")

def place_entry_positions(self) -> Dict[str, Position]:
Expand Down Expand Up @@ -257,6 +264,8 @@ def stop_profit(self, position: Position, tick: sj.TickSTKv1):
cross = "under"
for price_set in position.cond.stop_profit_price:
if op(tick.close, price_set.price):
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
continue
self.place_cover_order(position, [price_set])
logger.info(
f"{position.contract.code} | price: {tick.close} cross {cross} {price_set.price} "
Expand All @@ -278,6 +287,8 @@ def stop_loss(self, position: Position, tick: sj.TickSTKv1):
cross = "over"
for price_set in position.cond.stop_loss_price:
if op(tick.close, price_set.price):
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
continue
self.place_cover_order(position, [price_set])
logger.info(
f"{position.contract.code} | price: {tick.close} cross {cross} {price_set.price} "
Expand All @@ -298,9 +309,12 @@ def place_cover_order(
price_sets = self.stratagy.cover_price_set(
position, self.snapshots[position.contract.code]
)
position.cond.cover_price += price_sets
if cover_quantity == 0:
return
for price_set in price_sets:
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
continue
if price_set.quantity:
quantity_s = quantity_split(price_set.quantity, threshold=499)
for q in quantity_s:
Expand All @@ -319,8 +333,9 @@ def place_cover_order(
timeout=0,
)
logger.info(f"{trade.contract.code} | {trade.order}")
price_set.in_transit_quantity += q
position.cover_trades.append(trade)
api.update_status(trade=trade)
# api.update_status(trade=trade)

def open_position_cover(self, onclose: bool = True):
if self.simulation:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_sjtrader_place_entry_order(
cond_1605 = PositionCond(
quantity=-1,
entry_price=[
PriceSet(price=41.35, quantity=-1, price_type=TFTStockPriceType.LMT)
PriceSet(price=41.35, quantity=-1, price_type=TFTStockPriceType.LMT, in_transit_quantity=-1)
],
stop_loss_price=[
PriceSet(price=42.7, quantity=-1, price_type=TFTStockPriceType.MKT)
Expand All @@ -185,7 +185,7 @@ def test_sjtrader_place_entry_order(
cond_6290 = PositionCond(
quantity=-3,
entry_price=[
PriceSet(price=60.1, quantity=-3, price_type=TFTStockPriceType.LMT)
PriceSet(price=60.1, quantity=-3, price_type=TFTStockPriceType.LMT, in_transit_quantity=-3)
],
stop_loss_price=[
PriceSet(price=62.1, quantity=-3, price_type=TFTStockPriceType.MKT)
Expand Down

0 comments on commit fff5f5c

Please sign in to comment.