Пример #1
0
    def connect_quote(self):
        """
        Connect to market data server.
        """
        self.quote_ctx = OpenQuoteContext(self.host, self.port)

        class QuoteHandler(StockQuoteHandlerBase):
            gateway = self

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(QuoteHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.process_quote(content)
                return RET_OK, content

        class OrderBookHandler(OrderBookHandlerBase):
            gateway = self

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(OrderBookHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.process_orderbook(content)
                return RET_OK, content

        self.quote_ctx.set_handler(QuoteHandler())
        self.quote_ctx.set_handler(OrderBookHandler())
        self.quote_ctx.start()

        self.write_log("行情接口连接成功")
Пример #2
0
 def start(self, host = ct.FUTU_HOST, port = ct.FUTU_PORT):
     with self.lock:
         if self.quote_ctx is None:
             self.quote_ctx = OpenQuoteContext(host, port)
         else:
             self.quote_ctx.start()
         self.sub_dict = self.get_subscribed_dict()
         logger.debug("self.sub_dict:%s" % self.sub_dict)
         self._status = True
Пример #3
0
def get_stockdata(stock_list,start_date,end_date):
    df={}
    for stock in stock_list:
        code = stock
        filename = code+".csv"
        path = "./stock/"        
        if not os.path.exists(path):
            os.makedirs(path)
        if os.path.exists(path + filename):
            df_stock = pd.read_csv(path + filename)    
        else:
            quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
            ret, df_stock, page_req_key = quote_ctx.request_history_kline(stock, start=start_date, end=end_date)  
            quote_ctx.close() 
            df_stock['date']=pd.to_datetime(df_stock['time_key'])        
            df_stock.to_csv(path + filename) 
        df_stock.index = pd.to_datetime(df_stock.date)
        df_stock['openinterest']=0
        df_stock = df_stock[['open','high','low','close','volume','openinterest']]
        df.update({stock:df_stock})

    return df
Пример #4
0
    def connectQuote(self):
        """连接行情功能"""
        self.quoteCtx = OpenQuoteContext(self.host, self.port)

        # 继承实现处理器类
        class QuoteHandler(StockQuoteHandlerBase):
            """报价处理器"""
            gateway = self  # 缓存Gateway对象

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(QuoteHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.processQuote(content)
                return RET_OK, content

        class OrderBookHandler(OrderBookHandlerBase):
            """订单簿处理器"""
            gateway = self

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(OrderBookHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.processOrderBook(content)
                return RET_OK, content

        # 设置回调处理对象
        self.quoteCtx.set_handler(QuoteHandler())
        self.quoteCtx.set_handler(OrderBookHandler())

        # 启动行情
        self.quoteCtx.start()

        self.writeLog(u'行情接口连接成功')
Пример #5
0
# toolkit for caculate annual percentage yield of cash secured short put strategy
from futu import OpenQuoteContext, RET_OK, OptionType, OptionCondType, set_futu_debug_model
from datetime import datetime, timedelta, timezone
import sys

set_futu_debug_model(False)

TIMEZONE_OFFSET = timedelta(
    hours=-12
)  # North American Eastern Time Zone, known as ET (EDT is UTC-4, EST is UTC-5)
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)

group_name = sys.argv[1]
ret_code, stocks = quote_ctx.get_user_security(group_name)
if ret_code != RET_OK:
    print("get_user_security, ret_code: %s, error: %s" % (ret_code, stocks))
    sys.exit()

#print(stocks)
for i, stock in stocks.iterrows():
    code = stock["code"]
    name = stock["name"]
    print(code, name)

quote_ctx.close()
Пример #6
0
 def __enter__(self):
     self.quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
     self.connect_quote()
     return self