|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import unittest |
| 6 | + |
| 7 | +import six |
| 8 | +from hamcrest import * |
| 9 | +from requests import HTTPError |
| 10 | +from six.moves import configparser |
| 11 | + |
| 12 | +from shipane_sdk import Client |
| 13 | +from tests.shipane_sdk.matchers.dataframe_matchers import * |
| 14 | + |
| 15 | +if six.PY2: |
| 16 | + ConfigParser = configparser.RawConfigParser |
| 17 | +else: |
| 18 | + ConfigParser = configparser.ConfigParser |
| 19 | + |
| 20 | + |
| 21 | +class ClientMarginTradingTest(unittest.TestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + logging.basicConfig(level=logging.DEBUG) |
| 25 | + config = ConfigParser() |
| 26 | + dir_path = os.path.dirname(os.path.realpath(__file__)) |
| 27 | + config.read('{}/../config/config.ini'.format(dir_path)) |
| 28 | + cls.client = Client(logging.getLogger(), **dict(config.items('ShiPanE'))) |
| 29 | + cls.client_param = config.get('ShiPanE', 'client') |
| 30 | + |
| 31 | + def test_query(self): |
| 32 | + try: |
| 33 | + df = self.client.query(self.client_param, '融券卖出') |
| 34 | + assert_that(df, has_column(u'证券代码')) |
| 35 | + except HTTPError as e: |
| 36 | + self.fail() |
| 37 | + |
| 38 | + def test_buy_on_margin(self): |
| 39 | + try: |
| 40 | + order = self.client.execute( |
| 41 | + self.client_param, |
| 42 | + action='BUY_ON_MARGIN', symbol='000001', type='LIMIT', price=9, amount=100 |
| 43 | + ) |
| 44 | + self.assertIsNotNone(order['id']) |
| 45 | + except HTTPError as e: |
| 46 | + result = e.response.json() |
| 47 | + self.assertNotEqual(result['source'], "实盘易") |
| 48 | + |
| 49 | + def test_buy_on_margin_at_market_price(self): |
| 50 | + try: |
| 51 | + order = self.client.execute( |
| 52 | + self.client_param, |
| 53 | + action='BUY_ON_MARGIN', symbol='000001', type='MARKET', priceType=4, amount=100 |
| 54 | + ) |
| 55 | + self.assertIsNotNone(order['id']) |
| 56 | + except HTTPError as e: |
| 57 | + result = e.response.json() |
| 58 | + self.assertNotEqual(result['source'], "实盘易") |
| 59 | + |
| 60 | + def test_sell_then_repay(self): |
| 61 | + try: |
| 62 | + order = self.client.execute( |
| 63 | + self.client_param, |
| 64 | + action='SELL_THEN_REPAY', symbol='000001', type='LIMIT', price=9.5, amount=100 |
| 65 | + ) |
| 66 | + self.assertIsNotNone(order['id']) |
| 67 | + except HTTPError as e: |
| 68 | + result = e.response.json() |
| 69 | + self.assertNotEqual(result['source'], "实盘易") |
| 70 | + |
| 71 | + def test_sell_then_repay_at_market_price(self): |
| 72 | + try: |
| 73 | + order = self.client.execute( |
| 74 | + self.client_param, |
| 75 | + action='SELL_THEN_REPAY', symbol='000001', type='MARKET', priceType=4, amount=100 |
| 76 | + ) |
| 77 | + self.assertIsNotNone(order['id']) |
| 78 | + except HTTPError as e: |
| 79 | + result = e.response.json() |
| 80 | + self.assertNotEqual(result['source'], "实盘易") |
| 81 | + |
| 82 | + def test_sell_on_margin(self): |
| 83 | + try: |
| 84 | + order = self.client.execute( |
| 85 | + self.client_param, |
| 86 | + action='SELL_ON_MARGIN', symbol='000003', type='LIMIT', price=9.5, amount=100 |
| 87 | + ) |
| 88 | + self.assertIsNotNone(order['id']) |
| 89 | + except HTTPError as e: |
| 90 | + result = e.response.json() |
| 91 | + self.assertNotEqual(result['source'], "实盘易") |
| 92 | + |
| 93 | + def test_sell_on_margin_at_market_price(self): |
| 94 | + try: |
| 95 | + order = self.client.execute( |
| 96 | + self.client_param, |
| 97 | + action='SELL_ON_MARGIN', symbol='000001', type='MARKET', priceType=4, amount=100 |
| 98 | + ) |
| 99 | + self.assertIsNotNone(order['id']) |
| 100 | + except HTTPError as e: |
| 101 | + result = e.response.json() |
| 102 | + self.assertNotEqual(result['source'], "实盘易") |
| 103 | + |
| 104 | + def test_buy_then_repay(self): |
| 105 | + try: |
| 106 | + order = self.client.execute( |
| 107 | + self.client_param, |
| 108 | + action='BUY_THEN_REPAY', symbol='000001', type='LIMIT', price=8.9, amount=100 |
| 109 | + ) |
| 110 | + self.assertIsNotNone(order['id']) |
| 111 | + except HTTPError as e: |
| 112 | + result = e.response.json() |
| 113 | + self.assertNotEqual(result['source'], "实盘易") |
| 114 | + |
| 115 | + def test_buy_then_repay_at_market_price(self): |
| 116 | + try: |
| 117 | + order = self.client.execute( |
| 118 | + self.client_param, |
| 119 | + action='BUY_THEN_REPAY', symbol='000001', type='MARKET', priceType=4, amount=100 |
| 120 | + ) |
| 121 | + self.assertIsNotNone(order['id']) |
| 122 | + except HTTPError as e: |
| 123 | + result = e.response.json() |
| 124 | + self.assertNotEqual(result['source'], "实盘易") |
| 125 | + |
| 126 | + def test_repay_cash(self): |
| 127 | + try: |
| 128 | + order = self.client.execute( |
| 129 | + self.client_param, |
| 130 | + action='REPAY_CASH', symbol='', type='LIMIT', price=1.0, amount=1 |
| 131 | + ) |
| 132 | + self.assertIsNotNone(order['id']) |
| 133 | + except HTTPError as e: |
| 134 | + result = e.response.json() |
| 135 | + self.assertNotEqual(result['source'], "实盘易") |
| 136 | + |
| 137 | + def test_repay_sec(self): |
| 138 | + try: |
| 139 | + order = self.client.execute( |
| 140 | + self.client_param, |
| 141 | + action='REPAY_SEC', symbol='000001', type='LIMIT', price=0.0, amount=100 |
| 142 | + ) |
| 143 | + self.assertIsNotNone(order['id']) |
| 144 | + except HTTPError as e: |
| 145 | + result = e.response.json() |
| 146 | + self.assertNotEqual(result['source'], "实盘易") |
0 commit comments