Skip to content

Commit fff5f5c

Browse files
committed
fix: price set already send order
1 parent 19c535c commit fff5f5c

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

sjtrade/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
trading with shioaji
33
"""
44

5-
__version__ = "0.4.3"
5+
__version__ = "0.4.4"
66

77
def inject_env():
88
import os

sjtrade/position.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PriceSet:
1212
price: float
1313
quantity: int
1414
price_type: TFTStockPriceType
15+
in_transit_quantity: int = 0
1516
# time: datetime.time
1617
# time_cond: TimeCond
1718

sjtrade/trader.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
)
2323

2424

25+
logger.add("sjtrader.log", rotation="1 days")
26+
27+
2528
class SJTrader:
2629
def __init__(self, api: sj.Shioaji, simulation: bool = False):
2730
self.api = api
@@ -131,7 +134,7 @@ def place_entry_order(
131134
if not contract:
132135
logger.warning(f"Code: {code} not exist in TW Stock.")
133136
else:
134-
self.positions[code] = Position(
137+
position = self.positions[code] = Position(
135138
contract=contract,
136139
cond=PositionCond(
137140
quantity=pos,
@@ -143,10 +146,12 @@ def place_entry_order(
143146
)
144147
self.snapshots[code] = Snapshot(price=0.0)
145148
self.api.quote.subscribe(contract, version=QuoteVersion.v1)
146-
for price_set in self.positions[code].cond.entry_price:
149+
for price_set in position.cond.entry_price:
150+
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
151+
continue
147152
price, price_quantity = price_set.price, price_set.quantity
148153
quantity_s = quantity_split(price_quantity, threshold=499)
149-
with self.positions[code].lock:
154+
with position.lock:
150155
for q in quantity_s:
151156
trade = api.place_order(
152157
contract=contract,
@@ -163,7 +168,9 @@ def place_entry_order(
163168
),
164169
timeout=0,
165170
)
166-
self.positions[code].entry_trades.append(trade)
171+
price_set.in_transit_quantity += q
172+
# position.status.entry_order_in_transit += q
173+
position.entry_trades.append(trade)
167174
logger.info(f"{code} | {trade.order}")
168175

169176
def place_entry_positions(self) -> Dict[str, Position]:
@@ -257,6 +264,8 @@ def stop_profit(self, position: Position, tick: sj.TickSTKv1):
257264
cross = "under"
258265
for price_set in position.cond.stop_profit_price:
259266
if op(tick.close, price_set.price):
267+
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
268+
continue
260269
self.place_cover_order(position, [price_set])
261270
logger.info(
262271
f"{position.contract.code} | price: {tick.close} cross {cross} {price_set.price} "
@@ -278,6 +287,8 @@ def stop_loss(self, position: Position, tick: sj.TickSTKv1):
278287
cross = "over"
279288
for price_set in position.cond.stop_loss_price:
280289
if op(tick.close, price_set.price):
290+
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
291+
continue
281292
self.place_cover_order(position, [price_set])
282293
logger.info(
283294
f"{position.contract.code} | price: {tick.close} cross {cross} {price_set.price} "
@@ -298,9 +309,12 @@ def place_cover_order(
298309
price_sets = self.stratagy.cover_price_set(
299310
position, self.snapshots[position.contract.code]
300311
)
312+
position.cond.cover_price += price_sets
301313
if cover_quantity == 0:
302314
return
303315
for price_set in price_sets:
316+
if abs(price_set.quantity) == abs(price_set.in_transit_quantity):
317+
continue
304318
if price_set.quantity:
305319
quantity_s = quantity_split(price_set.quantity, threshold=499)
306320
for q in quantity_s:
@@ -319,8 +333,9 @@ def place_cover_order(
319333
timeout=0,
320334
)
321335
logger.info(f"{trade.contract.code} | {trade.order}")
336+
price_set.in_transit_quantity += q
322337
position.cover_trades.append(trade)
323-
api.update_status(trade=trade)
338+
# api.update_status(trade=trade)
324339

325340
def open_position_cover(self, onclose: bool = True):
326341
if self.simulation:

tests/test_trader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def test_sjtrader_place_entry_order(
172172
cond_1605 = PositionCond(
173173
quantity=-1,
174174
entry_price=[
175-
PriceSet(price=41.35, quantity=-1, price_type=TFTStockPriceType.LMT)
175+
PriceSet(price=41.35, quantity=-1, price_type=TFTStockPriceType.LMT, in_transit_quantity=-1)
176176
],
177177
stop_loss_price=[
178178
PriceSet(price=42.7, quantity=-1, price_type=TFTStockPriceType.MKT)
@@ -185,7 +185,7 @@ def test_sjtrader_place_entry_order(
185185
cond_6290 = PositionCond(
186186
quantity=-3,
187187
entry_price=[
188-
PriceSet(price=60.1, quantity=-3, price_type=TFTStockPriceType.LMT)
188+
PriceSet(price=60.1, quantity=-3, price_type=TFTStockPriceType.LMT, in_transit_quantity=-3)
189189
],
190190
stop_loss_price=[
191191
PriceSet(price=62.1, quantity=-3, price_type=TFTStockPriceType.MKT)

0 commit comments

Comments
 (0)