22
22
)
23
23
24
24
25
+ logger .add ("sjtrader.log" , rotation = "1 days" )
26
+
27
+
25
28
class SJTrader :
26
29
def __init__ (self , api : sj .Shioaji , simulation : bool = False ):
27
30
self .api = api
@@ -131,7 +134,7 @@ def place_entry_order(
131
134
if not contract :
132
135
logger .warning (f"Code: { code } not exist in TW Stock." )
133
136
else :
134
- self .positions [code ] = Position (
137
+ position = self .positions [code ] = Position (
135
138
contract = contract ,
136
139
cond = PositionCond (
137
140
quantity = pos ,
@@ -143,10 +146,12 @@ def place_entry_order(
143
146
)
144
147
self .snapshots [code ] = Snapshot (price = 0.0 )
145
148
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
147
152
price , price_quantity = price_set .price , price_set .quantity
148
153
quantity_s = quantity_split (price_quantity , threshold = 499 )
149
- with self . positions [ code ] .lock :
154
+ with position .lock :
150
155
for q in quantity_s :
151
156
trade = api .place_order (
152
157
contract = contract ,
@@ -163,7 +168,9 @@ def place_entry_order(
163
168
),
164
169
timeout = 0 ,
165
170
)
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 )
167
174
logger .info (f"{ code } | { trade .order } " )
168
175
169
176
def place_entry_positions (self ) -> Dict [str , Position ]:
@@ -257,6 +264,8 @@ def stop_profit(self, position: Position, tick: sj.TickSTKv1):
257
264
cross = "under"
258
265
for price_set in position .cond .stop_profit_price :
259
266
if op (tick .close , price_set .price ):
267
+ if abs (price_set .quantity ) == abs (price_set .in_transit_quantity ):
268
+ continue
260
269
self .place_cover_order (position , [price_set ])
261
270
logger .info (
262
271
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):
278
287
cross = "over"
279
288
for price_set in position .cond .stop_loss_price :
280
289
if op (tick .close , price_set .price ):
290
+ if abs (price_set .quantity ) == abs (price_set .in_transit_quantity ):
291
+ continue
281
292
self .place_cover_order (position , [price_set ])
282
293
logger .info (
283
294
f"{ position .contract .code } | price: { tick .close } cross { cross } { price_set .price } "
@@ -298,9 +309,12 @@ def place_cover_order(
298
309
price_sets = self .stratagy .cover_price_set (
299
310
position , self .snapshots [position .contract .code ]
300
311
)
312
+ position .cond .cover_price += price_sets
301
313
if cover_quantity == 0 :
302
314
return
303
315
for price_set in price_sets :
316
+ if abs (price_set .quantity ) == abs (price_set .in_transit_quantity ):
317
+ continue
304
318
if price_set .quantity :
305
319
quantity_s = quantity_split (price_set .quantity , threshold = 499 )
306
320
for q in quantity_s :
@@ -319,8 +333,9 @@ def place_cover_order(
319
333
timeout = 0 ,
320
334
)
321
335
logger .info (f"{ trade .contract .code } | { trade .order } " )
336
+ price_set .in_transit_quantity += q
322
337
position .cover_trades .append (trade )
323
- api .update_status (trade = trade )
338
+ # api.update_status(trade=trade)
324
339
325
340
def open_position_cover (self , onclose : bool = True ):
326
341
if self .simulation :
0 commit comments