예제 #1
0
파일: engine.py 프로젝트: sdywcd/vnpy-1
    def process_trade_event(self, event: Event) -> None:
        """"""
        trade: TradeData = event.data

        reference = self.order_reference_map.get(trade.vt_orderid, "")
        if not reference:
            return

        vt_symbol = trade.vt_symbol
        key = (reference, vt_symbol)

        contract_result = self.contract_results.get(key, None)
        if not contract_result:
            contract_result = ContractResult(self, reference, vt_symbol)
            self.contract_results[key] = contract_result

        contract_result.update_trade(trade)

        # Push trade data with reference
        trade.reference = reference
        self.event_engine.put(Event(EVENT_PM_TRADE, trade))

        # Auto-subscribe tick data
        if trade.vt_symbol in self.subscribed:
            return

        contract = self.main_engine.get_contract(trade.vt_symbol)
        if not contract:
            return

        req = SubscribeRequest(contract.symbol, contract.exchange)
        self.main_engine.subscribe(req, contract.gateway_name)
예제 #2
0
 def subscribe(self, contract: ContractData):
     """"""
     req = SubscribeRequest(
         symbol=contract.symbol,
         exchange=contract.exchange
     )
     self.main_engine.subscribe(req, contract.gateway_name)
예제 #3
0
    def get_leg(self, vt_symbol: str) -> LegData:
        """"""
        leg = self.legs.get(vt_symbol, None)

        if not leg:
            leg = LegData(vt_symbol)
            self.legs[vt_symbol] = leg

            # Subscribe market data
            contract = self.main_engine.get_contract(vt_symbol)
            if contract:
                leg.update_contract(contract)

                req = SubscribeRequest(
                    contract.symbol,
                    contract.exchange
                )
                self.main_engine.subscribe(req, contract.gateway_name)

            # Initialize leg position
            for direction in Direction:
                vt_positionid = f"{vt_symbol}.{direction.value}"
                position = self.main_engine.get_position(vt_positionid)

                if position:
                    leg.update_position(position)

        return leg
예제 #4
0
    def process_contract_event(self, event: Event) -> None:
        """"""
        contract = event.data

        if contract.vt_symbol in self.legs:
            req = SubscribeRequest(contract.symbol, contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)
예제 #5
0
파일: engine.py 프로젝트: arthurlirui/vnpy
    def _init_strategy(self, strategy_name: str):
        """
        Init strategies in queue.
        """
        strategy = self.strategies[strategy_name]

        if strategy.inited:
            self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作")
            return

        self.write_log(f"{strategy_name}开始执行初始化")

        # Call on_init function of strategy
        self.call_strategy_func(strategy, strategy.on_init)

        # Restore strategy data(variables)
        data = self.strategy_data.get(strategy_name, None)
        if data:
            for name in strategy.variables:
                value = data.get(name, None)
                if value:
                    setattr(strategy, name, value)

        # Subscribe market data
        contract = self.main_engine.get_contract(strategy.vt_symbol)
        if contract:
            req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)
        else:
            self.write_log(f"行情订阅失败,找不到合约{strategy.vt_symbol}", strategy)

        # Put event to update init completed status.
        strategy.inited = True
        self.put_strategy_event(strategy)
        self.write_log(f"{strategy_name}初始化完成")
예제 #6
0
    def subscribe_quote(self):

        for symbol, i in self.symbols.items():
            quote = SubscribeRequest(
                symbol,
                self.main_engine.gateways[self.engine_name].exchanges[i])
            self.main_engine.subscribe(quote, self.engine_name)
예제 #7
0
파일: engine.py 프로젝트: zhouwei4780/vnpy
 def subscribe(self, vt_symbols):
     """"""
     for vt_symbol in vt_symbols:
         contract = self.main_engine.get_contract(vt_symbol)
         if contract:
             req = SubscribeRequest(symbol=contract.symbol,
                                    exchange=contract.exchange)
             self.main_engine.subscribe(req, contract.gateway_name)
예제 #8
0
파일: rollover.py 프로젝트: zxd6655422/vnpy
    def subscribe(self, vt_symbol: str) -> None:
        """"""
        contract = self.main_engine.get_contract(vt_symbol)
        if not contract:
            return

        req = SubscribeRequest(contract.symbol, contract.exchange)
        self.main_engine.subscribe(req, contract.gateway_name)
예제 #9
0
파일: engine.py 프로젝트: arthurlirui/vnpy
 def re_connect(self, strategy_name: str):
     strategy = self.strategies[strategy_name]
     # Subscribe market data
     contract = self.main_engine.get_contract(strategy.vt_symbol)
     if contract:
         req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange)
         self.main_engine.subscribe(req, contract.gateway_name)
     else:
         self.write_log(f"行情订阅失败,找不到合约{strategy.vt_symbol}", strategy)
예제 #10
0
파일: engine.py 프로젝트: sdywcd/vnpy-1
    def process_contract_event(self, event: Event) -> None:
        """"""
        contract: ContractData = event.data
        if contract.vt_symbol not in self.result_symbols:
            return

        req = SubscribeRequest(contract.symbol, contract.exchange)
        self.main_engine.subscribe(req, contract.gateway_name)

        self.subscribed.add(contract.vt_symbol)
예제 #11
0
    def process_contract_event(self, event: Event) -> None:
        """"""
        contract = event.data
        leg = self.legs.get(contract.vt_symbol, None)

        if leg:
            # Update contract data
            leg.update_contract(contract)

            req = SubscribeRequest(contract.symbol, contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)
예제 #12
0
 def subscribe(self, contract: ContractData):
     """
     订阅数据 tick 数据
     :param contract: 
     :return: 
     """
     req = SubscribeRequest(
         symbol=contract.symbol,
         exchange=contract.exchange
     )
     # 从指定的gateway 订阅tick数据
     self.main_engine.subscribe(req, contract.gateway_name)
예제 #13
0
def get_ticker(gateway, symbol, exchange: "Exchange"):
    """
    获取 tick数据并存入数据库里
    :param gateway: 
    :param symbol: 
    :param exchange: 
    :return: 
    """
    gevent.sleep(1)
    req = SubscribeRequest(symbol=symbol, exchange=exchange)
    while True:
        gateway.subscribe(req)
        gevent.sleep(0.5)
    def subscribe_quote(self):

        for symbol in self.symbols:
            this_directory = PATH + symbol
            this_model_file = this_directory + '\\' + symbol + '_FUTURES' + '_5MINUTE_all.pkl'

            try:
                self.data_hist[symbol] = pd.read_pickle(this_model_file)
            except Exception as e:
                print('No data found error')

        for symbol in self.symbols:
            quote = SubscribeRequest(symbol, BinancesGateway.exchanges[0])
            self.main_engine.subscribe(quote, self.enginename)
예제 #15
0
    def get_leg(self, vt_symbol: str) -> LegData:
        """"""
        leg = self.legs.get(vt_symbol, None)

        if not leg:
            leg = LegData(vt_symbol)
            self.legs[vt_symbol] = leg

            # Subscribe market data
            contract = self.main_engine.get_contract(vt_symbol)
            req = SubscribeRequest(contract.symbol, contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)

        return leg
예제 #16
0
파일: engine.py 프로젝트: zhaoweijin/vnpy
    def subscribe(self, vt_symbol: str) -> None:
        """
        Subscribe tick data update.
        """
        contract = self.main_engine.get_contract(vt_symbol)
        if not contract:
            return

        if vt_symbol in self.subscribed:
            return
        self.subscribed.add(vt_symbol)

        req = SubscribeRequest(contract.symbol, contract.exchange)
        self.main_engine.subscribe(req, contract.gateway_name)
    def subscribe_topic(self):
        """
        Subscribe to all private topics.
        订阅ticker,5档行情,account账户,order订单,成交
        因此,账户信息的变动和order信息的变动,都在websocket里面订阅,不是restful里面订阅
        """
        self.callbacks["spot/ticker"] = self.on_ticker
        self.callbacks["spot/depth5"] = self.on_depth
        self.callbacks["spot/account"] = self.on_account
        self.callbacks["spot/order"] = self.on_order

        # Subscribe to order update
        # 在gateway的最上面写要订阅哪些合约,这里会自动订阅ticker和depth5
        channels = []
        for instrument_id in instruments:
            channel = f"spot/order:{instrument_id}"
            channels.append(channel)

        req = {
            "op": "subscribe",
            "args": channels
        }
        self.send_packet(req)

        # Subscribe to account update
        # 这里写订阅的账户信息,主要为币种的变化
        channels = []
        for currency in currencies:
            channel = f"spot/account:{currency}"
            channels.append(channel)

        req = {
            "op": "subscribe",
            "args": channels
        }
        self.send_packet(req)

        # Subscribe to BTC/USDT trade for keep connection alive
        # 这里一直订阅BTC-USDT以保证连接
        req = {
            "op": "subscribe",
            "args": ["spot/trade:BTC-USDT"]
        }
        self.send_packet(req)

        # 这里添加gateway的文件,以保证能够一开始就订阅,这里算是改动比较大的了
        for spot in mysubscribe:
            req = SubscribeRequest(symbol=spot, exchange=Exchange.OKEX)
            self.subscribe(req)
예제 #18
0
    def process_history_event(self, event: Event) -> None:
        """"""
        history: List[BarData] = event.data
        if not history:
            return

        bar = history[0]
        chart = self.charts[bar.vt_symbol]
        chart.update_history(history)

        # Subscribe following data update
        contract = self.main_engine.get_contract(bar.vt_symbol)

        req = SubscribeRequest(contract.symbol, contract.exchange)
        self.main_engine.subscribe(req, contract.gateway_name)
예제 #19
0
    def subscribe(self, algo: AlgoTemplate, vt_symbol: str):
        """"""
        contract = self.main_engine.get_contract(vt_symbol)
        if not contract:
            self.write_log(f'订阅行情失败,找不到合约:{vt_symbol}', algo)
            return

        algos = self.symbol_algo_map.setdefault(vt_symbol, set())

        if not algos:
            req = SubscribeRequest(symbol=contract.symbol,
                                   exchange=contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)

        algos.add(algo)
예제 #20
0
def test_restapi():
    from vnpy.trader.object import SubscribeRequest
    from vnpy.trader.constant import Exchange
    #from vnpy.api.rest import RestClient
    #from vnpy.gateway.huobi import HuobiRestApi
    event_engine = EventEngine()
    #main_engine = MainEngine(event_engine)
    #main_engine.add_gateway(HuobiGateway)

    sreq = SubscribeRequest(symbol='btcusdt', exchange=Exchange.HUOBI)
    pprint(sreq)

    #resclient = RestClient()
    huobi_gateway = HuobiGateway(event_engine=event_engine)
    print(huobi_gateway)
    huobi_gateway.subscribe(sreq)
예제 #21
0
    def get_quote(self):
        x = self.main_engine.engines['oms'].ticks
        info = self.symbol_timer + "." + self.main_engine.gateways[
            self.engine_name].exchanges[self.symbols[self.symbol_timer]].name
        if info not in list(x.keys()):

            print('no data, resubscribe')
            quote = SubscribeRequest(
                self.symbol_timer,
                self.main_engine.gateways[self.engine_name].exchanges[
                    self.symbols[self.symbol_timer]])
            self.main_engine.subscribe(quote, self.engine_name)
            self.main_engine.engines['oms'].register_event()
        else:
            x = x[info]
            this_info = self.futures_info[self.symbol_timer]
            this_info.market_price = x.last_price
예제 #22
0
    def subscribe_topic(self):
        """
        Subscribe to all private topics.
        """
        self.callbacks["futures/ticker"] = self.on_ticker
        self.callbacks["futures/depth5"] = self.on_depth
        self.callbacks["futures/account"] = self.on_account
        self.callbacks["futures/order"] = self.on_order
        self.callbacks["futures/position"] = self.on_position
        self.callbacks["futures/candle60s"] = self.on_candle

        # Subscribe to order update
        channels = []
        for instrument_id in instruments:
            channel = f"futures/order:{instrument_id}"
            channels.append(channel)

        req = {"op": "subscribe", "args": channels}
        self.send_packet(req)

        # Subscribe to account update
        channels = []
        for currency in currencies:
            if currency != "USD":
                channel = f"futures/account:{currency}"
                channels.append(channel)

        req = {"op": "subscribe", "args": channels}
        self.send_packet(req)

        # Subscribe to position update
        channels = []
        for instrument_id in instruments:
            channel = f"futures/position:{instrument_id}"
            channels.append(channel)

        req = {"op": "subscribe", "args": channels}
        self.send_packet(req)

        # 这里添加gateway的文件,以保证能够一开始就订阅,这里算是改动比较大的了
        # 为了减少服务器压力,这里只需要订阅需要的合约,也就是季度交割合约
        for futures in instruments:
            req = SubscribeRequest(symbol=futures, exchange=Exchange.OKEXF)
            self.subscribe(req)
예제 #23
0
    def _init_strategy(self, strategy_name: str):
        """
        Init strategies in queue.
        """
        strategy = self.strategies[strategy_name]

        print(datetime.now(), strategy_name, strategy.vt_symbol)

        if strategy.inited:
            self.write_log(
                f"{strategy_name} initialization has been completed , repeat ban "
            )
            return

        self.write_log(f"{strategy_name} begin initialization ")

        # Call on_init function of strategy
        self.call_strategy_func(strategy, strategy.on_init)

        # Restore strategy data(variables)
        data = self.strategy_data.get(strategy_name, None)
        if data:
            for name in strategy.variables:
                value = data.get(name, None)
                if value:
                    setattr(strategy, name, value)

        # Subscribe market data
        contract = self.main_engine.get_contract(strategy.vt_symbol)
        if contract:
            req = SubscribeRequest(symbol=contract.symbol,
                                   exchange=contract.exchange)
            self.main_engine.subscribe(req, contract.gateway_name)
        else:
            self.write_log(
                f" subscribe to market failure , can not find contract {strategy.vt_symbol}",
                strategy)

        # Put event to update init completed status.
        strategy.inited = True
        self.put_strategy_event(strategy)
        self.write_log(f"{strategy_name} loading finished ")
예제 #24
0
def main():
    """"""

    event_engine = EventEngine()

    main_engine = MainEngine(event_engine)
    main_engine.add_gateway(FmexGateway)

    gateway_name = "FMEX"
    symbol = "BTCUSD_P"
    api_key = loadJsonSetting('fmex_test.json')
    conn_setting = {
        "会话数": 3,
        "服务器": "TESTNET",  #REAL,TESTNET
        "代理地址": "",
        "代理端口": '',
        "symbols": symbol
    }
    conn_setting = {**api_key, **conn_setting}

    req = SubscribeRequest(symbol=symbol, exchange=Exchange(gateway_name))

    main_engine.connect(conn_setting, gateway_name)
    main_engine.subscribe(req, gateway_name)

    app_setting = {
        "template_name": 'MinerAlgo',
        "vt_symbol": symbol + "." + gateway_name,
        "volume": 900,  #一笔挂单量,单位USD
        "interval": 2,  #每隔多少秒循环一次挂单函数
        "minimum_distance": 40,  #离盘口的最小距离,单位USD
        "guadan_max_count": 15,  #单方向挂单最大数量
        "algo_name": 'FCOIN_Miner_guadan'
    }

    APP_NAME = "FcoinMiner"
    main_engine.add_app(AlgoTradingApp)
    algo_engine = main_engine.get_engine(APP_NAME)
    algo_engine.init_engine(app_setting)
    algo_engine.start_engine()
예제 #25
0
def get_bar(gateway, symbol, exchange: "Exchange", interval: "Interval"):
    """
    获取bar数据并存入数据库
    :param gateway: 
    :param symbol: 
    :param exchange: 
    :param interval: 事件间隔
    :return: 
    """
    gevent.sleep(1)
    req = SubscribeRequest(symbol=symbol, exchange=exchange)
    sleep_second = 0
    # 传入的数据转换成需要的格式  begin************
    if Interval.MINUTE == interval:
        sleep_second = 30
    elif Interval.HOUR == interval:
        sleep_second = 50 * ONE_MINUTE
    elif Interval.DAILY == interval:
        sleep_second = 12 * ONE_HOUR
    # 传入的数据转换成需要的格式     end*************
    while True:
        gateway.subscribe_1bar(req, interval)
        gevent.sleep(sleep_second)
예제 #26
0
def main():
    """"""

    event_engine = EventEngine()

    main_engine = MainEngine(event_engine)
    main_engine.add_gateway(FmexGateway)

    gateway_name = "FMEX"
    symbol = "BTCUSD_P"
    api_key = loadJsonSetting('fmex_test.json')
    conn_setting = {
        "会话数": 3,
        "服务器": "TESTNET",  #REAL
        "代理地址": "",
        "代理端口": '',
        "symbols": symbol
    }
    conn_setting = {**api_key, **conn_setting}

    req = SubscribeRequest(symbol=symbol, exchange=Exchange(gateway_name))

    main_engine.connect(conn_setting, gateway_name)
    main_engine.subscribe(req, gateway_name)

    app_setting = {
        "template_name": 'MinerAlgo',
        "vt_symbol": symbol + "." + gateway_name,
        "algo_name": 'FCOIN_Miner_guadan'
    }

    APP_NAME = "FcoinMiner"
    main_engine.add_app(AlgoTradingApp)
    algo_engine = main_engine.get_engine(APP_NAME)
    time.sleep(5)
    algo_engine.init_engine(app_setting)
    algo_engine.start_engine()
예제 #27
0
#加载ctp接口
main_engine.add_gateway(CtpGateway)
# 连接CTP
setting = {
    "用户名": "152402",
    "密码": "baiyun18",
    "经纪商代码": "9999",
    "交易服务器": "180.168.146.187:10101",
    "行情服务器": "180.168.146.187:10111",
    "产品名称": "simnow_client_test",
    "授权编码": "0000000000000000",
    "产品信息": ""
}
main_engine.connect(setting, 'CTP')
APP_NAME = "DataRecorder"

EVENT_RECORDER_LOG = "eRecorderLog"
EVENT_RECORDER_UPDATE = "eRecorderUpdate"
time.sleep(3)

contract_list = main_engine.get_all_contracts()
print(contract_list[1])
for contract in contract_list:
    req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange)
    main_engine.subscribe(req, contract.gateway_name)
recorder = RecorderEngine(main_engine, event_engine)
while True:
    now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    time.sleep(1)
    if now_time[-2:] == '00':
        print(now_time)
예제 #28
0
engine = init_cli_trading([IbGateway
                           ])  #返回Script_engine 示例,并且给main_engine注册了gateway
engine.connect_gateway(setting, "IB")  #链接

# 查询资金 - 自动
sleep(10)
print("***查询资金和持仓***")
print(engine.get_all_accounts(use_df=True))
# 查询持仓
print(engine.get_all_positions(use_df=True))

# 订阅行情
from vnpy.trader.constant import Exchange
from vnpy.trader.object import SubscribeRequest
# 从我测试直接用Script_engine有问题,IB的品种太多,get_all_contracts命令不行,需要指定具体后才可以,这里使用main_engine订阅
req1 = SubscribeRequest("AAPL", Exchange.NASDAQ)  #创建行情订阅
engine.main_engine.subscribe(req1, "IB")

print("***从IB读取历史数据, 返回历史数据输出到数据库和csv文件***")
from vnpy.trader.object import HistoryRequest
from vnpy.trader.object import Interval
start = datetime.strptime('20190901', "%Y%m%d")
end = datetime.strptime('20210220', "%Y%m%d")
historyreq = HistoryRequest(symbol="AAPL",
                            exchange=Exchange.NASDAQ,
                            start=start,
                            interval=Interval.HOUR)

# # 读取历史数据,并把历史数据BarData放入数据库
bardatalist = engine.main_engine.query_history(historyreq, "IB")
    def main_logic(self):

        while True:

            prev_time = pd.Timestamp.now()
            time.sleep(10)
            print('------------------------------------')
            self.cal_cash()

            for symbol in self.symbols:

                x = self.main_engine.engines['oms'].ticks
                info = symbol + "." + 'BINANCE'
                if info not in list(x.keys()):
                    print('no data, resubscribe')
                    quote = SubscribeRequest(symbol,
                                             BinancesGateway.exchanges[0])
                    self.main_engine.subscribe(quote, self.enginename)

                else:
                    x = x[info]
                    this_symbol_data = self.data_quote[symbol]
                    this_symbol_data.last = x.last_price
                    this_symbol_data.bid_price_1 = x.bid_price_1
                    this_symbol_data.ask_price_1 = x.ask_price_1
                    this_symbol_data.bid_price_5 = x.bid_price_5
                    this_symbol_data.ask_price_5 = x.ask_price_5
                # strategy
                self.order_logic(symbol)

                # update 5 min bar
                curtime = pd.Timestamp.now()

                # if symbol == 'YFIIUSDT':
                #   self.order_test(symbol)
                # algo logic when bar updated
                # curtime.minute % 5 == 0
                if curtime.minute % 1 == 0 and curtime.minute > prev_time.minute:

                    t1 = time.time()
                    hist_min = pd.Timestamp.now() - timedelta(minutes=20)
                    df = None
                    while df is None:
                        try:
                            quote = HistoryRequest(
                                symbol,
                                self.main_engine.gateways[
                                    self.enginename].exchanges[0],
                                start=datetime(hist_min.year, hist_min.month,
                                               hist_min.day, hist_min.hour, 0,
                                               0),
                                interval=Interval.MINUTE)

                            df = self.main_engine.query_history(
                                quote, self.enginename)
                        except:
                            time.sleep(20)
                            print('---------------reloading------------------')
                    df_new, df_5min = self.get_5min_data(df)
                    t2 = time.time()
                    # print(t2 - t1)
                    olddata = self.data_hist[symbol]
                    newdate = df_5min['datetime'].iloc[0]
                    olddata = olddata[olddata['datetime'] < newdate]
                    newdata = olddata.append(df_5min.iloc[:-1])
                    newdata.drop_duplicates(inplace=True)
                    self.data_hist[symbol] = newdata[[
                        'datetime', 'open', 'high', 'low', 'close', 'volume'
                    ]]

                    this_directory = PATH + symbol
                    this_model_file = this_directory + '\\' + symbol + '_FUTURES' + '_5MINUTE_all.pkl'
                    with open(this_model_file, 'wb') as f:
                        # second save training names
                        pickle.dump(self.data_hist[symbol], f)
                        f.close()
예제 #30
0
 def subscribe_tick(self, event: Event):
     req = SubscribeRequest(
         symbol=event.data.symbol,
         exchange=event.data.exchange,
     )
     self.subscribe(req)