def trade_apis():
    account = client_config.account
    openapi_client = TradeClient(client_config, logger=logger)

    # 通过请求获取合约
    contract = openapi_client.get_contracts('AAPL')[0]
    # contract = openapi_client.get_contract('AAPL', SecurityType.STK, currency=Currency.USD)

    # 本地构造合约
    # stock 股票
    # contract = stock_contract(symbol='AAPL', currency='USD')
    # option 期权
    # contract = option_contract(identifier='AAPL  190118P00160000')
    # contract = option_contract_by_symbol('AAPL', '20200110', strike=280.0, put_call='PUT', currency='USD')
    # future 期货
    # contract = future_contract('CHF', 'USD', '20190617', multiplier=125000, exchange='GLOBEX')
    # war 港股窝轮
    # contract = war_contract_by_symbol('02318', '20200326', 107.08, 'CALL', local_symbol='12616', currency='HKD')
    # iopt 港股牛熊证
    # contract = iopt_contract_by_symbol('02318', '20200420', 87.4, 'CALL', local_symbol='63379', currency='HKD')

    order = openapi_client.create_order(account, contract, 'BUY', 'LMT', 100, limit_price=5.0)
    # 或者本地构造订单对象
    # order = limit_order(account=account, contract=contract, action='BUY', quantity=100, limit_price=5.0)
    openapi_client.place_order(order)

    new_order = openapi_client.get_order(id=order.id)
    assert order.order_id == new_order.order_id
    openapi_client.modify_order(new_order, quantity=150)
    new_order = openapi_client.get_order(id=order.id)
    assert new_order.quantity == 150
    openapi_client.cancel_order(id=order.id)
    new_order = openapi_client.get_order(id=order.id)
    assert new_order.status == OrderStatus.CANCELLED or new_order.status == OrderStatus.PENDING_CANCEL

    # 预览订单 (下单前后保证金要求, 佣金等预览)
    result = openapi_client.preview_order(order)
    print(result)

    # 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
    stop_loss_order_leg = order_leg('LOSS', 8.0, time_in_force='GTC')  # 附加止损
    profit_taker_order_leg = order_leg('PROFIT', 12.0, time_in_force='GTC')  # 附加止盈

    main_order = openapi_client.create_order(account, contract, 'BUY', 'LMT', quantity=100, limit_price=10.0,
                                             order_legs=[stop_loss_order_leg, profit_taker_order_leg])
    # 本地构造限价单 + 附加订单
    # main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
    # order_legs=[stop_loss_order_leg])

    openapi_client.place_order(main_order)
    print(main_order)
    # 查询主订单所关联的附加订单
    order_legs = openapi_client.get_open_orders(account, parent_id=main_order.order_id)
    print(order_legs)
Beispiel #2
0
def trade_apis():
    account = client_config.account
    openapi_client = TradeClient(client_config, logger=logger)

    # stock
    contract = openapi_client.get_contracts('AAPL')[0]
    # 或者本地构造合约对象
    # contract = stock_contract(symbol='AAPL', currency='USD')

    # option
    # contract = option_contract(identifier='AAPL  190118P00160000')
    # future
    # contract = future_contract('CHF', 'USD', '20190617', multiplier=125000, exchange='GLOBEX')

    order = openapi_client.create_order(account,
                                        contract,
                                        'BUY',
                                        'LMT',
                                        100,
                                        limit_price=5.0)
    # 或者本地构造订单对象
    # order = limit_order(account=account, contract=contract, action='BUY', quantity=100, limit_price=5.0)
    openapi_client.place_order(order)
    order_id = order.order_id  # you can operate order via id too

    new_order = openapi_client.get_order(order_id=order.order_id)
    assert order.order_id == new_order.order_id
    openapi_client.modify_order(new_order, quantity=150)
    new_order = openapi_client.get_order(order_id=order_id)
    assert new_order.quantity == 150
    openapi_client.cancel_order(order_id=order_id)
    new_order = openapi_client.get_order(order_id=order_id)
    assert new_order.status == OrderStatus.CANCELLED or new_order.status == OrderStatus.PENDING_CANCEL

    # 预览订单 (下单前后保证金要求, 佣金等预览)
    result = openapi_client.preview_order(order)
    print(result)
Beispiel #3
0
def trade_apis():
    account = client_config.account
    openapi_client = TradeClient(client_config, logger=logger)

    # stock
    contract = openapi_client.get_contracts('AAPL')[0]
    # 或者本地构造合约对象
    # contract = stock_contract(symbol='AAPL', currency='USD')

    # option
    # contract = option_contract(identifier='AAPL  190118P00160000')
    # future
    # contract = future_contract('CHF', 'USD', '20190617', multiplier=125000, exchange='GLOBEX')

    order = openapi_client.create_order(account,
                                        contract,
                                        'BUY',
                                        'LMT',
                                        100,
                                        limit_price=5.0)
    # 或者本地构造订单对象
    # order = limit_order(account=account, contract=contract, action='BUY', quantity=100, limit_price=5.0)
    openapi_client.place_order(order)
    order_id = order.order_id  # you can operate order via id too

    new_order = openapi_client.get_order(order_id=order.order_id)
    assert order.order_id == new_order.order_id
    openapi_client.modify_order(new_order, quantity=150)
    new_order = openapi_client.get_order(order_id=order_id)
    assert new_order.quantity == 150
    openapi_client.cancel_order(order_id=order_id)
    new_order = openapi_client.get_order(order_id=order_id)
    assert new_order.status == OrderStatus.CANCELLED or new_order.status == OrderStatus.PENDING_CANCEL

    # 预览订单 (下单前后保证金要求, 佣金等预览)
    result = openapi_client.preview_order(order)
    print(result)

    # 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
    stop_loss_order_leg = order_leg('LOSS', 8.0, time_in_force='GTC')  # 附加止损
    profit_taker_order_leg = order_leg('PROFIT', 12.0,
                                       time_in_force='GTC')  # 附加止盈

    main_order = openapi_client.create_order(
        account,
        contract,
        'BUY',
        'LMT',
        quantity=100,
        limit_price=10.0,
        order_legs=[stop_loss_order_leg, profit_taker_order_leg])
    # 本地构造限价单 + 附加订单
    # main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
    # order_legs=[stop_loss_order_leg])

    openapi_client.place_order(main_order)
    print(main_order)
    # 查询主订单所关联的附加订单
    order_legs = openapi_client.get_open_orders(account,
                                                parent_id=main_order.order_id)
    print(order_legs)