Skip to content

Instantly share code, notes, and snippets.

@ypochien
Created December 1, 2021 05:27
Show Gist options
  • Save ypochien/d0236f4b9b89bf74a392118066d40d4c to your computer and use it in GitHub Desktop.
Save ypochien/d0236f4b9b89bf74a392118066d40d4c to your computer and use it in GitHub Desktop.
13:25之後用漲跌停價格反向出場
def clear_all():
"""13:25之後用漲跌停價格反向出場"""
#只處理今天新增的現股 (現股當沖、不含興櫃)
pnls = [one for one in api.list_positions() if abs(one.quantity) - one.yd_quantity!=0]
for one_pnl in pnls:
contract = api.Contracts.Stocks[one_pnl.code]
if contract == None:
print(f"無此商品 {one_pnl.code}")
continue
action = "Buy"
price = contract.limit_up
if one_pnl.direction=='Buy':
action = "Sell"
price = contract.limit_down
quantity = abs(one_pnl.quantity) - one_pnl.yd_quantity
if quantity > 0 and contract.exchange!="OES":
order = api.Order(price=price, quantity=quantity, action=action, price_type="LMT", order_type="ROD", order_lot="Common",first_sell="false")
for _ in range(0,quantity // 499):
order.quantity = 499
api.place_order(api.Contracts.Stocks[one_pnl.code],order)
print(f"{one_pnl.code} {[pnl.pnl for pnl in pnls if pnl.code==one_pnl.code]} {order.action.value} {order.quantity} 張 {order.price} 元")
left = quantity % 499
if left > 0:
order.quantity = left
api.place_order(api.Contracts.Stocks[one_pnl.code],order)
print(f"{one_pnl.code} {[pnl.pnl for pnl in pnls if pnl.code==one_pnl.code]} {order.action.value} {order.quantity} 張 {order.price} 元")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment