Ejemplo n.º 1
0
    def test_lib_insert_order_time_check_5(self):
        '''
        lib下单时间判断测试5

        回测时间:
            起始交易日(datetime.date)在非周一
        订阅:
            cu(有夜盘,凌晨1点结束夜盘),rb(夜盘23点结束),jd(无夜盘)
        测试:
            1 起始回测在21点后rb、cu下单,到第二日9点后jd下单
            2 本交易日白盘9:00后jd下单
        '''
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_lib_insert_order_time_check_5.script.lzma"))

        utils.RD = random.Random(4)
        api = TqApi(backtest=TqBacktest(datetime.date(2019, 12, 3),
                                        datetime.date(2019, 12, 4)),
                    _ins_url=self.ins_url_2019_12_04)  # 2019, 12, 3:周二
        symbol1 = "SHFE.cu2002"  # 有夜盘,凌晨1点结束夜盘
        symbol2 = "SHFE.rb2002"  # 夜盘23点结束
        symbol3 = "DCE.jd2002"  # 无夜盘
        quote1 = api.get_quote(symbol1)
        quote2 = api.get_quote(symbol2)
        quote3 = api.get_quote(symbol3)
        position1 = api.get_position(symbol1)
        position2 = api.get_position(symbol2)
        position3 = api.get_position(symbol3)
        target_pos1 = TargetPosTask(api, symbol1)
        target_pos2 = TargetPosTask(api, symbol2)
        target_pos3 = TargetPosTask(api, symbol3)
        orders = api.get_order()
        try:
            # 1 起始回测在21点后rb、cu下单,到第二日9点后jd下单
            target_pos1.set_target_volume(1)
            target_pos2.set_target_volume(2)
            target_pos3.set_target_volume(3)
            while max(quote1.datetime, quote2.datetime,
                      quote3.datetime) < "2019-12-02 21:05:00.000000":
                api.wait_update()
            self.assertEqual(len(orders), 2)
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 0)
            # 2 本交易日白盘9:00后jd下单
            while max(quote1.datetime, quote2.datetime,
                      quote3.datetime) < "2019-12-03 09:02:00.000000":
                api.wait_update()
            self.assertEqual(len(orders), 3)
            while True:
                api.wait_update()
        except BacktestFinished:
            self.assertEqual(len(orders), 3)
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 3)
            api.close()
Ejemplo n.º 2
0
    def test_lib_insert_order_time_check_7(self):
        """
        lib下单时间判断测试7

        订阅合约:
            订阅周六有行情的和周六无行情的
        测试:
            (测试:回测从周六开始时 可交易时间段的计算、判断)
            1 回测刚开始:current_datetime 为 0:00 , 只有cu能下单,另外两个合约直到白盘9点下单
        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(os.path.join(dir_path, "log_file", "test_lib_insert_order_time_check_7.script.lzma.lzma"))

        TqApi.RD = random.Random(4)
        api = TqApi(
            backtest=TqBacktest(datetime.datetime(2019, 11, 30, 0, 0, 0), datetime.datetime(2019, 12, 2, 9, 30)),
            _ins_url=self.ins_url_2019_12_04)
        symbol1 = "SHFE.cu2002"  # 有夜盘,凌晨1点结束夜盘
        symbol2 = "SHFE.rb2002"  # 夜盘23点结束
        symbol3 = "DCE.jd2002"  # 无夜盘
        quote1 = api.get_quote(symbol1)
        quote2 = api.get_quote(symbol2)
        quote3 = api.get_quote(symbol3)
        position1 = api.get_position(symbol1)
        position2 = api.get_position(symbol2)
        position3 = api.get_position(symbol3)
        target_pos1 = TargetPosTask(api, symbol1)
        target_pos2 = TargetPosTask(api, symbol2)
        target_pos3 = TargetPosTask(api, symbol3)
        orders = api.get_order()
        try:
            # 1 回测刚开始:current_datetime 为 0:00 , 只有cu能下单,另外两个合约直到白盘9点下单
            target_pos1.set_target_volume(1)
            target_pos2.set_target_volume(2)
            target_pos3.set_target_volume(3)
            while max(quote1.datetime, quote2.datetime, quote3.datetime) < "2019-11-30 00:02:00.000000":
                api.wait_update()
            self.assertEqual(len(orders), 1)
            while max(quote1.datetime, quote2.datetime, quote3.datetime) < "2019-11-30 00:15:00.000000":
                api.wait_update()
            self.assertEqual(len(orders), 1)
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 0)
            self.assertEqual(position3.pos, 0)

            while max(quote1.datetime, quote2.datetime, quote3.datetime) < "2019-12-02 09:05:00.000000":
                api.wait_update()
            self.assertEqual(len(orders), 3)
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 3)

            while True:
                api.wait_update()
        except BacktestFinished:
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 3)
            api.close()
def record_quotes(symbols, _stock, _md_url):
    api = TqApi(auth=AUTH, _stock=_stock, _md_url=_md_url)
    api._requests["quotes"] = set(symbols)
    api._send_chan.send_nowait({
        "aid": "subscribe_quote",
        "ins_list": ",".join(symbols)
    })
    api.wait_update()
    api.wait_update()
    quotes = {}
    csv_files = {}
    for s in symbols:
        quotes[s] = api.get_quote(s)
        csv_files[s] = (create_csvfile(s, "new" if _stock else "old"))
        csv_files[s][1].writerow(["local_nano_time", "quote_nano_time"] +
                                 HEADER_ROW)
    end = time() + 60 * 60  # 记录 30 min 分钟的数据分析
    while True:
        if end < time():
            break
        api.wait_update()
        for s, q in quotes.items():
            if api.is_changing(q):
                csv_files[s][1].writerow([
                    f"{time()*1e9:.0f}", f"{_str_to_nano(q['datetime']):.0f}"
                ] + [q[k] for k in HEADER_ROW])
    close_csvfiles(csv_files)
    api.close()
Ejemplo n.º 4
0
    def test_is_changing(self):
        """is_changing() 测试"""

        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(os.path.join(dir_path, "log_file", "test_func_basic_is_changing.script.lzma"))
        # 测试: 模拟账户下单
        TqApi.RD = random.Random(4)
        api = TqApi(_ins_url=self.ins_url, _td_url=self.td_url, _md_url=self.md_url)
        quote = api.get_quote("SHFE.rb2001")
        position = api.get_position("SHFE.rb2001")
        order1 = api.insert_order("DCE.m2001", "BUY", "OPEN", 1)
        api.wait_update()
        order2 = api.insert_order("SHFE.rb2001", "SELL", "OPEN", 2)
        api.wait_update()
        self.assertEqual(api.is_changing(order2, "status"), True)
        self.assertEqual(api.is_changing(position, "volume_short"), True)
        self.assertEqual(api.is_changing(position, "volume_long"), False)
        order3 = api.insert_order("SHFE.rb2001", "BUY", "CLOSETODAY", 1)
        while order3.status == "ALIVE":
            api.wait_update()
        self.assertEqual(api.is_changing(order3, "status"), True)
        self.assertEqual(api.is_changing(position, "volume_short"), True)
        self.assertEqual(api.is_changing(quote, "last_price"), False)

        api.close()
Ejemplo n.º 5
0
    def test_get_quote_backtest(self):
        """
        回测获取行情报价
        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(os.path.join(dir_path, "log_file", "test_md_backtest_get_quote.script.lzma"))
        # 测试
        try:
            utils.RD = random.Random(1)
            api = TqApi(backtest=TqBacktest(datetime(2019, 10, 15), datetime(2019, 10, 16)), _ins_url=self.ins_url_2019_07_03,
                        _td_url=self.td_url, _md_url=self.md_url)
            with closing(api):
                quote = api.get_quote("SHFE.cu2001")
                quote_data = {k: v for k, v in quote.items()}
                quote_data["trading_time"] = {k: v for k, v in quote_data["trading_time"].items()}

                self.assertEqual(json.dumps(quote_data, sort_keys=True),
                                 '{"amount": NaN, "ask_price1": 47070.0, "ask_price2": NaN, "ask_price3": NaN, "ask_price4": NaN, "ask_price5": NaN, "ask_volume1": 1, "ask_volume2": 0, "ask_volume3": 0, "ask_volume4": 0, "ask_volume5": 0, "average": NaN, "bid_price1": 47050.0, "bid_price2": NaN, "bid_price3": NaN, "bid_price4": NaN, "bid_price5": NaN, "bid_volume1": 1, "bid_volume2": 0, "bid_volume3": 0, "bid_volume4": 0, "bid_volume5": 0, "close": NaN, "commission": 11.594999999999999, "datetime": "2019-10-14 23:59:59.999999", "delivery_month": 1, "delivery_year": 2020, "expire_datetime": 1579071600.0, "expired": false, "highest": NaN, "ins_class": "FUTURE", "instrument_id": "SHFE.cu2001", "last_price": 47060.0, "lower_limit": NaN, "lowest": NaN, "margin": 16233.000000000002, "max_limit_order_volume": 500, "max_market_order_volume": 0, "min_limit_order_volume": 0, "min_market_order_volume": 0, "open": NaN, "open_interest": 45357, "option_class": "", "pre_close": NaN, "pre_open_interest": 0, "pre_settlement": NaN, "price_decs": 0, "price_tick": 10, "product_id": "cu", "settlement": NaN, "strike_price": NaN, "trading_time": {"day": [["09:00:00", "10:15:00"], ["10:30:00", "11:30:00"], ["13:30:00", "15:00:00"]], "night": [["21:00:00", "25:00:00"]]}, "underlying_symbol": "", "upper_limit": NaN, "volume": 0, "volume_multiple": 5}')

                # 其他取值方式
                self.assertNotEqual(quote["pre_close"], quote.pre_close)
                self.assertNotEqual(quote.get("pre_settlement"), quote.pre_settlement)
                self.assertNotEqual(quote.get("highest"), quote.highest)
                self.assertNotEqual(quote.get("lowest"), quote.lowest)
                self.assertNotEqual(quote["open"], quote.open)
                self.assertNotEqual(quote["close"], quote.close)
        except BacktestFinished:
            api.close()
            print("backtest finished")
Ejemplo n.º 6
0
def test_get_quote_normal_backtest():
    """
    回测获取行情报价

    """
    # 获取行情
    api = TqApi(
        backtest=TqBacktest(datetime(2019, 10, 15), datetime(2019, 10, 16)),
        _ins_url="https://openmd.shinnytech.com/t/md/symbols/2019-07-03.json")
    quote = api.get_quote("SHFE.cu2001")
    quote_data = {k: v for k, v in quote.items()}
    quote_data["trading_time"] = {
        k: v
        for k, v in quote_data["trading_time"].items()
    }

    assert json.dumps(quote_data, sort_keys=True) == \
           '{"amount": NaN, "ask_price1": 47070.0, "ask_price2": NaN, "ask_price3": NaN, "ask_price4": NaN, "ask_price5": NaN, "ask_volume1": 1, "ask_volume2": 0, "ask_volume3": 0, "ask_volume4": 0, "ask_volume5": 0, "average": NaN, "bid_price1": 47050.0, "bid_price2": NaN, "bid_price3": NaN, "bid_price4": NaN, "bid_price5": NaN, "bid_volume1": 1, "bid_volume2": 0, "bid_volume3": 0, "bid_volume4": 0, "bid_volume5": 0, "close": NaN, "commission": 11.594999999999999, "datetime": "2019-10-14 23:59:59.999999", "delivery_month": 1, "delivery_year": 2020, "expire_datetime": 1579071600.0, "expired": false, "highest": NaN, "ins_class": "FUTURE", "instrument_id": "SHFE.cu2001", "last_price": 47060.0, "lower_limit": NaN, "lowest": NaN, "margin": 16233.000000000002, "max_limit_order_volume": 500, "max_market_order_volume": 0, "min_limit_order_volume": 0, "min_market_order_volume": 0, "open": NaN, "open_interest": 90714, "pre_close": NaN, "pre_open_interest": 0, "pre_settlement": NaN, "price_decs": 0, "price_tick": 10, "settlement": NaN, "strike_price": NaN, "trading_time": {"day": [["09:00:00", "10:15:00"], ["10:30:00", "11:30:00"], ["13:30:00", "15:00:00"]], "night": [["21:00:00", "25:00:00"]]}, "underlying_symbol": "", "upper_limit": NaN, "volume": 0, "volume_multiple": 5}'

    # 其他取值方式
    assert quote["pre_close"] != quote.pre_close
    assert quote.get("pre_settlement") != quote.pre_settlement
    assert quote.get("highest") != quote.highest
    assert quote.get("lowest") != quote.lowest
    assert quote["open"] != quote.open
    assert quote["close"] != quote.close
    api.close()
Ejemplo n.º 7
0
 def test_api_vipuser(self):
     api = TqApi(auth="[email protected],MaYanQiong")
     self.assertEqual(api._md_url,
                      "wss://api.shinnytech.com/t/nfmd/front/mobile")
     quote = api.get_quote("SSE.10002513")
     print(quote.datetime, quote.last_price)
     api.close()
Ejemplo n.º 8
0
def test_tdsdk_quote():
    api = TqApi(TqSim())
    quote = api.get_quote("SHFE.cu1908")
    print(quote["last_price"], quote["volume"])
    while True:
        api.wait_update()
        print(quote["datetime"], quote["last_price"])
Ejemplo n.º 9
0
    def test_is_changing(self):
        """
            is_changing() 测试
            注:本函数不是回测,重新生成测试用例script文件时更改为当前可交易的合约代码,在盘中生成,且_ins_url可能需修改。
        """

        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_func_basic_is_changing.script.lzma"))
        # 测试: 模拟账户下单
        utils.RD = random.Random(4)
        api = TqApi(_ins_url=self.ins_url_2020_04_02,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        quote = api.get_quote("SHFE.rb2010")
        position = api.get_position("SHFE.rb2010")
        order1 = api.insert_order("DCE.m2009", "BUY", "OPEN", 1)
        api.wait_update()
        order2 = api.insert_order("SHFE.rb2010", "SELL", "OPEN", 2)
        api.wait_update()
        self.assertTrue(api.is_changing(order2, "status"))
        self.assertTrue(api.is_changing(position, "volume_short"))
        self.assertFalse(api.is_changing(position, "volume_long"))
        order3 = api.insert_order("SHFE.rb2010", "BUY", "CLOSETODAY", 1)
        while order3.status == "ALIVE":
            api.wait_update()
        self.assertTrue(api.is_changing(order3, "status"))
        self.assertTrue(api.is_changing(position, "volume_short"))
        self.assertFalse(api.is_changing(quote, "last_price"))

        api.close()
Ejemplo n.º 10
0
def run_tianqin_code(bid, user_id, pwd, td_url):
    api = TqApi(TqAccount(bid, user_id, pwd),
                auth="[email protected],MaYanQiong",
                _stock=True,
                _td_url=td_url)
    is_ctp = False if bid == "快期模拟" else True
    account = api.get_account()
    if bid == "快期模拟":
        assert account.ctp_balance == '-' or math.isnan(account.ctp_balance)
        assert account.ctp_available == '-' or math.isnan(
            account.ctp_available)
    else:
        logger.info(f"{account.ctp_balance}, {account.ctp_available}")

    logger.info(f"{'='*30} 登录成功后,账户初始状态 {'='*30}")
    positions = api._data["trade"][user_id]["positions"]
    orders = api._data["trade"][user_id]["orders"]
    check_orders(orders, api, is_ctp)
    check_positions(positions, api, is_ctp)
    check_account(account, positions, is_ctp)
    check_risk_rule(api, None)
    check_risk_data(api, "SSE.10002513")

    api.set_risk_management_rule("SSE", True)

    logger.info(f"{'='*12} 期权 开仓 {'='*12}")
    quote = api.get_quote("SSE.10002513")  # ETF 期权
    # 挂单
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.lower_limit + quote.price_tick, volume=2)
    order = api.insert_order(symbol="SSE.10002513",
                             direction="SELL",
                             offset="OPEN",
                             limit_price=quote.upper_limit - quote.price_tick,
                             volume=2)
    # 可成交
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=3)
    # 可成交 FAK 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=2, advanced="FAK")
    # 可成交 FOK
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=2, advanced="FOK")

    # BEST
    # order = api.insert_order(symbol="SSE.10002513", direction="SELL", offset="CLOSE", limit_price="BEST", volume=10)
    # BEST FOK 下单失败,已撤单报单被拒绝12038,合约代码:SSE.10002513,下单方向:买,开平标志:开仓,委托价格:最优价,委托手数:3
    # order = api.insert_order(symbol="SSE.10002513", direction="SELL", offset="CLOSE", limit_price="BEST", volume=3, advanced="FOK")

    # any_price 通知: 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", volume=3)
    # FIVELEVEL 通知: 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price="FIVELEVEL", volume=3)
    api.cancel_order(order)
    api.cancel_order(order)
    api.cancel_order(order)
    while order.status == "ALIVE":
        api.wait_update()
    check_all(api, bid, user_id)
    check_risk_rule(api, None)
    check_risk_data(api, "SSE.10002513")
    api.close()
Ejemplo n.º 11
0
    def test_lib_insert_order_time_check_2(self):
        """
        lib下单时间判断测试2

        回测时间:
            10:15 - 10:45
        订阅合约:
            IF、T(无盘中休息时间),cu(有盘中休息时间)
        测试:
          10:15 - 10:30期间IF和T能立即下单,cu等到10:30以后下单;
        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_lib_insert_order_time_check_2.script.lzma"))

        utils.RD = random.Random(4)
        api = TqApi(backtest=TqBacktest(
            datetime.datetime(2020, 2, 17, 10, 15, 0),
            datetime.datetime(2020, 2, 17, 10, 45, 0)),
                    _ins_url=self.ins_url_2020_02_18,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        symbol1 = "SHFE.cu2003"
        symbol2 = "CFFEX.T2003"
        symbol3 = "CFFEX.IF2003"
        quote3 = api.get_quote(symbol3)
        position1 = api.get_position(symbol1)
        position2 = api.get_position(symbol2)
        position3 = api.get_position(symbol3)
        target_pos1 = TargetPosTask(api, symbol1)
        target_pos2 = TargetPosTask(api, symbol2)
        target_pos3 = TargetPosTask(api, symbol3)
        orders = api.get_order()
        try:
            # 1  10:15 - 10:30期间IF和T能立即下单,cu等到10:30以后下单;
            target_pos1.set_target_volume(1)
            target_pos2.set_target_volume(2)
            target_pos3.set_target_volume(3)
            while datetime.datetime.strptime(
                    quote3.datetime,
                    "%Y-%m-%d %H:%M:%S.%f") < datetime.datetime(
                        2020, 2, 17, 10, 25):
                api.wait_update()
            self.assertEqual(len(orders), 2)
            self.assertEqual(position1.pos, 0)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 3)
            while True:
                api.wait_update()
        except BacktestFinished:
            # 验证下單情況
            self.assertEqual(len(orders), 3)
            self.assertEqual(position1.pos, 1)
            self.assertEqual(position2.pos, 2)
            self.assertEqual(position3.pos, 3)
            print("回测结束")
            api.close()
Ejemplo n.º 12
0
 def test_openmd_vipuser_bt(self):
     bt = TqBacktest(start_dt=datetime(2020, 6, 1),
                     end_dt=datetime(2020, 6, 2))
     api = TqApi(backtest=bt, _stock=False, auth="[email protected],MaYanQiong")
     quote = api.get_quote("DCE.m2009")
     print(quote.datetime, quote.last_price)
     self.assertEqual("2020-05-29 22:59:59.999999", quote.datetime)
     self.assertEqual(2826.0, quote.last_price)
     api.close()
Ejemplo n.º 13
0
 def test_get_quote_normal(self):
     """
     获取行情报价
     """
     # 预设服务器端响应
     self.mock.run("test_md_basic_get_quote_normal.script")
     # 获取行情
     api = TqApi(_ins_url=self.ins_url,
                 _td_url=self.td_url,
                 _md_url=self.md_url)
     q = api.get_quote("SHFE.cu1901")
     self.assertEqual(46940.0, q.last_price)
     api.close()
Ejemplo n.º 14
0
def acceptMD():
    api = TqApi()
    quote = api.get_quote("SHFE.fu2009")

    saveTick(quote)

    while True:
        try:
            api.wait_update()
            saveTick(quote)
        except FileNotFoundError:
            print("File Not Found Error")
            time.sleep(3)
Ejemplo n.º 15
0
    def test_get_quote_normal(self):
        """
        获取行情报价

        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_md_basic_get_quote_normal.script"))
        # 获取行情
        api = TqApi(_ins_url=self.ins_url,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        q = api.get_quote("SHFE.cu1909")
        self.assertEqual(
            str(q),
            "{'datetime': '2019-09-16 14:59:59.999500', 'ask_price1': 47650.0, 'ask_volume1': 10, 'bid_price1': 47570.0, 'bid_volume1': 5, 'last_price': 47580.0, 'highest': 47860.0, 'lowest': 47580.0, 'open': 47860.0, 'close': 47580.0, 'average': 47732.35, 'volume': 9020, 'amount': 2152729000.0, 'open_interest': 6940, 'settlement': 47730.0, 'upper_limit': 49650.0, 'lower_limit': 44920.0, 'pre_open_interest': 13260, 'pre_settlement': 47290.0, 'pre_close': 47590.0, 'price_tick': 10, 'price_decs': 0, 'volume_multiple': 5, 'max_limit_order_volume': 500, 'max_market_order_volume': 0, 'min_limit_order_volume': 0, 'min_market_order_volume': 0, 'underlying_symbol': '', 'strike_price': nan, 'change': nan, 'change_percent': nan, 'expired': False, 'margin': 16233.000000000002, 'commission': 11.594999999999999, 'instrument_id': 'SHFE.cu1909', 'ask_price5': '-', 'ask_volume5': 0, 'ask_price4': 49250.0, 'ask_volume4': 50, 'ask_price3': 47990.0, 'ask_volume3': 5, 'ask_price2': 47730.0, 'ask_volume2': 10, 'bid_price2': 46560.0, 'bid_volume2': 100, 'bid_price3': 45650.0, 'bid_volume3': 270, 'bid_price4': 44920.0, 'bid_volume4': 5, 'bid_price5': '-', 'bid_volume5': 0}"
        )
        self.assertEqual(q.datetime, "2019-09-16 14:59:59.999500")
        self.assertEqual(q.ask_price1, 47650.0)
        self.assertEqual(q.ask_volume1, 10)
        self.assertEqual(q.bid_price1, 47570.0)
        self.assertEqual(q.bid_volume1, 5)
        self.assertEqual(q.last_price, 47580.0)
        self.assertEqual(q.highest, 47860.0)
        self.assertEqual(q.lowest, 47580.0)
        self.assertEqual(q.open, 47860.0)
        self.assertEqual(q.close, 47580.0)
        self.assertEqual(q.average, 47732.35)
        self.assertEqual(q.volume, 9020)
        self.assertEqual(q.amount, 2152729000.0)
        self.assertEqual(q.open_interest, 6940)
        self.assertEqual(q.settlement, 47730.0)
        self.assertEqual(q.upper_limit, 49650.0)
        self.assertEqual(q.lower_limit, 44920)
        self.assertEqual(q.pre_open_interest, 13260)
        self.assertEqual(q.pre_settlement, 47290.0)
        self.assertEqual(q.pre_close, 47590.0)
        # 其他取值方式
        self.assertEqual(q["pre_close"], 47590.0)
        self.assertEqual(q.get("pre_settlement"), 47290.0)
        self.assertEqual(q.get("highest"), 47860.0)
        self.assertEqual(q.get("lowest"), 47580.0)
        self.assertEqual(q["open"], 47860.0)
        self.assertEqual(q["close"], 47580.0)
        # 报错测试
        self.assertRaises(Exception, api.get_quote, "SHFE.au1999")
        self.assertRaises(KeyError, q.__getitem__, "ask_price6")

        api.close()
def sell_close():
    api = TqApi(TqAccount(bid, user_id, pwd),
                url=td_url,
                auth="ringo,Shinnytech123")
    test_logger.info(f"{'='*12} 期权 卖平仓 {'='*12}")
    quote = api.get_quote(symbol)  # ETF 期权
    order = api.insert_order(symbol=symbol,
                             direction="SELL",
                             offset="CLOSE",
                             limit_price=quote.bid_price1,
                             volume=3)
    while order.status == "ALIVE":
        api.wait_update()
    api.wait_update()
    api.close()
Ejemplo n.º 17
0
def get_product_info(api: TqApi, symbol):
    q = api.get_quote(symbol)

    product_info = {
        'class': q.ins_class,
        'root': q.product_id,
        'symbol': q.instrument_id,
        'short_symbol': q.instrument_id.split(".")[1],
        'exchange': q.exchange_id,
        'price_tick': q.price_tick,
        'multiplier': q.volume_multiple,
        'trading_hours': q.trading_time,
        'max_limit_order_volume': q.max_limit_order_volume,
        'min_limit_order_volume': q.min_limit_order_volume
    }

    ds = pd.Series(product_info)
    return ds
Ejemplo n.º 18
0
    def test_lib_insert_order_time_check_6(self):
        '''
        lib下单时间判断测试6

        测试:
            设置目标持仓后在TargetPosTask未下单前调整目标持仓, lib等到10:30有行情之后调整到的是最新目标持仓
        '''
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_lib_insert_order_time_check_6.script.lzma"))

        utils.RD = random.Random(4)
        api = TqApi(backtest=TqBacktest(start_dt=datetime.datetime(
            2019, 7, 11, 10, 15),
                                        end_dt=datetime.date(2019, 7, 12)),
                    _ins_url=self.ins_url_2019_12_04,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        symbol1 = "SHFE.cu1908"
        symbol2 = "CFFEX.IF1908"  # 用于行情推进,到10:20
        quote2 = api.get_quote(symbol2)
        target_pos = TargetPosTask(api, symbol1)
        orders = api.get_order()
        position = api.get_position(symbol1)
        try:
            target_pos.set_target_volume(5)
            while quote2.datetime < "2019-07-11 10:20:00.000000":
                api.wait_update()
            self.assertEqual(len(api.get_order()), 0)
            target_pos.set_target_volume(2)
            while quote2.datetime < "2019-07-11 10:25:00.000000":
                api.wait_update()
            self.assertEqual(len(api.get_order()), 0)
            while True:
                api.wait_update()
        except BacktestFinished:
            self.assertEqual(len(orders), 1)
            self.assertEqual(position.pos, 2)
            api.close()
Ejemplo n.º 19
0
    def test_is_changing(self):
        # 预设服务器端响应
        self.mock.run("test_func_basic_is_changing.script")
        # 测试: 模拟账户下单
        TqApi.RD = random.Random(4)
        api = TqApi(_ins_url=self.ins_url,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        quote = api.get_quote("SHFE.rb2001")
        position = api.get_position("SHFE.rb2001")
        order1 = api.insert_order("DCE.m2001", "BUY", "OPEN", 1)
        api.wait_update()
        order2 = api.insert_order("SHFE.rb2001", "SELL", "OPEN", 2)
        api.wait_update()
        self.assertEqual(api.is_changing(order2, "status"), True)
        self.assertEqual(api.is_changing(position, "volume_short"), True)
        self.assertEqual(api.is_changing(position, "volume_long"), False)
        order3 = api.insert_order("SHFE.rb2001", "BUY", "CLOSETODAY", 1)
        while order3.status == "ALIVE":
            api.wait_update()
        self.assertEqual(api.is_changing(order3, "status"), True)
        self.assertEqual(api.is_changing(position, "volume_short"), True)

        self.assertEqual(api.is_changing(quote, "last_price"), False)
Ejemplo n.º 20
0
acc = TqSim(init_balance=1000000)

# 在创建 api 实例时传入 TqBacktest 就会进入回测模式
api = TqApi(acc,
            backtest=TqBacktest(start_dt=date(2018, 2, 1),
                                end_dt=date(2018, 5, 1)),
            web_gui='http://127.0.0.1:8889')  #

kind1 = 'SHFE.cu1901'
kind2 = 'SHFE.cu1902'

# 获得 m1901 5分钟K线的引用
klines1 = api.get_kline_serial([kind1], 1 * 30, data_length=8000)
klines2 = api.get_kline_serial([kind2], 1 * 30, data_length=8000)
quote1 = api.get_quote(kind1)
quote2 = api.get_quote(kind2)
# # 创建 m1901 的目标持仓 task,该 task 负责调整 m1901 的仓位到指定的目标仓位
target_pos1 = TargetPosTask(api, kind1)
target_pos2 = TargetPosTask(api, kind2)

now1 = datetime.datetime.strptime(quote1.datetime,
                                  "%Y-%m-%d %H:%M:%S.%f")  # 当前quote的时间
now2 = datetime.datetime.strptime(quote2.datetime,
                                  "%Y-%m-%d %H:%M:%S.%f")  # 当前quote的时间
# api.wait_update()
# time1 = time.ctime(klines2.iloc[-1].datetime/(10**9))

while True:
    api.wait_update()
    if not pd.isna(klines2['close'][0]):
Ejemplo n.º 21
0
from trading_strategies.technical_indicators import MACD_adj

ls_symbols = ['SHFE.rb1910', 'SHFE.hc1910', 'CZCE.MA909', 'CZCE.TA909', 'DCE.i1909']
# ls_symbols = ['SHFE.fu1909', 'SHFE.bu1912', 'DCE.jd1909']
api = TqApi('SIM')
# api = TqApi(TqSim(), backtest=TqBacktest(start_dt=dt.date(2019,1,2), end_dt=dt.date(2019,1,10)))

dict_quotes = {}
dict_klines = {}
dict_positions = {}
dict_target_pos = {}
dict_update_kline_chan = {}
dict_update_quote_chan = {}

for SYMBOL in ls_symbols:
    dict_quotes[SYMBOL] = api.get_quote(SYMBOL)  # 行情数据
    dict_klines[SYMBOL] = api.get_kline_serial(SYMBOL, duration_seconds=15 * 60)
    dict_positions[SYMBOL] = api.get_position(SYMBOL)
    dict_target_pos[SYMBOL] = TargetPosTask(api, SYMBOL)
    dict_update_kline_chan[SYMBOL] = api.register_update_notify(dict_klines[SYMBOL])
    dict_update_quote_chan[SYMBOL] = api.register_update_notify(dict_quotes[SYMBOL])


async def signal_generator(SYMBOL, strategy):
    """该task应用策略在价格触发时开仓,出发平仓条件时平仓"""
    klines = dict_klines[SYMBOL]
    position = dict_positions[SYMBOL]
    target_pos = dict_target_pos[SYMBOL]
    update_kline_chan = dict_update_kline_chan[SYMBOL]

    while True:
Ejemplo n.º 22
0
    short_value_ini = np.sum([
        short_cost_lst[i] * short_contract[i] * short_volume[i]
        for i in range(len(short_code_lst))
    ])

    while True:

        lst = []
        long_value_now = 0
        short_value_now = 0

        for i in range(len(long_code_lst)):
            index_code = long_code_lst[i]
            contract = long_contract[i]
            price_now = api.get_quote(
                exchageID[index_code.upper()]['ExchangeID'] + '.' +
                index_code).last_price
            print(price_now)
            long_value_now = long_value_now + price_now * long_contract[
                i] * long_volume[i]

        for i in range(len(short_code_lst)):
            index_code = short_code_lst[i]
            contract = short_contract[i]
            price_now = api.get_quote(
                exchageID[index_code.upper()]['ExchangeID'] + '.' +
                index_code).last_price
            print(price_now)
            short_value_now = short_value_now + price_now * short_contract[
                i] * short_volume[i]
        long_chng = long_value_now / long_value_ini - 1
Ejemplo n.º 23
0
 position_short = position_account.pos_short
 position = trading_info.loc[code]['position']
 if code == 'DCE.y2009':
     a = 0
 if position == 0 and position_short == 0 and position_long == 0:
     print('%s:   仓位%s手, 状态:%s' % (code, position, '完成'))
     continue
 elif position == position_long and position_short == 0:
     print('%s: 多头持仓%s手, 状态:%s' % (code, position, '完成'))
     continue
 elif position == -position_short and position_long == 0:
     print('%s: 空头持仓%s手, 状态:%s' % (code, position, '完成'))
     continue
 else:
     print('%s:   仓位%s手, 状态:%s' % (code, position, '未完成'))
 quote = api.get_quote(code)
 if position > 0:
     if position_short > 0:
         order_bp = Trd.insert_order_bp_limit(code)
     diff = position - position_long
     if diff > 0:
         order = Trd.insert_order_bk_limit(code, int(diff))
     elif diff < 0:
         order = Trd.insert_order_sp_limit(code, -int(diff))
 if position < 0:
     if position_long > 0:
         order_sp = Trd.insert_order_sp_limit(code)
     diff = -position - position_short
     if diff > 0:
         order = Trd.insert_order_sk_limit(code, int(diff))
     elif diff < 0:
Ejemplo n.º 24
0
Archivo: qq1.py Proyecto: bebeter/pyT
#!usr/bin/env python3
#-*- coding:utf-8 -*-

from tqsdk import TqApi, tafunc
from tqsdk.ta import *

api = TqApi()

underlying_quote = api.get_quote("CFFEX.IF2002")
klines = api.get_kline_serial('CFFEX.IF2002', 24 * 60 * 60, 20)
v = tafunc.get_his_volatility(klines, underlying_quote)
print("历史波动率:", v)

quote = api.get_quote("CFFEX.IO2002-C-3550")
bs_serise = BS_VALUE(klines, quote, 0.025)
print("理论价:", list(round(bs_serise['bs_price'], 2)))

klines2 = api.get_kline_serial(["CFFEX.IO2002-C-3550", "CFFEX.IF2002"],
                               24 * 60 * 60, 20)

values = OPTION_VALUE(klines2, quote)
print("内在价值:", list(values["intrins"]))
print("时间价值:", list(values["time"]))

impv = OPTION_IMPV(klines2, quote, 0.025)
print("隐含波动率:", list(round(impv['impv'] * 100, 2)))

greeks = OPTION_GREEKS(klines2, quote, 0.025, impv['impv'])
print("delta:", list(greeks["delta"]))
print("theta:", list(greeks["theta"]))
print("gamma:", list(greeks["gamma"]))
Ejemplo n.º 25
0
class Turtle:
    def __init__(self,
                 account,
                 symbol,
                 donchian_channel_open_position=20,
                 donchian_channel_stop_profit=10,
                 atr_day_length=20,
                 max_risk_ratio=0.5):
        self.account = account  # 交易账号
        self.symbol = symbol  # 合约代码
        self.donchian_channel_open_position = donchian_channel_open_position  # 唐奇安通道的天数周期(开仓)
        self.donchian_channel_stop_profit = donchian_channel_stop_profit  # 唐奇安通道的天数周期(止盈)
        self.atr_day_length = atr_day_length  # ATR计算所用天数
        self.max_risk_ratio = max_risk_ratio  # 最高风险度
        self.state = {
            "position": 0,  # 本策略净持仓数(正数表示多头,负数表示空头,0表示空仓)
            "last_price": float("nan"),  # 上次调仓价
        }

        self.n = 0  # 平均真实波幅(N值)
        self.unit = 0  # 买卖单位
        self.donchian_channel_high = 0  # 唐奇安通道上轨
        self.donchian_channel_low = 0  # 唐奇安通道下轨

        self.api = TqApi(self.account)
        self.quote = self.api.get_quote(self.symbol)
        # 由于ATR是路径依赖函数,因此使用更长的数据序列进行计算以便使其值稳定下来
        kline_length = max(donchian_channel_open_position + 1,
                           donchian_channel_stop_profit + 1,
                           atr_day_length * 5)
        self.klines = self.api.get_kline_serial(self.symbol,
                                                24 * 60 * 60,
                                                data_length=kline_length)
        self.account = self.api.get_account()
        self.target_pos = TargetPosTask(self.api,
                                        self.symbol,
                                        init_pos=self.state["position"])

    def recalc_paramter(self):
        # 平均真实波幅(N值)
        self.n = ATR(self.klines, self.atr_day_length)["atr"].iloc[-1]
        # 买卖单位
        self.unit = int((self.account.balance * 0.01) /
                        (self.quote.volume_multiple * self.n))
        # 唐奇安通道上轨:前N个交易日的最高价
        self.donchian_channel_high = max(
            self.klines.high[-self.donchian_channel_open_position - 1:-1])
        # 唐奇安通道下轨:前N个交易日的最低价
        self.donchian_channel_low = min(
            self.klines.low[-self.donchian_channel_open_position - 1:-1])
        print("唐其安通道上下轨: %f, %f" %
              (self.donchian_channel_high, self.donchian_channel_low))
        return True

    def set_position(self, pos):
        self.state["position"] = pos
        self.state["last_price"] = self.quote["last_price"]
        self.target_pos.set_target_volume(self.state["position"])

    def try_open(self):
        """开仓策略"""
        while self.state["position"] == 0:
            self.api.wait_update()
            if self.api.is_changing(self.klines.iloc[-1],
                                    "datetime"):  # 如果产生新k线,则重新计算唐奇安通道及买卖单位
                self.recalc_paramter()
            if self.api.is_changing(self.quote, "last_price"):
                print("最新价: %f" % self.quote.last_price)
                if self.quote.last_price > self.donchian_channel_high:  # 当前价>唐奇安通道上轨,买入1个Unit;(持多仓)
                    print("当前价>唐奇安通道上轨,买入1个Unit(持多仓): %d 手" % self.unit)
                    self.set_position(self.state["position"] + self.unit)
                elif self.quote.last_price < self.donchian_channel_low:  # 当前价<唐奇安通道下轨,卖出1个Unit;(持空仓)
                    print("当前价<唐奇安通道下轨,卖出1个Unit(持空仓): %d 手" % self.unit)
                    self.set_position(self.state["position"] - self.unit)

    def try_close(self):
        """交易策略"""
        while self.state["position"] != 0:
            self.api.wait_update()
            if self.api.is_changing(self.quote, "last_price"):
                print("最新价: ", self.quote.last_price)
                if self.state["position"] > 0:  # 持多单
                    # 加仓策略: 如果是多仓且行情最新价在上一次建仓(或者加仓)的基础上又上涨了0.5N,就再加一个Unit的多仓,并且风险度在设定范围内(以防爆仓)
                    if self.quote.last_price >= self.state[
                            "last_price"] + 0.5 * self.n and self.account.risk_ratio <= self.max_risk_ratio:
                        print("加仓:加1个Unit的多仓")
                        self.set_position(self.state["position"] + self.unit)
                    # 止损策略: 如果是多仓且行情最新价在上一次建仓(或者加仓)的基础上又下跌了2N,就卖出全部头寸止损
                    elif self.quote.last_price <= self.state[
                            "last_price"] - 2 * self.n:
                        print("止损:卖出全部头寸")
                        self.set_position(0)
                    # 止盈策略: 如果是多仓且行情最新价跌破了10日唐奇安通道的下轨,就清空所有头寸结束策略,离场
                    if self.quote.last_price <= min(
                            self.klines.
                            low[-self.donchian_channel_stop_profit - 1:-1]):
                        print("止盈:清空所有头寸结束策略,离场")
                        self.set_position(0)

                elif self.state["position"] < 0:  # 持空单
                    # 加仓策略: 如果是空仓且行情最新价在上一次建仓(或者加仓)的基础上又下跌了0.5N,就再加一个Unit的空仓,并且风险度在设定范围内(以防爆仓)
                    if self.quote.last_price <= self.state[
                            "last_price"] - 0.5 * self.n and self.account.risk_ratio <= self.max_risk_ratio:
                        print("加仓:加1个Unit的空仓")
                        self.set_position(self.state["position"] - self.unit)
                    # 止损策略: 如果是空仓且行情最新价在上一次建仓(或者加仓)的基础上又上涨了2N,就平仓止损
                    elif self.quote.last_price >= self.state[
                            "last_price"] + 2 * self.n:
                        print("止损:卖出全部头寸")
                        self.set_position(0)
                    # 止盈策略: 如果是空仓且行情最新价升破了10日唐奇安通道的上轨,就清空所有头寸结束策略,离场
                    if self.quote.last_price >= max(
                            self.klines.
                            high[-self.donchian_channel_stop_profit - 1:-1]):
                        print("止盈:清空所有头寸结束策略,离场")
                        self.set_position(0)

    def strategy(self):
        """海龟策略"""
        print("等待K线及账户数据...")
        deadline = time.time() + 5
        while not self.recalc_paramter():
            if not self.api.wait_update(deadline=deadline):
                raise Exception("获取数据失败,请确认行情连接正常并已经登录交易账户")
        while True:
            self.try_open()
            self.try_close()
Ejemplo n.º 26
0
#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'chengzhi'

from tqsdk import TqApi

# 可以指定debug选项将调试信息写入指定的文件中
api = TqApi(debug="debug.log")
quote = api.get_quote("SHFE.cu1909")
print(quote.datetime, quote.last_price, quote.ask_price1, quote.ask_price2)

while True:
    # 调用 wait_update 等待业务信息发生变化,例如: 行情发生变化, 委托单状态变化, 发生成交等等
    # 注意:其他合约的行情的更新也会触发业务信息变化,因此下面使用 is_changing 判断 cu1909 的行情是否有变化
    api.wait_update()
    # 如果 cu1909 的任何字段有变化,is_changing就会返回 True
    if api.is_changing(quote):
        print("行情变化", quote)
    # 只有当 cu1909 的最新价有变化,is_changing才会返回 True
    if api.is_changing(quote, "last_price"):
        print("最新价变化", quote.last_price)
    # 当 cu1909 的买1价/买1量/卖1价/卖1量中任何一个有变化,is_changing都会返回 True
    if api.is_changing(quote, ["ask_price1", "ask_volume1", "bid_price1", "bid_volume1"]):
        print("盘口变化", quote.ask_price1, quote.ask_volume1, quote.bid_price1, quote.bid_volume1)
def run_tianqin_code(bid, user_id, pwd, td_url):
    api = TqApi(TqAccount(bid, user_id, pwd), auth="[email protected],MaYanQiong", _stock=True,
                _md_url="wss://nfmd.shinnytech.com/t/nfmd/front/mobile", _td_url=td_url)
    print(api._md_url)
    is_ctp = False if bid == "快期模拟" else True
    account = api.get_account()
    if bid == "快期模拟":
        assert account.ctp_balance == '-' or math.isnan(account.ctp_balance)
        assert account.ctp_available == '-' or math.isnan(account.ctp_available)
    else:
        logger.info(f"{account.ctp_balance}, {account.ctp_available}")

    logger.info(f"{'='*30} 登录成功后,账户初始状态 {'='*30}")
    positions = api._data["trade"][user_id]["positions"]
    orders = api._data["trade"][user_id]["orders"]
    check_orders(orders, api, is_ctp)
    check_positions(positions, api, is_ctp)
    check_account(account, positions, is_ctp)

    logger.info(f"{'='*12} 期权 开仓 {'='*12}")
    quote = api.get_quote("CZCE.RM105")  # ETF 期权
    print(quote)
    # 挂单
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.lower_limit + quote.price_tick, volume=2)
    # 可成交
    order = api.insert_order(symbol="CZCE.RM105", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=1)
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=3)
    # 可成交 FAK 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=2, advanced="FAK")
    # 可成交 FOK
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price=quote.ask_price1, volume=2, advanced="FOK")

    # BEST
    # order = api.insert_order(symbol="SSE.10002513", direction="SELL", offset="CLOSE", limit_price="BEST", volume=10)
    # BEST FOK 下单失败,已撤单报单被拒绝12038,合约代码:SSE.10002513,下单方向:买,开平标志:开仓,委托价格:最优价,委托手数:3
    # order = api.insert_order(symbol="SSE.10002513", direction="SELL", offset="CLOSE", limit_price="BEST", volume=3, advanced="FOK")

    # any_price 通知: 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", volume=3)
    # FIVELEVEL 通知: 下单失败,CTP:交易所不支持的价格类型
    # order = api.insert_order(symbol="SSE.10002513", direction="BUY", offset="OPEN", limit_price="FIVELEVEL", volume=3)

    while order.status == "ALIVE":
        api.wait_update()
    api.wait_update()
    api.wait_update()
    check_all(api, bid, user_id)

    # logger.info(f"{'='*30} 发平仓挂单 {'='*30}")
    # positions = api._data["trade"][user_id]["positions"]
    # for pos in positions.values():
    #     symbol = f"{pos.exchange_id}.{pos.instrument_id}"
    #     quote = api.get_quote(symbol)
    #     if pos.pos_long > 0:
    #         api.insert_order(symbol=symbol, direction="SELL", offset="CLOSE",
    #                          limit_price=quote.upper_limit - quote.price_tick,
    #                          volume=pos.pos_long)
    #     if pos.pos_short > 0:
    #         api.insert_order(symbol=symbol, direction="BUY", offset="CLOSE",
    #                          limit_price=quote.lower_limit + quote.price_tick,
    #                          volume=pos.pos_short)
    # check_all(api, bid, user_id)
    api.close()
Ejemplo n.º 28
0
参考: https://www.shinnytech.com/blog/dual-thrust
'''

import logging
from tqsdk import TqApi, TqSim, TargetPosTask

SYMBOL = "DCE.jd1905"  # 合约代码
NDAY = 5  # 天数
K1 = 0.2  # 上轨K值
K2 = 0.2  # 下轨K值

api = TqApi(TqSim())
logger = logging.getLogger("TQ")
logger.info("策略开始运行")

quote = api.get_quote(SYMBOL)
klines = api.get_kline_serial(SYMBOL, 24 * 60 * 60)  # 86400使用日线
target_pos = TargetPosTask(api, SYMBOL)


def dual_thrust(quote, klines):
    current_open = klines[-1]["open"]
    HH = max(klines.high[-NDAY - 1:-1])  # N日最高价的最高价
    HC = max(klines.close[-NDAY - 1:-1])  # N日收盘价的最高价
    LC = min(klines.close[-NDAY - 1:-1])  # N日收盘价的最低价
    LL = min(klines.low[-NDAY - 1:-1])  # N日最低价的最低价
    range = max(HH - LC, HC - LL)
    buy_line = current_open + range * K1  # 上轨
    sell_line = current_open - range * K2  # 下轨
    logger.info("当前开盘价: %f, 上轨: %f, 下轨: %f" % (current_open, buy_line, sell_line))
    return buy_line, sell_line
Ejemplo n.º 29
0
'''
菲阿里四价 策略(日内突破策略, 在每日收盘前对所持合约进行平仓)
参考: https://www.shinnytech.com/blog/fairy-four-price/
注: 该示例策略仅用于功能示范, 实盘时请根据自己的策略/经验进行修改
'''

from tqsdk import TqApi, TargetPosTask
from datetime import datetime
import time

symbol = "SHFE.cu2002"  # 合约代码
close_hour, close_minute = 14, 50  # 平仓时间

api = TqApi()  # 使用模拟帐号直连行情和交易服务器
quote = api.get_quote(symbol)  # 获取指定合约的盘口行情
klines = api.get_kline_serial(symbol, 24 * 60 * 60)  # 获取日线
position = api.get_position(symbol)  # 持仓信息
target_pos = TargetPosTask(api, symbol)  # 目标持仓

top_rail = klines.high.iloc[-2]  # 上轨: 昨日高点
bottom_rail = klines.low.iloc[-2]  # 下轨: 昨日低点
print("上轨:", top_rail, ",下轨:", bottom_rail, ",昨日收盘价:", klines.close.iloc[-2], ",今日开盘价:", klines.open.iloc[-1])

while True:
    api.wait_update()
    if api.is_changing(klines.iloc[-1], "datetime"):  # 如果产生一根新日线 (即到达下一个交易日): 重新获取上下轨
        top_rail = klines.high.iloc[-2]
        bottom_rail = klines.low.iloc[-2]
        print("上轨:", top_rail, ",下轨:", bottom_rail, ",昨日收盘价:", klines.close.iloc[-2], ",今日开盘价:", klines.open.iloc[-1])
Ejemplo n.º 30
0
    def test_get_quote_normal(self):
        """
        获取行情报价

        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(
            os.path.join(dir_path, "log_file",
                         "test_md_basic_get_quote_normal.script.lzma"))
        # 获取行情
        api = TqApi(_ins_url=self.ins_url_2019_07_03,
                    _td_url=self.td_url,
                    _md_url=self.md_url)
        q = api.get_quote("SHFE.cu1909")
        self.assertEqual(q.datetime, "2019-09-16 14:59:59.999500")
        self.assertEqual(q.ask_price1, 47650.0)
        self.assertEqual(q.ask_volume1, 10)
        self.assertEqual(q.bid_price1, 47570.0)
        self.assertEqual(q.bid_volume1, 5)
        self.assertEqual(q.last_price, 47580.0)
        self.assertEqual(q.highest, 47860.0)
        self.assertEqual(q.lowest, 47580.0)
        self.assertEqual(q.open, 47860.0)
        self.assertEqual(q.close, 47580.0)
        self.assertEqual(q.average, 47732.35)
        self.assertEqual(q.volume, 9020)
        self.assertEqual(q.amount, 2152729000.0)
        self.assertEqual(q.open_interest, 6940)
        self.assertEqual(q.settlement, 47730.0)
        self.assertEqual(q.upper_limit, 49650.0)
        self.assertEqual(q.lower_limit, 44920)
        self.assertEqual(q.pre_open_interest, 13260)
        self.assertEqual(q.pre_settlement, 47290.0)
        self.assertEqual(q.pre_close, 47590.0)
        self.assertEqual(q.price_tick, 10)
        self.assertEqual(q.price_decs, 0)
        self.assertEqual(q.volume_multiple, 5)
        self.assertEqual(q.max_limit_order_volume, 500)
        self.assertEqual(q.max_market_order_volume, 0)
        self.assertEqual(q.min_limit_order_volume, 0)
        self.assertEqual(q.min_market_order_volume, 0)
        self.assertEqual(q.underlying_symbol, "")
        self.assertTrue(q.strike_price != q.strike_price)  # 判定nan
        self.assertEqual(q.expired, False)
        self.assertEqual(q.ins_class, "FUTURE")
        self.assertEqual(q.margin, 16233.000000000002)
        self.assertEqual(q.commission, 11.594999999999999)
        self.assertEqual(
            repr(q.trading_time.day),
            "[['09:00:00', '10:15:00'], ['10:30:00', '11:30:00'], ['13:30:00', '15:00:00']]"
        )
        self.assertEqual(repr(q.trading_time.night),
                         "[['21:00:00', '25:00:00']]")

        self.assertEqual(q.expire_datetime, 1568617200.0)
        self.assertEqual(q.delivery_month, 9)
        self.assertEqual(q.delivery_year, 2019)
        self.assertEqual(q.instrument_id, "SHFE.cu1909")
        self.assertEqual(q.ask_price2, 47730.0)
        self.assertEqual(q.ask_volume2, 10)
        self.assertEqual(q.ask_price3, 47990.0)
        self.assertEqual(q.ask_volume3, 5)
        self.assertEqual(q.ask_price4, 49250.0)
        self.assertEqual(q.ask_volume4, 50)
        self.assertEqual(q.ask_price5 != q.ask_price5, True)  # 判断nan
        self.assertEqual(q.ask_volume5, 0)
        self.assertEqual(q.bid_price2, 46560.0)
        self.assertEqual(q.bid_volume2, 100)
        self.assertEqual(q.bid_price3, 45650.0)
        self.assertEqual(q.bid_volume3, 270)
        self.assertEqual(q.bid_price4, 44920.0)
        self.assertEqual(q.bid_volume4, 5)
        self.assertEqual(q.bid_price5 != q.bid_price5, True)
        self.assertEqual(q.bid_volume5, 0)

        # 其他取值方式
        self.assertEqual(q["pre_close"], 47590.0)
        self.assertEqual(q.get("pre_settlement"), 47290.0)
        self.assertEqual(q.get("highest"), 47860.0)
        self.assertEqual(q.get("lowest"), 47580.0)
        self.assertEqual(q["open"], 47860.0)
        self.assertEqual(q["close"], 47580.0)
        # 报错测试
        self.assertRaises(Exception, api.get_quote, "SHFE.au1999")
        self.assertRaises(KeyError, q.__getitem__, "ask_price6")

        api.close()