Exemple #1
0
def get_option_quote():
    client_config = get_client_config()
    quote_client = QuoteClient(client_config)
    symbol = 'AAPL'
    expirations = quote_client.get_option_expirations(symbols=[symbol])
    if len(expirations) > 1:
        expiry = int(
            expirations[expirations['symbol'] == symbol].at[0, 'timestamp'])
        quote_client.get_option_chain(symbol, expiry)

    quote_client.get_option_briefs(['AAPL  190104C00121000'])
    quote_client.get_option_bars(['AAPL  190104P00134000'])
    quote_client.get_option_trade_ticks(['AAPL  190104P00134000'])
class Test:
    def __init__(self):
        self.client_config = get_client_config()
        self.trade_client = TradeClient(self.client_config)
        self.openapi_client = QuoteClient(self.client_config, logger=logger)
        # 初始化 pushclient

        protocol, host, port = self.client_config.socket_host_port
        self.push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))
        self.push_client.connect(self.client_config.tiger_id, self.client_config.private_key)



    def trade(self):
        stock = stock_contract(symbol='AAPL', currency='USD')
        option = option_contract(identifier='AAPL  190927P00200000')

        emu_account = []
        # stock_contract = trade_client.get_contracts(symbol='FB')[0]

        account = self.trade_client.get_managed_accounts()
        print(account)
        emu_account = account[1]
        account = self.client_config.paper_account
        assets = self.trade_client.get_assets(account=account)
        print(assets)
        posinfo = self.trade_client.get_positions(account=account)
        print(posinfo)
        stock_order = market_order(account=account,   # 下单账户,可以使用标准、环球、或模拟账户
                                  contract = option,       # 第1步中获取的合约对象
                                  action = 'BUY',
                                  quantity = 1)
        print(stock_order)
        self.trade_client.place_order(stock_order)
        print(stock_order)

# 直接本地构造contract对象。 期货 contract 的构造方法请参考后面的文档
    def get_option_quote(self):
        symbol = 'AAPL'
        expirations = self.openapi_client.get_option_expirations(symbols=[symbol])
        if len(expirations) > 1:
            print(expirations)
            expiry = int(expirations[expirations['symbol'] == symbol].at[0, 'timestamp'])
            chains = self.openapi_client.get_option_chain(symbol, expiry)
            print(chains)
        for index,row in chains.iterrows():
            print(row["identifier"], row["strike"], row["put_call"])
        briefs = self.openapi_client.get_option_briefs(['AAPL  190927P00200000'])
        print(briefs)
        bars = self.openapi_client.get_option_bars(['AAPL  190927P00200000'])
        print(bars)
        ticks = self.openapi_client.get_option_trade_ticks(['AAPL  190927P00200000'])
        print(ticks)
        for index,row in ticks.iterrows():
            print(row["identifier"], row["time"], row["price"])

    def on_quote_changed(self, symbol, items, hour_trading):
        print(symbol, items, hour_trading)


    def test1(self):
        info = app.openapi_client.get_briefs(symbols)
        print(info)

    def run(self):
        option_trade_ticks = self.openapi_client.get_option_trade_ticks(['AAPL  190927P00200000'])
        print(option_trade_ticks)

    def subscribe(self):
        # self.push_client.connect(self.client_config.tiger_id, self.client_config.private_key)
        self.push_client.quote_changed = self.on_quote_changed
        self.push_client.subscribe_quote(symbols=['AAPL  190927P00200000', 'GOOG', 'FB'], quote_key_type=QuoteKeyType.ALL)
        self.push_client.subscribe_asset()
        time.sleep(30)
        # self.push_client.unsubscribe_quote(['AAPL', 'GOOG'])

    def subscribe2(self):

        self.push_client.quote_changed = self.on_quote_changed
        self.push_client.subscribe_quote(symbols=['aapl'], quote_key_type=QuoteKeyType.ALL)
        self.push_client.subscribe_asset()

    def on_changed(self, symbol, items, hour_trading):
        print(symbol, items, hour_trading)
        data = dict(items)
        latest_price = data.get('latest_price')
        volume = data.get('volume')


    def query_subscribed(self):
        def on_subscribed_symbols(symbols, focus_keys, limit, used):
            print(symbols, focus_keys, limit, used)

        self.push_client.subscribed_symbols = on_subscribed_symbols
        self.push_client.query_subscribed_quote()